diff --git a/tools/fastreport-cli/FastReportCliGenerator.csproj b/tools/fastreport-cli/FastReportCliGenerator.csproj index ea92c89e..149b9161 100644 --- a/tools/fastreport-cli/FastReportCliGenerator.csproj +++ b/tools/fastreport-cli/FastReportCliGenerator.csproj @@ -11,9 +11,9 @@ - 1.0 - 1.0 - 1.0 + 1.1 + 1.1.0.8 + 1.1.0.8 diff --git a/tools/fastreport-cli/FastReportCliGenerator.slnx b/tools/fastreport-cli/FastReportCliGenerator.slnx new file mode 100644 index 00000000..d405c80f --- /dev/null +++ b/tools/fastreport-cli/FastReportCliGenerator.slnx @@ -0,0 +1,3 @@ + + + diff --git a/tools/fastreport-cli/Program.cs b/tools/fastreport-cli/Program.cs index e25dd7f4..bd78e780 100644 --- a/tools/fastreport-cli/Program.cs +++ b/tools/fastreport-cli/Program.cs @@ -1,106 +1,182 @@ +using System.Reflection; + using FastReport; using FastReport.Data.JsonConnection; using FastReport.Export.Html; using FastReport.Export.PdfSimple; -using System; -using System.Reflection; class Program { - static int Main(string[] args) - { - // Si el usuario pasa --version, mostrar versión y salir - if (args.Length == 1 && (args[0] == "--version" || args[0] == "-v")) - { - var version = Assembly.GetExecutingAssembly().GetName().Version; - Console.WriteLine($"FastReportCliGenerator version {version}"); - return 0; - } + static int Main(string[] args) + { + var options = ParseArgs(args); - Console.WriteLine($"Running FastReportCliGenerator version {Assembly.GetExecutingAssembly().GetName().Version}"); - if (args.Length < 4) - { - Console.Error.WriteLine("Usage: FastReportCliGenerator --format="); - return 1; - } + // --version + if (options.ContainsKey("version")) + { + PrintVersion(); + return 0; + } + + // --help o sin argumentos + if (options.ContainsKey("help") || options.Count == 0) + { + PrintHelp(); + return 0; + } + + // Validación de obligatorios + if (!options.TryGetValue("template", out var frxPath) || + !options.TryGetValue("data", out var jsonPath) || + !options.TryGetValue("output", out var outputPath)) + { + Console.Error.WriteLine("❌ Missing required arguments.\n"); + PrintHelp(); + return 1; + } + + var format = options.TryGetValue("format", out var f) + ? f.ToLowerInvariant() + : "pdf"; + + try + { + // Leer JSON en memoria + string jsonData = File.ReadAllText(jsonPath); + + // 2) Busca la conexión JSON ya definida + var builder = new JsonDataSourceConnectionStringBuilder(); + builder.Json = jsonData; + + JsonDataSourceConnection jsonConn = new JsonDataSourceConnection(); + + using var report = new Report(); + + // Cargar el informe + report.Load(frxPath); - string frxPath = args[0]; - string jsonPath = args[1]; - string outputPath = args[2]; - string formatArg = args[3].ToLower(); - - // Extrae el formato - string format = "html"; - if (formatArg.StartsWith("--format=")) - { - format = formatArg.Split('=')[1]; - } + foreach (var conn in report.Dictionary.Connections) + { + Console.WriteLine($"Connection found: {conn.ToString()} ({conn.GetType().Name})"); + if (conn is JsonDataSourceConnection jdc) + { + jsonConn = jdc; + break; + } + } - try - { - // Leer JSON en memoria - string jsonData = File.ReadAllText(jsonPath); + if (jsonConn == null) + { + throw new Exception("No JSON data connection found in report."); + } - using var report = new Report(); + - // Cargar el informe - report.Load(frxPath); - - // 2) Busca la conexión JSON ya definida - JsonDataSourceConnection? jsonConn = null; - foreach (var conn in report.Dictionary.Connections) - { - if (conn is JsonDataSourceConnection jdc) - { - jsonConn = jdc; - break; - } - } + // 3) Cambia la cadena de conexión para usar el JSON real + jsonConn.ConnectionString = builder.ToString(); - if (jsonConn == null) - { - throw new Exception("No JSON data connection found in report."); - } + // 4) Crea las tablas internas basadas en ese JSON + jsonConn.CreateAllTables(); - // 3) Cambia la cadena de conexión para usar el JSON real - // "Json=" es la clave usada por FastReport para indicar JSON embebido - jsonConn.ConnectionString = $"Json={jsonData}"; + // 5) Preparar el reporte + report.Prepare(); - // 4) Crea las tablas internas basadas en ese JSON - jsonConn.CreateAllTables(); + if (format == "pdf") + { + // Exportar a PDF + using var pdfExport = new PDFSimpleExport(); + using var fs = new FileStream(outputPath, FileMode.Create); + pdfExport.Export(report, fs); + Console.WriteLine($"Generated PDF: {outputPath}"); + } + else + { + // Exportar a HTML + using var htmlExport = new HTMLExport + { + EmbedPictures = true, + SinglePage = true, + SubFolder = false + }; + using var fs = new FileStream(outputPath, FileMode.Create); + report.Export(htmlExport, fs); + Console.WriteLine($"Generated HTML: {outputPath}"); + } - // 5) Preparar el reporte - report.Prepare(); + return 0; + } + catch (Exception ex) + { + Console.Error.WriteLine("Error generating report: " + ex.Message); + return 2; + } + } - if (format == "pdf") - { - // Exportar a PDF - using var pdfExport = new PDFSimpleExport(); - using var fs = new FileStream(outputPath, FileMode.Create); - pdfExport.Export(report, fs); - Console.WriteLine($"Generated PDF: {outputPath}"); - } - else - { - // Exportar a HTML - using var htmlExport = new HTMLExport - { - EmbedPictures = true, - SinglePage = true, - SubFolder = false - }; - using var fs = new FileStream(outputPath, FileMode.Create); - report.Export(htmlExport, fs); - Console.WriteLine($"Generated HTML: {outputPath}"); - } - return 0; - } - catch (Exception ex) - { - Console.Error.WriteLine("Error generating report: " + ex.Message); - return 2; - } - } + // ---------------------------- + // Helpers + // ---------------------------- + + static Dictionary ParseArgs(string[] args) + { + var dict = new Dictionary(StringComparer.OrdinalIgnoreCase); + + foreach (var arg in args) + { + if (!arg.StartsWith("--")) + continue; + + var parts = arg.Substring(2).Split('=', 2); + + if (parts.Length == 1) + dict[parts[0]] = "true"; // flags: --help, --version + else + dict[parts[0]] = parts[1]; + } + + return dict; + } + + static void PrintVersion() + { + var version = Assembly.GetExecutingAssembly().GetName().Version; + Console.WriteLine(); + Console.WriteLine($"FastReportCliGenerator version {version}"); + } + + static void PrintHelp() + { + PrintVersion(); + Console.WriteLine(@" +Usage: + FastReportCliGenerator \ + --template= \ + --data= \ + --output= \ + [--format=html|pdf] + +Options: + --template Path to FRX template (required) + --data Path to JSON data file (required) + --output Output file path (required) + --format html or pdf (default) + --version Show version and exit + --help Show this help + +Examples: + FastReportCliGenerator \ + --template=invoice.frx \ + --data=invoice.json \ + --output=invoice.html + + FastReportCliGenerator \ + --template=invoice.frx \ + --data=invoice.json \ + --output=invoice.pdf \ + --format=pdf +"); + } } + + diff --git a/tools/fastreport-cli/publish/linux/FastReportCliGenerator b/tools/fastreport-cli/publish/linux/FastReportCliGenerator index 3c6346c9..dd9fa083 100755 Binary files a/tools/fastreport-cli/publish/linux/FastReportCliGenerator and b/tools/fastreport-cli/publish/linux/FastReportCliGenerator differ diff --git a/tools/fastreport-cli/publish/windows/FastReportCliGenerator.exe b/tools/fastreport-cli/publish/windows/FastReportCliGenerator.exe index 331db3ea..474aa468 100755 Binary files a/tools/fastreport-cli/publish/windows/FastReportCliGenerator.exe and b/tools/fastreport-cli/publish/windows/FastReportCliGenerator.exe differ