Esempio n. 1
0
 public FixCommand(
     FixCommandLineOptions options,
     DiagnosticSeverity severityLevel,
     IEnumerable <KeyValuePair <string, string> > diagnosticFixMap,
     IEnumerable <KeyValuePair <string, string> > diagnosticFixerMap,
     FixAllScope fixAllScope,
     in ProjectFilter projectFilter) : base(projectFilter)
Esempio n. 2
0
        private static async Task <int> FixAsync(FixCommandLineOptions options)
        {
            if (!options.TryParseDiagnosticSeverity(CodeFixerOptions.Default.SeverityLevel, out DiagnosticSeverity severityLevel))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixMap, out List <KeyValuePair <string, string> > diagnosticFixMap))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixerMap, out List <KeyValuePair <string, string> > diagnosticFixerMap))
            {
                return(1);
            }

            if (!options.TryGetProjectFilter(out ProjectFilter projectFilter))
            {
                return(1);
            }

            var command = new FixCommand(
                options: options,
                severityLevel: severityLevel,
                diagnosticFixMap: diagnosticFixMap,
                diagnosticFixerMap: diagnosticFixerMap,
                projectFilter: projectFilter);

            CommandResult result = await command.ExecuteAsync(options.Path, options.MSBuildPath, options.Properties);

            return((result.Kind == CommandResultKind.Success) ? 0 : 1);
        }
Esempio n. 3
0
        private static async Task <int> FixAsync(FixCommandLineOptions options)
        {
            if (!options.TryGetDiagnosticSeverity(CodeFixerOptions.Default.SeverityLevel, out DiagnosticSeverity severityLevel))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixMap, out Dictionary <string, string> diagnosticFixMap))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixerMap, out Dictionary <string, string> diagnosticFixerMap))
            {
                return(1);
            }

            if (!options.TryGetLanguage(out string language))
            {
                return(1);
            }

            var command = new FixCommand(
                options: options,
                severityLevel: severityLevel,
                diagnosticFixMap: diagnosticFixMap?.ToImmutableDictionary() ?? ImmutableDictionary <string, string> .Empty,
                diagnosticFixerMap: diagnosticFixerMap?.ToImmutableDictionary() ?? ImmutableDictionary <string, string> .Empty,
                language: language);

            CommandResult result = await command.ExecuteAsync(options.Path, options.MSBuildPath, options.Properties);

            return((result.Kind == CommandResultKind.Success) ? 0 : 1);
        }
Esempio n. 4
0
 public FixCommand(
     FixCommandLineOptions options,
     DiagnosticSeverity severityLevel,
     ImmutableDictionary <string, string> diagnosticFixMap,
     ImmutableDictionary <string, string> diagnosticFixerMap,
     string language) : base(language)
 {
     Options            = options;
     SeverityLevel      = severityLevel;
     DiagnosticFixMap   = diagnosticFixMap;
     DiagnosticFixerMap = diagnosticFixerMap;
 }
Esempio n. 5
0
        private static async Task <int> FixAsync(FixCommandLineOptions options)
        {
            if (!options.TryParseDiagnosticSeverity(CodeFixerOptions.Default.SeverityLevel, out DiagnosticSeverity severityLevel))
            {
                return(ExitCodes.Error);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixMap, out List <KeyValuePair <string, string> > diagnosticFixMap))
            {
                return(ExitCodes.Error);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixerMap, out List <KeyValuePair <string, string> > diagnosticFixerMap))
            {
                return(ExitCodes.Error);
            }

            if (!TryParseOptionValueAsEnum(options.FixScope, OptionNames.FixScope, out FixAllScope fixAllScope, FixAllScope.Project))
            {
                return(ExitCodes.Error);
            }

            if (!options.TryGetProjectFilter(out ProjectFilter projectFilter))
            {
                return(ExitCodes.Error);
            }

            if (!TryParsePaths(options.Paths, out ImmutableArray <string> paths))
            {
                return(ExitCodes.Error);
            }

            var command = new FixCommand(
                options: options,
                severityLevel: severityLevel,
                diagnosticFixMap: diagnosticFixMap,
                diagnosticFixerMap: diagnosticFixerMap,
                fixAllScope: fixAllScope,
                projectFilter: projectFilter);

            CommandStatus status = await command.ExecuteAsync(paths, options.MSBuildPath, options.Properties);

            return(GetExitCode(status));
        }
Esempio n. 6
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);
        }