forked from SonarSource/sonar-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory.Build.targets
81 lines (70 loc) · 5.67 KB
/
Directory.Build.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="OverrideRoslynCodeAnalysisPropertiesWithSonarAnalyzer"
AfterTargets="ResolveCodeAnalysisRuleSet"
BeforeTargets="CoreCompile">
<PropertyGroup>
<BinariesFolder>$(MSBuildStartupDirectory)\..\packaging\binaries</BinariesFolder>
<AnalysisOutputPath>$(MSBuildStartupDirectory)\output\$(PROJECT)\$(AssemblyName)</AnalysisOutputPath>
<ErrorLogFolder>$(MSBuildStartupDirectory)\output\$(PROJECT)\issues</ErrorLogFolder>
<ErrorLogPrefix Condition="'$(TargetFramework)' == ''">$(ErrorLogFolder)\$(AssemblyName)</ErrorLogPrefix>
<ErrorLogPrefix Condition="'$(TargetFramework)' != ''">$(ErrorLogFolder)\$(AssemblyName)-$(TargetFramework)</ErrorLogPrefix>
<SonarAnalysisConfigPath>$(MSBuildStartupDirectory)\SonarQubeAnalysisConfig.xml</SonarAnalysisConfigPath>
<SonarProjectOutputPath Condition="'$(TargetFramework)' != ''">$(AnalysisOutputPath)\$(TargetFramework)</SonarProjectOutputPath>
<SonarProjectOutputPath Condition="'$(TargetFramework)' == ''">$(AnalysisOutputPath)</SonarProjectOutputPath>
<SonarProjectConfigFilePath>$(SonarProjectOutputPath)\SonarProjectConfig.xml</SonarProjectConfigFilePath>
<SonarFilesToAnalyzePath>$(AnalysisOutputPath)\FilesToAnalyze.txt</SonarFilesToAnalyzePath>
<!-- Only projects with explicit SonarQubeTestProject=true will be classified as test ones. -->
<SonarProjectType Condition="$(SonarQubeTestProject) != 'true'">Product</SonarProjectType>
<SonarProjectType Condition="$(SonarQubeTestProject) == 'true'">Test</SonarProjectType>
<!-- When SonarUnknownProjectType is present and it's true, indicated that we emulate the SonarLint envirorment -->
<SonarProjectType Condition="$(SonarUnknownProjectType) == 'true'">Unknown</SonarProjectType>
</PropertyGroup>
<Error Text="Could not find '$(BinariesFolder)\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.dll'" Condition="!Exists('$(BinariesFolder)\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.dll')" />
<Error Text="Could not find '$(BinariesFolder)\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.dll'" Condition="!Exists('$(BinariesFolder)\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.dll')" />
<!-- This section builds content of SonarProjectConfig.xml additional file. We need to simulate S4MSB behavior to configure the analyzer. -->
<ItemGroup>
<SonarProjectConfigLine Include="<?xml version="1.0" encoding="utf-8"?>" />
<SonarProjectConfigLine Include="<SonarProjectConfig xmlns="http://www.sonarsource.com/msbuild/analyzer/2021/1">" />
<SonarProjectConfigLine Include=" <AnalysisConfigPath>$(SonarAnalysisConfigPath)</AnalysisConfigPath>" />
<SonarProjectConfigLine Include=" <ProjectPath>$(MSBuildProjectFullPath)</ProjectPath>" />
<SonarProjectConfigLine Include=" <FilesToAnalyzePath>$(SonarFilesToAnalyzePath)</FilesToAnalyzePath>" />
<SonarProjectConfigLine Include=" <OutPath>$(SonarProjectOutputPath)</OutPath>" />
<SonarProjectConfigLine Include=" <ProjectType>$(SonarProjectType)</ProjectType>" />
<SonarProjectConfigLine Include=" <TargetFramework>$(TargetFramework)</TargetFramework>" />
<SonarProjectConfigLine Include="</SonarProjectConfig>" />
</ItemGroup>
<MakeDir Directories="$(AnalysisOutputPath)"/>
<MakeDir Directories="$(SonarProjectOutputPath)"/>
<MakeDir Directories="$(ErrorLogFolder)"/>
<!-- First FINDSTR removes \bin\, \obj\ and \packages\ subdirectories from FilesToAnalyze.txt. Second FINDSTR includes only files with an extension, because DIR lists also directories themselves.-->
<Exec Command="dir "$(MSBuildStartupDirectory)\Projects\$(PROJECT)" /s /b | findstr /v "\\bin\\ \\obj\\ \\packages\\" | findstr /e /r "\\[^\\]*\.[^\\]*" > "$(SonarFilesToAnalyzePath)"" Condition="'$(TargetFrameworks)' == ''"/>
<WriteLinesToFile File="$(SonarProjectConfigFilePath)"
Lines="@(SonarProjectConfigLine)"
WriteOnlyWhenDifferent="true"
ContinueOnError="WarnAndContinue"
Overwrite="true" />
<ItemGroup>
<!-- Remove all previously added analyzers, except built-in SourceGenerators -->
<Analyzer Remove="@(Analyzer)" Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('%(Filename)', '(Generator|Generators|SourceGeneration|SourceGenerators)$'))"/>
<!-- Add the SonarAnalyzer analyzer DLLs -->
<Analyzer Include="$(BinariesFolder)\SonarAnalyzer.CSharp\*.dll" />
<Analyzer Include="$(BinariesFolder)\SonarAnalyzer.VisualBasic\*.dll" />
<AdditionalFiles Include="$(MSBuildStartupDirectory)\output\$(PROJECT)\SonarLint.xml" />
<AdditionalFiles Include="$(SonarProjectConfigFilePath)" />
</ItemGroup>
<PropertyGroup>
<!-- Set the path to the SARIF report -->
<ErrorLog>$(ErrorLogPrefix).json</ErrorLog>
<RazorCompilationErrorLog>$(ErrorLogPrefix).Views.json</RazorCompilationErrorLog>
<!-- Override the ruleset with all SonarAnalyzer rules -->
<ResolvedCodeAnalysisRuleSet>$(MSBuildStartupDirectory)\output\AllSonarAnalyzerRules.ruleset</ResolvedCodeAnalysisRuleSet>
<!-- Don't fail the build -->
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors></WarningsAsErrors>
<!-- Keep all messages, including AD0001 -->
<WarningLevel>4</WarningLevel>
<!-- Enable the reporting of analyzer performances -->
<ReportAnalyzer>true</ReportAnalyzer>
</PropertyGroup>
</Target>
</Project>