230 lines
8.8 KiB
C#
230 lines
8.8 KiB
C#
using Newtonsoft.Json;
|
|
using S7Explorer.Models;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace S7Explorer.Services
|
|
{
|
|
public class ExportService
|
|
{
|
|
private readonly LogService _logService;
|
|
|
|
public ExportService()
|
|
{
|
|
_logService = LogService.Instance;
|
|
}
|
|
|
|
public async Task<bool> ExportProjectAsync(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
return await Task.Run(() => ExportProject(project, settings));
|
|
}
|
|
|
|
private bool ExportProject(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
try
|
|
{
|
|
_logService.LogInfo($"Starting export to {settings.ExportPath}");
|
|
|
|
string exportContent = GenerateExportContent(project, settings);
|
|
|
|
// Ensure directory exists
|
|
string directory = Path.GetDirectoryName(settings.ExportPath) ?? string.Empty;
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
// Write to file based on format
|
|
switch (settings.ExportFormat)
|
|
{
|
|
case ExportFormat.PlainText:
|
|
File.WriteAllText(settings.ExportPath, exportContent);
|
|
break;
|
|
case ExportFormat.MarkDown:
|
|
File.WriteAllText(settings.ExportPath, GenerateMarkdownContent(project, settings));
|
|
break;
|
|
case ExportFormat.HTML:
|
|
File.WriteAllText(settings.ExportPath, GenerateHtmlContent(project, settings));
|
|
break;
|
|
case ExportFormat.JSON:
|
|
File.WriteAllText(settings.ExportPath, GenerateJsonContent(project, settings));
|
|
break;
|
|
}
|
|
|
|
_logService.LogInfo($"Export completed successfully to {settings.ExportPath}");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logService.LogError($"Error during export: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private string GenerateExportContent(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
// Project info
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine($"PROJECT DOCUMENTATION: {project.Name}");
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine();
|
|
sb.AppendLine(project.GetExportText());
|
|
|
|
// Hardware configuration
|
|
if (settings.ExportHardware)
|
|
{
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine("HARDWARE CONFIGURATION");
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine();
|
|
|
|
ExportChildren(project.HardwareFolder, sb, settings, 0);
|
|
}
|
|
|
|
// Symbols
|
|
if (settings.ExportSymbols)
|
|
{
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine("SYMBOL TABLE");
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine();
|
|
|
|
ExportChildren(project.SymbolsFolder, sb, settings, 0);
|
|
}
|
|
|
|
// Blocks
|
|
if (settings.ExportBlocks)
|
|
{
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine("PROGRAM BLOCKS");
|
|
sb.AppendLine("==========================================================");
|
|
sb.AppendLine();
|
|
|
|
ExportChildren(project.BlocksFolder, sb, settings, 0);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private void ExportChildren(ProjectItem item, StringBuilder sb, ExportSettings settings, int level)
|
|
{
|
|
// Don't export if no children
|
|
if (item.Children.Count == 0)
|
|
return;
|
|
|
|
string indent = new string(' ', level * 2);
|
|
|
|
foreach (var child in item.Children)
|
|
{
|
|
// Skip block code if settings say so
|
|
if (child is BlockItem blockItem && !settings.IncludeBlockCode)
|
|
{
|
|
string text = blockItem.GetExportText();
|
|
// Remove the block content section
|
|
int contentIndex = text.IndexOf("// Block Content");
|
|
if (contentIndex > 0)
|
|
{
|
|
text = text.Substring(0, contentIndex) + "// Block Content omitted per export settings\r\n\r\n";
|
|
}
|
|
sb.Append(indent);
|
|
sb.AppendLine(text.Replace("\r\n", "\r\n" + indent));
|
|
}
|
|
else
|
|
{
|
|
// For other item types, just get export text
|
|
sb.Append(indent);
|
|
sb.AppendLine(child.GetExportText().Replace("\r\n", "\r\n" + indent));
|
|
}
|
|
|
|
// Recursively export children
|
|
ExportChildren(child, sb, settings, level + 1);
|
|
}
|
|
}
|
|
|
|
private string GenerateMarkdownContent(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
// This would generate Markdown-formatted content
|
|
// Simplified implementation
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.AppendLine($"# PROJECT DOCUMENTATION: {project.Name}");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"**Project Path:** {project.ProjectPath}");
|
|
sb.AppendLine($"**Version:** {project.ProjectVersion}");
|
|
sb.AppendLine($"**Created:** {project.CreationDate}");
|
|
sb.AppendLine($"**Last Modified:** {project.LastModifiedDate}");
|
|
sb.AppendLine();
|
|
|
|
// Rest of the implementation similar to plain text but with Markdown formatting
|
|
// ...
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private string GenerateHtmlContent(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
// This would generate HTML-formatted content
|
|
// Simplified implementation
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.AppendLine("<!DOCTYPE html>");
|
|
sb.AppendLine("<html>");
|
|
sb.AppendLine("<head>");
|
|
sb.AppendLine($"<title>Project Documentation: {project.Name}</title>");
|
|
sb.AppendLine("<style>");
|
|
sb.AppendLine("body { font-family: Arial, sans-serif; margin: 20px; }");
|
|
sb.AppendLine("h1, h2 { color: #0066cc; }");
|
|
sb.AppendLine("pre { background-color: #f5f5f5; padding: 10px; border: 1px solid #ddd; }");
|
|
sb.AppendLine("</style>");
|
|
sb.AppendLine("</head>");
|
|
sb.AppendLine("<body>");
|
|
sb.AppendLine($"<h1>PROJECT DOCUMENTATION: {project.Name}</h1>");
|
|
|
|
// Rest of the implementation similar to plain text but with HTML formatting
|
|
// ...
|
|
|
|
sb.AppendLine("</body>");
|
|
sb.AppendLine("</html>");
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private string GenerateJsonContent(ProjectStructure project, ExportSettings settings)
|
|
{
|
|
// Create an object structure to export to JSON
|
|
var exportObject = new
|
|
{
|
|
Project = new
|
|
{
|
|
Name = project.Name,
|
|
Path = project.ProjectPath,
|
|
Version = project.ProjectVersion,
|
|
CreationDate = project.CreationDate,
|
|
LastModifiedDate = project.LastModifiedDate
|
|
},
|
|
Hardware = settings.ExportHardware ? GetExportObjectForItem(project.HardwareFolder) : null,
|
|
Symbols = settings.ExportSymbols ? GetExportObjectForItem(project.SymbolsFolder) : null,
|
|
Blocks = settings.ExportBlocks ? GetExportObjectForItem(project.BlocksFolder) : null
|
|
};
|
|
|
|
return JsonConvert.SerializeObject(exportObject, Formatting.Indented);
|
|
}
|
|
|
|
private object GetExportObjectForItem(ProjectItem item)
|
|
{
|
|
// Create a JSON-friendly object structure for an item and its children
|
|
var result = new
|
|
{
|
|
Name = item.Name,
|
|
Children = item.Children.Select(c => GetExportObjectForItem(c)).ToArray()
|
|
};
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |