-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
95 lines (80 loc) · 2.15 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { execa } from "execa";
import fse from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Config
const appName = "SunsetTime";
const binaryName = "sunset-time";
const outDir = path.join(__dirname, "dist");
const targets = [
"windows/amd64",
"windows/arm64",
"darwin/amd64",
"darwin/arm64",
];
// https://stackoverflow.com/a/70483079
// -s Omit the symbol table and debug information.
// -w Omit the DWARF symbol table
const ldFlags = `-s -w`;
function getFlags(os) {
if (os === "windows") {
// hide terminal window for windows
return `${ldFlags} -H=windowsgui`;
}
return ldFlags;
}
function makeMacApp(dir) {
/**
* Structure:
*
* <name>.app
* Contents
* Info.plist
* MacOS
* <binaryName>
* Resouces
* icon.icns
*/
const appDir = path.join(dir, `${appName}.app`, "Contents");
fse.ensureDirSync(appDir);
fse.copyFileSync(path.join(__dirname, "Info.plist"), `${appDir}/Info.plist`);
const resourcesDir = path.join(appDir, "Resources");
fse.ensureDirSync(resourcesDir);
fse.copyFileSync(
path.join(__dirname, "assets", "icon.icns"),
path.join(resourcesDir, "icon.icns")
);
const binaryDir = path.join(appDir, "MacOS");
fse.ensureDirSync(binaryDir);
fse.moveSync(path.join(dir, binaryName), path.join(binaryDir, binaryName));
}
// Prepare directory
if (fse.existsSync(outDir)) {
fse.removeSync(outDir);
}
fse.ensureDirSync(outDir);
targets.forEach(async (target) => {
const [os, arch] = target.split("/");
const dir = `${outDir}/${appName}-${os}-${arch}`;
fse.ensureDirSync(dir);
const suffix = os === "windows" ? ".exe" : "";
const dist = `${dir}/${binaryName}${suffix}`;
const args = ["build", "-ldflags", getFlags(os), "-o", dist, "."];
const { stderr } = await execa("go", args, {
env: {
CGO_ENABLED: 1,
GOOS: os,
GOARCH: arch,
},
});
if (stderr) {
console.error(`${target} ❌`);
console.error(stderr);
process.exit(1);
}
if (os === "darwin") {
makeMacApp(dir);
}
console.log(`${target} ✔️`);
});