build-windows-browser-import-helper.mjs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. import { execFileSync } from "node:child_process";
  3. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  4. import { arch, platform } from "node:os";
  5. import { dirname, resolve } from "node:path";
  6. import { fileURLToPath } from "node:url";
  7. import { runCommand } from "../../../scripts/spawn-command.mjs";
  8. const scriptDir = dirname(fileURLToPath(import.meta.url));
  9. const desktopRoot = resolve(scriptDir, "..");
  10. const workspaceRoot = resolve(desktopRoot, "../..");
  11. const sourceRoot = resolve(desktopRoot, "native/windows-browser-import-helper");
  12. const targetArch = (process.env.ZCODE_TARGET_ARCH ?? arch()).toLowerCase();
  13. const targetKey = `win32-${targetArch}`;
  14. const outputDir = resolve(desktopRoot, `bundled-tools/${targetKey}/browser-import`);
  15. const outputPath = resolve(outputDir, "zcode-browser-import-helper.exe");
  16. const generatedAssemblyInfoPath = resolve(outputDir, "BrowserImportAssemblyInfo.g.cs");
  17. const appVersion = JSON.parse(readFileSync(resolve(workspaceRoot, "package.json"), "utf8")).version;
  18. const buildCommit = (
  19. process.env.ZCODE_COMMIT ??
  20. execFileSync("git", ["rev-parse", "--short=8", "HEAD"], {
  21. cwd: workspaceRoot,
  22. encoding: "utf8",
  23. })
  24. ).trim();
  25. const cscCandidates = [
  26. process.env.ZCODE_CSC_PATH,
  27. "C:/Windows/Microsoft.NET/Framework64/v4.0.30319/csc.exe",
  28. "C:/Windows/Microsoft.NET/Framework/v4.0.30319/csc.exe",
  29. ].filter(Boolean);
  30. const cscPath = cscCandidates.find((candidate) => existsSync(candidate));
  31. if (platform() !== "win32") {
  32. console.log("[browser-import-helper] skip: Windows helper is built only on Windows");
  33. process.exit(0);
  34. }
  35. if (targetArch !== "x64" && targetArch !== "arm64") {
  36. throw new Error(`Unsupported Windows browser import helper architecture: ${targetArch}`);
  37. }
  38. if (targetArch === "arm64" && !process.env.ZCODE_CSC_PATH) {
  39. throw new Error(
  40. "Windows arm64 browser import helper requires ZCODE_CSC_PATH pointing to an arm64-capable Roslyn csc.exe",
  41. );
  42. }
  43. if (!cscPath) {
  44. throw new Error("Windows C# compiler not found; set ZCODE_CSC_PATH to a trusted csc.exe");
  45. }
  46. mkdirSync(outputDir, { recursive: true });
  47. rmSync(outputPath, { force: true });
  48. const numericVersion = appVersion.split("-", 1)[0].split(".");
  49. while (numericVersion.length < 4) numericVersion.push("0");
  50. if (
  51. !/^\d+(?:\.\d+){2}(?:-[0-9A-Za-z.-]+)?$/.test(appVersion) ||
  52. !/^[0-9A-Za-z.-]+$/.test(buildCommit) ||
  53. numericVersion.some((part) => !/^\d+$/.test(part))
  54. ) {
  55. throw new Error("Invalid Windows browser import helper build identity");
  56. }
  57. writeFileSync(
  58. generatedAssemblyInfoPath,
  59. [
  60. "using System.Reflection;",
  61. `[assembly: AssemblyVersion("${numericVersion.slice(0, 4).join(".")}")]`,
  62. `[assembly: AssemblyFileVersion("${numericVersion.slice(0, 4).join(".")}")]`,
  63. `[assembly: AssemblyInformationalVersion("${appVersion}|${buildCommit}")]`,
  64. "",
  65. ].join("\n"),
  66. "utf8",
  67. );
  68. try {
  69. runCommand(cscPath, [
  70. "/nologo",
  71. "/optimize+",
  72. "/target:exe",
  73. `/platform:${targetArch}`,
  74. `/win32manifest:${resolve(sourceRoot, "app.manifest")}`,
  75. "/reference:System.Core.dll",
  76. "/reference:System.Numerics.dll",
  77. `/out:${outputPath}`,
  78. resolve(sourceRoot, "Program.cs"),
  79. generatedAssemblyInfoPath,
  80. ]);
  81. } finally {
  82. rmSync(generatedAssemblyInfoPath, { force: true });
  83. }
  84. console.log(`[browser-import-helper] built ${outputPath}`);