Skip to content

Commit 595a250

Browse files
authored
Merge pull request #2488 from sharwell/organize-dotnet-doc
Update .NET Core documentation to focus on current practices first
2 parents bc21552 + 08f42bc commit 595a250

1 file changed

Lines changed: 45 additions & 40 deletions

File tree

documentation/DotNetCli.md

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
1+
# Using StyleCop Analyzers with .NET Core
12

2-
# Using StyleCop Analyzers with dotnet cli
3+
StyleCop Analyzers can be used with the **dotnet** tooling, including ASP.NET Core.
34

4-
StyleCop Analyzers can be used in dotnet cli projects, including asp.net core.
5-
The tooling support is currently not great and the analyzers only run when the project is compiled and there is currently no way to invoke the code fixes. What does work however is running Stylecop Analyzers in ubuntu on coreclr and OSX (probably) works too.
6-
To set this up a few steps are required:
5+
## .NET SDK Projects (*.csproj)
6+
7+
Edit the project file and add a package reference to **StyleCop.Analyzers**. Make sure to set **PrivateAssets** so the
8+
reference is not included when transitive dependencies are calculated across project references:
79

8-
First the StyleCopAnalyzers nuget package has to be added to the project. Optimally the package should be marked as `build` type so it is not included as a package by other projects consuming it.
9-
For this add the following to the dependencies section of the project.json file:
10-
```json
11-
"StyleCop.Analyzers": {
12-
"version": "1.0.0",
13-
"type": "build"
14-
}
15-
```
16-
For VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects edit the `.csproj` file and mark the `StyleCop.Analyzers` as `PrivateAssets`:
1710
```xml
18-
<ItemGroup>
19-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta001" PrivateAssets="All" />
20-
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
13+
</ItemGroup>
2114
```
15+
2216
If the project is restored and built right now this will already run the analyzers. A few extra steps are needed if they should be configured.
2317

24-
## Rulesets and stylecop.json
18+
### Rulesets and stylecop.json
2519

26-
To supply a ruleset file and a stylecop.json configuration file to the compiler they have to be manually added as arguments to the compiler. For this add the following under the `buildOptions` node in the project.json file:
27-
```json
28-
"additionalArguments": [ "/ruleset:path/to/ruleset.ruleset", "/additionalfile:path/to/stylecop.json" ]
20+
Update the project file as follows to apply settings and custom rules:
21+
22+
```xml
23+
<PropertyGroup>
24+
...
25+
<CodeAnalysisRuleSet>stylecop.ruleset</CodeAnalysisRuleSet>
26+
</PropertyGroup>
27+
<ItemGroup>
28+
<AdditionalFiles Include="stylecop.json" />
29+
</ItemGroup>
2930
```
3031

31-
**Note: ** `additionalArguments` is not currently defined in the schema but does exist and is passed during the build
32+
### Enabling XML documentation processing
33+
34+
Analyzers for XML documentation can only run if the XML documentation comment processing is enabled. See [SA0001](SA0001.md) for more information.
35+
36+
Update the project file to include the following:
3237

33-
On VS VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects update `.csproj` as follows to apply settings and custom rules:
3438
```xml
35-
<PropertyGroup>
36-
...
37-
<CodeAnalysisRuleSet>stylecop.ruleset</CodeAnalysisRuleSet>
38-
</PropertyGroup>
39-
<ItemGroup>
40-
<AdditionalFiles Include="stylecop.json" />
41-
</ItemGroup>
39+
<PropertyGroup>
40+
...
41+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
42+
</PropertyGroup>
4243
```
4344

44-
## Enabling xml documentation processing
45+
## Legacy Projects (*.xproj)
46+
47+
Legacy projects use **project.json** to configure analyzers and other build options. Start by adding the following to
48+
the `dependencies` section of **project.json**:
4549

46-
All analyzers regarding xml documentation can only run if the xml processing is enabled. To do this add
4750
```json
48-
"xmlDoc": "true"
51+
"StyleCop.Analyzers": {
52+
"version": "1.0.2",
53+
"type": "build"
54+
}
4955
```
5056

51-
to the `buildOptions` node of the project.json file. Note that this might cause additional CS1591 diagnostics to appear by the compiler.
52-
They can be suppressed in the ruleset file if necessary.
57+
The rule set and configuration file can be configured by adding the following to the `buildOptions` section:
5358

54-
For VS VS2017 / dotnet core 1.1.1 SDK 1.0.1 projects update `.csproj`
55-
```xml
56-
<PropertyGroup>
57-
...
58-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
59-
</PropertyGroup>
59+
```json
60+
"additionalArguments": [
61+
"/ruleset:path/to/ruleset.ruleset",
62+
"/additionalfile:path/to/stylecop.json"
63+
],
64+
"xmlDoc": "true"
6065
```

0 commit comments

Comments
 (0)