dotnet 手工打一个 dotnet tool 包
現(xiàn)在依靠 dotnet 平臺,可以方便分發(fā)工具,利用 NuGet 服務(wù)進行分發(fā)和使用工具。打一個 dotnet tool 包,可以將這個包上傳到 NuGet 上,小伙伴通過和安裝 NuGet 相同方式就可以將工具安裝在本機上。本文將告訴大家如何手工打一個 dotnet tool 包,方便小伙伴自己寫工具用來創(chuàng)建代碼
所有可執(zhí)行項目可以打包為 dotnet tool 包,通過?dotnet xx?的命令就可以執(zhí)行對應(yīng)的軟件。而 dotnet tool 包本身就是 NuGet 包,如果是在 dotnet 生成或 VisualStudio 中,只需要在 csporj 文件添加下面代碼
<PackAsTool>true</PackAsTool><ToolCommandName>nugetfix</ToolCommandName>如下面代碼
<PropertyGroup><OutputType>WinExe</OutputType><TargetFramework>netcoreapp3.1</TargetFramework><UseWPF>true</UseWPF><ApplicationIcon>Icon.ico</ApplicationIcon><AssemblyName>NugetMergeFixTool</AssemblyName><RootNamespace>dotnetCampus.NugetMergeFixTool</RootNamespace><GeneratePackageOnBuild>true</GeneratePackageOnBuild><PackAsTool>true</PackAsTool><ToolCommandName>nugetfix</ToolCommandName></PropertyGroup>然后打包就可以了
代碼請看github?歡迎小伙伴訪問
打包的 NuGet 包,可以通過下面命令安裝
dotnet tool install --global --add-source .\bin\debug NugetMergeFixTool接下來可以使用?nugetfix?啟動這個應(yīng)用,傳入的命令行也可以傳入應(yīng)用
那么?nugetfix?這個參數(shù)是從哪里獲取的?實際上在?<ToolCommandName>nugetfix</ToolCommandName>?設(shè)置的
如果我想要用?dotnet nugetfix?啟動命令,那么請將?nugetfix?修改為?dotnet-nugetfix?就可以
這個 NuGet 包和其他的 NuGet 有什么不同
如果我需要手動打包,我先需要可執(zhí)行文件,例如 Windows 下的 exe 文件,注意沒有限制平臺,也就是 Linux 也可以。這里說的可執(zhí)行文件在 Windows 下可能是 dll 哦,只要通過?dotnet?命令可以啟動這個 dll 就可以
我假設(shè)拿到可執(zhí)行文件和他的所有依賴文件,放在 lindexi 文件夾里面
接下來就是手工打包了
創(chuàng)建準備打包文件夾,如 packing 文件夾,在 packing 文件夾里面創(chuàng)建 tools 文件夾,在 tools 文件夾創(chuàng)建對應(yīng)框架文件夾,如?netcoreapp3.1?再創(chuàng)建 any 文件夾(AnyCPU) 請看下面路徑
tools\netcoreapp3.1\any\請將 lindexi 文件夾里面的所有文件放在 any 文件夾里面,需要確定 any 文件夾里面存在可執(zhí)行文件,如 NugetMergeFixTool.dll 文件
接著在 any 文件夾里面創(chuàng)建 DotnetToolSettings.xml 文件,內(nèi)容請看代碼
<?xml version="1.0" encoding="utf-8"?> <DotNetCliTool Version="1"><Commands><Command Name="nugetfix" EntryPoint="NugetMergeFixTool.dll" Runner="dotnet" /></Commands> </DotNetCliTool>這里 Command 的 Name 就是?nugetfix?中的命令,而 EntryPoint 就是入口文件,請將代碼修改為你需要的代碼
返回 packing 文件夾,創(chuàng)建 nuspec 文件,我期望閱讀本文的小伙伴都知道 nuspec 文件應(yīng)該如何寫,我不會告訴大家細節(jié)
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"><metadata><id>NugetMergeFixTool</id><version>0.1.19026-alpha</version><authors>dotnet-campus</authors><owners>dotnet-campus</owners><requireLicenseAcceptance>false</requireLicenseAcceptance><license type="expression">MIT</license><licenseUrl>https://licenses.nuget.org/MIT</licenseUrl><projectUrl>https://github.com/dotnet-campus/dotnetCampus.NugetMergeFixTool</projectUrl><description>讀寫文件升級NuGet庫,修復(fù) NuGet 庫引用</description><copyright>Copyright (c) 2020 dotnet-campus</copyright><tags>dotnet nuget msbuild</tags><packageTypes><packageType name="DotnetTool" /></packageTypes><repository type="git" url="https://github.com/dotnet-campus/dotnetCampus.NugetMergeFixTool.git" /><frameworkReferences><group targetFramework=".NETCoreApp3.1"><frameworkReference name="Microsoft.WindowsDesktop.App.WPF" /></group></frameworkReferences></metadata> </package>核心是 packageTypes 代碼
<packageTypes><packageType name="DotnetTool" /></packageTypes>然后將 packing 文件夾作為壓縮包,注意修改壓縮包名為 id.版本.nupkg 文件
這樣就完成手工打包
總結(jié)
以上是生活随笔為你收集整理的dotnet 手工打一个 dotnet tool 包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gRPC in ASP.NET Core
- 下一篇: 【译】探索更轻量的Electron替代品