public void ShouldKeepOnConsolidateTemplates(string[] templatesPaths, string expectedTemplateFolder, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy, int expectedTemplatesCount) { var templates = CodeGenRunner.GetTemplates(templatesPaths.ToList(), templateDuplicationHandlingStrategy).ToList(); templates.Count.ShouldBe(expectedTemplatesCount); foreach (var template in templates) { Assert.Contains(expectedTemplateFolder, template.FilePath); } }
public void ShouldThrowExceptionOnConsolidateTemplates(string[] templatesPaths, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy, string expectedMessage) { Should.Throw <InvalidDataException>(() => { CodeGenRunner.GetTemplates(templatesPaths.ToList(), templateDuplicationHandlingStrategy).ToList(); } ).Message.ShouldBe(expectedMessage); }
public void ShouldNotThrowExceptionOnConsolidateTemplates(string[] templatesPaths, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy) { var templates = CodeGenRunner.GetTemplates(templatesPaths.ToList(), templateDuplicationHandlingStrategy).ToList(); Assert.Equal(5, templates.Count); Assert.Contains(templates, template => template.FileName == "path1"); Assert.Contains(templates, template => template.FileName == "path2"); Assert.Contains(templates, template => template.FileName == "path3"); Assert.Contains(templates, template => template.FileName == "path4"); }
public async Task ShouldParseCommandLineArgumentsAndApplyTemplate(string source, string[] templates, string expectedDirectory, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy, SourceSchemaType sourceSchemaType) { var expectedMd5Output = Directory.GetFiles(expectedDirectory).Select(file => ComputeMD5(file)).ToList(); var outputDirectory = GetTemporaryDirectory(); try { var args = new[] { "-l", sourceSchemaType.ToString(), "-d", templateDuplicationHandlingStrategy.ToString(), "-s", source, "-o", outputDirectory } .Concat(templates.SelectMany(t => new[] { "-t", t })) .ToArray(); await Program.Main(args); var actualMd5Output = Directory.GetFiles(outputDirectory).Select(file => ComputeMD5(file)).ToList(); Assert.Equal(expectedMd5Output.Count, actualMd5Output.Count); foreach (var expectedMd5 in expectedMd5Output) { Assert.Contains(expectedMd5, actualMd5Output); } } finally { Directory.Delete(outputDirectory, true); } }
internal static IEnumerable <TemplateInfos> GetTemplates(IEnumerable <string> templatesPaths, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy) { var templates = templatesPaths .SelectMany(templatePath => TemplateHelper.GetTemplates(templatePath, "*.hbs")) .Where(t => !t.FileName.StartsWith("_")) .ToArray(); if (templates.Length == 0) { throw new InvalidDataException($"No template found in path(s) : {string.Join(" | ", templatesPaths)}"); } var templateGroups = templates.GroupBy(template => template.FileName).ToArray(); if (templateDuplicationHandlingStrategy == TemplateDuplicationHandlingStrategy.Throw) { var duplicates = templateGroups.Where(group => group.Count() > 1).ToArray(); if (duplicates.Length != 0) { var templateNames = string.Join(" | ", duplicates.Select(g => g.Key).OrderBy(template => template)); throw new InvalidDataException($"Possible template(s) duplication - please use a unique template name [{templateNames}]"); } } return(templateGroups.Select(g => { switch (templateDuplicationHandlingStrategy) { case TemplateDuplicationHandlingStrategy.Throw: // should be only one element in the group now case TemplateDuplicationHandlingStrategy.KeepFirst: return g.First(); case TemplateDuplicationHandlingStrategy.KeepLast: return g.Last(); default: throw new NotImplementedException(); } })); }
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 static async Task RunAsync(IEnumerable <string> sourcePath, ISchemaLoader schemaLoader, IEnumerable <string> templatesPaths, string outputPath, IEnumerable <IHelper>?customHelpers, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy = TemplateDuplicationHandlingStrategy.Throw, string?authorization = null) { var jsonObject = await schemaLoader.LoadSchemaAsync(sourcePath, authorization); await RunAsync(jsonObject, templatesPaths, outputPath, customHelpers, templateDuplicationHandlingStrategy); }
public static Task RunAsync(string sourcePath, ISchemaLoader schemaLoader, string templatePath, string outputPath, IEnumerable <IHelper>?customHelpers = null, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy = TemplateDuplicationHandlingStrategy.Throw, string?authorization = null) => RunAsync(new[] { sourcePath }, schemaLoader, new[] { templatePath }, outputPath, customHelpers, templateDuplicationHandlingStrategy, authorization);