Skip to content

Commit 87a6171

Browse files
authored
Merge pull request #3809 from gewarren/codemetrix
Add info about code metrics nuget package
2 parents 6ccad6f + f673757 commit 87a6171

File tree

1 file changed

+81
-41
lines changed

1 file changed

+81
-41
lines changed

docs/code-quality/how-to-generate-code-metrics-data.md

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,58 @@ The results are generated and the **Code Metrics Results** window is displayed.
4343

4444
## Command-line code metrics
4545

46-
You can generate code metrics data from the command line for C# and Visual Basic projects for .NET Framework, .NET Core, and .NET Standard apps. The command line code metrics tools is called *Metrics.exe*.
46+
You can generate code metrics data from the command line for C# and Visual Basic projects for .NET Framework, .NET Core, and .NET Standard apps. To run code metrics from the command line, install the [Microsoft.CodeAnalysis.Metrics NuGet package](#microsoftcodeanalysismetrics-nuget-package) or build the [Metrics.exe](#metricsexe) executable yourself.
4747

48-
To obtain the *Metrics.exe* executable, you must [generate it yourself](#generate-the-executable). In the near future, a [published version of *Metrics.exe* will be available](https://github.com/dotnet/roslyn-analyzers/issues/1756) so you don't have to build it yourself.
48+
### Microsoft.CodeAnalysis.Metrics NuGet package
4949

50-
### Generate the executable
50+
The easiest way to generate code metrics data from the command line is by installing the [Microsoft.CodeAnalysis.Metrics](https://www.nuget.org/packages/Microsoft.CodeAnalysis.Metrics/) NuGet package. After you've installed the package, run `msbuild /t:Metrics` from the directory that contains your project file. For example:
5151

52-
To generate the executable *Metrics.exe*, follow these steps:
53-
54-
1. Clone the [dotnet/roslyn-analyzers](https://github.com/dotnet/roslyn-analyzers) repo.
55-
2. Open Developer Command Prompt for Visual Studio as an administrator.
56-
3. From the root of the **roslyn-analyzers** repo, execute the following command: `Restore.cmd`
57-
4. Change directory to *src\Tools*.
58-
5. Execute the following command to build the **Metrics.csproj** project:
59-
60-
```shell
61-
msbuild /m /v:m /p:Configuration=Release Metrics.csproj
62-
```
63-
64-
An executable named *Metrics.exe* is generated in the *artifacts\bin* directory under the repo root.
65-
66-
> [!TIP]
67-
> To build *Metrics.exe* in [legacy mode](#legacy-mode), execute the following command:
68-
>
69-
> ```shell
70-
> msbuild /m /v:m /t:rebuild /p:LEGACY_CODE_METRICS_MODE=true Metrics.csproj
71-
> ```
72-
73-
### Usage
52+
```shell
53+
C:\source\repos\ClassLibrary3\ClassLibrary3>msbuild /t:Metrics
54+
Microsoft (R) Build Engine version 16.0.360-preview+g9781d96883 for .NET Framework
55+
Copyright (C) Microsoft Corporation. All rights reserved.
56+
57+
Build started 1/22/2019 4:29:57 PM.
58+
Project "C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj" on node 1 (Metrics target(s))
59+
.
60+
Metrics:
61+
C:\source\repos\ClassLibrary3\packages\Microsoft.CodeMetrics.2.6.4-ci\build\\..\Metrics\Metrics.exe /project:C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj /out:ClassLibrary3.Metrics.xml
62+
Loading ClassLibrary3.csproj...
63+
Computing code metrics for ClassLibrary3.csproj...
64+
Writing output to 'ClassLibrary3.Metrics.xml'...
65+
Completed Successfully.
66+
Done Building Project "C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj" (Metrics target(s)).
67+
68+
Build succeeded.
69+
0 Warning(s)
70+
0 Error(s)
71+
```
7472

75-
To run *Metrics.exe*, supply a project or solution and an output XML file as arguments. For example:
73+
You can override the output file name by specifying `/p:MetricsOutputFile=<filename>`. You can also get [legacy-style](#previous-versions) code metrics data by specifying `/p:LEGACY_CODE_METRICS_MODE=true`. For example:
7674

7775
```shell
78-
C:\>Metrics.exe /project:ConsoleApp20.csproj /out:report.xml
79-
Loading ConsoleApp20.csproj...
80-
Computing code metrics for ConsoleApp20.csproj...
81-
Writing output to 'report.xml'...
82-
Completed Successfully.
76+
C:\source\repos\ClassLibrary3\ClassLibrary3>msbuild /t:Metrics /p:LEGACY_CODE_METRICS_MODE=true /p:MetricsOutputFile="Legacy.xml"
77+
Microsoft (R) Build Engine version 16.0.360-preview+g9781d96883 for .NET Framework
78+
Copyright (C) Microsoft Corporation. All rights reserved.
79+
80+
Build started 1/22/2019 4:31:00 PM.
81+
The "MetricsOutputFile" property is a global property, and cannot be modified.
82+
Project "C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj" on node 1 (Metrics target(s))
83+
.
84+
Metrics:
85+
C:\source\repos\ClassLibrary3\packages\Microsoft.CodeMetrics.2.6.4-ci\build\\..\Metrics.Legacy\Metrics.Legacy.exe /project:C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj /out:Legacy.xml
86+
Loading ClassLibrary3.csproj...
87+
Computing code metrics for ClassLibrary3.csproj...
88+
Writing output to 'Legacy.xml'...
89+
Completed Successfully.
90+
Done Building Project "C:\source\repos\ClassLibrary3\ClassLibrary3\ClassLibrary3.csproj" (Metrics target(s)).
91+
92+
Build succeeded.
93+
0 Warning(s)
94+
0 Error(s)
8395
```
8496

85-
### Output
97+
### Code metrics output
8698

8799
The generated XML output takes the following format:
88100

@@ -118,7 +130,7 @@ The generated XML output takes the following format:
118130
<Metric Name="LinesOfCode" Value="7" />
119131
</Metrics>
120132
<Members>
121-
<Method Name="void Program.Main(string[] args)" File="C:\Users\mavasani\source\repos\ConsoleApp20\ConsoleApp20\Program.cs" Line="7">
133+
<Method Name="void Program.Main(string[] args)" File="C:\source\repos\ConsoleApp20\ConsoleApp20\Program.cs" Line="7">
122134
<Metrics>
123135
<Metric Name="MaintainabilityIndex" Value="100" />
124136
<Metric Name="CyclomaticComplexity" Value="1" />
@@ -137,28 +149,56 @@ The generated XML output takes the following format:
137149
</CodeMetricsReport>
138150
```
139151

140-
### Tool differences
152+
### Metrics.exe
141153

142-
Previous versions of Visual Studio, including Visual Studio 2015, included a command-line code metrics tool called *Metrics.exe*. This previous version of the tool did a binary analysis, that is, an assembly-based analysis. The new tool analyzes source code instead. Because the new *Metrics.exe* is source code-based, the results are different to what's generated by previous versions of *Metrics.exe* and within the Visual Studio 2017 IDE.
154+
If you don't want to install the NuGet package, you can generate and use the *Metrics.exe* executable directly. To generate the *Metrics.exe* executable:
143155

144-
The new *Metrics.exe* tool can compute metrics even in the presence of source code errors, as long as the solution and project can be loaded.
156+
1. Clone the [dotnet/roslyn-analyzers](https://github.com/dotnet/roslyn-analyzers) repo.
157+
2. Open Developer Command Prompt for Visual Studio as an administrator.
158+
3. From the root of the **roslyn-analyzers** repo, execute the following command: `Restore.cmd`
159+
4. Change directory to *src\Tools*.
160+
5. Execute the following command to build the **Metrics.csproj** project:
145161

146-
#### Metric value differences
162+
```shell
163+
msbuild /m /v:m /p:Configuration=Release Metrics.csproj
164+
```
147165

148-
The `LinesOfCode` metric is more accurate and reliable in the new *Metrics.exe*. It is independent of any codegen differences and doesn’t change when the toolset or runtime changes. The new *Metrics.exe* counts actual lines of code, including blank lines and comments.
166+
An executable named *Metrics.exe* is generated in the *artifacts\bin* directory under the repo root.
167+
168+
#### Metrics.exe usage
169+
170+
To run *Metrics.exe*, supply a project or solution and an output XML file as arguments. For example:
149171

150-
Other metrics such as `CyclomaticComplexity` and `MaintainabilityIndex` use the same formulas as previous versions of *Metrics.exe*, but the new *Metrics.exe* counts the number of `IOperations` (logical source instructions) instead of intermediate language (IL) instructions. The numbers will be slightly different from previous versions of *Metrics.exe* and from the Visual Studio 2017 IDE code metrics results.
172+
```shell
173+
C:\>Metrics.exe /project:ConsoleApp20.csproj /out:report.xml
174+
Loading ConsoleApp20.csproj...
175+
Computing code metrics for ConsoleApp20.csproj...
176+
Writing output to 'report.xml'...
177+
Completed Successfully.
178+
```
151179

152-
### Legacy mode
180+
#### Legacy mode
153181

154-
You can also choose to build *Metrics.exe* in *legacy mode*. The legacy mode version of the tool generates metric values that are closer to what older versions of the tool generated. Additionally, in legacy mode, *Metrics.exe* generates code metrics for the same set of method types that previous versions of the tool generated code metrics for. For example, it doesn't generate code metrics data for field and property initializers. Legacy mode is useful for backwards compatibility or if you have code check-in gates based on code metrics numbers. The command to build *Metrics.exe* in legacy mode is:
182+
You can choose to build *Metrics.exe* in *legacy mode*. The legacy mode version of the tool generates metric values that are closer to what [older versions of the tool generated](#previous-versions). Additionally, in legacy mode, *Metrics.exe* generates code metrics for the same set of method types that previous versions of the tool generated code metrics for. For example, it doesn't generate code metrics data for field and property initializers. Legacy mode is useful for backwards compatibility or if you have code check-in gates based on code metrics numbers. The command to build *Metrics.exe* in legacy mode is:
155183

156184
```shell
157185
msbuild /m /v:m /t:rebuild /p:LEGACY_CODE_METRICS_MODE=true Metrics.csproj
158186
```
159187

160188
For more information, see [Enable generating code metrics in legacy mode](https://github.com/dotnet/roslyn-analyzers/pull/1841).
161189

190+
### Previous versions
191+
192+
Previous versions of Visual Studio, including Visual Studio 2015, included a command-line code metrics tool that was also called *Metrics.exe*. This previous version of the tool did a binary analysis, that is, an assembly-based analysis. The new tool analyzes source code instead. Because the new command-line code metrics tool is source code-based, the results are different to what's generated by previous versions of *Metrics.exe* and within the Visual Studio 2017 IDE.
193+
194+
The new command-line code metrics tool computes metrics even in the presence of source code errors, as long as the solution and project can be loaded.
195+
196+
#### Metric value differences
197+
198+
The `LinesOfCode` metric is more accurate and reliable in the new command-line code metrics tool. It's independent of any codegen differences and doesn’t change when the toolset or runtime changes. The new tool counts actual lines of code, including blank lines and comments.
199+
200+
Other metrics such as `CyclomaticComplexity` and `MaintainabilityIndex` use the same formulas as previous versions of *Metrics.exe*, but the new tool counts the number of `IOperations` (logical source instructions) instead of intermediate language (IL) instructions. The numbers will be slightly different from previous versions of *Metrics.exe* and from the Visual Studio 2017 IDE code metrics results.
201+
162202
## See also
163203

164204
- [Use the Code Metrics Results window](../code-quality/working-with-code-metrics-data.md)

0 commit comments

Comments
 (0)