diff --git a/TopModel.Generator/Program.cs b/TopModel.Generator/Program.cs index c8f15b433..3d1aa9cd2 100644 --- a/TopModel.Generator/Program.cs +++ b/TopModel.Generator/Program.cs @@ -1,4 +1,4 @@ -using System.CommandLine; +using System.CommandLine; using System.Diagnostics; using System.Reflection; using System.Security.Cryptography; @@ -33,6 +33,66 @@ var command = new RootCommand("Lance le générateur topmodel.") { Name = "modgen" }; +var initCommand = new Command("init", "Initialise un nouveau fichier de configuration topmodel."); +var initAppOption = new Option(["-a", "--app"], "Nom de l'application."); +var initFileOption = new Option(["-f", "--file"], "Nom du fichier de configuration à créer."); +initCommand.AddOption(initAppOption); +initCommand.AddOption(initFileOption); +initCommand.SetHandler( + (app, file) => + { + var fileName = file ?? "topmodel.config"; + var filePath = Path.GetFullPath(fileName); + + if (File.Exists(filePath)) + { + AnsiConsole.MarkupLine($"[red]Le fichier '{fileName}' existe déjà.[/]"); + returnCode = 1; + return; + } + + var appName = app ?? Path.GetFileName(Directory.GetCurrentDirectory()); + + var availableGenerators = new[] { "csharp", "jpa", "javascript", "sql" }; + var selectedGenerators = AnsiConsole.Prompt( + new MultiSelectionPrompt() + .Title("Sélectionnez les générateurs à utiliser :") + .NotRequired() + .PageSize(10) + .InstructionsText( + "[grey](Appuyez sur [blue][/] pour sélectionner, [green][/] pour valider)[/]" + ) + .AddChoices(availableGenerators) + ); + + var configBuilder = new StringBuilder(); + configBuilder.AppendLine("---"); + configBuilder.AppendLine($"app: {appName}"); + + foreach (var generator in selectedGenerators) + { + configBuilder.AppendLine(); + configBuilder.AppendLine($"{generator}:"); + configBuilder.AppendLine(" - tags:"); + configBuilder.AppendLine( + $" - {((generator == "csharp" || generator == "jpa" || generator == "sql") ? "back" : "front")}" + ); + configBuilder.AppendLine(" outputDirectory: ./generated"); + } + + File.WriteAllText(filePath, configBuilder.ToString()); + AnsiConsole.MarkupLine($"[green]Fichier de configuration '{fileName}' créé avec succès.[/]"); + AnsiConsole.MarkupLine($"[grey]Application : {appName}[/]"); + if (selectedGenerators.Count > 0) + { + AnsiConsole.MarkupLine($"[grey]Générateurs : {string.Join(", ", selectedGenerators)}[/]"); + } + }, + initAppOption, + initFileOption +); +command.AddCommand(initCommand); + var fileOption = new Option>(["-f", "--file"], "Chemin vers un fichier de config."); var excludeOption = new Option>(["-e", "--exclude"], "Tag à ignorer lors de la génération."); var watchOption = new Option(["-w", "--watch"], "Lance le générateur en mode 'watch'"); diff --git a/TopModel.ModelGenerator/OpenApi/OpenApiUtils.cs b/TopModel.ModelGenerator/OpenApi/OpenApiUtils.cs index 5560584f9..d760bd052 100644 --- a/TopModel.ModelGenerator/OpenApi/OpenApiUtils.cs +++ b/TopModel.ModelGenerator/OpenApi/OpenApiUtils.cs @@ -131,6 +131,7 @@ public static IDictionary GetProperties(this IOpenApiSch .Where(a => a.Type == JsonSchemaType.Object) .SelectMany(a => a.Properties ?? new Dictionary()) ) + .DistinctBy(a => a.Key) .ToDictionary(a => a.Key, a => a.Value) ?? []; }