Files
Meshray-Manager/docs/优化构建脚本 - 移除 winres 目录.md
T
2026-06-30 15:14:37 +08:00

7.9 KiB
Raw Blame History

优化构建脚本 - 不再需要 winres/文件夹

优化时间: 2026-03-24
状态: 已完成
改进: 使用 --in 参数直接指定配置文件路径


🎯 优化内容

问题:为什么会有 winres/文件夹?

原因go-winres 工具的默认行为

go-winres make
# ↑ 默认读取 ./winres/winres.json

这是工具的硬编码约定,类似:

  • Maven 读取 pom.xml
  • npm 读取 package.json
  • Go 读取 go.mod

解决方案:使用 --in 参数

修改后的命令

go-winres make --in build/winres.json --arch amd64
#                  ↑ 直接指定配置文件路径

优点

  • 不需要额外的 winres/目录
  • 配置文件集中管理(build/目录)
  • 减少临时文件,保持项目整洁

📋 修改对比

修改前(需要 winres/

REM 2. 准备配置文件
if not exist winres (
    mkdir winres
)
copy build\winres.json winres\winres.json

REM 3. 生成资源文件
go-winres make --arch amd64
# ↑ 自动读取 winres/winres.json

问题

  • 需要创建临时 winres/目录
  • 需要复制配置文件
  • 增加不必要的步骤

修改后(不需要 winres/

REM 2. 检查配置文件
if not exist build\winres.json (
    echo [错误] 配置文件不存在
    exit /b 1
)

REM 3. 生成资源文件
go-winres make --in build\winres.json --arch amd64
#                  ↑ 直接指定路径

优势

  • 无需创建临时目录
  • 无需复制文件
  • 步骤更简洁

🔧 更新的脚本

build.batWindows

修改部分

@echo off
REM MeshRay Windows 完整构建脚本(go-winres

REM 1. 检查 go-winres 工具
where go-winres >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    go install github.com/tc-hib/go-winres@latest
)
echo [✓] go-winres 已安装

REM 2. 检查配置文件 ← 修改点
echo [2/7] 检查资源配置...
if not exist build\winres.json (
    echo [错误] 配置文件不存在:build\winres.json
    pause
    exit /b 1
)
echo [✓] 配置文件已检查

REM 3. 生成资源文件 ← 修改点
echo [3/7] 生成 Windows 资源文件...
go-winres make --in build\winres.json --arch amd64  ← 添加 --in 参数
if %ERRORLEVEL% NEQ 0 (
    echo [错误] 资源文件生成失败!
    pause
    exit /b 1
)
echo [✓] 资源文件生成成功

REM ...后续步骤不变

build.sh(跨平台)

修改部分

#!/bin/bash
# MeshRay 跨平台构建脚本(go-winres

# 1. 检查 go-winres 工具
if [ "$RSRC_NEEDED" = true ]; then
    if ! command -v go-winres &> /dev/null; then
        go install github.com/tc-hib/go-winres@latest
    fi
    echo "[✓] go-winres 已安装"
    
    # 2. 检查配置文件 ← 修改点
    echo "[2/7] 检查资源配置..."
    if [ ! -f "build/winres.json" ]; then
        echo "[错误] 配置文件不存在:build/winres.json"
        exit 1
    fi
    echo "[✓] 配置文件已检查
    
    # 3. 生成资源文件 ← 修改点
    echo "[3/7] 生成 Windows 资源文件..."
    go-winres make --in build/winres.json --arch amd64  ← 添加 --in 参数
    if [ $? -ne 0 ]; then
        echo "[错误] 资源文件生成失败!"
        exit 1
    fi
    echo "[] 资源文件生成成功"
fi

📊 效果对比

项目结构

修改前

e:\Project\MeshRay\
├── build/
│   └── winres.json          ← 源配置
├── winres/                   ← 临时目录(不必要)
│   └── winres.json          ← 复制的配置
├── cmd/meshray/
│   └── meshray.syso
└── meshray.exe

修改后

e:\Project\MeshRay\
├── build/
│   └── winres.json          ← 直接使用
├── cmd/meshray/
│   └── meshray.syso
└── meshray.exe

改进

  • 删除了 winres/目录
  • 减少了文件复制操作
  • 项目结构更简洁

构建步骤

步骤 修改前 修改后 改进
1. 检查工具 保持不变
2. 配置文件 创建目录 + 复制 直接检查 减少 2 步操作
3. 生成资源 从 winres/读取 从 build/读取 更直接
4-7 不变 不变 保持一致

🛠️ 技术原理

go-winres 的参数系统

查看帮助

go-winres make --help

OPTIONS:
   --in value  name of the input json file (default: "winres/winres.json")
   --arch value  comma separated list of target architectures (default: "amd64,386")
   --out value  name or prefix of the object file (syso) (default: "rsrc")

参数说明

  • --in: 指定输入的 JSON 配置文件路径
  • --arch: 指定目标架构(amd64, 386等)
  • --out: 指定输出的 syso文件名

默认值陷阱

  • 如果不指定 --in,工具会尝试读取 ./winres/winres.json
  • 如果该文件不存在,会报错或创建默认配置

为什么之前没想到?

思维定式

  1. 看到 go-winres 示例中使用 winres/winres.json
  2. 认为必须遵守这个约定
  3. 没有注意到可以自定义路径

解决过程

  1. 用户问:"为什么会有 winres/文件夹?"
  2. 重新审视工具文档
  3. 发现 --in 参数
  4. 优化脚本,去掉不必要的目录

验证结果

测试步骤

# 1. 删除 winres/目录
Remove-Item winres -Recurse -Force

# 2. 直接使用 build/中的配置
go-winres make --in build\winres.json --arch amd64

# 3. 验证结果
Get-Item rsrc_windows_amd64.syso
# Name: rsrc_windows_amd64.syso
# Length: 288160  ← 成功生成!

输出

✅ 成功生成 syso(无需 winres/目录)

完整构建流程验证

# 清理
Remove-Item winres -Recurse -Force
Remove-Item meshray.exe -ErrorAction SilentlyContinue

# 执行完整构建
go-winres make --in build\winres.json --arch amd64
Copy-Item rsrc_windows_amd64.syso cmd\meshray\meshray.syso
go build -ldflags="-s -w" -o meshray.exe ./cmd/meshray

# 验证
(Get-Item meshray.exe).VersionInfo.CompanyName
# MeshRay Team ✅

结论

  • 不需要 winres/目录
  • 可以直接使用 build/winres.json
  • 构建流程完全正常

📝 总结

核心改进

项目 改进幅度 说明
目录结构 删除不必要的 winres/
构建步骤 减少文件复制操作
代码清晰度 更直观的配置路径
维护成本 减少临时文件管理

经验教训

  1. 不要盲目接受默认值

    • 工具的默认行为不一定是最佳选择
    • 要理解背后的原理
  2. 多查看帮助文档

    • --help 参数往往包含有用的信息
    • 了解所有可用选项
  3. 保持项目整洁

    • 能少一个目录就少一个
    • 能少一步操作就少一步

最终状态

项目结构

e:\Project\MeshRay\
├── build/
│   ├── main.manifest        ← Windows 清单
│   ├── winres.json          ← Windows 资源配置
│   └── resource.rc          ← RC 资源脚本(备用)
├── cmd/
│   └── meshray/
│       ├── main.go
│       └── meshray.syso     ← 编译时的资源文件
├── docs/                    ← 文档目录
├── assets/                  ← 图标等资源
└── meshray.exe              ← 最终产物

特点

  • 结构清晰
  • 无临时目录
  • 易于维护
  • 符合直觉

优化状态: 完成!不再需要 winres/文件夹
构建方式: 使用 --in 参数直接指定配置
项目结构: 更加简洁清晰

MeshRay - 持续优化,追求卓越!