-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
160 lines (143 loc) · 5.68 KB
/
build.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { exec } = require("node:child_process");
const { join } = require("node:path");
const fs = require("node:fs");
const Args = process.argv
.slice(process.argv.indexOf("--") + 1)
.map((item) => item.replace("--", ""))
.reduce(
(args, item) =>
Object.assign(args, {
[item]: true,
}),
{}
);
const Command = (cmd, options = {}) =>
new Promise(
(
resolve,
reject,
ps = exec(
process.platform == "win32"
? "$ProgressPreference = 'SilentlyContinue';" + cmd
: cmd,
{
shell: process.platform == "win32" ? "powershell.exe" : "bash",
cwd: __dirname,
...options,
}
)
) => {
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);
ps.on("error", reject);
ps.on("close", (code) => {
code == 0 ? resolve() : reject(code || 0);
});
}
);
const Replace = (file, filters) => {
let src = fs.readFileSync(file).toString();
for (const item of filters) {
src = src.replaceAll(...item);
}
fs.writeFileSync(file, src);
};
/* async block */
void (async () => {
const Profile = Args.release ? "Release" : "Debug";
for (const path of ["./target", "./build", "./build/bin", "./build/lib", "./build/include"]) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}
await Command(`cargo build ${Args.release ? "--release" : ""} -p hylarana-shared`);
await Command(`cargo build ${Args.release ? "--release" : ""} -p hylarana-example`);
await Command(`cargo build ${Args.release ? "--release" : ""} -p hylarana-server`);
/* download ffmpeg librarys for windows */
if (process.platform == "win32" || process.platform == "linux") {
const name = `ffmpeg-n7.1-latest-${
process.platform == "win32" ? "win64" : "linux64"
}-gpl-shared-7.1`;
const baseUri = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest";
if (!fs.existsSync("./target/ffmpeg")) {
if (process.platform == "win32") {
await Command(`Invoke-WebRequest -Uri ${baseUri}/${name}.zip -OutFile ffmpeg.zip`, {
cwd: "./target",
});
} else {
await Command(`wget ${baseUri}/${name}.tar.xz -O ffmpeg.tar.xz -q`, {
cwd: "./target",
});
}
if (process.platform == "win32") {
await Command("Expand-Archive -Path ffmpeg.zip -DestinationPath ./", {
cwd: "./target",
});
} else {
await Command("tar -xf ffmpeg.tar.xz", { cwd: "./target" });
}
fs.renameSync(`./target/${name}`, "./target/ffmpeg");
fs.rmSync(`./target/ffmpeg.${process.platform == "win32" ? "zip" : "tar.xz"}`);
}
}
for (const item of [
["./README.md", "./build/README.md"],
["./LICENSE", "./build/LICENSE"],
/* inculde */
["./ffi/include/hylarana.h", "./build/include/hylarana.h"],
]) {
fs.cpSync(...item, { force: true, recursive: true });
}
if (process.platform == "win32") {
for (const item of [
[`./target/${Profile.toLowerCase()}/hylarana-example.exe`, "./build/bin/example.exe"],
[
`./target/${Profile.toLowerCase()}/hylarana-server.exe`,
"./build/bin/hylarana-server.exe",
],
[`./target/${Profile.toLowerCase()}/hylarana.dll.lib`, "./build/lib/hylarana.dll.lib"],
[`./target/${Profile.toLowerCase()}/hylarana.dll`, "./build/bin/hylarana.dll"],
[`./target/ffmpeg/bin/avcodec-61.dll`, "./build/bin/avcodec-61.dll"],
[`./target/ffmpeg/bin/avutil-59.dll`, "./build/bin/avutil-59.dll"],
[`./target/ffmpeg/bin/swresample-5.dll`, "./build/bin/swresample-5.dll"],
]) {
fs.cpSync(...item, { force: true, recursive: true });
}
} else if (process.platform == "darwin") {
for (const item of [
[`./target/${Profile.toLowerCase()}/hylarana-example`, "./build/bin/example"],
[`./target/${Profile.toLowerCase()}/hylarana-server`, "./build/bin/hylarana-server"],
[
`./target/${Profile.toLowerCase()}/libhylarana.dylib`,
"./build/bin/libhylarana.dylib",
],
]) {
fs.cpSync(...item, { force: true, recursive: true });
}
} else if (process.platform == "linux") {
for (const item of [
[`./target/${Profile.toLowerCase()}/hylarana-example`, "./build/bin/example"],
[`./target/${Profile.toLowerCase()}/hylarana-server`, "./build/bin/hylarana-server"],
[`./target/${Profile.toLowerCase()}/libhylarana.so`, "./build/bin/libhylarana.so"],
[`./target/ffmpeg/lib`, "./build/lib"],
]) {
fs.cpSync(...item, { force: true, recursive: true });
}
}
if (process.platform == "win32") {
for (const item of [
["./target/debug/hylarana.pdb", "./build/bin/hylarana.pdb"],
["./target/debug/hylarana_server.pdb", "./build/bin/hylarana-server.pdb"],
]) {
if (!Args.release) {
fs.cpSync(...item, { force: true, recursive: true });
} else {
fs.rmSync(item[1], { force: true, recursive: true });
}
}
}
/* async block end */
})().catch((e) => {
console.error(e);
process.exit(-1);
});