You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/code-quality/how-to-generate-code-metrics-data.md
+81-41Lines changed: 81 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -43,46 +43,58 @@ The results are generated and the **Code Metrics Results** window is displayed.
43
43
44
44
## Command-line code metrics
45
45
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.
47
47
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
49
49
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:
51
51
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:
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
+
```
74
72
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:
@@ -137,28 +149,56 @@ The generated XML output takes the following format:
137
149
</CodeMetricsReport>
138
150
```
139
151
140
-
### Tool differences
152
+
### Metrics.exe
141
153
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:
143
155
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:
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:
149
171
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.
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:
For more information, see [Enable generating code metrics in legacy mode](https://github.com/dotnet/roslyn-analyzers/pull/1841).
161
189
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
+
162
202
## See also
163
203
164
204
-[Use the Code Metrics Results window](../code-quality/working-with-code-metrics-data.md)
0 commit comments