Disable code analysis at solution level in Microsoft Visual Studio 2012

前端 未结 5 1638
逝去的感伤
逝去的感伤 2020-12-24 08:47

In our product, there are around 400 projects, so in VS 2012, if I want to make a build then it generates code analysis for all 400 projects and I can\'t manually disable co

5条回答
  •  心在旅途
    2020-12-24 09:41

    You could write a little console application that reads all the project files out of a solution file and then toggle the Xml Node of each project.

    Function to get the project files out of the solution:

    public IEnumerable Parse(string solutionFile)
    {
        if (solutionFile == null)
            throw new ArgumentNullException("solutionFile");
    
        if (!File.Exists(solutionFile))
            throw new FileNotFoundException("Solution file does not exist", solutionFile);
    
        var projectFiles = new List();
    
        using (var reader = new StreamReader(solutionFile, true))
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (line == null)
                    continue;
    
                line = line.TrimStart();
                if (!line.StartsWith("Project(", StringComparison.OrdinalIgnoreCase)) 
                    continue;
    
                var projectData = line.Split(',');
                var projectFile = projectData[1].Trim().Trim('"');
                if (!string.IsNullOrEmpty(Path.GetExtension(projectFile)))
                    projectFiles.Add(projectFile);
            }
        }
    
        return projectFiles;
    }
    

    And the function to toggle the RunCodeAnalysis Node(s):

    public void ToggleCodeAnalysis(string projectFile)
    {
        if (projectFile == null)
            throw new ArgumentNullException("projectFile");
    
        if (!File.Exists(projectFile))
            throw new FileNotFoundException("Project file does not exist", projectFile);
    
        var xmlDocument = new XmlDocument();
        xmlDocument.Load(projectFile);
    
        var namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
        namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
    
        var nodes = xmlDocument.SelectNodes("//ns:RunCodeAnalysis", namespaceManager);
        if (nodes == null)
            return;
    
        var hasChanged = false;
        foreach (XmlNode node in nodes)
        {
            bool value;
            if (!Boolean.TryParse(node.InnerText, out value))
                continue;
    
            node.InnerText = value ? "false" : "true";
            hasChanged = true;
        }
    
        if (!hasChanged)
            return;
    
        xmlDocument.Save(projectFile);
    }
    

提交回复
热议问题