Example #1
0
        private static async Task <int> FixAsync(FixCommandLineOptions options)
        {
            var cts = new CancellationTokenSource();

            Console.CancelKeyPress += (sender, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
            };

            CancellationToken cancellationToken = cts.Token;

            if (options.MSBuildPath != null)
            {
                MSBuildLocator.RegisterMSBuildPath(options.MSBuildPath);
            }
            else
            {
                VisualStudioInstance instance = MSBuildLocator.QueryVisualStudioInstances()
                                                .OrderBy(f => f.Version)
                                                .LastOrDefault();

                if (instance == null)
                {
                    WriteLine("MSBuild location not found. Use option '--msbuild-path' to specify MSBuild location", ConsoleColor.Red);
                    return(1);
                }

                WriteLine($"MSBuild location is '{instance.MSBuildPath}'");

                MSBuildLocator.RegisterInstance(instance);
            }

            var properties = new Dictionary <string, string>();

            foreach (string property in options.Properties)
            {
                int index = property.IndexOf("=");

                if (index == -1)
                {
                    WriteLine($"Unable to parse property '{property}'", ConsoleColor.Red);
                    return(1);
                }

                string key = property.Substring(0, index);

                properties[key] = property.Substring(index + 1);
            }

            if (properties.Count > 0)
            {
                WriteLine("Add MSBuild properties");

                int maxLength = properties.Max(f => f.Key.Length);

                foreach (KeyValuePair <string, string> kvp in properties)
                {
                    WriteLine($"  {kvp.Key.PadRight(maxLength)} = {kvp.Value}");
                }
            }

            // https://github.com/Microsoft/MSBuildLocator/issues/16
            if (!properties.ContainsKey("AlwaysCompileMarkupFilesInSeparateDomain"))
            {
                properties["AlwaysCompileMarkupFilesInSeparateDomain"] = bool.FalseString;
            }

            using (MSBuildWorkspace workspace = MSBuildWorkspace.Create(properties))
            {
                workspace.WorkspaceFailed += (o, e) => WriteLine(e.Diagnostic.Message, ConsoleColor.Yellow);

                string solutionPath = options.SolutionPath;

                WriteLine($"Load solution '{solutionPath}'", ConsoleColor.Cyan);

                try
                {
                    Solution solution;

                    try
                    {
                        solution = await workspace.OpenSolutionAsync(solutionPath, new ConsoleProgressReporter(), cancellationToken);
                    }
                    catch (Exception ex)
                    {
                        if (ex is FileNotFoundException ||
                            ex is InvalidOperationException)
                        {
                            WriteLine(ex.ToString(), ConsoleColor.Red);
                            return(1);
                        }
                        else
                        {
                            throw;
                        }
                    }

                    WriteLine($"Done loading solution '{solutionPath}'", ConsoleColor.Green);

                    var codeFixerOptions = new CodeFixerOptions(
                        ignoreCompilerErrors: options.IgnoreCompilerErrors,
                        ignoreAnalyzerReferences: options.IgnoreAnalyzerReferences,
                        ignoredDiagnosticIds: options.IgnoredDiagnostics,
                        ignoredCompilerDiagnosticIds: options.IgnoredCompilerDiagnostics,
                        ignoredProjectNames: options.IgnoredProjects,
                        batchSize: options.BatchSize);

                    var codeFixer = new CodeFixer(workspace, analyzerAssemblies: options.AnalyzerAssemblies, options: codeFixerOptions);

                    await codeFixer.FixAsync(cancellationToken);
                }
                catch (OperationCanceledException)
                {
                    WriteLine("Fixing was canceled.");
                }
            }

            return(0);
        }