欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

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

在上一篇[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 --help

usage: cake.exe [script] [--target=value] [--verbosity=value]
                [--showdescription] [--dryrun] [..]

example: cake.exe
example: cake.exe build.cake --verbosity=quiet
example: cake.exe build.cake --showdescription

options:
    --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 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.

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

# 安装
dotnet tool install dotnet-format

# 检查并保存
dotnet format

# 只检查不保存,检查失败则返回非0的exit code
dotnet format --check --dry-run

结合ci使用非常方便,当你push的代码不符合格式要求时就直接失败了(一个失败的示例:)。

示例,它会提示出那些地方不符合.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

3. 参考

源码:

我的.editorconfig :

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