如何配置C#解决方案以在任何违反代码样式的情况下失败?
我创建了一个示例Web API项目,并希望在CI管道期间执行编码惯例(如果样式错误在开发过程中出现,则奖励点奖励点)。基本上,我只想为C#项目设置Eslint(来自JavaScript世界)。
我想强制执行这些规则解决方案,因此我添加了a directory.build.props.props root目录中的文件中有以下内容(我想使用 Roslyn Analyzers 和在警告中失败)
<Project>
<PropertyGroup Label="Compiler Settings">
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<WarningLevel>5</WarningLevel>
</PropertyGroup>
</Project>
接下来,我安装了 dotnet格式要访问dotnet格式./mysln.sln-verify-no-no-changes
命令。
我将生成的 WeatherForeCastController 修改为
using Microsoft.AspNetCore.Mvc;
using System;
namespace LinterCheckSample.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
string unused = "";
return
new
List
<
WeatherForecast
>
()
{
new WeatherForecast() {}
};
}
private void Do() { }
}
运行格式命令时,我期望以下内容,有些人被识别为
Sytem
systemsummaries
的冗余导入。 y- 从未使用
摘要
- _logger 的冗余数组创建表达式从不使用
未使用的
从不使用✅weatherforecast ❌
- 空间错误✅
- 删除
get
方法中的空行 - <代码>中的修复凹痕 get 方法❌
- 方法
do
从不使用❌
我认为一件事是一件事我缺少一个.editorConfig文件?我复制粘贴 roslyn Analyzers .editorConfig .editorConfig 这是我的项目,这 如果我的方法是正确的方法,
我可以改善一些方法吗? (directory.build.props
文件),是否有一个官方的超级严格.editorConfig?
如果我的方法是错误的,那么在C#世界中解决这个问题的正确方法是什么?
提前致谢
I created a sample Web API project and want to enforce coding conventions during my CI pipeline ( bonus points if the style errors come up during development ). Basically I just want to setup eslint ( from the JavaScript world ) for C# projects.
I want to enforce those rules solution wide so I added a Directory.Build.props file in the root directory with the following content ( I want to use the Roslyn Analyzers and fail on warnings )
<Project>
<PropertyGroup Label="Compiler Settings">
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<WarningLevel>5</WarningLevel>
</PropertyGroup>
</Project>
Next I installed dotnet format to get access to the dotnet format ./MySln.sln --verify-no-changes
command.
I modified the generated WeatherForecastController to
using Microsoft.AspNetCore.Mvc;
using System;
namespace LinterCheckSample.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
string unused = "";
return
new
List
<
WeatherForecast
>
()
{
new WeatherForecast() {}
};
}
private void Do() { }
}
When running the format command I expected the following things, some got identified
- Redundant import of
System
❌ Summaries
is never used ✅- Redundant array creation expression for
Summaries
❌ _logger
is never used ❌unused
is never used ✅- Redundant type specification for
WeatherForecast
❌ - Whitespace errors ✅
- Remove empty lines in
Get
method ❌ - Fix indentation in
Get
method ❌ - Method
Do
is never used ❌
I think the one thing I'm missing is an .editorconfig file? I copy pasted the Roslyn Analyzers .editorconfig into my project and this helped a little bit ( it didn't catch all of them )
If my approach is the correct way is there something I can improve? ( Directory.Build.props
file ) and is there one official super strict .editorconfig?
If my approach is wrong, what is the correct way to solve this in the C# world?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
R#命令行工具不可能。
对于Roslyn分析仪,它可以帮助您在
csproj
文件中设置treatwarningsaserrors
:AFAIK that's not possible with R# Command Line Tools.
For Roslyn analyzers, it could help setting
TreatWarningsAsErrors
within yourcsproj
file:不要更改您的
.csproj
文件。在解决方案目录中,添加a directory.build.props 文件以下文件:根据您的需求调整设置。
Don't change your
.csproj
files. In the solution directory, add a Directory.Build.props file with the following:Adjust the settings to your needs.