在上一篇[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 的说明,这里也贴一个我在使用的.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

[Cake] 3. dotnet 本地工具 cake & dotnet format的更多相关文章

  1. [Cake] 2. dotnet 全局工具 cake

    在上篇博客[Cake] 1. CI中的Cake中介绍了如何在CI中利用Cake来保持与CI/CD环境的解耦. 1. 简化cake的安装 当时dotnet 2.1还未正式发布,dotnet 还没有工具的 ...

  2. dotnet CLI工具是如何运行你的代码的

    原文连接:https://mattwarren.org/2016/07/04/How-the-dotnet-CLI-tooling-runs-your-code/作者 Matt Warren.授权翻译 ...

  3. [Cake] 1. CI中的Cake

    在上一篇C#Make自动化构建-简介中,简单的介绍了下Cake的脚本如何编写以及通过Powershell在本地运行Cake脚本.本篇在此基础上,介绍下如何在CI环境中使用Cake. 1. Cake简介 ...

  4. .NET Core 3.0 本地工具

    .NET Core从最早期的版本就开始支持全局工具了.如果仅仅需要在某个项目中或某个文件夹中使用特定的工具,那么.NET Core 3.0就允许您这样做. 使用.NET Core 3.0,您可以在特定 ...

  5. X86上搭建交叉工具链,来给龙芯笔记本编译本地工具链(未完待续)

    故事的背景是,我买了一台龙芯2F的笔记本来装B. 为什么说是装B呢?因为不但操作系统是Linux,而且CPU还是龙芯的. 一般人有这么酷的装备吗?简直是装B大圣啊. 这里一定要申明一点,本人不是IT技 ...

  6. X86给龙芯笔记本编译本地工具链(未完待续)

    我买了一台龙芯2F的笔记本来当玩具. 买回来发现,这台笔记本上没法安装软件,因为既没有软件仓库,也没有GCC. 因此需要构建交叉工具链和构建本地工具链. 下面是我研究如何搞定着一切的笔记. 工具链组件 ...

  7. 【dotnet跨平台】&quot;dotnet restore&quot;和&quot;dotnet run&quot;都做了些什么?

    [dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...

  8. 解决本地工具无法连接服务器上的mysql的问题

    当本地工具尝试连接服务器的时候,如果出现无法连接的情况,可能是权限没有开. 首先: mysql> show  databases: 发现有mysql数据库 进入该数据库,找到user表 mysq ...

  9. .net core下的dotnet全局工具

    .net core 2.1后支持了一个全新的部署和扩展命令,可以自己注册全局命令行. dotnet install tool -g dotnetsaydotnetsay 也可以自己构建自己的命令行,一 ...

随机推荐

  1. Jquery 处理返回的 Json 数组

    Jquery 处理返回的 Json 数组 <script> for (var i = 0; i < photos.length; ++ i) { console.log(photos ...

  2. 自制window下core animation引擎 - demo第二弹 - 仿QQ电脑管家加速小火箭

    一年前想写一个像cocoa那样,可以方便层动画开发的引擎,写着写着又逆向它的QuartzCore.framework,也就是CoreAnimation的底层,已经大半年没有搞windows这个引擎.大 ...

  3. 20191107-10 beta发布

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9962 1.视频地址:https://v.youku.com/v_show/ ...

  4. 2019-11-7:sql注入防御,webshell概念,学习笔记

    sql注入防护GPC,magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序,特 ...

  5. 2019-9-28:渗透测试,基础学习,DNS投毒

    该文章仅供学习,利用方法来自网络文章,仅供参考 DNS劫持 目标机:虚拟机下的win7系统 目标ip:192.168.20.131 目标:使用ettercap进行apr投毒,对win7系统就行,DNS ...

  6. [ch03-01] 均方差损失函数

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 3.1 均方差函数 MSE - Mean Square ...

  7. day 31 网络基础的补充

    一.网络基础 1.端口 - 端口,是什么?为什么要有? 端口是为了将同一个电脑上的不同程序进行隔离. IP是找电脑 端口是找电脑上的程序 示例: MySQL是一个软件,软件帮助我们在硬盘上进行文件操作 ...

  8. 使用最新AndroidStudio编写Android编程权威指南(第3版)中的代码会遇到的一些问题

    Android编程权威指南(第3版)这本书是基于Android7.0的,到如今已经过于古老,最新的Android版本已经到10,而这本书的第四版目前还没有正式发售,在最近阅读这本书时,我发现这本书的部 ...

  9. 【JavaEE】之MyBatis开发DAO

    在SSM框架中的DAO层就是MyBatis中的Mapper,Mapper分为两部分:Mapper接口(JAVA文件)和Mapper映射文件(XML文件).DAO开发(Mapper开发)有两种方式:原始 ...

  10. e = e || window.event的区别及用法

    本文链接:https://blog.csdn.net/qq_41348029/article/details/81288481 e = e || window.event 在做事件处理时,用于区分IE ...