Skip to content

Commit 31a2a0e

Browse files
Add template for custom analyzers (#9789)
1 parent 0892e86 commit 31a2a0e

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"author": "Microsoft",
4+
"classifications": [
5+
"Common",
6+
"Library"
7+
],
8+
"name": "MSBuild custom analyzer skeleton project.",
9+
"generatorVersions": "[1.0.0.0-*)",
10+
"description": "A project for creating a MSBuild analyzer library that targets .NET Standard",
11+
"groupIdentity": "Microsoft.AnalyzerTemplate",
12+
"identity": "Microsoft.AnalyzerTemplate",
13+
"shortName": "msbuildanalyzer",
14+
"tags": {
15+
"language": "C#",
16+
"type": "project"
17+
},
18+
"sourceName": "Company.AnalyzerTemplate",
19+
"preferNameDirectory": true,
20+
"primaryOutputs": [
21+
{
22+
"path": "Company.AnalyzerTemplate.csproj"
23+
}
24+
],
25+
"symbols": {
26+
"MicrosoftBuildVersion": {
27+
"type": "parameter",
28+
"description": "Overrides the default Microsoft.Build version where analyzer's interfaces are placed",
29+
"datatype": "text",
30+
"defaultValue": "17.9.5",
31+
"replaces": "1.0.0-MicrosoftBuildPackageVersion",
32+
"displayName": "Microsoft.Build default package version override"
33+
}
34+
},
35+
"postActions": [
36+
{
37+
"id": "restore",
38+
"condition": "(!skipRestore)",
39+
"description": "Restore NuGet packages required by this project.",
40+
"manualInstructions": [
41+
{
42+
"text": "Run 'dotnet restore'"
43+
}
44+
],
45+
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
46+
"continueOnError": true
47+
}
48+
]
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace Company.AnalyzerTemplate
4+
{
5+
public sealed class Analyzer1 : BuildAnalyzer
6+
{
7+
public static BuildAnalyzerRule SupportedRule = new BuildAnalyzerRule("X01234", "Title",
8+
"Description", "Category",
9+
"Message format: {0}",
10+
new BuildAnalyzerConfiguration() { Severity = BuildAnalyzerResultSeverity.Warning, IsEnabled = true });
11+
12+
public override string FriendlyName => "Company.Analyzer1";
13+
14+
public override IReadOnlyList<BuildAnalyzerRule> SupportedRules { get; } =[SupportedRule];
15+
16+
public override void Initialize(ConfigurationContext configurationContext)
17+
{
18+
// configurationContext to be used only if analyzer needs external configuration data.
19+
}
20+
21+
public override void RegisterActions(IBuildCopRegistrationContext registrationContext)
22+
{
23+
registrationContext.RegisterEvaluatedPropertiesAction(EvaluatedPropertiesAction);
24+
}
25+
26+
private void EvaluatedPropertiesAction(BuildCopDataContext<EvaluatedPropertiesAnalysisData> context)
27+
{
28+
context.ReportResult(BuildCopResult.Create(
29+
SupportedRule,
30+
ElementLocation.EmptyLocation,
31+
"Argument for the message format");
32+
}
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<DevelopmentDependency>true</DevelopmentDependency>
6+
<IncludeBuildOutput>false</IncludeBuildOutput>
7+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
8+
<!-- The output structure was modified for msbuild develomplent needs.-->
9+
<NoWarn>NU5101;NU5128</NoWarn>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<None Include="Directory.Build.props" Pack="true" PackagePath="build\Directory.Build.props" />
14+
<Content Include="README.md" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<!-- Please add IncludeInPackage field to all third party dependencies. See the example below.-->
19+
<PackageReference Include="Microsoft.Build" PrivateAssets="all" IncludeInPackage="true" />
20+
</ItemGroup>
21+
22+
<Target Name="AddNuGetDlls" BeforeTargets="_GetPackageFiles">
23+
<!-- Merge the collection of PackageReference and Assemblies using the NuGetPackageId key.
24+
This produces a new list containing the DLL path and the "IncludeInPackage" metadata-->
25+
<JoinItems Left="@(ResolvedCompileFileDefinitions)" LeftKey="NuGetPackageId" LeftMetadata="*" Right="@(PackageReference)" RightKey="" RightMetadata="*" ItemSpecToUse="Left">
26+
<Output TaskParameter="JoinResult" ItemName="_PackagesToPack" />
27+
</JoinItems>
28+
29+
<ItemGroup>
30+
<!-- Remove NETStandard DLLs -->
31+
<_PackagesToPack Remove="@(_PackagesToPack)" Condition="%(NuGetPackageId) == 'NETStandard.Library'" />
32+
<_PackagesToPack Remove="@(_PackagesToPack)" Condition="%(_PackagesToPack.IncludeInPackage) != 'true'" />
33+
</ItemGroup>
34+
35+
<Message Importance="High" Text="Adding DLLs from the following packages: @(_PackagesToPack->'%(NuGetPackageId)')" />
36+
37+
<ItemGroup>
38+
<!-- Update the collection of items to pack with the DLLs from the NuGet packages -->
39+
<None Include="@(_PackagesToPack)" Pack="true" PackagePath="lib" Visible="false" />
40+
41+
<!-- Add the DLL produced by the current project to the NuGet package -->
42+
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="lib" Visible="false" />
43+
</ItemGroup>
44+
</Target>
45+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAnalyzer>$([MSBuild]::RegisterAnalyzer($(MSBuildThisFileDirectory)..\lib\Company.AnalyzerTemplate.dll))</MSBuildAnalyzer>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageVersion Include="Microsoft.Build" Version="1.0.0-MicrosoftBuildPackageVersion" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MSBuild Custom Analyzer Template
2+
3+
## Overview
4+
MSBuild Custom Analyzer Template is a .NET template designed to streamline the creation of MSBuild analyzer libraries. This template facilitates the development of custom analyzers targeting .NET Standard, enabling developers to inspect and enforce conventions, standards, or patterns within their MSBuild builds.
5+
6+
## Features
7+
- Simplified template for creating MSBuild analyzer libraries.
8+
- Targeting .NET Standard for cross-platform compatibility.
9+
- Provides a starting point for implementing custom analysis rules.
10+
11+
## Getting Started
12+
To use the MSBuild Custom Analyzer Template, follow these steps:
13+
1. Install the template using the following command:
14+
```bash
15+
dotnet new install msbuildanalyzer
16+
2. Instantiate a custom template:
17+
```bash
18+
dotnet new msbuildanalyzer -n <ProjectName>
19+
20+
### Prerequisites
21+
- .NET SDK installed on your machine.

0 commit comments

Comments
 (0)