generated from actions/typescript-action
/
main.ts
363 lines (319 loc) · 10.5 KB
/
main.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import * as crypto from "crypto";
import * as os from "os";
import * as path from "path";
import * as process from "process";
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import * as glob from "glob";
import { compare, CompareOperator } from "compare-versions";
const nativePath = process.platform === "win32" ? path.win32.normalize : path.normalize;
const compareVersions = (v1: string, op: CompareOperator, v2: string): boolean => {
return compare(v1, v2, op);
};
const setOrAppendEnvVar = (name: string, value: string): void => {
const oldValue = process.env[name];
let newValue = value;
if (oldValue) {
newValue = `${oldValue}:${newValue}`;
}
core.exportVariable(name, newValue);
};
const execPython = async (command: string, args: readonly string[]): Promise<number> => {
const python = process.platform === "win32" ? "python" : "python3";
return exec(`${python} -m ${command} ${args.join(" ")}`);
};
class Inputs {
readonly host: "windows" | "mac" | "linux";
readonly target: "desktop" | "android" | "ios";
readonly version: string;
readonly arch: string;
readonly dir: string;
readonly modules: string[];
readonly archives: string[];
readonly tools: string[];
readonly extra: string[];
readonly installDeps: boolean | "nosudo";
readonly cache: boolean;
readonly cacheKeyPrefix: string;
readonly toolsOnly: boolean;
readonly setEnv: boolean;
readonly aqtVersion: string;
readonly py7zrVersion: string;
constructor() {
const host = core.getInput("host");
// Set host automatically if omitted
if (!host) {
switch (process.platform) {
case "win32": {
this.host = "windows";
break;
}
case "darwin": {
this.host = "mac";
break;
}
default: {
this.host = "linux";
break;
}
}
} else {
// Make sure host is one of the allowed values
if (host === "windows" || host === "mac" || host === "linux") {
this.host = host;
} else {
throw TypeError(`host: "${host}" is not one of "windows" | "mac" | "linux"`);
}
}
const target = core.getInput("target");
// Make sure target is one of the allowed values
if (target === "desktop" || target === "android" || target === "ios") {
this.target = target;
} else {
throw TypeError(`target: "${target}" is not one of "desktop" | "android" | "ios"`);
}
this.version = core.getInput("version");
this.arch = core.getInput("arch");
// Set arch automatically if omitted
if (!this.arch) {
if (this.host === "windows") {
if (compareVersions(this.version, ">=", "5.15.0")) {
this.arch = "win64_msvc2019_64";
} else if (compareVersions(this.version, "<", "5.6.0")) {
this.arch = "win64_msvc2013_64";
} else if (compareVersions(this.version, "<", "5.9.0")) {
this.arch = "win64_msvc2015_64";
} else {
this.arch = "win64_msvc2017_64";
}
} else if (this.target === "android") {
if (compareVersions(this.version, ">=", "5.14.0")) {
this.arch = "android";
} else {
this.arch = "android_armv7";
}
}
}
const dir = core.getInput("dir") || process.env.RUNNER_WORKSPACE;
if (!dir) {
throw TypeError(`"dir" input may not be empty`);
}
this.dir = `${dir}/Qt`;
const modules = core.getInput("modules");
if (modules) {
this.modules = modules.split(" ");
} else {
this.modules = [];
}
const archives = core.getInput("archives");
if (archives) {
this.archives = archives.split(" ");
} else {
this.archives = [];
}
const tools = core.getInput("tools");
if (tools) {
this.tools = [];
for (const tool of tools.split(" ")) {
// The tools inputs have the tool name, variant, and arch delimited by a comma
// aqt expects spaces instead
this.tools.push(tool.replace(/,/g, " "));
}
} else {
this.tools = [];
}
const extra = core.getInput("extra");
if (extra) {
this.extra = extra.split(" ");
} else {
this.extra = [];
}
const installDeps = core.getInput("install-deps").toLowerCase();
if (installDeps === "nosudo") {
this.installDeps = "nosudo";
} else {
this.installDeps = installDeps === "true";
}
this.cache = Inputs.getBoolInput("cache");
this.cacheKeyPrefix = core.getInput("cache-key-prefix");
this.toolsOnly = Inputs.getBoolInput("tools-only");
this.setEnv = Inputs.getBoolInput("set-env");
this.aqtVersion = core.getInput("aqtversion");
this.py7zrVersion = core.getInput("py7zrversion");
}
public get versionDir(): string {
// Weird naming scheme exception for qt 5.9
const version = this.version === "5.9.0" ? "5.9" : this.version;
return nativePath(`${this.dir}/${version}`);
}
public get cacheKey(): string {
let cacheKey = this.cacheKeyPrefix;
for (const keyStringArray of [
[
this.host,
os.release(),
this.target,
this.arch,
this.version,
this.dir,
this.py7zrVersion,
this.aqtVersion,
],
this.modules,
this.archives,
this.extra,
this.tools,
]) {
for (const keyString of keyStringArray) {
if (keyString) {
cacheKey += `-${keyString}`;
}
}
}
// Cache keys cannot contain commas
cacheKey = cacheKey.replace(/,/g, "-");
// Cache keys cannot be larger than 512 characters
const maxKeyLength = 512;
if (cacheKey.length > maxKeyLength) {
const hashedCacheKey = crypto.createHash("sha256").update(cacheKey).digest("hex");
cacheKey = `${this.cacheKeyPrefix}-${hashedCacheKey}`;
}
return cacheKey;
}
private static getBoolInput(name: string): boolean {
return core.getInput(name).toLowerCase() === "true";
}
}
const run = async (): Promise<void> => {
try {
const inputs = new Inputs();
// Qt installer assumes basic requirements that are not installed by
// default on Ubuntu.
if (process.platform === "linux") {
if (inputs.installDeps) {
const dependencies = [
"build-essential",
"libgl1-mesa-dev",
"libpulse-dev",
"libxcb-glx0",
"libxcb-icccm4",
"libxcb-image0",
"libxcb-keysyms1",
"libxcb-randr0",
"libxcb-render-util0",
"libxcb-render0",
"libxcb-shape0",
"libxcb-shm0",
"libxcb-sync1",
"libxcb-util1",
"libxcb-xfixes0",
"libxcb-xinerama0",
"libxcb1",
"libxkbcommon-x11-0",
].join(" ");
const updateCommand = "apt-get update";
const installCommand = `apt-get install ${dependencies} -y`;
if (inputs.installDeps === "nosudo") {
await exec(updateCommand);
await exec(installCommand);
} else {
await exec(`sudo ${updateCommand}`);
await exec(`sudo ${installCommand}`);
}
}
}
// Restore internal cache
let internalCacheHit = false;
if (inputs.cache) {
const cacheHitKey = await cache.restoreCache([inputs.dir], inputs.cacheKey);
if (cacheHitKey) {
core.info(`Automatic cache hit with key "${cacheHitKey}"`);
internalCacheHit = true;
} else {
core.info("Automatic cache miss, will cache this run");
}
}
// Install Qt and tools if not cached
if (!internalCacheHit) {
// 7-zip is required, and not included on macOS
if (process.platform === "darwin") {
await exec("brew install p7zip");
}
// Install aqtinstall
await execPython("pip install", [
"setuptools",
"wheel",
`"py7zr${inputs.py7zrVersion}"`,
`"aqtinstall${inputs.aqtVersion}"`,
]);
// Install Qt
if (!inputs.toolsOnly) {
const qtArgs = [inputs.host, inputs.target, inputs.version];
if (inputs.arch) {
qtArgs.push(inputs.arch);
}
qtArgs.push("--outputdir", inputs.dir);
if (inputs.modules.length) {
qtArgs.push("--modules");
for (const module of inputs.modules) {
qtArgs.push(module);
}
}
if (inputs.archives.length) {
qtArgs.push("--archives");
for (const archive of inputs.archives) {
qtArgs.push(archive);
}
}
qtArgs.push(...inputs.extra);
await execPython("aqt install-qt", qtArgs);
}
// Install tools
for (const tool of inputs.tools) {
const toolArgs = [inputs.host, inputs.target, tool];
toolArgs.push("--outputdir", inputs.dir);
toolArgs.push(...inputs.extra);
await execPython("aqt install-tool", toolArgs);
}
}
// Save automatic cache
if (!internalCacheHit && inputs.cache) {
const cacheId = await cache.saveCache([inputs.dir], inputs.cacheKey);
core.info(`Automatic cache saved with id ${cacheId}`);
}
// Set environment variables
const qtPath = nativePath(glob.sync(`${inputs.versionDir}/**/*`)[0]);
if (inputs.setEnv) {
if (inputs.tools.length) {
core.exportVariable("IQTA_TOOLS", nativePath(`${inputs.dir}/Tools`));
}
if (!inputs.toolsOnly) {
if (process.platform === "linux") {
setOrAppendEnvVar("LD_LIBRARY_PATH", nativePath(`${qtPath}/lib`));
}
if (process.platform !== "win32") {
setOrAppendEnvVar("PKG_CONFIG_PATH", nativePath(`${qtPath}/lib/pkgconfig`));
}
// If less than qt6, set qt5_dir variable, otherwise set qt6_dir variable
if (compareVersions(inputs.version, "<", "6.0.0")) {
core.exportVariable("Qt5_Dir", qtPath); // Incorrect name that was fixed, but kept around so it doesn't break anything
core.exportVariable("Qt5_DIR", qtPath);
} else {
core.exportVariable("Qt6_DIR", qtPath);
}
core.exportVariable("QT_PLUGIN_PATH", nativePath(`${qtPath}/plugins`));
core.exportVariable("QML2_IMPORT_PATH", nativePath(`${qtPath}/qml`));
core.addPath(nativePath(`${qtPath}/bin`));
}
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error);
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
core.setFailed(`unknown error: ${error}`);
}
}
};
void run();