コード例 #1
0
ファイル: Program.cs プロジェクト: grokys/StyleCopMagic
        private static void Process(IWorkspace workspace)
        {
            ISolution solution = workspace.CurrentSolution;
            ISolution newSolution = solution;
            IEnumerable<IProject> csharpProjects = solution.Projects.Where(x => x.LanguageServices.Language == LanguageNames.CSharp);

            foreach (IProject project in csharpProjects)
            {
                IProject newProject = project;
                string settingsFilePath = Path.Combine(Path.GetDirectoryName(project.FilePath), "Settings.StyleCop");
                ISettings settings = (File.Exists(settingsFilePath)) ? new SettingsFile(settingsFilePath) : new SettingsFile();

                foreach (DocumentId documentId in project.DocumentIds)
                {
                    IDocument document = newSolution.GetDocument(documentId);

                    if (includeFiles == null || includeFiles.Contains(document.Name))
                    {
                        if (document.LanguageServices.Language == LanguageNames.CSharp)
                        {
                            SyntaxTree tree = (SyntaxTree)document.GetSyntaxTree();
                            SyntaxNode contents = tree.GetRoot();

                            if (!RuleRewriter.IsAutogenerated(contents))
                            {
                                Console.WriteLine(document.Name);

                                foreach (Type type in rewriters)
                                {
                                    try
                                    {
                                        RuleRewriter fixer = RuleRewriterFactory.Create(type, settings, () =>
                                            (SemanticModel)newProject.GetCompilation().GetSemanticModel(tree));
                                        contents = fixer.Visit(contents);
                                        document = document.UpdateSyntaxRoot(contents.Format(FormattingOptions.GetDefaultOptions()).GetFormattedRoot());
                                        newProject = document.Project;
                                        newSolution = newProject.Solution;

                                        tree = (SyntaxTree)document.GetSyntaxTree();
                                        contents = tree.GetRoot();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e.Message);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            workspace.ApplyChanges(solution, newSolution);
        }