Disable code analysis at solution level in Microsoft Visual Studio 2012

前端 未结 5 1628
逝去的感伤
逝去的感伤 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:42

    You can use the Package Manager Console window of Nuget to do that.

    Create a new text file in the directory "C:\Users{your user}\Documents\WindowsPowerShell" named "NuGet_profile.ps1" and add the following code:

    function Disable-CodeAnalysis(){
      ForEach ($project in $dte.Solution.Projects) {
        Set-CodeAnalysis($project, $false)
      }
    }
    
    function Enable-CodeAnalysis(){
      ForEach ($project in $dte.Solution.Projects) {
        Set-CodeAnalysis($project, $true)
      }
    }
    
    function Set-CodeAnalysis($project, $value){
      $projectName = $project.Name
      $projectFilePath = $project.FullName
      if ([System.String]::IsNullOrWhiteSpace($projectFilePath)){
        if($proj.Kind -eq "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"){
          ForEach ($item in $proj.ProjectItems) {
            if($item.SubProject -ne $null){
              Set-CodeAnalysis($item.SubProject, $value)
            }
          }
        }
        continue;
      }
      $xmlDocument = new-object System.Xml.XmlDocument
      $action = "Enable"
      if($value -ne $true){
        $action = "Disable"
      }
    
      Write-Host "$action code analysis for $projectName at $projectFilePath"
      $xmlDocument.Load([string]$projectFilePath);
    
      $namespaceManager = new-object System.Xml.XmlNamespaceManager($xmlDocument.NameTable);
      $namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
    
      $nodes = $xmlDocument.SelectNodes("//ns:RunCodeAnalysis", $namespaceManager);
      if ($nodes -eq $null){
        continue;
      }
      foreach ($node in $nodes){
        $node.InnerText = "$value";
      }
      $xmlDocument.Save($projectFilePath);
    }
    

    Restart Visual Studio. Click the menu "View" | "Other Windows" | "Package Manager Console". Now you can execute the following commands:

    > Enable-CodeAnalysis
    > Disable-CodeAnalysis
    

提交回复
热议问题