尝鲜.net core2.1 ——编写一个global tool
本文內(nèi)容參考微軟工程師Nate McMaster的博文.NET Core 2.1 Global Tools
用過(guò)npm開發(fā)都知道,npm包都可以以全局的方式安裝,例如安裝一個(gè)http-server服務(wù),可以使用npm i http-server -g來(lái)將http-server包安裝到全局環(huán)境。安裝完之后,就可以通過(guò)cmd或者powershell運(yùn)行全局工具h(yuǎn)ttp-server命令,來(lái)使用靜態(tài)托管服務(wù)。dotnet tool 就是一個(gè)類似npm全局工具的新特性,在.net core2.1正式加入。它的詳細(xì)使用方法可在微軟官方文檔查看,本文主要介紹如何編寫一個(gè)global tool并發(fā)布至nuget。
安裝.net core 2.1
安裝最新版.net core SDK 可前往DotNet官方站點(diǎn)的下載頁(yè)面,下載完成后雙擊安裝即可。安裝完成后打開cmd運(yùn)行dotnet --version?返回版本大于或等于2.1.300表示安裝成功。
安裝global tool 項(xiàng)目模板
打開cmd 鍵入dotnet new --install McMaster.DotNet.GlobalTool.Templates安裝完成后運(yùn)行dotnet new
模板? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 短名稱? ? ? ? ? ? ? ? 語(yǔ)言? ? ? ? ? ? ? ? 標(biāo)記
----------------------------------------------------------------------------------------------------------------------------
Console Application? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?console? ? ? ? ? ? [C#], F#, VB? ? ? Common/Console
Class library? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?classlib? ? ? ? ? ?[C#], F#, VB? ? ? Common/Library
.NET Core Global Console Tool? ? ? ? ? ? ? ? ? ? ?global-tool? ? ? ? [C#]? ? ? ? ? ? ? Console/Empty
Unit Test Project? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mstest? ? ? ? ? ? ?[C#], F#, VB? ? ? Test/MSTest
xUnit Test Project? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? xunit? ? ? ? ? ? ? [C#], F#, VB? ? ? Test/xUnit
Razor Page? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? page? ? ? ? ? ? ? ?[C#]? ? ? ? ? ? ? Web/ASP.NET
MVC ViewImports? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?viewimports? ? ? ? [C#]? ? ? ? ? ? ? Web/ASP.NET
MVC ViewStart? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?viewstart? ? ? ? ? [C#]? ? ? ? ? ? ? Web/ASP.NET
ASP.NET Core Empty? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? web? ? ? ? ? ? ? ? [C#], F#? ? ? ? ? Web/Empty
ASP.NET Core Web App (Model-View-Controller)? ? ? mvc? ? ? ? ? ? ? ? [C#], F#? ? ? ? ? Web/MVC
ASP.NET Core Web App? ? ? ? ? ? ? ? ? ? ? ? ? ? ? razor? ? ? ? ? ? ? [C#]? ? ? ? ? ? ? Web/MVC/Razor Pages
ASP.NET Core with Angular? ? ? ? ? ? ? ? ? ? ? ? ?angular? ? ? ? ? ? [C#]? ? ? ? ? ? ? Web/MVC/SPA
ASP.NET Core with React.js? ? ? ? ? ? ? ? ? ? ? ? react? ? ? ? ? ? ? [C#]? ? ? ? ? ? ? Web/MVC/SPA
ASP.NET Core with React.js and Redux? ? ? ? ? ? ? reactredux? ? ? ? ?[C#]? ? ? ? ? ? ? Web/MVC/SPA
Razor Class Library? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?razorclasslib? ? ? [C#]? ? ? ? ? ? ? Web/Razor/Library/Razor Class Library
ASP.NET Core Web API? ? ? ? ? ? ? ? ? ? ? ? ? ? ? webapi? ? ? ? ? ? ?[C#], F#? ? ? ? ? Web/WebAPI
global.json file? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? globaljson? ? ? ? ? ? ? ? ? ? ? ? ? ?Config
NuGet Config? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nugetconfig? ? ? ? ? ? ? ? ? ? ? ? ? Config
Web Config? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? webconfig? ? ? ? ? ? ? ? ? ? ? ? ? ? Config
Solution File? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sln? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Solution
多出一個(gè)global-tool模板
.NET Core Global Console Tool ? ?global-tool ? ??[C#] ? ? ? ? ? ? ?Console/Empty
編寫一個(gè)網(wǎng)頁(yè)下載工具
接下來(lái)通過(guò)編寫一個(gè)網(wǎng)頁(yè)下載的小工具來(lái)演示global tool的創(chuàng)建過(guò)程,此小工具的功能是根據(jù)網(wǎng)址,下載相應(yīng)的頁(yè)面html并保存為文件。
首先新建一個(gè)WebDownloader文件夾。在文件夾中運(yùn)行dotnet new global-tool生成項(xiàng)目如下
objProgram.csWebDownloader.csproj打開WebDownloader.csproj修改為
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><ToolCommandName>web-downloader</ToolCommandName><PackAsTool>True</PackAsTool><OutputType>Exe</OutputType><TargetFramework>netcoreapp2.1</TargetFramework></PropertyGroup><ItemGroup><PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.3" /></ItemGroup></Project>打開Program.cs修改為
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Net.Http;
using McMaster.Extensions.CommandLineUtils;
namespace WebDownloader
{
? ? [Command(Description = "網(wǎng)頁(yè)下載小工具")]
? ? class Program
? ? {
? ? ? ? public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
? ? ? ? [Argument(0, Description = "網(wǎng)址")]
? ? ? ? [Required]
? ? ? ? public string Url { get; }
? ? ? ? [Option(Description = "保存路徑")]
? ? ? ? public string Path { get; } = "./";
? ? ? ? [Option(Description = "文件名")]
? ? ? ? public string Name { get; } = "content.txt";
? ? ? ? private int OnExecute()
? ? ? ? {
? ? ? ? ? ? var client = new HttpClient();
? ? ? ? ? ? var content = client.GetStringAsync(Url).Result;
? ? ? ? ? ? var path = System.IO.Path.Combine(Path, Name);
? ? ? ? ? ? File.WriteAllText(path, content);
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
修改完成后全部保存文件,運(yùn)行dotnet pack -o ./會(huì)在項(xiàng)目根目錄生成一個(gè)WebDownloader.1.0.0.nupkg包。此包就是最終的nuget包,可上傳至nuget.org共享。
為了測(cè)試,我們直接將此包安裝至本地計(jì)算機(jī)。運(yùn)行dotnet tool install WebDownloader -g --add-source ./完成安裝。運(yùn)行web-downloader -h可以看到項(xiàng)目幫助文檔
網(wǎng)頁(yè)下載小工具Usage: WebDownloader [arguments] [options]Arguments:Url ? ? ? ? ? ? ? 網(wǎng)址Options:-p|--path <PATH> ?保存路徑-n|--name <NAME> ?文件名-?|-h|--help ? ? ?Show help information運(yùn)行web-downloader http://www.sina.com后我們發(fā)現(xiàn)項(xiàng)目根目錄生成了一個(gè)content.txt文件內(nèi)容為新浪的首頁(yè)html
<!-- [ published at 2018-05-31 23:35:00 ] --><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><title>新浪首頁(yè)</title><meta name="keywords" content="新浪,新浪網(wǎng),SINA,sina,sina.com.cn,新浪首頁(yè),門戶,資訊" /><meta name="description" content="新浪網(wǎng)為全球用戶24小時(shí)提供全面及時(shí)的中文資訊,內(nèi)容覆蓋國(guó)內(nèi)外突發(fā)新聞事件、體壇賽事、娛樂(lè)時(shí)尚、產(chǎn)業(yè)資訊、實(shí)用信息等,設(shè)有新聞、體育、娛樂(lè)、財(cái)經(jīng)、科技、房產(chǎn)、汽車等30多個(gè)內(nèi)容頻道,同時(shí)開設(shè)博客、視頻、論壇等自由互動(dòng)交流空間。" /><link rel="mask-icon" sizes="any" href="//www.sina.com.cn/favicon.svg" color="red"><meta name="stencil" content="PGLS000022" /><meta name="publishid" content="30,131,1" /><meta name="verify-v1" content="6HtwmypggdgP1NLw7NOuQBI2TW8+CfkYCoyeB8IDbn8=" /><meta name="360-site-verification" content="63349a2167ca11f4b9bd9a8d48354541" /><meta name="application-name" content="新浪首頁(yè)"/><meta name ="msapplication-TileImage" content="//i1.sinaimg.cn/dy/deco/2013/0312/logo.png"/><meta name="msapplication-TileColor" content="#ffbf27"/><meta name="sogou_site_verification" content="Otg5irx9wL"/><link rel="apple-touch-icon" href="//i3.sinaimg.cn/home/2013/0331/U586P30DT20130331093840.png" />... ...如果不再使用此工具通過(guò)dotnet tool uninstall WebDownloader -g卸載即可。
原文地址:https://www.cnblogs.com/huanent/p/9119213.html
.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的尝鲜.net core2.1 ——编写一个global tool的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET Core Identit
- 下一篇: 微软75亿收购Github,微软以开发者