示例#1
0
        private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, string language, CancellationToken cancellationToken)
        {
            Console.WriteLine(Path.GetFileName(item));
            string extension = Path.GetExtension(item);

            if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp"))
            {
                using (var workspace = ResponseFileWorkspace.Create())
                {
//                    Project project = workspace.OpenCommandLineProject(item, 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(item, cancellationToken);
//                    await engine.FormatSolutionAsync(solution, cancellationToken);
//                }
            }
            else
            {
//                using (var workspace = MSBuildWorkspace.Create())
//                {
//                    workspace.LoadMetadataForReferencedProjects = true;
//                    var project = await workspace.OpenProjectAsync(item, cancellationToken);
//                    await engine.FormatProjectAsync(project, cancellationToken);
//                }
            }
        }
示例#2
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);
                }
            }
        }
示例#3
0
        private static async Task RunItemAsync(
            IFormattingEngine engine,
            string item,
            string language,
            bool useAnalyzers,
            bool useTabs,
            CancellationToken cancellationToken)
        {
            Console.WriteLine(Path.GetFileName(item));
            string extension = Path.GetExtension(item);

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

                        await engine.FormatSolutionAsync(solution, useAnalyzers, cancellationToken);
                    }
                }
                else
                {
                    using (var workspace = MSBuildWorkspace.Create())
                    {
                        ModifyWorkspaceOptions(workspace, language, useTabs);
                        workspace.LoadMetadataForReferencedProjects = true;
                        var project = await workspace.OpenProjectAsync(item, cancellationToken);

                        await engine.FormatProjectAsync(project, useAnalyzers, cancellationToken);
                    }
                }
            }
            catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
            {
                // Can occur if for example a Mono based project with unknown targets files is supplied
                Console.WriteLine("Invalid project file in target {0}", item);
            }
        }
示例#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,
            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);
                }
            }
        }