当前位置: 欣欣网 > 码农

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