electron-builder.config.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { existsSync } from "node:fs"
  2. import { execFile } from "node:child_process"
  3. import path from "node:path"
  4. import { fileURLToPath } from "node:url"
  5. import { promisify } from "node:util"
  6. import type { Configuration } from "electron-builder"
  7. import { Arch } from "electron-builder"
  8. import { runtimeRelease, validateRuntimeBundle } from "./scripts/runtime-bundle"
  9. const execFileAsync = promisify(execFile)
  10. const packageDir = path.dirname(fileURLToPath(import.meta.url))
  11. const rootDir = path.resolve(packageDir, "../..")
  12. const signScript = path.join(rootDir, "script", "sign-windows.ps1")
  13. const metainfoFpm = (appId: string) =>
  14. `${path.join(packageDir, "resources", `${appId}.metainfo.xml`)}=/usr/share/metainfo/${appId}.metainfo.xml`
  15. async function signWindows(configuration: { path: string }) {
  16. if (process.platform !== "win32") return
  17. if (process.env.LAMBDA_AGENT_ALLOW_UNSIGNED === "1") return
  18. if (process.env.GITHUB_ACTIONS !== "true")
  19. throw new Error("Windows 正式包需要配置签名流水线;本机试用请显式使用 package:beta --unsigned。")
  20. await execFileAsync(
  21. "pwsh",
  22. ["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", signScript, configuration.path],
  23. { cwd: rootDir },
  24. )
  25. }
  26. const channel = (() => {
  27. const raw = process.env.OPENCODE_CHANNEL
  28. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  29. return "dev"
  30. })()
  31. const APP_IDS = {
  32. dev: "org.lambda-scientist.desktop.dev",
  33. beta: "org.lambda-scientist.desktop.beta",
  34. prod: "org.lambda-scientist.desktop",
  35. } as const
  36. const getBase = (appId: string): Configuration => ({
  37. forceCodeSigning:
  38. channel !== "dev" && process.env.LAMBDA_AGENT_ALLOW_UNSIGNED !== "1" && process.platform !== "linux",
  39. beforePack: async (context) => {
  40. if (channel === "dev" || process.env.LAMBDA_AGENT_EXTERNAL_SERVICES === "1") return
  41. if (context.electronPlatformName !== process.platform || Arch[context.arch] !== process.arch)
  42. throw new Error("完整版请在目标系统与架构构建并验收,不能将当前本地运行组件打入其他系统或架构安装包。")
  43. await validateRuntimeBundle(path.join(packageDir, "resources/agentpaas-runtime"), await runtimeRelease())
  44. },
  45. artifactName: "lambda-agent-${version}-${os}-${arch}.${ext}",
  46. directories: {
  47. output: "dist",
  48. buildResources: "resources",
  49. },
  50. // Linux launchers are .desktop files, so this is the desktop file name,
  51. // not just the app id. For prod, app id "org.lambda-scientist.desktop" becomes
  52. // "org.lambda-scientist.desktop.desktop".
  53. // https://developer.gnome.org/documentation/guidelines/maintainer/integrating.html
  54. // https://www.electron.build/docs/linux/
  55. extraMetadata: {
  56. desktopName: `${appId}.desktop`,
  57. },
  58. files: ["out/**/*", "resources/**/*", "!resources/opencode-cli*", "!resources/agentpaas-runtime/**/*"],
  59. extraResources: [
  60. ...(process.env.LAMBDA_AGENT_EXTERNAL_SERVICES !== "1" &&
  61. existsSync(path.join(packageDir, "resources/agentpaas-runtime/desktop-runtime.json"))
  62. ? [{ from: "resources/agentpaas-runtime", to: "agentpaas-runtime" }]
  63. : []),
  64. ...(channel === "dev"
  65. ? [
  66. {
  67. from: "resources/",
  68. to: "",
  69. filter: ["opencode-cli*"],
  70. },
  71. ]
  72. : []),
  73. ],
  74. mac: {
  75. category: "public.app-category.developer-tools",
  76. icon: `resources/icons/icon.icns`,
  77. hardenedRuntime: true,
  78. gatekeeperAssess: false,
  79. entitlements: "resources/entitlements.plist",
  80. entitlementsInherit: "resources/entitlements.plist",
  81. notarize: true,
  82. target: ["dmg", "zip"],
  83. },
  84. dmg: {
  85. sign: true,
  86. },
  87. protocols: {
  88. name: "λ-Agent",
  89. schemes: ["lambda-scientist"],
  90. },
  91. win: {
  92. ...(channel === "beta" && process.env.LAMBDA_AGENT_WINDOWS_RUNTIME
  93. ? {
  94. extraResources: ["git", "ripgrep"].map((tool) => ({
  95. from: path.join(process.env.LAMBDA_AGENT_WINDOWS_RUNTIME!, tool),
  96. to: `windows-runtime/${tool}`,
  97. })),
  98. }
  99. : {}),
  100. icon: `resources/icons/icon.ico`,
  101. signtoolOptions: {
  102. sign: signWindows,
  103. },
  104. target: ["nsis"],
  105. verifyUpdateCodeSignature: true,
  106. },
  107. nsis: {
  108. oneClick: true,
  109. perMachine: false,
  110. installerIcon: `resources/icons/icon.ico`,
  111. installerHeaderIcon: `resources/icons/icon.ico`,
  112. },
  113. linux: {
  114. icon: `resources/icons`,
  115. category: "Development",
  116. executableName: appId,
  117. desktop: {
  118. entry: {
  119. // Match the installed .desktop file and hicolor icon basename so
  120. // Linux shells can associate the running Electron window with its launcher.
  121. StartupWMClass: appId,
  122. },
  123. },
  124. target: ["AppImage", "deb", "rpm"],
  125. },
  126. })
  127. function updateSource(): Configuration["publish"] {
  128. const url = process.env.LAMBDA_SCIENTIST_UPDATE_URL
  129. if (!url) return null
  130. if (new URL(url).protocol !== "https:") throw new Error("Update URL must use HTTPS")
  131. return { provider: "generic", url, channel: channel === "beta" ? "beta" : "latest" }
  132. }
  133. function getConfig() {
  134. const appId = APP_IDS[channel]
  135. const base = getBase(appId)
  136. switch (channel) {
  137. case "dev": {
  138. return {
  139. ...base,
  140. appId,
  141. productName: "λ-Agent Dev",
  142. deb: { fpm: [metainfoFpm(appId)] },
  143. rpm: { packageName: "lambda-scientist-dev", fpm: [metainfoFpm(appId)] },
  144. }
  145. }
  146. case "beta": {
  147. return {
  148. ...base,
  149. appId,
  150. productName: "λ-Agent Beta",
  151. protocols: { name: "λ-Agent Beta", schemes: ["lambda-scientist"] },
  152. publish: updateSource(),
  153. deb: { fpm: [metainfoFpm(appId)] },
  154. rpm: { packageName: "lambda-scientist-beta", fpm: [metainfoFpm(appId)] },
  155. }
  156. }
  157. case "prod": {
  158. return {
  159. ...base,
  160. appId,
  161. productName: "λ-Agent",
  162. protocols: { name: "λ-Agent", schemes: ["lambda-scientist"] },
  163. publish: updateSource(),
  164. deb: { fpm: [metainfoFpm(appId)] },
  165. rpm: { packageName: "lambda-scientist", fpm: [metainfoFpm(appId)] },
  166. }
  167. }
  168. }
  169. }
  170. export default getConfig()