private static bool IncludeDiffType(SuggestedVersionChange prevent, IDiffItem change)
        {
            switch (prevent)
            {
            case SuggestedVersionChange.Major:
                return(change.IsBreakingChange);

            case SuggestedVersionChange.Minor:
                return(change.IsBreakingChange || change.DiffType == DiffType.New);

            case SuggestedVersionChange.Patch:
                return(true);

            default:
                return(false);
            }
        }
Пример #2
0
        private static int Main(string[] args)
        {
            var providers = new AssemblyProviderFactoryCollection(
                new AssemblyProviderFactory(),
                new DirectoryAssemblyProviderFactory(),
                new NuGetAssemblyProviderFactory(new Providers.NuGet.NuGet()),
                new PreviousNuGetAssemblyProviderFactory(new PreviousNugetLocator()),
                new GitHubAssemblyProviderFactory(new Git(Environment.GetEnvironmentVariable("GIT")))
                );

            var exporters = new ExporterCollection(
                new XmlExporter(),
                new MarkdownExporter(),
                new AsciiDocExporter(),
                new GitHubActionCommentExporter()
                );

            var options = new OptionSet
            {
                { "t|target=", "the assembly targets. Defaults to *all* assemblies located by the provider", AddTarget },
                { "f|format=", $"the format of the diff output. Supported formats are {exporters.SupportedFormats}. Defaults to {_format}", f => _format = f },
                { "o|output=", "the output directory or file name. If not specified only prints to console", o => _output = new OutputWriterFactory(o) },
                { "p|prevent-change=", "Fail if the change detected is higher then specified: none|patch|minor|major. Defaults to `none` which will never fail.",
                  c => _preventChange = Enum.Parse <SuggestedVersionChange>(c, true) },
                { "a|allow-empty-previous-nuget=", "",
                  n => _allowEmptyPreviousNuget = n != null },
                { "h|?|help", "show this message and exit", h => _help = h != null },
            };

            if (_help)
            {
                ShowHelp(options, providers);
                return(2);
            }


            if (args.Length < 2)
            {
                ShowHelp(options, providers);
                return(2);
            }

            List <string> unflaggedArgs;

            try
            {
                unflaggedArgs = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Try 'Differ.exe --help' for more information.");
                Environment.ExitCode = 1;
                return(2);
            }

            try
            {
                var firstProvider      = providers.GetProvider(unflaggedArgs[0]);
                var secondProvider     = providers.GetProvider(unflaggedArgs[1]);
                var firstProviderName  = providers.GetProviderName(unflaggedArgs[0]);
                var secondProviderName = providers.GetProviderName(unflaggedArgs[1]);

                if (!exporters.Contains(_format))
                {
                    throw new Exception($"No exporter for format '{_format}'");
                }

                var exporter = exporters[_format];

                var first  = firstProvider.GetAssemblies(Targets).ToList();
                var second = secondProvider.GetAssemblies(Targets).ToList();
                var pairs  = CreateAssemblyPairs(first, second).ToList();

                if (!pairs.Any())
                {
                    if (_allowEmptyPreviousNuget &&
                        (firstProviderName == "previous-nuget" || secondProviderName == "previous-nuget"))
                    {
                        Console.WriteLine($"[diff] No previous nuget found for: {firstProviderName}");
                        Console.WriteLine($"[diff]   {firstProvider.GetType().Name}: {first.Count()} assemblies");
                        Console.WriteLine($"[diff]   {secondProvider.GetType().Name}: {second.Count()} assemblies");
                        Console.WriteLine($"[diff]");
                        Console.WriteLine($"[diff]   NOT treated as an error because --allow-empty-previous-nuget was set");
                        return(0);
                    }

                    Console.Error.WriteLine($"[diff] Unable to create diff!");
                    Console.Error.WriteLine($"[diff]   {firstProvider.GetType().Name}: {first.Count()} assemblies");
                    Console.Error.WriteLine($"[diff]   {secondProvider.GetType().Name}: {second.Count()} assemblies");
                    return(1);
                }

                var result = new AllComparisonResults(pairs, _preventChange);
                foreach (var assemblyPair in pairs)
                {
                    assemblyPair.Diff =
                        APIDiffHelper.GetAPIDifferences(assemblyPair.First.FullName, assemblyPair.Second.FullName);

                    if (assemblyPair.Diff == null)
                    {
                        Console.WriteLine($"[diff] No diff between {assemblyPair.First.FullName} and {assemblyPair.Second.FullName}");
                        continue;
                    }
                    Console.WriteLine($"[diff] Difference found: {firstProvider.GetType().Name}:{assemblyPair.First.Name} and {secondProvider.GetType().Name}:{assemblyPair.Second.Name}");

                    if (exporter is IAssemblyComparisonExporter c)
                    {
                        c.Export(assemblyPair, _output);
                    }
                }
                if (exporter is IAllComparisonResultsExporter allExporter)
                {
                    allExporter.Export(result, _output);
                }

                if (_preventChange > SuggestedVersionChange.None && result.SuggestedVersionChange >= _preventChange)
                {
                    Console.Error.WriteLine($"[diff] Needed version change '{result.SuggestedVersionChange}' exceeds or equals configured lock: '{_preventChange}");
                    return(4);
                }
                Console.WriteLine($"[diff] Suggested version change: {result.SuggestedVersionChange}");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                return(1);
            }
            return(0);
        }
Пример #3
0
 public AllComparisonResults(List <AssemblyComparison> results, SuggestedVersionChange preventChange) =>
 (Comparisons, PreventVersionChange) = (results, preventChange);