Taskfile.yml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. version: '3'
  2. includes:
  3. common: ../Taskfile.yml
  4. vars:
  5. # Signing configuration - edit these values for your project
  6. SIGN_CERTIFICATE: "signing.pfx"
  7. # SIGN_THUMBPRINT: "certificate-thumbprint" # Alternative to SIGN_CERTIFICATE
  8. # TIMESTAMP_SERVER: "http://timestamp.digicert.com"
  9. #
  10. # Password is stored securely in system keychain. Run: wails3 setup signing
  11. # Docker image for cross-compilation with CGO (used when CGO_ENABLED=1 on non-Windows)
  12. CROSS_IMAGE: wails-cross
  13. tasks:
  14. build:
  15. summary: Builds the application for Windows
  16. cmds:
  17. # Auto-detect CGO: if CGO_ENABLED=1, use Docker; otherwise use native Go cross-compile
  18. - task: '{{if and (ne OS "windows") (eq .CGO_ENABLED "1")}}build:docker{{else}}build:native{{end}}'
  19. vars:
  20. ARCH: '{{.ARCH}}'
  21. DEV: '{{.DEV}}'
  22. EXTRA_TAGS: '{{.EXTRA_TAGS}}'
  23. OBFUSCATED: '{{.OBFUSCATED}}'
  24. GARBLE_ARGS: '{{.GARBLE_ARGS}}'
  25. vars:
  26. # 鼠标提醒依赖 CGO(syscall.NewCallback 从系统钩子线程进入 Go runtime),
  27. # 故默认启用 CGO=1;如无需 CGO 可显式传 CGO_ENABLED=0。
  28. CGO_ENABLED: '{{.CGO_ENABLED | default "1"}}'
  29. build:native:
  30. summary: Builds the application using native Go cross-compilation
  31. internal: true
  32. deps:
  33. - task: common:go:mod:tidy
  34. - task: common:build:frontend
  35. vars:
  36. BUILD_FLAGS:
  37. ref: .BUILD_FLAGS
  38. OBFUSCATED:
  39. ref: .OBFUSCATED
  40. DEV:
  41. ref: .DEV
  42. - task: common:generate:icons
  43. preconditions:
  44. - sh: '{{if eq .OBFUSCATED "true"}}command -v garble >/dev/null 2>&1{{else}}true{{end}}'
  45. msg: "garble is required for obfuscated builds. Install it with: go install mvdan.cc/garble@v0.16.0 (requires Go 1.24+). See https://github.com/burrowers/garble/releases for version/toolchain compatibility."
  46. cmds:
  47. - task: generate:syso
  48. vars:
  49. ARCH: '{{.ARCH}}'
  50. - '{{if eq .OBFUSCATED "true"}}garble {{.GARBLE_ARGS}} build{{else}}go build{{end}} {{.BUILD_FLAGS}} -o "{{.BIN_DIR}}/{{.APP_NAME}}.exe"'
  51. # 独立服务二进制:cmd/toolbox-service 自包含实现(无 Wails/Gin/GUI 依赖,仅标准库 + x/sys)
  52. - '{{if eq .OBFUSCATED "true"}}garble {{.GARBLE_ARGS}} build{{else}}go build{{end}} {{.SERVICE_BUILD_FLAGS}} -o "{{.BIN_DIR}}/{{.APP_NAME}}-service.exe" ./cmd/toolbox-service'
  53. - task: upx:compress
  54. vars:
  55. TARGET_FILE: '{{.BIN_DIR}}/{{.APP_NAME}}.exe'
  56. - task: upx:compress
  57. vars:
  58. TARGET_FILE: '{{.BIN_DIR}}/{{.APP_NAME}}-service.exe'
  59. - cmd: powershell Remove-item *.syso
  60. platforms: [windows]
  61. - cmd: rm -f *.syso
  62. platforms: [linux, darwin]
  63. vars:
  64. BUILD_FLAGS: '{{if eq .DEV "true"}}{{if or .EXTRA_TAGS (eq .OBFUSCATED "true")}}-tags {{if eq .OBFUSCATED "true"}}wails_obfuscated{{if .EXTRA_TAGS}},{{end}}{{end}}{{.EXTRA_TAGS}} {{end}}-buildvcs=false -gcflags=all="-l"{{else}}-tags production{{if eq .OBFUSCATED "true"}},wails_obfuscated{{end}}{{if .EXTRA_TAGS}},{{.EXTRA_TAGS}}{{end}} -trimpath -buildvcs=false -ldflags="-w -s -H windowsgui"{{end}}'
  65. # 服务构建:同样遵循 DEV/OBFUSCATED,但无 GUI 标签需求(不含 windowsgui,服务无窗口)
  66. SERVICE_BUILD_FLAGS: '{{if eq .DEV "true"}} -buildvcs=false -gcflags=all="-l"{{else}} -trimpath -buildvcs=false -ldflags="-w -s"{{end}}'
  67. env:
  68. GOOS: windows
  69. CGO_ENABLED: '{{.CGO_ENABLED | default "1"}}'
  70. GOARCH: '{{.ARCH | default ARCH}}'
  71. build:docker:
  72. summary: Cross-compiles for Windows using Docker with Zig (for CGO builds on non-Windows)
  73. internal: true
  74. deps:
  75. - task: common:build:frontend
  76. vars:
  77. OBFUSCATED:
  78. ref: .OBFUSCATED
  79. - task: common:generate:icons
  80. preconditions:
  81. - sh: docker info > /dev/null 2>&1
  82. msg: "Docker is required for CGO cross-compilation. Please install Docker."
  83. - sh: docker image inspect {{.CROSS_IMAGE}} > /dev/null 2>&1
  84. msg: |
  85. Docker image '{{.CROSS_IMAGE}}' not found.
  86. Build it first: wails3 task setup:docker
  87. cmds:
  88. - task: generate:syso
  89. vars:
  90. ARCH: '{{.ARCH}}'
  91. - docker run --rm -v "{{.ROOT_DIR}}:/app" {{.DOCKER_MOUNTS}} -e APP_NAME="{{.APP_NAME}}" {{if .EXTRA_TAGS}}-e EXTRA_TAGS="{{.EXTRA_TAGS}}"{{end}} {{if eq .OBFUSCATED "true"}}-e OBFUSCATED=true{{end}} {{if .GARBLE_ARGS}}-e GARBLE_ARGS="{{.GARBLE_ARGS}}"{{end}} {{.CROSS_IMAGE}} windows {{.DOCKER_ARCH}}
  92. - cmd: docker run --rm -v "{{.ROOT_DIR}}:/app" alpine chown -R $(id -u):$(id -g) /app/bin
  93. platforms: [linux, darwin]
  94. - rm -f *.syso
  95. vars:
  96. DOCKER_ARCH: '{{.ARCH | default "amd64"}}'
  97. # Generate Docker volume mounts: Go module cache + go.mod replace directives
  98. # Uses wails3 tool docker-mounts for cross-platform compatibility (Windows/Linux/macOS)
  99. DOCKER_MOUNTS:
  100. sh: 'wails3 tool docker-mounts'
  101. package:
  102. summary: Packages the application
  103. cmds:
  104. - task: '{{if eq (.FORMAT | default "nsis") "msix"}}create:msix:package{{else}}create:nsis:installer{{end}}'
  105. vars:
  106. INSTALL_SCOPE: '{{.INSTALL_SCOPE | default "machine"}}'
  107. vars:
  108. FORMAT: '{{.FORMAT | default "nsis"}}'
  109. INSTALL_SCOPE: '{{.INSTALL_SCOPE | default "machine"}}'
  110. generate:syso:
  111. summary: Generates Windows `.syso` file
  112. dir: build
  113. cmds:
  114. - wails3 generate syso -arch {{.ARCH}} -icon windows/icon.ico -manifest windows/wails.exe.manifest -info windows/info.json -out ../wails_windows_{{.ARCH}}.syso
  115. vars:
  116. ARCH: '{{.ARCH | default ARCH}}'
  117. upx:compress:
  118. summary: Compress the built binary with UPX (if available)
  119. internal: true
  120. cmds:
  121. - |
  122. if [ "{{.DEV}}" = "true" ]; then
  123. echo "DEV mode, skipping UPX."
  124. exit 0
  125. fi
  126. if command -v upx >/dev/null 2>&1; then
  127. echo "Compressing {{.TARGET_FILE}} with UPX..."
  128. upx --best --lzma "{{.TARGET_FILE}}" || true
  129. else
  130. echo "UPX not found in PATH, skipping compression."
  131. fi
  132. vars:
  133. TARGET_FILE: '{{.TARGET_FILE}}'
  134. create:nsis:installer:
  135. summary: Creates an NSIS installer
  136. dir: build/windows/nsis
  137. deps:
  138. - task: build
  139. preconditions:
  140. - sh: '[ "{{.INSTALL_SCOPE}}" = "user" ] || [ "{{.INSTALL_SCOPE}}" = "machine" ]'
  141. msg: "INSTALL_SCOPE must be 'user' or 'machine', got '{{.INSTALL_SCOPE}}'"
  142. cmds:
  143. # Create the Microsoft WebView2 bootstrapper if it doesn't exist
  144. - wails3 generate webview2bootstrapper -dir "{{.ROOT_DIR}}/build/windows/nsis"
  145. - |
  146. {{if eq OS "windows"}}
  147. makensis {{if eq .INSTALL_SCOPE "user"}}-DWAILS_INSTALL_SCOPE=user -DREQUEST_EXECUTION_LEVEL=user {{end}}-DARG_WAILS_{{.ARG_FLAG}}_BINARY="{{.ROOT_DIR}}\{{.BIN_DIR}}\{{.APP_NAME}}.exe" -DARG_WAILS_SERVICE_BINARY="{{.ROOT_DIR}}\{{.BIN_DIR}}\{{.APP_NAME}}-service.exe" project.nsi
  148. {{else}}
  149. makensis {{if eq .INSTALL_SCOPE "user"}}-DWAILS_INSTALL_SCOPE=user -DREQUEST_EXECUTION_LEVEL=user {{end}}-DARG_WAILS_{{.ARG_FLAG}}_BINARY="{{.ROOT_DIR}}/{{.BIN_DIR}}/{{.APP_NAME}}.exe" -DARG_WAILS_SERVICE_BINARY="{{.ROOT_DIR}}/{{.BIN_DIR}}/{{.APP_NAME}}-service.exe" project.nsi
  150. {{end}}
  151. vars:
  152. ARCH: '{{.ARCH | default ARCH}}'
  153. ARG_FLAG: '{{if eq .ARCH "amd64"}}AMD64{{else}}ARM64{{end}}'
  154. INSTALL_SCOPE: '{{.INSTALL_SCOPE | default "machine"}}'
  155. create:msix:package:
  156. summary: Creates an MSIX package
  157. deps:
  158. - task: build
  159. cmds:
  160. - |-
  161. wails3 tool msix \
  162. --config "{{.ROOT_DIR}}/build/config.yml" \
  163. --name "{{.APP_NAME}}.exe" \
  164. --executable "{{.ROOT_DIR}}/{{.BIN_DIR}}/{{.APP_NAME}}.exe" \
  165. --arch "{{.ARCH}}" \
  166. --out "{{.ROOT_DIR}}/{{.BIN_DIR}}/{{.APP_NAME}}-{{.ARCH}}.msix" \
  167. {{if .CERT_PATH}}--cert "{{.CERT_PATH}}"{{end}} \
  168. {{if .PUBLISHER}}--publisher "{{.PUBLISHER}}"{{end}} \
  169. {{if eq .USE_MSIX_TOOL "true"}}--use-msix-tool{{else}}--use-makeappx{{end}}
  170. vars:
  171. ARCH: '{{.ARCH | default ARCH}}'
  172. CERT_PATH: '{{.CERT_PATH | default ""}}'
  173. PUBLISHER: '{{.PUBLISHER | default ""}}'
  174. USE_MSIX_TOOL: '{{.USE_MSIX_TOOL | default "false"}}'
  175. install:msix:tools:
  176. summary: Installs tools required for MSIX packaging
  177. cmds:
  178. - wails3 tool msix-install-tools
  179. run:
  180. cmds:
  181. - '{{.BIN_DIR}}/{{.APP_NAME}}.exe'
  182. sign:
  183. summary: Signs the Windows executable
  184. desc: |
  185. Signs the .exe with an Authenticode certificate.
  186. Set SIGN_CERTIFICATE or SIGN_THUMBPRINT to override the certificate
  187. configured globally via `wails3 setup` (~/.config/wails/defaults.yaml).
  188. Password is retrieved from system keychain (run: wails3 setup signing)
  189. deps:
  190. - task: build
  191. cmds:
  192. # Certificate vars are optional: if unset, `wails3 tool sign` falls back to
  193. # the certificate configured globally via `wails3 setup`.
  194. - wails3 tool sign --input "{{.BIN_DIR}}/{{.APP_NAME}}.exe" {{if .SIGN_CERTIFICATE}}--certificate "{{.SIGN_CERTIFICATE}}"{{end}} {{if .SIGN_THUMBPRINT}}--thumbprint "{{.SIGN_THUMBPRINT}}"{{end}} {{if .TIMESTAMP_SERVER}}--timestamp "{{.TIMESTAMP_SERVER}}"{{end}}
  195. sign:installer:
  196. summary: Signs the NSIS installer
  197. desc: |
  198. Creates and signs the NSIS installer.
  199. Set SIGN_CERTIFICATE or SIGN_THUMBPRINT to override the certificate
  200. configured globally via `wails3 setup` (~/.config/wails/defaults.yaml).
  201. Password is retrieved from system keychain (run: wails3 setup signing)
  202. deps:
  203. - task: create:nsis:installer
  204. vars:
  205. INSTALL_SCOPE: '{{.INSTALL_SCOPE | default "machine"}}'
  206. vars:
  207. INSTALL_SCOPE: '{{.INSTALL_SCOPE | default "machine"}}'
  208. cmds:
  209. - wails3 tool sign --input "build/windows/nsis/{{.APP_NAME}}-installer.exe" {{if .SIGN_CERTIFICATE}}--certificate "{{.SIGN_CERTIFICATE}}"{{end}} {{if .SIGN_THUMBPRINT}}--thumbprint "{{.SIGN_THUMBPRINT}}"{{end}} {{if .TIMESTAMP_SERVER}}--timestamp "{{.TIMESTAMP_SERVER}}"{{end}}