public void VerifyChangelogWithExclusionsIsGenerated()
        {
            var tagsToExclude = new List <string> {
                "Removed"
            };
            var now        = DateTimeOffset.Now;
            var yesterday  = now.AddDays(-1);
            var readerMock = new Mock <IGenericReader <ProjectChange> >();
            var renderer   = new StubbleBuilder().Build();
            var readers    = new List <IGenericReader <ProjectChange> >
            {
                readerMock.Object
            };
            const string projectA        = "ProjectA";
            const string projectB        = "ProjectB";
            var          expectedChanges = new List <ProjectChange>
            {
                new ProjectChange(projectA, "1.0.0", "Added", $"Added some other {projectA} change", now),
                new ProjectChange(projectB, "1.0.0", "Added", $"Added some other {projectB} change", now),
                new ProjectChange(projectA, "1.0.0", "Removed", $"Removed some other {projectA} change", now),
                new ProjectChange(projectB, "1.0.0", "Removed", $"Removed some other {projectB} change", now),
                new ProjectChange(projectA, "0.1.0", "Added", $"Added some {projectA} change", yesterday),
                new ProjectChange(projectB, "0.1.0", "Added", $"Added some {projectB} change", yesterday),
                new ProjectChange(projectA, "0.1.0", "Removed", $"Removed some {projectA} change", yesterday),
                new ProjectChange(projectB, "0.1.0", "Removed", $"Removed some {projectB} change", yesterday),
            };

            readerMock.Setup(r => r.Values()).Returns(expectedChanges);

            var expectedA = $@"
## [1.0.0] - {now:yyyy-MM-dd}
### Added
- Added some other {projectA} change

## [0.1.0] - {yesterday:yyyy-MM-dd}
### Added
- Added some {projectA} change

";
            var expectedB = $@"
## [1.0.0] - {now:yyyy-MM-dd}
### Added
- Added some other {projectB} change

## [0.1.0] - {yesterday:yyyy-MM-dd}
### Added
- Added some {projectB} change

";

            var generator       = new ProjectChangelogGenerator(readers, Template, renderer);
            var projectToActual = generator.Generate(tagsToExclude: tagsToExclude);

            Assert.That(projectToActual.GetValueOrDefault(projectA), Is.EqualTo(expectedA));
            Assert.That(projectToActual.GetValueOrDefault(projectB), Is.EqualTo(expectedB));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                var configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                Parser.Default.ParseArguments <Options>(args)
                .WithParsed(options =>
                {
                    var additionalSettings = new List <KeyValuePair <string, string> >();

                    if (!string.IsNullOrEmpty(options.CustomSettingsPath))
                    {
                        configBuilder.AddJsonFile(options.CustomSettingsPath);
                    }

                    if (!string.IsNullOrEmpty(options.CustomTemplatePath))
                    {
                        additionalSettings.Add(new KeyValuePair <string, string>("Template", options.CustomTemplatePath));
                    }

                    if (!string.IsNullOrEmpty(options.ChangeTypesToExclude))
                    {
                        additionalSettings.Add(new KeyValuePair <string, string>("ChangeTypesToExclude", options.ChangeTypesToExclude));
                    }

                    if (!string.IsNullOrEmpty(options.MinVersion))
                    {
                        additionalSettings.Add(new KeyValuePair <string, string>("MinVersion", options.MinVersion));
                    }

                    if (!string.IsNullOrEmpty(options.RepositoryPath))
                    {
                        additionalSettings.Add(new KeyValuePair <string, string>("Repository:Path", options.RepositoryPath));
                    }

                    if (!string.IsNullOrEmpty(options.FileSourcePath))
                    {
                        additionalSettings.Add(new KeyValuePair <string, string>("FileSource", options.FileSourcePath));
                    }

                    configBuilder.AddInMemoryCollection(additionalSettings);
                });

                var config    = TryOrExit(() => configBuilder.Build(), "Failed to build configuration");
                var appConfig = config.Get <AppConfig>();

                var template             = GetTemplate(appConfig.Template);
                var repo                 = TryOrExit(() => new Repository(appConfig.Repository.Path), "Failed to initialize repository");
                var idToOverrideChange   = new Dictionary <string, IChange>();
                var changeTypesToExclude = appConfig.ChangeTypesToExclude.Split(",");
                var renderer             = new StubbleBuilder().Build();
                var minVersion           = string.IsNullOrEmpty(appConfig.MinVersion) ? null : new ChangeVersion(appConfig.MinVersion);

                if (appConfig.MultiProject)
                {
                    var readers           = new List <IGenericReader <ProjectChange> >();
                    var idToProjectChange = new Dictionary <string, ProjectChange>();
                    if (!string.IsNullOrEmpty(appConfig.FileSource))
                    {
                        var fileReader = new FileReader <ProjectChange>(appConfig.FileSource, new ProjectFileSourceRowParser(Console.Error));
                        readers.Add(fileReader);
                    }
                    if (!string.IsNullOrEmpty(appConfig.Repository.OverrideSource))
                    {
                        var overrideFileReader = new FileReader <OverrideProjectChange>(appConfig.Repository.OverrideSource, new OverrideProjectSourceRowParser(Console.Error));
                        readers.Add(overrideFileReader);
                        idToProjectChange = overrideFileReader.Values().ToDictionary <OverrideProjectChange, string, ProjectChange>(change => change.Id, change => change);
                    }
                    var parser    = new ProjectCommitParser(appConfig.Parsing);
                    var gitReader = new GitReader <ProjectChange>(repo, parser, idToProjectChange);
                    readers.Add(gitReader);

                    var generator       = new ProjectChangelogGenerator(readers, template, renderer);
                    var projectToOutput = generator.Generate(minVersion, changeTypesToExclude);

                    foreach (var(project, output) in projectToOutput)
                    {
                        File.WriteAllText($@"{project}-changelog.md", output);
                    }
                }
                else
                {
                    var readers = new List <IGenericReader <IChange> >();

                    if (!string.IsNullOrEmpty(appConfig.FileSource))
                    {
                        var fileReader = new FileReader <DefaultChange>(appConfig.FileSource, new DefaultFileSourceRowParser(Console.Error));
                        readers.Add(fileReader);
                    }

                    if (!string.IsNullOrEmpty(appConfig.Repository.OverrideSource))
                    {
                        var overrideFileReader = new FileReader <OverrideChange>(appConfig.Repository.OverrideSource, new OverrideSourceRowParser(Console.Error));
                        idToOverrideChange = overrideFileReader.Values().ToDictionary <OverrideChange, string, IChange>(change => change.Id, change => change);
                    }
                    Dictionary <string, string> commitShaToTagName = null;
                    if (UsesTagAsSource(appConfig.Parsing))
                    {
                        commitShaToTagName = new Dictionary <string, string>();
                        foreach (var tag in repo.Tags)
                        {
                            commitShaToTagName.Add(tag.Target.Sha, tag.FriendlyName);
                        }
                    }
                    var commitParser = new DefaultCommitParser(appConfig.Parsing, commitShaToTagName);
                    var gitReader    = new GitReader <IChange>(repo, commitParser, idToOverrideChange);
                    readers.Add(gitReader);

                    var cache     = new ChangeCache();
                    var generator = new StringChangelogGenerator(readers, cache, template, renderer);
                    var output    = generator.Generate(minVersion, changeTypesToExclude);
                    File.WriteAllText(@"changelog.md", output);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e.Message}");
                Environment.Exit(-1);
            }
        }