打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Cake] 3. dotnet 本地工具 cake & dotnet format

在上一篇[Cake] 2. dotnet 全局工具 cake中介绍了通过.Net Core 2.1 的全局工具dotnet tool命令来简化cake的安装和使用。因为是全局安装,则无法适应每个项目对特定版本的要求。随着.Net Core 3.0中增加的对本地工具(项目级别)的支持,使得这一问题得以解决。

1. cake的安装和还原

# 创建一个本地的工具清单文件dotnet new tool-manifest# 安装本地工具 dotnet tool install cake.tool --version 0.35.0

dotnet new tool-manifest命令会在当前目录下创建一个.config/dotnet-tools.json的文件。当我们执行dotnet tool install cake.tool时,就会把cake.tool的相关信息写入到这个文件。

{  "version": 1,  "isRoot": true,  "tools": {    "cake.tool": {      "version": "0.35.0",      "commands": [        "dotnet-cake"      ]    },    "dotnet-format": {      "version": "3.1.37601",      "commands": [        "dotnet-format"      ]    }  }}

之后就可以执行dotnet cake(或者dotnet tool run dotnet-cake)命令了。

$ dotnet cake --helpUsage: Cake.exe [script] [--target=value] [--verbosity=value]                [--showdescription] [--dryrun] [..]Example: Cake.exeExample: Cake.exe build.cake --verbosity=quietExample: Cake.exe build.cake --showdescriptionOptions:    --target <TARGET>    Target task to invoke. Script must support this explicitly.    --verbosity=value    Specifies the amount of information to be displayed.                         (Quiet, Minimal, Normal, Verbose, Diagnostic)    --debug              Performs a debug.    --showdescription    Shows description about tasks.    --showtree           Shows the task dependency tree.    --dryrun             Performs a dry run.    --exclusive          Execute a single task without any dependencies.    --bootstrap          Download/install modules defined by #module directives    --version            Displays version information.    --info               Displays additional information about Cake execution.    --help               Displays usage information.

当我们在CI/CD或者另外一个环境上时,只需要执行

dotnet tool restore

就可以把.config/dotnet-tools.json文件中配置的相关工具安装在本地了。

2. dotnet format 格式化

介绍一下另外一个非常有用的工具dotnet-format。看下官方介绍:

dotnet-format is a code formatter for dotnet that applies style preferences to a project or solution. Preferences will be read from an .editorconfig file, if present, otherwise a default set of preferences will be used. At this time dotnet-format is able to format C# and Visual Basic projects with a subset of supported .editorconfig options.

它会使用.editorconfig中的格式化配置,来统一项目的文件编码和格式。 安装方式同上面的cake一样。

# 安装dotnet tool install dotnet-format# 检查并保存dotnet format# 只检查不保存,检查失败则返回非0的exit codedotnet format --check --dry-run

结合CI使用非常方便,当你push的代码不符合格式要求时就直接失败了(一个失败的示例:https://github.com/linianhui/cake.example/commit/471f58754c390cb9946a5282c6d73275b90549d9/checks?check_suite_id=361927437)。

示例,它会提示出那些地方不符合.editorconfig的要求:

$ dotnet format --check --dry-run  1-src/Cake.Example/Animals/Cat.cs(17,2): Add final newline.  1-src/Cake.Example/Animals/Dog.cs(17,2): Add final newline.  1-src/Cake.Example/IAnimal.cs(14,2): Add final newline.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(18,2): Add final newline.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(18,2): Add final newline.  1-src/Cake.Example/Animals/Cat.cs(1,31): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(2,2): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(3,18): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(4,12): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(5,19): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(6,38): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(7,6): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(8,22): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(9,15): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(10,23): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(11,32): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(12,29): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(13,10): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(14,25): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(15,10): Fix end of line marker.  1-src/Cake.Example/Animals/Cat.cs(16,6): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(1,31): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(2,2): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(3,18): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(4,11): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(5,19): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(6,38): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(7,6): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(8,22): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(9,15): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(10,23): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(11,32): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(12,29): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(13,10): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(14,25): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(15,10): Fix end of line marker.  1-src/Cake.Example/Animals/Dog.cs(16,6): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(1,23): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(2,2): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(3,18): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(4,13): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(5,19): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(6,29): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(7,6): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(8,22): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(9,16): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(10,23): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(11,32): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(12,23): Fix end of line marker.  1-src/Cake.Example/IAnimal.cs(13,6): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(1,28): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(2,13): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(2,13): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(4,42): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(5,2): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(6,32): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(7,6): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(8,15): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(9,39): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(10,10): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(11,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(11,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(13,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(13,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(15,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(16,10): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/CatTest.cs(17,6): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(1,28): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(2,13): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(2,13): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(4,42): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(5,2): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(6,32): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(7,6): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(8,15): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(9,39): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(10,10): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(11,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(11,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(13,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(13,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(15,40): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(16,10): Fix end of line marker.  2-test/Cake.Example.Tests/AnimalsTests/DotTest.cs(17,6): Fix end of line marker.  Formatted code file 'Cat.cs'.  Formatted code file 'Dog.cs'.  Formatted code file 'IAnimal.cs'.  Formatted code file 'CatTest.cs'.  Formatted code file 'DotTest.cs'.  Format complete in 3529ms.

dotnet-foramt支持的.editorconfig信息比较丰富,具体的参考 https://github.com/dotnet/format/wiki/Supported-.editorconfig-options 的说明,这里也贴一个我在使用的.editorconfig
https://github.com/linianhui/code.guide/blob/master/csharp/.editorconfig

3. 参考

源码: https://github.com/linianhui/cake.example

我的.editorconfig : https://github.com/linianhui/code.guide/blob/master/csharp/.editorconfig

https://editorconfig.org/

https://github.com/dotnet/format/wiki/Supported-.editorconfig-options

https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#local-tools

https://github.com/dotnet/format

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【百度地图API】多家地图API内存消耗对比测验(带源码)
ZanUI
涨停板标注K线颜色 通达信公式代码(图解)
Google Map API使用详解(十七)——实现自定义叠加层
tests
通达信金牛启动主升浪主图指标公式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服