参考官方文档来入门如何制作 Global Tool.
总体分如下几个大步骤:
-
建立命令行程序工程.
-
编写相关代码.
-
测试命令行工具代码, 在命令行中传入参数可以使用
--分隔, 在--之后的所有字符串都会作为参数传入到命令行程序中. -
在工程的
csproj文件中加入相关配置信息:xml<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.2</TargetFramework> <PackAsTool>true</PackAsTool> <ToolCommandName>botsay</ToolCommandName> <PackageOutputPath>./nupkg</PackageOutputPath> </PropertyGroup>其中:
-
PackAsTool: 必选参数, 表示这个是一个 Global Tool 工具工程. -
ToolCommandName: 可选参数, 表示这个工具的名称. -
PackageOutputPath: 可选参数, 表示包含这个 Tool 的 Nuget Package 的生成位置.(在一个 Nuget Package 中可以包含多个 Tool).
即便其中有两个是可选参数, 但在实际开发时一般都需要将二者设置.
-
-
执行
dotnet pack命令将 Package 打包到PackageOutputPath所指定的目录下.另外可以使用
dotnet pack -c Debug来生成 Debug 配置的包. -
安装和卸载命令行工具.
-
安装:
dotnet tool install -g --add-source ./nupkg ToolName使用
--add-source ./nupkg参数来为dotnet tool install提供额外的包路径, 这里指定的是当前目录下的 nupkg 文件夹. -
卸载:
dotnet tool uninstall -g ToolName -
列出所有已安装的 Global Tool 列表:
dotnet tool list -g
-