Exemple #1
0
        private static void AddCompileItemsToProjectFile(string projFile, ProjectKind kind, CheckData check)
        {
            XDocument xmlFile = XDocument.Load(projFile);

            Debug.Assert(xmlFile.Root.Name.LocalName == "Project");

            var first = xmlFile.Root.Descendants().Where(c => c.Name.LocalName == "Compile").First();

            var      parent = first.Parent;
            XElement elem   = null;

            switch (kind)
            {
            case ProjectKind.Core:
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateAnalyzerFileName(check));
                parent.Add(elem);
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateCodeFixFileName(check));
                parent.Add(elem);
                break;

            case ProjectKind.CSharp:
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateCSharpAnalyzerFileName(check));
                parent.Add(elem);
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateCSharpCodeFixFileName(check));
                parent.Add(elem);
                break;

            case ProjectKind.VisualBasic:
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateBasicAnalyzerFileName(check));
                parent.Add(elem);
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateBasicCodeFixFileName(check));
                parent.Add(elem);
                break;

            case ProjectKind.UnitTests:
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateAnalyzerTestsFileName(check));
                parent.Add(elem);
                elem = new XElement(first);
                elem.SetAttributeValue("Include", CodeTemplates.GenerateCodeFixTestsFileName(check));
                parent.Add(elem);
                break;

            default:
                throw new ArgumentException(kind.ToString());
            }
            xmlFile.Save(projFile);
            return;
        }
Exemple #2
0
        public static void CreateStubFiles(ProjectKind kind, string target, string analyzer, IList <CheckData> checks, Dictionary <string, IEnumerable <string> > categories)
        {
            if (kind == ProjectKind.Setup || kind == ProjectKind.NuGet)
            {
                return;
            }

            foreach (var check in checks)
            {
                if (kind == ProjectKind.Core)
                {
                    Utilities.CreateFile(CodeTemplates.GenerateAnalyzer(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateAnalyzerFileName(check));
                    Utilities.CreateFile(CodeTemplates.GenerateCodeFix(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateCodeFixFileName(check));
                    Utilities.CreateFile(CodeTemplates.GenerateCategory(analyzer, categories[analyzer]),
                                         target,
                                         CodeTemplates.CategoryFileName);
                }
                else if (kind == ProjectKind.CSharp)
                {
                    Utilities.CreateFile(CodeTemplates.GenerateCSharpAnalyzer(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateCSharpAnalyzerFileName(check));
                    Utilities.CreateFile(CodeTemplates.GenerateCSharpCodeFix(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateCSharpCodeFixFileName(check));
                }
                else if (kind == ProjectKind.VisualBasic)
                {
                    Utilities.CreateFile(CodeTemplates.GenerateBasicAnalyzer(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateBasicAnalyzerFileName(check));
                    Utilities.CreateFile(CodeTemplates.GenerateBasicCodeFix(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateBasicCodeFixFileName(check));
                }
                else if (kind == ProjectKind.UnitTests)
                {
                    Utilities.CreateFile(CodeTemplates.GenerateAnalyzerTests(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateAnalyzerTestsFileName(check));
                    Utilities.CreateFile(CodeTemplates.GenerateCodeFixTests(analyzer, check),
                                         target,
                                         CodeTemplates.GenerateCodeFixTestsFileName(check));
                }
            }
        }
Exemple #3
0
        private static void AddNewChecksToCoreProject(string analyzer, List <CheckData> checks)
        {
            var analyzerFullName = analyzer + ".Analyzers";
            var analyzerDir      = Path.Combine(_sourceFilesDirectory, analyzerFullName, "Core");
            var coreProjFile     = Path.Combine(analyzerDir, analyzerFullName + ".csproj");

            foreach (var check in checks)
            {
                AddCompileItemsToProjectFile(coreProjFile, ProjectKind.Core, check);

                Utilities.CreateFile(CodeTemplates.GenerateAnalyzer(analyzer, check),
                                     analyzerDir,
                                     CodeTemplates.GenerateAnalyzerFileName(check));

                Utilities.CreateFile(CodeTemplates.GenerateCodeFix(analyzer, check),
                                     analyzerDir,
                                     CodeTemplates.GenerateCodeFixFileName(check));
            }
        }
Exemple #4
0
        public static string CopyRenamedFile(string source, string target, string fileName, string analyzer, IList <CheckData> checks, Dictionary <string, IEnumerable <string> > categories, Dictionary <string, Dictionary <string, string> > projectGuids)
        {
            string fileContents = File.ReadAllText(source + @"\" + fileName);

            fileContents = fileContents.Replace("REPLACE.ME", analyzer);
            fileContents = fileContents.Replace("REPLACEME", analyzer.Replace(".", String.Empty));

            if (fileName == "REPLACEMEAnalyzersResources.resx")
            {
                var sb = new StringBuilder();
                // add diagnostic resource strings
                foreach (var check in checks)
                {
                    sb.Append(CodeTemplates.GenerateDiagnosticResourceData(check));
                }

                Debug.Assert(categories != null && categories.ContainsKey(analyzer));
                // add category resource strings
                sb.Append(CodeTemplates.GenerateCategoriesResourceData(categories[analyzer]));

                fileContents = fileContents.Replace("INSERTRESOURCEDATA", sb.ToString());
            }
            else if (fileName == "REPLACE.ME.Analyzers.csproj")
            {
                var sb = new StringBuilder();
                foreach (var check in checks)
                {
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateAnalyzerFileName(check)));
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateCodeFixFileName(check)));
                }
                sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.CategoryFileName));
                fileContents = fileContents.Replace("INSERTSOURCEFILES", sb.ToString());
            }
            else if (fileName == "REPLACE.ME.CSharp.Analyzers.csproj")
            {
                var sb = new StringBuilder();
                foreach (var check in checks)
                {
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateCSharpAnalyzerFileName(check)));
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateCSharpCodeFixFileName(check)));
                }
                fileContents = fileContents.Replace("INSERTSOURCEFILES", sb.ToString());
            }
            else if (fileName == "REPLACE.ME.VisualBasic.Analyzers.vbproj")
            {
                var sb = new StringBuilder();
                foreach (var check in checks)
                {
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateBasicAnalyzerFileName(check)));
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateBasicCodeFixFileName(check)));
                }
                fileContents = fileContents.Replace("INSERTSOURCEFILES", sb.ToString());
            }
            else if (fileName == "REPLACE.ME.Analyzers.UnitTests.csproj")
            {
                var sb = new StringBuilder();
                foreach (var check in checks)
                {
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateAnalyzerTestsFileName(check)));
                    sb.Append(CodeTemplates.GenerateCompileItem(CodeTemplates.GenerateCodeFixTestsFileName(check)));
                }
                fileContents = fileContents.Replace("INSERTSOURCEFILES", sb.ToString());
            }

            fileName = fileName.Replace("REPLACE.ME", analyzer);
            fileName = fileName.Replace("REPLACEME", analyzer.Replace(".", String.Empty));

            string guid = GetGuid();

            fileContents = fileContents.Replace("INSERTGUID", guid);
            // a new GUID for product ID in vsixmanifest only
            fileContents = fileContents.Replace("INSERTNOBRACESGUID", Guid.NewGuid().ToString());

            // insert GUIDs of referenced projects
            if (fileName.EndsWith("proj"))
            {
                foreach (var projectKind in Enum.GetValues(typeof(ProjectKind)).Cast <ProjectKind>())
                {
                    string sub;
                    switch (projectKind)
                    {
                    case ProjectKind.Core:
                        sub = "CORE";
                        break;

                    case ProjectKind.CSharp:
                        sub = "CSHARP";
                        break;

                    case ProjectKind.VisualBasic:
                        sub = "VB";
                        break;

                    case ProjectKind.UnitTests:
                    case ProjectKind.Setup:
                    case ProjectKind.NuGet:
                        continue;

                    default:
                        throw new InvalidOperationException();
                    }
                    string toReplace = string.Format("INSERT{0}GUID", sub);

                    // project GUIDs for this kind
                    Dictionary <string, string> kindGuids;
                    if (projectGuids.TryGetValue(projectKind.ToString(), out kindGuids))
                    {
                        string projectGuid;
                        if (kindGuids.TryGetValue(analyzer, out projectGuid))
                        {
                            fileContents = fileContents.Replace(toReplace, projectGuid);
                        }
                    }
                }
            }

            CreateFile(fileContents, target, fileName);
            return(guid);
        }