public static async Task RunAsync(JToken json, IEnumerable <string> templatesPaths, string outputPath, IEnumerable <IHelperBase>?helpers, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy = TemplateDuplicationHandlingStrategy.Throw) { helpers ??= new IHelper[0]; var templates = GetTemplates(templatesPaths, templateDuplicationHandlingStrategy); var handlebars = HandlebarsConfigurationHelper.GetHandlebars(templatesPaths, helpers); foreach (var template in templates) { var compiled = handlebars.Compile(File.ReadAllText(template.FilePath)); var result = compiled(json); var context = new ProcessorContext(result, outputPath); using var processor = new FilesProcessor(context, new WriteLineToFileInstruction("FILE"), new WriteLineToConsoleInstruction("CONSOLE"), new CleanInstruction("CLEAN"), new SuppressLineInstruction() ); await processor.RunAsync(); } }
public void LoadHelpersFromTheExampleProjectBeside() { var customHelpersProjectPath = "../../../../../src/CodegenUP.CustomHelpers"; var tmpFolder = Path.GetRandomFileName(); try { var helpers = HandlebarsConfigurationHelper.GetHelpersFromFolder(customHelpersProjectPath, tmpFolder); _output.WriteLine(string.Join(Environment.NewLine, helpers.Select(h => h.ToString()))); helpers.Length.ShouldNotBe(0); } finally { //if (Directory.Exists(tmpFolder)) //{ // Directory.Delete(tmpFolder, true); //} } }
public static async Task Main(string[] args) { //https://msdn.microsoft.com/fr-fr/magazine/mt763239.aspx var app = new CommandLineApplication(); app.HelpOption(HelpOptions); var schemaTypeName = app.Option(SCHEMA_LOADER_TYPE_OPTION, $"Enter a schema loader type between those values [{string.Join(" | ", Enum.GetNames(typeof(SourceSchemaType)))}]", CommandOptionType.SingleValue); var duplicatesTemplateHandlingStrategyName = app.Option(TEMPLATE_DUPLICATES_HANDLING_STRATEGY_OPTION, $"Enter a template duplication handling strategy between those values [{string.Join(" | ", Enum.GetNames(typeof(TemplateDuplicationHandlingStrategy)))}]", CommandOptionType.SingleValue); var sourceFiles = app.Option(SOURCE_FILE_OPTION, "Enter a path (relative or absolute) to an source document.", CommandOptionType.MultipleValue) ?? throw new InvalidOperationException(); var authorization = app.Option(AUTHORIZATION_OPTION, "Enter an authorization token to access source documents", CommandOptionType.SingleValue) ?? throw new InvalidOperationException(); var outputPath = app.Option(OUTPUT_PATH_OPTION, "Enter the path (relative or absolute) to the output path (content will be overritten)", CommandOptionType.SingleValue) ?? throw new InvalidOperationException(); var templatesPaths = app.Option(TEMPLATES_PATH_OPTION, "Enter a path (relative or absolute / file or folder) to a template.", CommandOptionType.MultipleValue) ?? throw new InvalidOperationException(); var intermediate = app.Option(INTERMEDIATE_OPTION, "Enter a path (relative or absolute) to a file for intermediate 'all refs merged' output of the json document", CommandOptionType.SingleValue) ?? throw new InvalidOperationException(); var customHelpers = app.Option(CUSTOM_HELPERS, "Enter a path (relative or absolute) to a folder with a custom helpers project (.csproj)", CommandOptionType.MultipleValue) ?? throw new InvalidOperationException(); var artifacts = app.Option(CUSTOM_HELPERS_ARTIFACTS_DIRECTORY, $"Enter a path (relative or absolute) where the custom helpers builds process can output artifacts (default {CUSTOM_HELPERS_ARTIFACTS_DIRECTORY_DEFAULT})", CommandOptionType.SingleValue) ?? throw new InvalidOperationException(); var globalParamValues = app.Option(GLOBAL_PARAMETER_OPTION, $"Enter a global parameter value on the form key=value, it'll be available throught the " + GlobalParametersHelper.NAME + "helper", CommandOptionType.MultipleValue); app.OnExecute(async() => { var errors = new List <string>(); if (sourceFiles.Values == null || sourceFiles.Values.Count == 0) { errors.Add($"At least one source file ({SOURCE_FILE_OPTION}) option is required."); } if (templatesPaths.Values == null || templatesPaths.Values.Count == 0) { errors.Add($"At least one tempalte ({TEMPLATES_PATH_OPTION}) option is required."); } if (outputPath.Values == null || outputPath.Values.Count != 1) { errors.Add($"Exactly one output path ({OUTPUT_PATH_OPTION}) option is required."); } var schemaType = SchemaTypeExtensions.DEFAULT_SHEMA_TYPE; if (schemaTypeName.HasValue()) { if (Enum.TryParse <SourceSchemaType>(schemaTypeName.Value(), out var schema)) { schemaType = schema; } else { errors.Add($"The schema type '{schemaTypeName.Value()}' is unknown."); } } var duplicatesTemplateHandlingStrategy = TemplateDuplicationHandlingStrategy.Throw; if (duplicatesTemplateHandlingStrategyName.HasValue()) { if (Enum.TryParse <TemplateDuplicationHandlingStrategy>(duplicatesTemplateHandlingStrategyName.Value(), out var strategy)) { duplicatesTemplateHandlingStrategy = strategy; } else { errors.Add($"The duplicates template handling strategy type '{duplicatesTemplateHandlingStrategyName.Value()}' is unknown."); } } if (errors.Count != 0) { foreach (var err in errors) { Console.Error.WriteLine(err, ERROR_COLOR); } app.ShowHelp(); return(1); } var auth = authorization.HasValue() ? authorization.Value() : null; try { var schemaLoader = schemaType.GetSchemaLoader(); var jsonObject = await schemaLoader.LoadSchemaAsync( sourceFiles.Values ?? throw new InvalidOperationException(), auth ); if (intermediate.HasValue()) { File.WriteAllText(intermediate.Value(), jsonObject.ToString()); } var helpers = new List <IHelperBase>(); var artifactDirectory = artifacts.HasValue() ? artifacts.Value() : CUSTOM_HELPERS_ARTIFACTS_DIRECTORY_DEFAULT; foreach (var helperPath in customHelpers.Values) { var helps = HandlebarsConfigurationHelper.GetHelpersFromFolder(helperPath, artifactDirectory); helpers.AddRange(helps); Console.WriteLine($"Adding helpers : {string.Join(',', (IEnumerable<IHelper>)helps)}"); } var globalParametersValues = new Dictionary <string, string>(); foreach (var kv in globalParamValues.Values.Select(v => v.Split('=', 2))) { string value = string.Empty; var len = kv.Length; var key = len != 0 ? kv[0].Trim() : string.Empty; switch (kv.Length) { case 0: case 1 when key == string.Empty: Console.WriteLine("A global parameter option is empty, skipping"); continue; case 1: key = kv[0]; Console.WriteLine($"Global parameter '{key}' will be an empty string"); break; default: key = kv[0]; value = kv[1]; Console.WriteLine($"Global parameter '{key}' will have the value {value}"); break; } globalParametersValues.Add(key, value); } var globalParamerersHelper = new GlobalParametersHelper(globalParametersValues); helpers.Add(globalParamerersHelper); await CodeGenRunner.RunAsync( jsonObject, templatesPaths.Values ?? throw new InvalidOperationException(), outputPath.Value(), helpers, duplicatesTemplateHandlingStrategy ); } catch (Exception ex) { Console.WriteLine(ex.GetBaseException().Message, ERROR_COLOR); throw; } return(0); }); try { var result = app.Execute(args); #if (!DEBUG) Environment.Exit(result); #endif } catch (Exception ex) { Console.Error.WriteLine(ex.Message); #if (!DEBUG) Environment.Exit(1); #endif } }
public static IEnumerable <object[]> HelpersTests_Data() => HelpersTesterHelper.GetHelpersTestsData(HandlebarsConfigurationHelper.GetHelpersFromAssembly(typeof(TestsBootStraper).Assembly));