forked from Sloaix/Gofi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
117 lines (94 loc) · 3.7 KB
/
gulpfile.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { series } = require("gulp");
const gulp = require("gulp");
const path = require("path");
const shell = require("shelljs");
const modePreview = "preview";
const modeProduction = "production";
let currentMode = modeProduction;
const frontendFolder = path.join(__dirname, "gofi-frontend");
const backendFolder = path.join(__dirname, "gofi-backend");
const frontendDistFolder = path.join(frontendFolder, "dist");
const backendDistFolder = path.join(backendFolder, "env", "dist");
const backendOutputFolder = path.join(backendFolder, "output");
const outputFolder = path.join(__dirname, "output");
const goPath = shell.exec("go env GOPATH", { silent: true }).stdout.trim();
const goBinDir = `${goPath}/bin`;
const lastCommitId = shell
.exec("git rev-parse --short=8 HEAD", { silent: true })
.stdout.trim();
const lastCommitMessage = shell
.exec("git log -1 --pretty=%B", { silent: true })
.stdout.trim();
// 只有 chore: release v1.0.2 这样的格式才是合法的release commit
const isReleaseCommit =
/^chore: release v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/g.test(
lastCommitMessage
);
const version = isReleaseCommit
? lastCommitMessage.replace("chore: release", "").trim()
: lastCommitId;
function printCurrentBuildInfo() {
console.log(`====> Current Build Mode: ${currentMode}`);
console.log(`====> Build Version: ${version}`);
console.log(`====> frontendFolder ${frontendFolder}`);
console.log(`====> backendFolder ${backendFolder}`);
console.log(`====> frontendDistFolder ${frontendDistFolder}`);
console.log(`====> backendDistFolder ${backendDistFolder}`);
console.log(`====> backendOutputFolder ${backendOutputFolder}`);
console.log(`====> outputFolder ${outputFolder}`);
console.log(`====> goPath ${goPath}`);
console.log(`====> goBinDir ${goBinDir}`);
}
gulp.task("build-frontend", (done) => {
shell.cd(frontendFolder);
shell.exec(`yarn && yarn build --mode ${currentMode}`);
done();
});
gulp.task("build-backend", (done) => {
shell.rm("-rf", backendDistFolder);
shell.mv(frontendDistFolder, backendDistFolder);
shell.rm("-rf", backendOutputFolder);
shell.mkdir("-p", backendOutputFolder);
shell.cd(backendFolder);
// get all dependencies
shell.exec("go get -v -t -d");
// use flag [-linkmode "external" -extldflags "-static"] to compile by static link, see https://johng.cn/cgo-enabled-affect-go-static-compile/
shell.exec(
`go build -tags=${currentMode} -ldflags='-w -s -X gofi/db.version=${version} -linkmode "external" -extldflags "-static"' -o gofi-linux-amd64 && cp gofi-linux-amd64 ./output/`
);
shell.exec(
`env CGO_ENABLED=0 GOOS=android GOARCH=arm64 go build -tags=${currentMode} -ldflags='-w -s -X gofi/db.version=${version} ' -o gofi-android-arm64 && cp gofi-android-arm64 ./output/`
);
done();
});
gulp.task("after-build", (done) => {
shell.rm("-rf", outputFolder);
shell.mv(backendOutputFolder, outputFolder);
done();
});
gulp.task("build", series("build-frontend", "build-backend", "after-build"));
gulp.task("preview-mode", (done) => {
currentMode = modePreview;
done();
});
gulp.task("production-mode", (done) => {
currentMode = modeProduction;
done();
});
gulp.task("print-info", (done) => {
printCurrentBuildInfo();
done();
});
gulp.task("build-preview", series("preview-mode", "print-info", "build"));
gulp.task("build-production", series("production-mode", "print-info", "build"));
function clean(done) {
shell.rm("-rf", outputFolder, backendOutputFolder, frontendDistFolder);
done();
}
exports.default = series("build-production");
exports.preview = series("build-preview");
exports.clean = clean;
exports.printinfo = (done) => {
printCurrentBuildInfo();
done();
};