Exemplo n.º 1
0
        private static async Task <int> RunAsync(CommandLineOptions options, CancellationToken cancellationToken)
        {
            var assemblies = OptionsHelper.DefaultCompositionAssemblies;
            var engine     = FormattingEngine.Create(assemblies);

            var configBuilder = ImmutableArray.CreateBuilder <string[]>();

            configBuilder.Add(options.PreprocessorConfigurations.ToArray());
            engine.PreprocessorConfigurations = configBuilder.ToImmutableArray();

            engine.FormattingOptionsFilePath = options.OptionsFilePath;
            engine.Verbose         = options.Verbose;
            engine.AllowTables     = options.DefineDotNetFormatter;
            engine.FileNames       = options.FileFilters.ToImmutableArray();
            engine.CopyrightHeader = options.CopyrightHeaderText;
            engine.ApplyFixes      = options.ApplyFixes;
            engine.LogOutputPath   = options.LogOutputPath;

            if (options.TargetAnalyzers != null && options.TargetAnalyzerText != null && options.TargetAnalyzerText.Count() > 0)
            {
                AddCustomAnalyzers(engine, options.TargetAnalyzerText);
            }

            // Analyzers will hydrate rule enabled/disabled settings
            // directly from the options referenced by file path
            // in options.OptionsFilePath
            if (!options.UseAnalyzers)
            {
                if (!SetRuleMap(engine, options.RuleMap))
                {
                    return(FAILED);
                }
            }

            foreach (var item in options.Targets)
            {
                // target was a text file with a list of project files to run against
                if (StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(item), ".txt"))
                {
                    var targets = File.ReadAllLines(item);
                    foreach (var target in targets)
                    {
                        try
                        {
                            await RunItemAsync(engine, target, options.Language, options.UseAnalyzers, options.UseTabs, cancellationToken);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception: {0} with project {1}", e.Message, target);
                        }
                    }
                }
                else
                {
                    await RunItemAsync(engine, item, options.Language, options.UseAnalyzers, options.UseTabs, cancellationToken);
                }
            }

            return(SUCCEEDED);
        }
Exemplo n.º 2
0
        private static async Task RunAsync(
            string projectOrSolutionPath,
            ImmutableArray <string> ruleTypes,
            ImmutableArray <string> fileNames,
            ImmutableArray <string[]> preprocessorConfigurations,
            ImmutableArray <string> copyrightHeader,
            CancellationToken cancellationToken)
        {
            var workspace = MSBuildWorkspace.Create();
            var engine    = FormattingEngine.Create(ruleTypes);

            engine.PreprocessorConfigurations = preprocessorConfigurations;
            engine.FileNames       = fileNames;
            engine.CopyrightHeader = copyrightHeader;

            string extension = Path.GetExtension(projectOrSolutionPath);

            if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
            {
                var solution = await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);

                await engine.FormatSolutionAsync(solution, cancellationToken);
            }
            else
            {
                workspace.LoadMetadataForReferencedProjects = true;
                var project = await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);

                await engine.FormatProjectAsync(project, cancellationToken);
            }
        }
Exemplo n.º 3
0
 public CombinationTest()
 {
     _formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create();
     _formattingEngine.CopyrightHeader            = ImmutableArray.Create("// header");
     _formattingEngine.AllowTables                = true;
     _formattingEngine.FormatLogger               = new EmptyFormatLogger();
     _formattingEngine.PreprocessorConfigurations = ImmutableArray <string[]> .Empty;
 }
Exemplo n.º 4
0
        private static async Task RunAsync(
            string projectSolutionOrRspPath,
            ImmutableArray <string> ruleTypes,
            ImmutableArray <string> fileNames,
            ImmutableArray <string[]> preprocessorConfigurations,
            ImmutableArray <string> copyrightHeader,
            string language,
            bool allowTables,
            bool convertUnicode,
            bool verbose,
            FormattingLevel formattingLevel,
            CancellationToken cancellationToken)
        {
            var engine = FormattingEngine.Create(ruleTypes);

            engine.PreprocessorConfigurations = preprocessorConfigurations;
            engine.FileNames                = fileNames;
            engine.CopyrightHeader          = copyrightHeader;
            engine.AllowTables              = allowTables;
            engine.ConvertUnicodeCharacters = convertUnicode;
            engine.Verbose         = verbose;
            engine.FormattingLevel = formattingLevel;

            Console.WriteLine(Path.GetFileName(projectSolutionOrRspPath));
            string extension = Path.GetExtension(projectSolutionOrRspPath);

            if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp"))
            {
                using (var workspace = ResponseFileWorkspace.Create())
                {
                    Project project = workspace.OpenCommandLineProject(projectSolutionOrRspPath, language);
                    await engine.FormatProjectAsync(project, cancellationToken);
                }
            }
            else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
            {
                using (var workspace = MSBuildWorkspace.Create())
                {
                    workspace.LoadMetadataForReferencedProjects = true;
                    var solution = await workspace.OpenSolutionAsync(projectSolutionOrRspPath, cancellationToken);

                    await engine.FormatSolutionAsync(solution, cancellationToken);
                }
            }
            else
            {
                using (var workspace = MSBuildWorkspace.Create())
                {
                    workspace.LoadMetadataForReferencedProjects = true;
                    var project = await workspace.OpenProjectAsync(projectSolutionOrRspPath, cancellationToken);

                    await engine.FormatProjectAsync(project, cancellationToken);
                }
            }
        }
Exemplo n.º 5
0
        private static async Task <int> RunFormatAsync(CommandLineOptions options, CancellationToken cancellationToken)
        {
            var engine = FormattingEngine.Create();

            engine.PreprocessorConfigurations = options.PreprocessorConfigurations;
            engine.FileNames       = options.FileNames;
            engine.CopyrightHeader = options.CopyrightHeader;
            engine.AllowTables     = options.AllowTables;
            engine.Verbose         = options.Verbose;

            if (!SetRuleMap(engine, options.RuleMap))
            {
                return(1);
            }

            foreach (var item in options.FormatTargets)
            {
                await RunFormatItemAsync(engine, item, options.Language, cancellationToken);
            }

            return(0);
        }
Exemplo n.º 6
0
 static CombinationTest()
 {
     s_formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create(ImmutableArray <string> .Empty);
 }
Exemplo n.º 7
0
        private static async Task RunAsync(
            string projectSolutionOrRspPath,
            ImmutableArray <string> ruleTypes,
            ImmutableArray <string> fileNames,
            ImmutableArray <string[]> preprocessorConfigurations,
            ImmutableArray <string> copyrightHeader,
            string language,
            bool allowTables,
            bool convertUnicode,
            bool verbose,
            CancellationToken cancellationToken)
        {
            var engine = FormattingEngine.Create(ruleTypes);

            engine.PreprocessorConfigurations = preprocessorConfigurations;
            engine.FileNames                = fileNames;
            engine.CopyrightHeader          = copyrightHeader;
            engine.AllowTables              = allowTables;
            engine.ConvertUnicodeCharacters = convertUnicode;
            engine.Verbose = verbose;

            Console.WriteLine(Path.GetFileName(projectSolutionOrRspPath));
            string extension = Path.GetExtension(projectSolutionOrRspPath);

            if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp"))
            {
                using (var workspace = ResponseFileWorkspace.Create())
                {
                    Project project = workspace.OpenCommandLineProject(projectSolutionOrRspPath, language);
                    await engine.FormatProjectAsync(project, cancellationToken);
                }
            }
            else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
            {
                using (var workspace = MSBuildWorkspace.Create())
                {
                    workspace.LoadMetadataForReferencedProjects = true;
                    workspace.Options = workspace.Options
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInMethods, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInAnonymousMethods, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInAnonymousTypes, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInControlBlocks, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInLambdaExpressionBody, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInObjectInitializers, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInTypes, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLineForCatch, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLineForElse, false)
                                        .WithChangedOption(CSharpFormattingOptions.NewLineForFinally, false)
                                        .WithChangedOption(CSharpFormattingOptions.SpaceAfterComma, true)
                                        .WithChangedOption(CSharpFormattingOptions.SpaceAfterColonInBaseTypeDeclaration, true)
                                        .WithChangedOption(CSharpFormattingOptions.SpaceAfterControlFlowStatementKeyword, true)
                                        .WithChangedOption(CSharpFormattingOptions.SpaceBeforeColonInBaseTypeDeclaration, true)
                                        .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, true)
                                        .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, 4);

                    var solution = await workspace.OpenSolutionAsync(projectSolutionOrRspPath, cancellationToken);

                    await engine.FormatSolutionAsync(solution, cancellationToken);
                }
            }
            else
            {
                using (var workspace = MSBuildWorkspace.Create())
                {
                    workspace.LoadMetadataForReferencedProjects = true;
                    var project = await workspace.OpenProjectAsync(projectSolutionOrRspPath, cancellationToken);

                    await engine.FormatProjectAsync(project, cancellationToken);
                }
            }
        }