Beispiel #1
0
        private static int GenerateReleaseNotes(string[] args)
        {
            var modelBindingDefinition = Args.Configuration.Configure <GitReleaseNotesArguments>();

            if (args.Any(a => a == "/?" || a == "?" || a.Equals("/help", StringComparison.InvariantCultureIgnoreCase)))
            {
                var help = new HelpProvider().GenerateModelHelp(modelBindingDefinition);
                var f    = new ConsoleHelpFormatter();
                f.WriteHelp(help, Console.Out);

                return(0);
            }

            var arguments = modelBindingDefinition.CreateAndBind(args);

            if (!ArgumentVerifier.VerifyArguments(arguments))
            {
                return(1);
            }

            var workingDirectory = arguments.WorkingDirectory ?? Directory.GetCurrentDirectory();

            var gitDirectory = GitDirFinder.TreeWalkForGitDir(workingDirectory);

            if (string.IsNullOrEmpty(gitDirectory))
            {
                throw new Exception("Failed to find .git directory.");
            }

            Console.WriteLine("Git directory found at {0}", gitDirectory);

            var repositoryRoot = Directory.GetParent(gitDirectory).FullName;

            var gitRepo = new Repository(gitDirectory);

            CreateIssueTrackers(gitRepo, arguments);

            IIssueTracker issueTracker = null;

            if (arguments.IssueTracker == null)
            {
                var firstOrDefault = _issueTrackers.FirstOrDefault(i => i.Value.RemotePresentWhichMatches);
                if (firstOrDefault.Value != null)
                {
                    issueTracker = firstOrDefault.Value;
                }
            }
            if (issueTracker == null)
            {
                if (!_issueTrackers.ContainsKey(arguments.IssueTracker.Value))
                {
                    throw new Exception(string.Format("{0} is not a known issue tracker", arguments.IssueTracker.Value));
                }

                issueTracker = _issueTrackers[arguments.IssueTracker.Value];
            }
            if (!issueTracker.VerifyArgumentsAndWriteErrorsToConsole())
            {
                return(1);
            }

            var    fileSystem           = new FileSystem.FileSystem();
            var    releaseFileWriter    = new ReleaseFileWriter(fileSystem);
            string outputFile           = null;
            var    previousReleaseNotes = new SemanticReleaseNotes();

            if (!string.IsNullOrEmpty(arguments.OutputFile))
            {
                outputFile = Path.IsPathRooted(arguments.OutputFile)
                    ? arguments.OutputFile
                    : Path.Combine(repositoryRoot, arguments.OutputFile);
                previousReleaseNotes = new ReleaseNotesFileReader(fileSystem, repositoryRoot).ReadPreviousReleaseNotes(outputFile);
            }

            var          categories     = arguments.Categories == null ? Categories : Categories.Concat(arguments.Categories.Split(',')).ToArray();
            TaggedCommit tagToStartFrom = arguments.AllTags
                ? GitRepositoryInfoFinder.GetFirstCommit(gitRepo)
                : GitRepositoryInfoFinder.GetLastTaggedCommit(gitRepo) ?? GitRepositoryInfoFinder.GetFirstCommit(gitRepo);
            var currentReleaseInfo = GitRepositoryInfoFinder.GetCurrentReleaseInfo(gitRepo);

            if (!string.IsNullOrEmpty(arguments.Version))
            {
                currentReleaseInfo.Name = arguments.Version;
                currentReleaseInfo.When = DateTimeOffset.Now;
            }
            var releaseNotes = ReleaseNotesGenerator.GenerateReleaseNotes(
                gitRepo, issueTracker,
                previousReleaseNotes, categories,
                tagToStartFrom, currentReleaseInfo,
                issueTracker.DiffUrlFormat);

            var releaseNotesOutput = releaseNotes.ToString();

            releaseFileWriter.OutputReleaseNotesFile(releaseNotesOutput, outputFile);

            return(0);
        }