-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
97 lines (83 loc) · 2.77 KB
/
index.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
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as util from "util";
const esyPrefix = core.getInput("esy-prefix");
const cacheKey = core.getInput("cache-key");
const sourceCacheKey = core.getInput("source-cache-key");
const manifestKey = core.getInput("manifest");
async function run(name: string, command: string, args: string[]) {
const PATH = process.env.PATH ? process.env.PATH : "";
core.startGroup(name);
await exec(command, args, { env: { ...process.env, PATH } });
core.endGroup();
}
function runEsyCommand(name: string, args: string[]) {
return run(name, "esy", manifestKey ? [`@${manifestKey}`, ...args] : args);
}
async function main() {
try {
const workingDirectory =
core.getInput("working-directory") || process.cwd();
fs.statSync(workingDirectory);
process.chdir(workingDirectory);
const platform = os.platform();
const arch = os.arch();
const installPath = ["~/.esy/source"];
const installKey = `source-${platform}-${arch}-${sourceCacheKey}`;
core.startGroup("Restoring install cache");
const installCacheKey = await cache.restoreCache(
installPath,
installKey,
[]
);
if (installCacheKey) {
console.log("Restored the install cache");
}
core.endGroup();
await runEsyCommand("Run esy install", ["install"]);
if (installCacheKey != installKey) {
await cache.saveCache(installPath, installKey);
}
const ESY_FOLDER = esyPrefix ? esyPrefix : path.join(os.homedir(), ".esy");
const esy3 = fs
.readdirSync(ESY_FOLDER)
.filter((name: string) => name.length > 0 && name[0] === "3")
.sort()
.pop();
const depsPath = [path.join(ESY_FOLDER, esy3!, "i")];
const buildKey = `build-${platform}-${arch}-${cacheKey}`;
const restoreKeys = [`build-${platform}-${arch}-`, `build-`];
core.startGroup("Restoring build cache");
const buildCacheKey = await cache.restoreCache(
depsPath,
buildKey,
restoreKeys
);
if (buildCacheKey) {
console.log("Restored the build cache");
}
core.endGroup();
if (!buildCacheKey) {
await runEsyCommand("Run esy build-dependencies", ["build-dependencies"]);
}
await runEsyCommand("Run esy build", ["build"]);
if (buildCacheKey != buildKey) {
await cache.saveCache(depsPath, buildKey);
}
// TODO: support cleanup + manifest
if (!manifestKey && !buildCacheKey) {
await run("Run esy cleanup", "esy", ["cleanup", "."]);
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
} else {
core.setFailed(util.inspect(error));
}
}
}
main();