共用方式為


教學課程:建立項目範本

使用 .NET,您可以建立和部署產生專案、檔案和資源的範本。 本教學課程是系列課程的第一部分,教導您如何建立、安裝及解除安裝範本以與命令搭配 dotnet new 使用。

您可以在 .NET 範例 GitHub 存放庫中檢視已完成的範本。

小提示

專案 範本不會顯示在 Visual Studio 的 [ 新增>專案 ] 對話方塊中。

在本系列的這一部分中,您將瞭解如何:

  • 為項目範本建立類別。
  • 建立範本設定資料夾和檔案。
  • 從檔案路徑安裝範本。
  • 測試項目範本。
  • 解除安裝項目範本。

先決條件

  • .NET 9 或更新版本。

  • 參考文章解釋了有關模板的基礎知識以及它們如何組合在一起。 這裡重申其中一些信息。

  • 開啟終端機並導航到您將儲存和測試範本的資料夾。

建立所需的資料夾

本系列使用包含範本來源的「工作資料夾」,以及用於測試範本的「測試資料夾」。 工作資料夾和測試資料夾應該位於相同的父資料夾下。

首先,創建父文件夾,名稱無所謂。 然後,建立兩個名為 workingtest 的子資料夾。 在 工作 資料夾內,建立名為 content 的子資料夾。

資料夾結構應該如下所示。

parent_folder
├───test
└───working
    └───content

建立項目範本

項目範本是包含一或多個檔案的特定範本類型。 當您已經有一個專案並且想要產生另一個檔案(例如設定檔或程式碼檔案)時,這些類型的範本非常有用。 在此範例中,您會建立類別,將擴充方法新增至字串類型。

在終端機中,導覽至 working\content 資料夾,並建立名為 extensions 的新子資料夾。

working
└───content
    └───extensions

導航到 擴展文件 夾並創建一個名為 StringExtensions.cs 的新文件。 在文字編輯器中開啟檔案。 這個類別提供了一個名為 Reverse 的擴充方法,用於反轉字串的內容。 貼上以下程式碼並儲存檔案:

namespace System;

public static class StringExtensions
{
    public static string Reverse(this string value)
    {
        char[] tempArray = value.ToCharArray();
        Array.Reverse(tempArray);
        return new string(tempArray);
    }
}

現在範本的內容已完成,請建立範本設定。

建立範本設定

在教學課程的這一部分中,您的範本資料夾位於 working\content\extensions

.NET 可以辨識範本,因為它們具有存在於範本資料夾根目錄的特殊資料夾和組態檔。

首先,建立一個名為 .template.config的新子資料夾,並輸入它。 然後,建立名為 template.json的新檔案。 您的資料夾結構應該如下所示:

working
└───content
    └───extensions
        └───.template.config
                template.json

使用您最喜歡的文字編輯器開啟 template.json ,然後貼上以下 JSON 程式碼並儲存。

{
    "$schema": "http://json.schemastore.org/template",
    "author": "Me",
    "classifications": [ "Common", "Code" ],
    "identity": "ExampleTemplate.StringExtensions",
    "name": "Example templates: string extensions",
    "shortName": "stringext",
    "tags": {
      "language": "C#",
      "type": "item"
    },
    "symbols": {
      "ClassName":{
        "type": "parameter",
        "description": "The name of the code file and class.",
        "datatype": "text",
        "replaces": "StringExtensions",
        "fileRename": "StringExtensions",
        "defaultValue": "StringExtensions"
      }
    }
  }

此設定檔包含範本的所有設定。 您可以看到基本設定,例如 nameshortName,但也有一個 tags/type 值設定為 item。 這會將您的模板歸類為“項目”模板。 您建立的範本類型沒有限制。 item值和project值是.NET建議的通用名稱,使使用者可以輕鬆篩選他們要搜尋的範本類型。

classifications項目代表標籤欄位,您在執行dotnet new並獲得範本清單時看到它。 使用者也可以根據分類標籤進行搜尋。 請勿將 tags 檔案中的屬性與標籤清單混淆classifications。 它們是兩個不同的概念,不幸的是,它們的名稱相同。 template.json 檔案的完整結構描述可在JSON結構描述存放區中找到,並在 template.json參考中說明。 如需 template.json 檔案的詳細資訊,請參閱 dotnet templating wiki

symbols此 JSON 物件的一部分可用來定義可在範本中使用的參數。 在此情況下,定義了一個參數 ClassName。 定義的參數包含下列設定:

  • type - 這是必要設定,必須設為 parameter
  • description - 參數的描述,列印在範本說明中。
  • datatype - 使用參數時參數值的資料類型。
  • replaces - 指定應在所有範本檔案中以參數值取代的文字值。
  • fileRename - 與 replaces類似,這會指定一個文字值,該值會在所有範本檔案的名稱中以參數的值取代。
  • defaultValue - 當使用者未指定參數時,此參數的預設值。

使用範本時,使用者可以提供參數的 ClassName 值,而此值會取代所有出現的 StringExtensions。 如果未提供值,則會使用 。defaultValue 對於此範本,有兩個出現的項目 StringExtensions:檔案 StringExtensions.cs 和類別 StringExtensions。 因為參數defaultValue的 是 StringExtensions ,如果在使用範本時未指定參數,則檔案名稱和類別名稱會保持不變。 例如 dotnet new stringext -ClassName MyExts,指定值時,檔案會重新命名為 MyExts.cs ,且類別會重新命名為 MyExts

若要查看範本可用的參數,請使用 -? 具有範本名稱的參數:

dotnet new stringext -?

這會產生下列輸出:

Example templates: string extensions (C#)
Author: Me

Usage:
  dotnet new stringext [options] [template options]

Options:
  -n, --name <name>       The name for the output being created. If no name is specified, the name of the output directory is used.
  -o, --output <output>   Location to place the generated output.
  --dry-run               Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force                 Forces content to be generated even if it would change existing files.
  --no-update-check       Disables checking for the template package updates when instantiating a template.
  --project <project>     The project that should be used for context evaluation.
  -lang, --language <C#>  Specifies the template language to instantiate.
  --type <item>           Specifies the template type to instantiate.

Template options:
  -C, --ClassName <ClassName>  The name of the code file and class.
                               Type: text
                               Default: StringExtensions

現在您已經有了有效的 .template.config/template.json 文件,您的模板已準備好安裝。 在終端機中,導覽至 extensions 資料夾並執行下列命令,以安裝位於目前資料夾的範本:

  • 在 Windows 上dotnet new install .\
  • 在 Linux 或 macOS 上dotnet new install ./

此命令會輸出已安裝的範本清單,其中應包含您的範本。

The following template packages will be installed:
   <root path>\working\content\extensions

Success: <root path>\working\content\extensions installed the following templates:
Templates                                         Short Name               Language          Tags
--------------------------------------------      -------------------      ------------      ----------------------
Example templates: string extensions              stringext                [C#]              Common/Code

測試項目範本

現在您已安裝項目範本,請對其進行測試。

  1. 導覽至 測試 資料夾。

  2. 使用 dotnet new console 建立新的主控台應用程式,這將產生一個工作專案,您可以使用 dotnet run 命令輕鬆進行測試。

    dotnet new console
    

    您會得到類似以下的輸出。

    The template "Console Application" was created successfully.
    
    Processing post-creation actions...
    Running 'dotnet restore' on C:\test\test.csproj...
      Restore completed in 54.82 ms for C:\test\test.csproj.
    
    Restore succeeded.
    
  3. 使用下列命令執行專案。

    dotnet run
    

    您會得到以下輸出。

    Hello, World!
    
  4. 執行 dotnet new stringext 從範本產生 StringExtensions.cs 檔案。

    dotnet new stringext
    

    您會得到以下輸出。

    The template "Example templates: string extensions" was created successfully.
    
  5. 變更 Program.cs 中的程式碼,以使用範本提供的擴充方法反轉 "Hello, World!" 字串。

    Console.WriteLine("Hello, World!".Reverse());
    

    再次運行程序並查看結果是否反轉。

    dotnet run
    

    您會得到以下輸出。

    !dlroW ,olleH
    

祝賀! 您已使用 .NET 建立並部署專案範本。 為了準備本教學課程系列的下一部分,請解除安裝您建立的範本。 確保刪除 測試 文件夾中的所有文件和文件夾。 這可讓您回到乾淨的狀態,為本教學課程系列的下一部分做好準備。

解除安裝範本

在終端機中,導覽至 extensions 資料夾並執行下列命令,以卸載目前資料夾中的範本:

  • 在 Windows 上dotnet new uninstall .\
  • 在 Linux 或 macOS 上dotnet new uninstall ./

此命令會輸出已卸載的範本清單,其中應包含您的範本。

Success: <root path>\working\content\extensions was uninstalled.

您可以 dotnet new uninstall 隨時查看已安裝範本套件的清單,包括每個範本套件的解除安裝命令。

後續步驟

在本教學課程中,您已建立項目範本。 若要瞭解如何建立專案範本,請繼續本教學課程系列。