Quick.Build
1.0.25
dotnet add package Quick.Build --version 1.0.25
NuGet\Install-Package Quick.Build -Version 1.0.25
<PackageReference Include="Quick.Build" Version="1.0.25" />
<PackageVersion Include="Quick.Build" Version="1.0.25" />
<PackageReference Include="Quick.Build" />
paket add Quick.Build --version 1.0.25
#r "nuget: Quick.Build, 1.0.25"
#:package Quick.Build@1.0.25
#addin nuget:?package=Quick.Build&version=1.0.25
#tool nuget:?package=Quick.Build&version=1.0.25
Quick.Build
一组面向构建脚本的 .NET 小工具。
写 _build 控制台工程时,总要重复实现「删目录 / 递归复制 / 改一行版本号 / 下载依赖 / 控制台里选个目标框架 / 清理 publish 里的 runtimes」这些琐事。这个库把它们收敛成 8 个静态类,直接用,不用再写一遍。
特性一览
| 类 | 命名空间 | 用途 |
|---|---|---|
QbCommand |
Quick.Build | 封装 SimpleExec 执行外部命令 / 进程 |
QbConsole |
Quick.Build | 同行刷新输出(进度条效果) |
QbDotNet |
Quick.Build | 裁剪 publish/runtimes 下的运行时目录 |
QbFile |
Quick.Build | 文件头改写、安全复制/删除、按行读写文本 |
QbFolder |
Quick.Build | 当前程序目录、递归复制/删除、按通配符搜索 |
QbGui |
Quick.Build | 跨平台打开文件夹(资源管理器 / Finder / xdg-open) |
QbJson |
Quick.Build | 直接读写 JSON 文件里的单个属性,无需定义模型 |
QbNet |
Quick.Build | 浏览器化 HttpClient、带进度回调的文件下载 |
QbSelect |
Quick.Build | 控制台单选 / 多选 / 方向键选择,自动降级重定向场景 |
安装
dotnet add package Quick.Build
唯一的外部依赖是 SimpleExec(供 QbGui 与 QbCommand 使用)。
快速开始
典型用法是新建一个 _build 控制台工程引用本库,用顶层语句写构建流程:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Quick.Build\Quick.Build.csproj" />
</ItemGroup>
</Project>
Program.cs 里通常先做一次目录定位,让脚本无论在 dotnet run 还是双击 exe 时都能站在仓库根目录:
using Quick.Build;
var appFolder = QbFolder.GetAppFolder();
if (appFolder == Environment.CurrentDirectory)
Environment.CurrentDirectory = Path.GetFullPath("../../../../");
var baseFolder = Environment.CurrentDirectory;
// 清理上一轮的产物
QbFolder.Delete(Path.Combine(baseFolder, "publish"));
// 版本号自增:直接改 JSON 里的一个属性
var versionFile = Path.Combine(baseFolder, "_build/version.json");
var patch = QbJson.ReadInt32(versionFile, "Patch");
QbJson.WriteInt32(versionFile, "Patch", patch + 1);
// 让用户挑一个目标 RID
var rid = QbSelect.ArrowSelect(new Dictionary<string, string>
{
["win-x64"] = "Windows x64",
["linux-x64"] = "Linux x64",
["0"] = "退出"
}.ToArray(), selectedForegroundColor: ConsoleColor.Green);
// 下载依赖,进度同行刷新
await QbNet.DownloadFile(url, targetFile, CancellationToken.None, p =>
QbConsole.DisplaySameLineInConsole(
$"{p.Current * 100d / p.Total:F1}% {p.Speed / 1024d / 1024d:F1} MB/s 剩余 {p.RemainingTime:hh\\:mm\\:ss}"));
// 只保留需要的运行时目录,减小发布体积
QbDotNet.KeepPublishRuntimes(Path.Combine(baseFolder, "publish"), rid);
// 打开输出目录
QbGui.OpenFolder(Path.Combine(baseFolder, "publish"));
API 参考
QbFile —— 文件
| 方法 | 说明 |
|---|---|
ChangeHeader(file, fileHeader) |
用 ASCII 覆盖写文件头 N 个字节(自解压头、PE 标记等场景) |
Copy(srcFile, destFile, overwrite) |
自动创建目标目录;源文件不存在时静默返回 |
Delete(file) |
不存在则忽略,不抛异常 |
DeleteFiles(folder, pattern, searchOption) |
按通配符批量删除 |
CopyFiles(srcFolder, destFolder, pattern, overwrite) |
按通配符批量复制(CopyTo,目标目录需已存在) |
ReadLine(path, lineIndex[, encoding]) |
读取指定行,默认 UTF-8 |
WriteLine(path, lineIndex, content[, encoding]) |
替换指定行(整文件读入再写回,编码需显式传入以保留) |
QbFolder —— 目录
| 方法 | 说明 |
|---|---|
GetAppFolder() |
入口程序集所在目录(基于 Assembly.GetEntryAssembly()) |
Delete(folder) |
递归删除,不存在则忽略 |
Copy(src, dest, overwrite) |
递归复制,自动创建目标目录 |
SearchFiles(folder, pattern, searchOption) |
返回 FileInfo[],目录不存在时返回空数组 |
SearchFolders(folder, pattern, searchOption) |
返回 DirectoryInfo[],同上 |
DeleteFolders(folder, pattern, searchOption) |
按通配符批量删除目录 |
QbJson —— JSON 单属性读写
基于 System.Text.Json.Nodes,不定义模型也能改配置里的一个字段。
var name = QbJson.ReadString("appsettings.json", "Name");
var port = QbJson.ReadInt32("appsettings.json", "Port");
var ok = QbJson.ReadBoolean("appsettings.json", "Enabled");
QbJson.WriteString("appsettings.json", "Name", "Quick.Build");
QbJson.Write("appsettings.json", "Custom", new[] { 1, 2, 3 });
读取侧:Read<T> / ReadString / ReadInt32 / ReadInt64 / ReadDouble / ReadBoolean。
写入侧:Write<T> / WriteString / WriteInt32 / WriteInt64 / WriteDouble / WriteBoolean。
所有写入统一使用静态属性 DefaultJsonSerializerOptions 序列化,其默认值为:WriteIndented=true(美化缩进)、DefaultIgnoreCondition=WhenWritingNull(忽略 null 值)、Encoder=JavaScriptEncoder.UnsafeRelaxedJsonEscaping(宽松转义,不转义非 ASCII 字符)。可直接修改该静态属性全局调整写入格式。
QbNet —— 下载
| 成员 | 说明 |
|---|---|
GetHttpClient() |
预置浏览器 UA / Accept / Accept-Encoding 的 HttpClient |
DownloadFile(url, file, ct, onProgress) |
32KB 缓冲流式下载,约 0.5s 回调一次进度 |
DownloadFile(client, url, file, ct, onProgress) |
复用现有 HttpClient |
TransferProgress |
Current / Total / Speed(B/s) / RemainingTime |
QbCommand —— 执行外部命令
对 SimpleExec 的 Command.Run 做一层静态封装,参数全部透传。提供两个 Run 重载,区别仅在 args 的类型:
| 重载 | args 类型 |
说明 |
|---|---|---|
Run(name, args, ...) |
string |
以整串字符串传入命令行参数 |
Run(name, args, ...) |
IEnumerable<string> |
以参数数组传入,由 SimpleExec 负责转义与拼接 |
其余共用参数(均有默认值):
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
name |
string |
— | 可执行文件 / 命令名 |
workingDirectory |
string |
"" |
工作目录,空则用当前目录 |
configureEnvironment |
Action<IDictionary<string, string>> |
null |
注入 / 修改环境变量 |
secrets |
IEnumerable<string> |
null |
回显时掩码处理的敏感片段 |
handleExitCode |
Func<int, bool> |
null |
返回 false 时抛 NonZeroExitCodeException |
echoPrefix |
string |
null |
回显前缀 |
noEcho |
bool |
false |
为 true 时不回显命令与输出 |
cancellationIgnoresProcessTree |
bool |
false |
取消时是否忽略进程树 |
createNoWindow |
bool |
false |
为 true 时隐藏新进程窗口 |
ct |
CancellationToken |
default |
取消令牌 |
// 字符串参数
QbCommand.Run("dotnet", "build -c Release", workingDirectory: "src/MyApp");
// 数组参数(推荐,避免手动拼接与转义)
QbCommand.Run("git", new[] { "commit", "-m", "feat: bump" }, noEcho: true);
// 自定义环境变量 + 处理非零退出码
QbCommand.Run("npm", "ci",
configureEnvironment: env => env["NODE_ENV"] = "production",
handleExitCode: code => code == 0);
QbConsole —— 控制台输出
DisplaySameLineInConsole(line):光标归零覆写当前行,做出进度条效果;当输出被重定向时自动降级为 WriteLine,不会在 CI 日志里刷满回车。
QbSelect —— 交互式选择
items 统一为 KeyValuePair<string, string>[],返回被选中项的 Key。
| 方法 | 交互方式 | 返回 |
|---|---|---|
InputSelect(items, inputText, template) |
输入序号 | 单个 Key |
ArrowSelect(items, selectedPrefix, ...colors) |
↑/↓ + Enter | 单个 Key |
MultiSelect(items, ...prefixes, ...colors) |
↑/↓ 移动、空格切换、Enter 确认 | Key 数组 |
三者都检测 Console.IsOutputRedirected:被重定向时改为打印列表并读取一行输入(MultiSelect 支持 1 3,5 这类写法),方便在 CI 里跑。条目数超过窗口高度时会自动滚动窗口。前缀、前景/背景色均可定制,用于高亮或反色主题。
QbGui —— 打开目录
OpenFolder(folder) 按平台分派:Windows 走 explorer(忽略其非零退出码),macOS 走 open,其他走 xdg-open。
QbDotNet —— 发布体积裁剪
QbDotNet.KeepPublishRuntimes(publishFolder, "win-x64", "linux-x64"); // 白名单,删掉其他
QbDotNet.RemovePublishRuntimes(publishFolder, "win-x86"); // 黑名单,只删指定
只处理 publish/runtimes/<rid> 这一层子目录,runtimes 不存在时直接返回。
注意事项
QbNet.GetHttpClient()不校验 TLS 证书(ServerCertificateCustomValidationCallback恒返回true)。它面向内网/构建机下载场景,不要用它承载生产流量。QbJson只支持顶层对象属性,且目标文件必须已存在(内部是File.ReadAllText)。嵌套属性请自行JsonNode处理。QbFile.WriteLine会整文件读入后重写,行尾风格随File.WriteAllLines归一;需要保留原始换行符时请谨慎使用。QbFolder.GetAppFolder()依赖Assembly.GetEntryAssembly(),在单元测试宿主或自托管场景下可能为null。- 编号基准不一致:
InputSelect从 1 开始显示,MultiSelect/ArrowSelect从 0 开始。
License
MIT © 2021 张鹏
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- SimpleExec (>= 13.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.25 | 104 | 11/18/2026 |
| 1.0.24 | 107 | 11/10/2026 |
| 1.0.23 | 99 | 11/10/2026 |
| 1.0.22 | 96 | 11/10/2026 |
| 1.0.21 | 134 | 9/21/2026 |
| 1.0.20 | 379 | 4/20/2025 |
| 1.0.19 | 278 | 4/10/2025 |
| 1.0.18 | 243 | 2/24/2025 |
| 1.0.17 | 218 | 1/24/2025 |
| 1.0.16 | 209 | 1/21/2025 |
| 1.0.15 | 210 | 1/17/2025 |
| 1.0.14 | 197 | 1/17/2025 |
| 1.0.13 | 194 | 1/16/2025 |
| 1.0.12 | 268 | 10/16/2024 |
| 1.0.11 | 203 | 7/30/2024 |
| 1.0.10 | 226 | 7/12/2024 |
| 1.0.9 | 226 | 7/12/2024 |
| 1.0.8 | 254 | 6/28/2024 |
| 1.0.7 | 244 | 5/20/2024 |
| 1.0.6 | 747 | 2/5/2021 |