當前位置: 妍妍網 > 碼農

dotnet-exec 0.17.0 Released

2024-02-10碼農

dotnet-exec 0.17.0 Released

Intro

dotnet-exec 是一個 dotnet tool 一個命令列小工具,可以幫助我們方便地執行 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 即可看到一些使用說明

dotnet-exec help

也可以透過 docker/podman 來在不安裝 dotnet sdk 的情況下體驗,例如:

docker run --rm --pull=always weihanli/dotnet-exec:latest "ApplicationHelper.RuntimeInfo"

docker

Features

Debug Env

在有些情況下,我們的執行指令碼可能已經配置好了,不想再改變,但是可能需要偵錯模式來看到更多的資訊

此時我們可以透過配置一個環境變量 DOTNET_EXEC_DEBUG_ENABLED 來啟用 debug 模式,下面是在 powershell 中的一個範例

Info option

新增了 --info 來打印 tool 相關的版本資訊,類似於 `dotnet --info`,範例如下:

Enhancements

NuGet Helper

dotnet-exec 計畫中提取了一個 ReferenceResolver 計畫用來簡化 Roslyn 中的依賴參照,其中一個重要的部份就是對 NuGet 的一些操作進行了封裝, dotnet-exec 也正是基於它實作的 NuGet 參照和 NuGet config 的支持

之前的版本中, 只支持選擇一個 source 作為過濾,在這個版本中支持多個 source ,主要是參考了 Rider 裏的 NuGet Package 的管理界面,允許使用者選擇多個 source,對於 dotnet-exec 並無影響

Docker Image

之前的 docker image 執行時需要顯式的指定 dotnet-exec ,新版本開始添加了預設的 entrypoint,不需要再指定 dotnet-exec 直接指定參數即可,例如:

docker run --rm weihanli/dotnet-exec:0.17.0 "Guid.NewGuid()"

如果不指定參數會使用預設的 CMD 配置,會打印出幫助資訊

dotnet-exec 的 docker image 預設是用 root 使用者, dotnet-exec 之前未配置可執行許可權,在嘗試直接用 dotnet-exec 的 image 來執行 github action 的時候發現了這個問題,非 root 使用者執行會沒有許可權,在這個版本中將 dotnet-exec 顯式地設定成了可執行許可權

這些變化使得 dotnet-exec Github Action 變得更加簡單,可以直接使用 dotnet-exec 的 docker image

Dockerfile 中新增了下面三行配置

RUN chmod +x ./dotnet-exec
ENTRYPOINT [ "dotnet-exec" ]
CMD [ "--help" ]

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.16.0...0.17.0