forked from samuelmeuli/action-snapcraft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
88 lines (78 loc) · 2.34 KB
/
index.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
const { execSync } = require("child_process");
/**
* Logs to the console
*/
const log = (msg) => console.log(`\n${msg}`); // eslint-disable-line no-console
/**
* Executes the provided shell command and redirects stdout/stderr to the console
*/
const run = (cmd, printStdio = true) =>
execSync(cmd, { encoding: "utf8", stdio: printStdio ? "inherit" : "pipe" });
/**
* Determines the current operating system (one of ["mac", "windows", "linux"])
*/
const getPlatform = () => {
switch (process.platform) {
case "darwin":
return "mac";
case "win32":
return "windows";
default:
return "linux";
}
};
/**
* Installs Snapcraft on Linux
*/
const runLinuxInstaller = () => {
// TODO: Remove setting up lxd once the `use-lxd` input is removed
// as lxd is available on the latest ubuntu by default
// tag: deprecate_use_lxd
const useLxd = process.env.INPUT_USE_LXD === "true";
const lxdNotInstalled = process.env.ImageOS === "ubuntu18";
const setupLxd = useLxd && lxdNotInstalled;
run(`sudo snap install snapcraft --classic --channel ${process.env.INPUT_CHANNEL}`);
if (setupLxd) {
run("sudo snap install lxd");
run(`sudo usermod --append --groups lxd ${process.env.USER}`);
}
run(`echo /snap/bin >> ${process.env.GITHUB_PATH}`); // Add `/snap/bin` to PATH for subsequent actions
run("sudo chown root:root /"); // Fix root ownership
if (setupLxd) {
run("sudo /snap/bin/lxd.migrate -yes");
run("sudo /snap/bin/lxd waitready");
run("sudo /snap/bin/lxd init --auto");
}
};
/**
* Installs Snapcraft on macOS
*/
const runMacInstaller = () => {
run("brew install snapcraft");
};
/**
* Installs Snapcraft and logs the user in
*/
const runAction = () => {
const platform = getPlatform();
// Install Snapcraft
if (platform === "windows") {
log("Snapcraft is not yet available for Windows. Skipping");
process.exit(0);
} else if (process.env.INPUT_SKIP_INSTALL === "true") {
log("Skipping install");
} else if (platform === "linux") {
log("Installing Snapcraft for Linux…");
runLinuxInstaller();
} else if (platform === "mac") {
log("Installing Snapcraft for macOS…");
runMacInstaller();
} else {
log("Unknown platform");
process.exit(1);
}
const snapcraftPath =
platform === "linux" ? "/snap/bin/snapcraft" : run("which snapcraft", false).trim();
log(`Snapcraft is installed at ${snapcraftPath}`);
};
runAction();