文档章节

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

o
 osc_1njk964l
发布于 2019/12/17 14:56
字数 1611
阅读 36
收藏 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。看下官方介绍:

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 code
dotnet 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 的说明,这里也贴一个我在使用的.editorconfighttps://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

原文出处:https://www.cnblogs.com/linianhui/p/cake-with-dotnet-local-tool.html

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
我的架构演化笔记 功能1: 基本的用户注册

“咚咚”,一阵急促的敲门声, 我从睡梦中惊醒,我靠,这才几点,谁这么早, 开门一看,原来我的小表弟放暑假了,来南京玩,顺便说跟我后面学习一个网站是怎么做出来的。 于是有了下面的一段...

强子哥哥
2014/05/31
976
3
CDH5: 使用parcels配置lzo

一、Parcel 部署步骤 1 下载: 首先需要下载 Parcel。下载完成后,Parcel 将驻留在 Cloudera Manager 主机的本地目录中。 2 分配: Parcel 下载后,将分配到群集中的所有主机上并解压缩。 3 激...

cloud-coder
2014/07/01
6.8K
1
树莓派(Raspberry Pi):完美的家用服务器

自从树莓派发布后,所有在互联网上的网站为此激动人心的设备提供了很多有趣和具有挑战性的使用方法。虽然这些想法都很棒,但树莓派( RPi )最明显却又是最不吸引人的用处是:创建你的完美家用...

异次元
2013/11/09
6.3K
8
5分钟 maven3 快速入门指南

前提条件 你首先需要了解如何在电脑上安装软件。如果你不知道如何做到这一点,请询问你办公室,学校里的人,或花钱找人来解释这个给你。 不建议给Maven的服务邮箱来发邮件寻求支持。 安装Mav...

fanl1982
2014/01/23
1.2W
6
Linux 反汇编工具--LDasm

LDasm (Linux 反汇编工具) 是一个基于 Perl/TK 的 objdump/binutils 图形化工具,试图模仿 W32Dasm 工具的外观。可搜索相互参照,将代码从 GAS 转换成 MASM 风格代码等等。...

匿名
2013/01/22
5.5K
1

没有更多内容

加载失败,请刷新页面

加载更多

如何在Pandas的DataFrame中的行上进行迭代? - How to iterate over rows in a DataFrame in Pandas?

问题: I have a DataFrame from pandas: 我有一个来自熊猫的DataFrame : import pandas as pdinp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]df = pd.DataFrame(......

富含淀粉
27分钟前
20
0
与电商节伴生,平台销量榜单背后还有被低估的营销价值

文 | 曾响铃 来源 | 科技向令说(xiangling0815) 时至今日,电商节已经成为各巨头零售平台例行的活动,花样在不断创新,玩法也在不断涌现。 在这个过程中,伴随电商节的各种品牌、产品销量榜...

曾响铃
29分钟前
22
0
volatile关键字详解

前言 提到JAVA的并发编程,就不得不提volatile关键字,不管是在面试还是实际开发中,volatile关键字的使用都是一个应该掌握的技能。它之所以重要,是因为它和JAVA并发编程中会遇到三种重要问...

ls_cherish
55分钟前
10
0
比继承更偏爱组成? - Prefer composition over inheritance?

问题: Why prefer composition over inheritance? 为什么更喜欢使用组合而不是继承? What trade-offs are there for each approach? 每种方法都有哪些取舍? When should you choose inher...

javail
57分钟前
17
0
2020软件测试工程师史上最全面试题(内含答案)-看完BATJ面试官对你竖起大拇指!

以下是软件测试相关的面试题及答案,欢迎大家参考! 我不是至尊宝,也不是孙悟空,我只是那城墙下的一猿。  1、你的测试职业发展是什么?  测试经验越多,测试能力越高。所以我的职业发展是需...

程序员一凡
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部