未验证 提交 4511a45b 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #49967 from mavasani/CodeStyleConfigFileGenerator

Add tooling to CodeStyle layer to generate global configs
......@@ -127,10 +127,16 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.CodeStyle", "src\CodeStyle\CSharp\Analyzers\Microsoft.CodeAnalysis.CSharp.CodeStyle.csproj", "{AA87BFED-089A-4096-B8D5-690BDC7D5B24}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes", "src\CodeStyle\CSharp\CodeFixes\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.csproj", "{A07ABCF5-BC43-4EE9-8FD8-B2D77FD54D73}"
ProjectSection(ProjectDependencies) = postProject
{432F4461-E198-44AA-8022-9E8C06A17C93} = {432F4461-E198-44AA-8022-9E8C06A17C93}
EndProjectSection
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.VisualBasic.CodeStyle", "src\CodeStyle\VisualBasic\Analyzers\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.vbproj", "{2531A8C4-97DD-47BC-A79C-B7846051E137}"
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes", "src\CodeStyle\VisualBasic\CodeFixes\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.vbproj", "{0141285D-8F6C-42C7-BAF3-3C0CCD61C716}"
ProjectSection(ProjectDependencies) = postProject
{432F4461-E198-44AA-8022-9E8C06A17C93} = {432F4461-E198-44AA-8022-9E8C06A17C93}
EndProjectSection
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "CSharpAnalyzerDriver", "src\Compilers\CSharp\CSharpAnalyzerDriver\CSharpAnalyzerDriver.shproj", "{54E08BF5-F819-404F-A18D-0AB9EA81EA04}"
EndProject
......@@ -158,6 +164,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuildValidator", "src\Tools
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrepareTests", "src\Tools\PrepareTests\PrepareTests.csproj", "{9B25E472-DF94-4E24-9F5D-E487CE5A91FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeStyleConfigFileGenerator", "src\CodeStyle\Tools\CodeStyleConfigFileGenerator.csproj", "{432F4461-E198-44AA-8022-9E8C06A17C93}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.CodeAnalysis.Collections", "src\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.shproj", "{E919DD77-34F8-4F57-8058-4D3FF4C2B241}"
EndProject
Global
......@@ -454,6 +462,10 @@ Global
{9B25E472-DF94-4E24-9F5D-E487CE5A91FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B25E472-DF94-4E24-9F5D-E487CE5A91FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B25E472-DF94-4E24-9F5D-E487CE5A91FB}.Release|Any CPU.Build.0 = Release|Any CPU
{432F4461-E198-44AA-8022-9E8C06A17C93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{432F4461-E198-44AA-8022-9E8C06A17C93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{432F4461-E198-44AA-8022-9E8C06A17C93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{432F4461-E198-44AA-8022-9E8C06A17C93}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -527,6 +539,7 @@ Global
{706CFC25-B6E0-4DAA-BCC4-F6FAAFEEDF87} = {3FF38FD4-DF16-44B0-924F-0D5AE155495B}
{E9211052-7700-4A2F-B1AE-34FD5CCAB8AF} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
{9B25E472-DF94-4E24-9F5D-E487CE5A91FB} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
{432F4461-E198-44AA-8022-9E8C06A17C93} = {B20208C3-D3A6-4020-A274-6BE3786D29FB}
{E919DD77-34F8-4F57-8058-4D3FF4C2B241} = {3CDEA9FB-CD44-4AB4-98A8-5537AAA2169B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
......
......@@ -315,10 +315,16 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.CodeStyle", "src\CodeStyle\CSharp\Analyzers\Microsoft.CodeAnalysis.CSharp.CodeStyle.csproj", "{AA87BFED-089A-4096-B8D5-690BDC7D5B24}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes", "src\CodeStyle\CSharp\CodeFixes\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.csproj", "{A07ABCF5-BC43-4EE9-8FD8-B2D77FD54D73}"
ProjectSection(ProjectDependencies) = postProject
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0} = {41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}
EndProjectSection
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.VisualBasic.CodeStyle", "src\CodeStyle\VisualBasic\Analyzers\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.vbproj", "{2531A8C4-97DD-47BC-A79C-B7846051E137}"
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes", "src\CodeStyle\VisualBasic\CodeFixes\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.vbproj", "{0141285D-8F6C-42C7-BAF3-3C0CCD61C716}"
ProjectSection(ProjectDependencies) = postProject
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0} = {41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities", "src\CodeStyle\Core\Tests\Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj", "{9FF1205F-1D7C-4EE4-B038-3456FE6EBEAF}"
EndProject
......@@ -475,6 +481,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.XunitHook", "src\EditorFeatures\XunitHook\Microsoft.CodeAnalysis.XunitHook.csproj", "{967A8F5E-7D18-436C-97ED-1DB303FE5DE0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeStyleConfigFileGenerator", "src\CodeStyle\Tools\CodeStyleConfigFileGenerator.csproj", "{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.CodeAnalysis.Collections", "src\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.shproj", "{E919DD77-34F8-4F57-8058-4D3FF4C2B241}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.Collections.Package", "src\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.Package.csproj", "{0C2E1633-1462-4712-88F4-A0C945BAD3A8}"
......@@ -1249,6 +1257,10 @@ Global
{967A8F5E-7D18-436C-97ED-1DB303FE5DE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{967A8F5E-7D18-436C-97ED-1DB303FE5DE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{967A8F5E-7D18-436C-97ED-1DB303FE5DE0}.Release|Any CPU.Build.0 = Release|Any CPU
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0}.Release|Any CPU.Build.0 = Release|Any CPU
{0C2E1633-1462-4712-88F4-A0C945BAD3A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C2E1633-1462-4712-88F4-A0C945BAD3A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C2E1633-1462-4712-88F4-A0C945BAD3A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -1472,6 +1484,7 @@ Global
{5D94ED65-EFA3-44EB-A5DA-62E85BAC9DD6} = {A41D1B99-F489-4C43-BBDF-96D61B19A6B9}
{21B50E65-D601-4D82-B98A-FFE6DE3B25DC} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{967A8F5E-7D18-436C-97ED-1DB303FE5DE0} = {EE97CB90-33BB-4F3A-9B3D-69375DEC6AC6}
{41ED1BFA-FDAD-4FE4-8118-DB23FB49B0B0} = {DC014586-8D07-4DE6-B28E-C0540C59C085}
{E919DD77-34F8-4F57-8058-4D3FF4C2B241} = {C2D1346B-9665-4150-B644-075CF1636BAA}
{0C2E1633-1462-4712-88F4-A0C945BAD3A8} = {C2D1346B-9665-4150-B644-075CF1636BAA}
EndGlobalSection
......
......@@ -29,6 +29,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AnalyzerHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DeserializationConstructorCheck.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DiagnosticHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EnforceOnBuild.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IDEDiagnosticIds.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IDEDiagnosticIdToOptionMappingHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RemoveRedundantEquality\AbstractRemoveRedundantEqualityDiagnosticAnalyzer.cs" />
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.CodeAnalysis.CodeStyle
{
/// <summary>
/// Build enforcement recommendation for a code style analyzer.
/// </summary>
internal enum EnforceOnBuild
{
Never,
WhenExplicitlyEnabled,
Recommended,
HighlyRecommended,
}
internal static class EnforceOnBuildExtensions
{
public static string ToCustomTag(this EnforceOnBuild enforceOnBuild)
=> $"{nameof(EnforceOnBuild)}_{enforceOnBuild}";
}
}
......@@ -21,15 +21,29 @@
</PropertyGroup>
<Target Name="_GetFilesToPackage">
<PropertyGroup>
<CodeStyleAnalyzerArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)</CodeStyleAnalyzerArtifactsBinDir>
<CodeStyleFixesArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)</CodeStyleFixesArtifactsBinDir>
<CSharpCodeStyleAnalyzerArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle\$(Configuration)\$(TargetFramework)</CSharpCodeStyleAnalyzerArtifactsBinDir>
<CSharpCodeStyleFixesArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)</CSharpCodeStyleFixesArtifactsBinDir>
<CSharpCodeStyleTargetsFileName>Microsoft.CodeAnalysis.CSharp.CodeStyle.targets</CSharpCodeStyleTargetsFileName>
<DotNetExecutable Condition="'$(OS)' == 'Windows_NT'">$(DotNetRoot)dotnet.exe</DotNetExecutable>
<DotNetExecutable Condition="'$(DotNetExecutable)' == ''">$(DotNetRoot)dotnet</DotNetExecutable>
</PropertyGroup>
<Exec Command='"$(DotNetExecutable)" "$(ArtifactsBinDir)CodeStyleConfigFileGenerator\$(Configuration)\netcoreapp3.1\CodeStyleConfigFileGenerator.dll" "CSharp" "$(CSharpCodeStyleFixesArtifactsBinDir)" "$(CSharpCodeStyleTargetsFileName)" "$(CodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.dll;$(CSharpCodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll"' />
<ItemGroup>
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CodeStyle.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CSharp.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CSharpCodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CSharpCodeStyleFixesArtifactsBinDir)\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CodeStyleFixesArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CSharpCodeStyleAnalyzerArtifactsBinDir)\**\Microsoft.CodeAnalysis.CSharp.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CSharpCodeStyleFixesArtifactsBinDir)\**\Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CodeStyleAnalyzerArtifactsBinDir)\**\Microsoft.CodeAnalysis.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CodeStyleFixesArtifactsBinDir)\**\Microsoft.CodeAnalysis.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/cs" />
<_File Include="$(CSharpCodeStyleFixesArtifactsBinDir)\$(CSharpCodeStyleTargetsFileName)" TargetDir="build" />
<_File Include="$(CSharpCodeStyleFixesArtifactsBinDir)\config\**\*.*" TargetDir="build/config" />
<_File Include="$(MSBuildProjectDirectory)\build\**\*.*" TargetDir="build" />
<TfmSpecificPackageFile Include="@(_File)" PackagePath="%(_File.TargetDir)/%(_File.RecursiveDir)%(_File.FileName)%(_File.Extension)" />
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NonShipping>true</NonShipping>
<UseAppHost>false</UseAppHost>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\Analyzers\Core\Analyzers\EnforceOnBuild.cs" Link="EnforceOnBuild.cs" />
<Compile Include="..\..\Compilers\Core\Portable\InternalUtilities\ExceptionUtilities.cs" Link="ExceptionUtilities.cs" />
<Compile Include="..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\CodeStyle\EditorConfigSeverityStrings.cs" Link="EditorConfigSeverityStrings.cs" />
<Compile Include="..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Extensions\DiagnosticSeverityExtensions.cs" Link="DiagnosticSeverityExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Compilers\Core\Portable\Microsoft.CodeAnalysis.csproj" />
<ProjectReference Include="..\..\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj" />
<ProjectReference Include="..\..\Compilers\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
</ItemGroup>
</Project>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;
namespace CodeStyleConfigFileGenerator
{
public static class Program
{
private const int ExpectedArguments = 4;
private static readonly string s_neverTag = EnforceOnBuild.Never.ToCustomTag();
private static readonly string s_recommendedTag = EnforceOnBuild.Recommended.ToCustomTag();
private static readonly string s_highlyRecommendedTag = EnforceOnBuild.HighlyRecommended.ToCustomTag();
public static int Main(string[] args)
{
if (args.Length != ExpectedArguments)
{
Console.Error.WriteLine($"Excepted {ExpectedArguments} arguments, found {args.Length}: {string.Join(';', args)}");
return 1;
}
var language = args[0];
var outputDir = args[1];
var targetsFileName = args[2];
var assemblyList = args[3].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToImmutableArray();
CreateGlobalConfigFiles(language, outputDir, assemblyList);
CreateTargetsFile(language, outputDir, targetsFileName);
return 0;
}
private static void CreateGlobalConfigFiles(string language, string outputDir, ImmutableArray<string> assemblyList)
{
Debug.Assert(language is "CSharp" or "VisualBasic");
var languageForGetAnalyzers = language == "CSharp" ? LanguageNames.CSharp : LanguageNames.VisualBasic;
var allRulesById = new SortedList<string, DiagnosticDescriptor>();
foreach (var assembly in assemblyList)
{
var analyzerFileReference = new AnalyzerFileReference(assembly, AnalyzerAssemblyLoader.Instance);
analyzerFileReference.AnalyzerLoadFailed += AnalyzerFileReference_AnalyzerLoadFailed;
var analyzers = analyzerFileReference.GetAnalyzers(languageForGetAnalyzers);
foreach (var analyzer in analyzers)
{
foreach (var rule in analyzer.SupportedDiagnostics)
{
allRulesById[rule.Id] = rule;
}
}
}
var configDir = Path.Combine(outputDir, "config");
foreach (var analysisMode in Enum.GetValues(typeof(AnalysisMode)))
{
CreateGlobalconfig(
configDir,
$"AnalysisLevelStyle_{analysisMode}.editorconfig",
(AnalysisMode)analysisMode!,
allRulesById);
}
return;
static void AnalyzerFileReference_AnalyzerLoadFailed(object? sender, AnalyzerLoadFailureEventArgs e)
=> throw e.Exception ?? new InvalidOperationException(e.Message);
static void CreateGlobalconfig(
string folder,
string configFileName,
AnalysisMode analysisMode,
SortedList<string, DiagnosticDescriptor> sortedRulesById)
{
var text = GetGlobalconfigText(analysisMode, sortedRulesById);
var directory = Directory.CreateDirectory(folder);
var configFilePath = Path.Combine(directory.FullName, configFileName);
File.WriteAllText(configFilePath, text);
return;
// Local functions
static string GetGlobalconfigText(AnalysisMode analysisMode, SortedList<string, DiagnosticDescriptor> sortedRulesById)
{
var result = new StringBuilder();
StartGlobalconfig();
AddRules(analysisMode);
return result.ToString();
void StartGlobalconfig()
{
result.AppendLine("# NOTE: Requires **VS2019 16.7** or later");
result.AppendLine();
result.AppendLine($"# Style rules with '{analysisMode}' analysis mode");
result.AppendLine();
result.AppendLine("is_global = true");
result.AppendLine();
// Append 'global_level' to ensure conflicts are properly resolved between different global configs:
// 1. Lowest precedence (-1): SDK config file generated by us.
// 2. Highest predence (non-negative integer): User provided config.
// See https://github.com/dotnet/roslyn/issues/48634 for further details.
result.AppendLine("global_level = -1");
result.AppendLine();
}
bool AddRules(AnalysisMode analysisMode)
{
Debug.Assert(sortedRulesById.Count > 0);
var addedRule = false;
foreach (var rule in sortedRulesById)
{
if (AddRule(rule.Value))
{
addedRule = true;
}
}
return addedRule;
bool AddRule(DiagnosticDescriptor rule)
{
var (isEnabledByDefault, severity) = GetEnabledByDefaultAndSeverity(rule, analysisMode);
if (rule.IsEnabledByDefault == isEnabledByDefault &&
severity == rule.DefaultSeverity)
{
// Rule had the same default severity and enabled state.
// We do not need to generate any entry.
return false;
}
var severityString = isEnabledByDefault ? severity.ToEditorConfigString() : EditorConfigSeverityStrings.None;
result.AppendLine();
result.AppendLine($"# {rule.Id}: {rule.Title}");
result.AppendLine($"dotnet_diagnostic.{rule.Id}.severity = {severityString}");
return true;
}
(bool isEnabledByDefault, DiagnosticSeverity effectiveSeverity) GetEnabledByDefaultAndSeverity(DiagnosticDescriptor rule, AnalysisMode analysisMode)
{
bool isEnabledInNonDefaultMode;
switch (analysisMode)
{
case AnalysisMode.None:
// Disable all rules by default.
return (isEnabledByDefault: false, DiagnosticSeverity.Warning);
case AnalysisMode.All:
// Escalate all rules which can be enabled on build.
isEnabledInNonDefaultMode = !rule.CustomTags.Contains(s_neverTag);
break;
case AnalysisMode.Minimum:
// Escalate all highly recommended rules.
isEnabledInNonDefaultMode = rule.CustomTags.Contains(s_highlyRecommendedTag);
break;
case AnalysisMode.Recommended:
// Escalate all recommended and highly recommended rules.
isEnabledInNonDefaultMode = rule.CustomTags.Contains(s_highlyRecommendedTag) || rule.CustomTags.Contains(s_recommendedTag);
break;
case AnalysisMode.Default:
// Retain the default severity and enabled by default values.
isEnabledInNonDefaultMode = false;
break;
default:
throw ExceptionUtilities.UnexpectedValue(analysisMode);
}
return (isEnabledByDefault: rule.IsEnabledByDefault,
effectiveSeverity: isEnabledInNonDefaultMode ? DiagnosticSeverity.Warning : rule.DefaultSeverity);
}
}
}
}
}
private static void CreateTargetsFile(string language, string outputDir, string targetsFileName)
{
var fileContents =
$@"<Project>{GetTargetContents(language)}
</Project>";
var directory = Directory.CreateDirectory(outputDir);
var fileWithPath = Path.Combine(directory.FullName, targetsFileName);
File.WriteAllText(fileWithPath, fileContents);
return;
static string GetTargetContents(string language)
{
return $@"
<Target Name=""AddGlobalAnalyzerConfigForPackage_MicrosoftCodeAnalysis{language}CodeStyle"" BeforeTargets=""CoreCompile"" Condition=""'$(SkipGlobalAnalyzerConfigForPackage)' != 'true'"">
<!-- PropertyGroup to compute global analyzer config file to be used -->
<PropertyGroup>
<!-- Default 'AnalysisLevelStyle' to the core 'AnalysisLevel' -->
<AnalysisLevelStyle Condition=""'$(AnalysisLevelStyle)' == ''"">$(AnalysisLevel)</AnalysisLevelStyle>
<!-- AnalysisLevelStyle can also contain compound values with a prefix and suffix separated by a '-' character.
The prefix indicates the core AnalysisLevel for CA analyzers, which we ignore for IDE style analyzers.
The suffix indicates the bucket of rules to enable for 'Style' rules by default. It is used to map to the correct global config.
-->
<AnalysisLevelPrefixStyle Condition=""$(AnalysisLevelStyle.Contains('-'))"">$([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '-(.)*', ''))</AnalysisLevelPrefixStyle>
<AnalysisLevelSuffixStyle Condition=""'$(AnalysisLevelPrefixStyle)' != ''"">$([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '$(AnalysisLevelPrefixStyle)-', ''))</AnalysisLevelSuffixStyle>
<!-- Default 'AnalysisLevelSuffixStyle' to the core 'AnalysisLevelSuffix' -->
<AnalysisLevelSuffixStyle Condition=""'$(AnalysisLevelSuffixStyle)' == ''"">$(AnalysisLevelSuffix)</AnalysisLevelSuffixStyle>
<!-- Default 'AnalysisModeStyle' to the core 'AnalysisMode' -->
<AnalysisModeStyle Condition=""'$(AnalysisModeStyle)' == ''"">$(AnalysisMode)</AnalysisModeStyle>
<!-- Set the default analysis mode, if not set by the user -->
<_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>$(AnalysisLevelSuffixStyle)</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle)' == ''"">$(AnalysisModeStyle)</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle)' == 'AllEnabledByDefault'"">{nameof(AnalysisMode.All)}</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle)' == 'AllDisabledByDefault'"">{nameof(AnalysisMode.None)}</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle)' == ''"">{nameof(AnalysisMode.Default)}</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysis{language}CodeStyle>AnalysisLevelStyle_$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle).editorconfig</_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysis{language}CodeStyle)' == ''"">$(MSBuildThisFileDirectory)config</_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysis{language}CodeStyle>
<_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysis{language}CodeStyle Condition=""'$(_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysis{language}CodeStyle)' != ''"">$(_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysis{language}CodeStyle)\$(_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysis{language}CodeStyle)</_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysis{language}CodeStyle>
</PropertyGroup>
<!-- Add the global config, if required. -->
<ItemGroup Condition=""Exists('$(_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysis{language}CodeStyle)') and
('$(AnalysisLevelStyle)' != '$(AnalysisLevel)' or '$(AnalysisModeStyle)' != '$(AnalysisMode)')"">
<EditorConfigFiles Include=""$(_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysis{language}CodeStyle)"" />
</ItemGroup>
</Target>
";
}
}
// NOTE: **Do not** change the names of the fields for this enum - that would be a breaking change for user visible property setting for `AnalysisMode` property in MSBuild project file.
private enum AnalysisMode
{
Default,
None,
Minimum,
Recommended,
All
}
private sealed class AnalyzerAssemblyLoader : IAnalyzerAssemblyLoader
{
public static IAnalyzerAssemblyLoader Instance = new AnalyzerAssemblyLoader();
private AnalyzerAssemblyLoader() { }
public void AddDependencyLocation(string fullPath) { }
public Assembly LoadFromPath(string fullPath) => Assembly.LoadFrom(fullPath);
}
}
}
......@@ -21,15 +21,29 @@
</PropertyGroup>
<Target Name="_GetFilesToPackage">
<PropertyGroup>
<CodeStyleAnalyzerArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)</CodeStyleAnalyzerArtifactsBinDir>
<CodeStyleFixesArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)</CodeStyleFixesArtifactsBinDir>
<VisualBasicCodeStyleAnalyzerArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle\$(Configuration)\$(TargetFramework)</VisualBasicCodeStyleAnalyzerArtifactsBinDir>
<VisualBasicCodeStyleFixesArtifactsBinDir>$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)</VisualBasicCodeStyleFixesArtifactsBinDir>
<VisualBasicCodeStyleTargetsFileName>Microsoft.CodeAnalysis.VisualBasic.CodeStyle.targets</VisualBasicCodeStyleTargetsFileName>
<DotNetExecutable Condition="'$(OS)' == 'Windows_NT'">$(DotNetRoot)dotnet.exe</DotNetExecutable>
<DotNetExecutable Condition="'$(DotNetExecutable)' == ''">$(DotNetRoot)dotnet</DotNetExecutable>
</PropertyGroup>
<Exec Command='"$(DotNetExecutable)" "$(ArtifactsBinDir)CodeStyleConfigFileGenerator\$(Configuration)\netcoreapp3.1\CodeStyleConfigFileGenerator.dll" "VisualBasic" "$(VisualBasicCodeStyleFixesArtifactsBinDir)" "$(VisualBasicCodeStyleTargetsFileName)" "$(CodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.dll;$(VisualBasicCodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.dll"' />
<ItemGroup>
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CodeStyle.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\Microsoft.CodeAnalysis.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.CodeStyle.Fixes\$(Configuration)\$(TargetFramework)\**\Microsoft.CodeAnalysis.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(VisualBasicCodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(VisualBasicCodeStyleFixesArtifactsBinDir)\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(CodeStyleAnalyzerArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(CodeStyleFixesArtifactsBinDir)\Microsoft.CodeAnalysis.CodeStyle.Fixes.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(VisualBasicCodeStyleAnalyzerArtifactsBinDir)\**\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(VisualBasicCodeStyleFixesArtifactsBinDir)\**\Microsoft.CodeAnalysis.VisualBasic.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(CodeStyleAnalyzerArtifactsBinDir)\**\Microsoft.CodeAnalysis.CodeStyle.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(CodeStyleFixesArtifactsBinDir)\**\Microsoft.CodeAnalysis.CodeStyle.Fixes.resources.dll" TargetDir="analyzers/dotnet/vb" />
<_File Include="$(VisualBasicCodeStyleFixesArtifactsBinDir)\$(VisualBasicCodeStyleTargetsFileName)" TargetDir="build" />
<_File Include="$(VisualBasicCodeStyleFixesArtifactsBinDir)\config\**\*.*" TargetDir="build/config" />
<_File Include="$(MSBuildProjectDirectory)\build\**\*.*" TargetDir="build" />
<TfmSpecificPackageFile Include="@(_File)" PackagePath="%(_File.TargetDir)/%(_File.RecursiveDir)%(_File.FileName)%(_File.Extension)" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册