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

来源:https://www.cnblogs.com/linianhui/archive/2019/12/17/cake-with-dotnet-local-tool.html
-Advertisement-
Play Games

在上一篇 "[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。看下官方介紹:

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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在Web滲透流程的暴力登錄場景和爬蟲抓取場景中,經常會遇到一些登錄表單用DES之類的加密方式來加密參數,也就是說,你不搞定這些前端加密,你的編寫的腳本是不可能Login成功的。針對這個問題,現在有三種解決方式: ①看懂前端的加密流程,然後用腳本編寫這些方法(或者找開源的源碼),模擬這個加密的流程。缺 ...
  • 登錄系統(註意這裡啟動 tomcat 的用戶) 使用 MAT 分析 下載 dump.hprof ,使用 MAT 打開分析 ...
  • 通常系統都會限制同一個賬號的登錄人數,多人登錄要麼限制後者登錄,要麼踢出前者,Spring Security 提供了這樣的功能,本文講解一下在沒有使用Security的時候如何手動實現這個功能 demo 技術選型 SpringBoot JWT Filter Redis + Redisson JWT( ...
  • 一、概述通常來說,“行為請求者”與“行為實現者”是緊耦合的。但在某些場合,比如要對行為進行“記錄、撤銷/重做、事務”等處理,這種無法抵禦變化的緊耦合是不合適的。在這些情況下,將“行為請求者”與“行為實現者”解耦,實現二者之間的松耦合就至關重要。命令模式是解決這類問題的一個比較好的方法。二、命令模式命 ...
  • 環境:VS2019 .net 4.0 framework 根據教材使用ScriptManager在JavaScript中調用Web service 時,失敗。現將過程和解決方法記錄如下: 1、定義Web Service 2、定義JavaScript和.aspx頁面; 整個項目的目錄如下: 3、運行程 ...
  • 1、前言: WebAPI主要開放數據給手機APP,Pad,其他需要得知數據的系統,或者軟體應用。Web 用戶的身份驗證,及頁面操作許可權驗證是B/S系統的基礎功能。我上次寫的《Asp.Net MVC WebAPI的創建與前臺Jquery ajax後臺HttpClient調用詳解》這種跟明顯安全性不是那 ...
  • 現在好多使用gitlab ci的持續集成的教程,大部分都是發佈到linux系統上的,但是目前還是有很大一部分企業使用的都是windows系統使用IIS在部署.NET應用程式。這裡寫一下如何使用gitlab ci配合gitlab runner持續集成到IIS吧。 安裝gitlab 網上有很多安裝git ...
  • 學習編程語言之前,首先要搞清楚“編程語言”這個概念。 很小的時候,父母就教我們開口說話,也教我們如何理解別人講話的意思。經過長時間的熏陶和自我學習,我們竟然在不知不覺中學會了說話,同時也能聽懂其他小朋友說話的意思了,我們開始向父母要零花錢買零食和玩具、被欺負了向父母傾訴…… 我們說的是漢語,是“中國 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...