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
You can use a little trick to disable the static code analysis for a whole Visual Studio instance as described here. In short:
Developer Command Prompt for VS2012
set DevDivCodeAnalysisRunType=Disabled
devenv
to start Visual StudioSame solution works for Visual Studio 2015 via Developer Command Prompt for VS2015
.
Not sure there is an easy to do this with VS2012. CodeAnalysis is defined at project level and depends on your build configuration. For example, there is no code analysis in Release.
First, try to create a configuration based on Release.
Another solution (but very bad) may be to run a batch to modify all your project files.
Here is a sample project file (check the element named RunCodeAnalysis) :
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
To disable code analysis for particular project :-
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<string> 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<string>();
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);
}
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