dotnet-exec 0.11.0 released
Intro
dotnet-exec
是一個 C# 程序的小工具,可以用來運行一些簡單的 C# 程序而無需創建項目文件,讓 C# 像 python/nodejs 一樣簡單,而且可以自定義項目的入口方法,支持但不限于 Main 方法。
Install/Update
dotnet-exec
是一個 dotnet tool,可以使用安裝 dotnet tool 的命令來安裝
安裝/更新最新穩定版本:
dotnet?tool?update?-g?dotnet-execute
安裝最新的 preview 版本:
dotnet?tool?update?-g?dotnet-execute?--prerelease
執行 dotnet-exec -h
或者 dotnet-exec --help
即可看到一些使用說明
Features
Static using && using alias
在之前版本中對于 script,我們是不支持 static using 和 using alias 的,只支持 code 中使用,在 0.10.0 版本中我們支持了在 script 中使用 static using 和 using 的別名了
舉個栗子
dotnet-exec?'MyConsole.WriteLine(PI)'?--using?'MyConsole=System.Console'?--using?static?'System.Math'
實現原理其實也比較簡單,script 默認的 import 選項只能引入普通的命名空間,于是我們曲線救國,把 using 作為代碼先執行了一下,之后再執行我們的 script 代碼,在同一個上下文中會記住之前的 using 信息
具體實現代碼可以參考:https://github.com/WeihanLi/dotnet-exec/blob/2b2d2d4d47da5561001fb9f172bea65a8daa0932/src/dotnet-exec/CSharpScriptingExecutor.cs#L50-L52
Execute without SDK
在之前的版本中我們進行編譯的時候始終會選擇去使用引用程序集進行編譯,在沒有 SDK 的環境里會嘗試從 nuget 上下載引用程序集進行編譯
在 0.10.0 版本中,我們默認會使用 runtime 的程序集進行編譯,這樣即使沒有網絡,沒有 SDK,只有 runtime 依然是可以工作的,當然你仍然可以使用 --ref-compile
選項來指定始終使用引用程序集來編譯
Execute without web
在之前的版本中我們的 docker 鏡像使用的是 aspnet
?的鏡像,考慮很多場景可能用不到 web 框架,所以從 0.11.0
版本開始默認不會再引用 web 框架引用,當然你也可以使用 -w
/--web
來顯式添加 web 框架引用
對于 docker 鏡像,新的 latest
tag 的鏡像的基礎鏡像換成了 runtime
,另外單獨提供了一個 web
tag 的鏡像基礎鏡像是 aspnet
,這樣我們的鏡像就能小很多了
但是即使你使用的是 latest
指定了使用 web 框架引用那你仍然可以運行,只是本地沒有 aspnet runtime 的時候會嘗試從 nuget 下載,而 web
則內置了 runtime 不需要再去下載,來個例子
首先我們可以來看一下最新的 docker 鏡像中只有一個 .NET Core runtime 的信息,沒有 aspnet 的 runtime 了
我們用這個鏡像來跑一個 web api 項目
docker?run?--rm?--pull=always?weihanli/dotnet-exec:latest?dotnet-exec?'WebApplication.Create().Run();'?--web
從上面的截圖可以看到我們的 webapi 已經跑起來了,我們可以進一步使用暴露一個端口,請求一下我們的 API 試一下,讓我們稍加改造
docker?run?--rm?--pull=always?-p?5000:80?weihanli/dotnet-exec:latest?dotnet-exec?'WebApplication.Create().Chain(_?=>?_.MapRuntimeInfo()).Run();'?--web?--reference?"nuget:WeihanLi.Web.Extensions"?--using?'WeihanLi.Web.Extensions'
這里引用了一個我自己封裝的一個擴展,會注冊一個 runtime-info
的 endpoint,我這里使用的是 Github 的 CodeSpaces,會自動暴露一個 endpoint 來訪問我們的服務,我們可以訪問我們的 runtime-info
endpoint 會看到一個類似下面的 response
我們也可以使用 curl 或者可以使用 dotnet-httpie
來訪問這個 endpoint
dotnet-http?:5000/runtime-info
ProjectReference
在 0.11.0 版本中我們增加項目引用的支持,實現原理是嘗試 build 項目,引用 build 之后的 dll
使用方式如下:
dotnet-exec?'ApplicationHelper.GetLibraryInfo(typeof(CsvHelper))'?--reference?'project:C:\projects\sources\WeihanLi.Npoi\src\WeihanLi.Npoi\WeihanLi.Npoi.csproj'?--using?'WeihanLi.Npoi'
和引用 nuget 的效果基本一致
ApplicationHelper.GetLibraryInfo
是從 assembly 信息中獲取信息封裝的一個方法,可以參考:https://github.com/WeihanLi/WeihanLi.Common/blob/d2db73a0e02cef009dc61190a41263ad6cb2b6bc/src/WeihanLi.Common/Helpers/ApplicationHelper.cs#L28
More
原來引用本地的 dll 需要指定一個絕對路徑(full path),在 0.11.0
版本中我們支持了相對路徑,使用起來也是更加的簡單,新增加的項目引用也是支持相對路徑的
上面的更新包含了 0.10.0 版本和 0.11.0 版本,具體更新可以參考 Github,代碼變更可以參考:https://github.com/WeihanLi/dotnet-exec/compare/0.9.0...0.11.0
References
https://github.com/WeihanLi/dotnet-exec
https://www.nuget.org/packages/dotnet-execute/
https://hub.docker.com/r/weihanli/dotnet-exec
https://github.com/WeihanLi/dotnet-exec/compare/0.9.0...0.11.0
https://github.com/WeihanLi/WeihanLi.Common/blob/d2db73a0e02cef009dc61190a41263ad6cb2b6bc/src/WeihanLi.Common/Helpers/ApplicationHelper.cs#L28