-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.js
122 lines (110 loc) · 3.17 KB
/
common.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
const os = require('os');
const path = require('path');
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const VERSIONS_JSON = 'https://ziglang.org/download/index.json';
const MACH_VERSIONS_JSON = 'https://pkg.machengine.org/zig/index.json';
const CACHE_PREFIX = "setup-zig-global-cache-";
let _cached_version = null;
async function getVersion() {
if (_cached_version != null) {
return _cached_version;
}
const raw = core.getInput('version');
if (raw === 'master') {
const resp = await fetch(VERSIONS_JSON);
const versions = await resp.json();
_cached_version = versions['master'].version;
} else if (raw === 'latest') {
const resp = await fetch(VERSIONS_JSON);
const versions = await resp.json();
let latest = null;
let latest_major;
let latest_minor;
let latest_patch;
for (const version in versions) {
if (version === 'master') continue;
const [major_str, minor_str, patch_str] = version.split('.')
const major = Number(major_str);
const minor = Number(minor_str);
const patch = Number(patch_str);
if (latest === null) {
latest = version;
latest_major = major;
latest_minor = minor;
latest_patch = patch;
continue;
}
if (major > latest_major ||
(major == latest_major && minor > latest_minor) ||
(major == latest_major && minor == latest_minor && patch > latest_patch))
{
latest = version;
latest_major = major;
latest_minor = minor;
latest_patch = patch;
}
}
_cached_version = latest;
} else if (raw.includes("mach")) {
const resp = await fetch(MACH_VERSIONS_JSON);
const versions = await resp.json();
if (!(raw in versions)) {
throw new Error(`Mach nominated version '${raw}' not found`);
}
_cached_version = versions[raw].version;
} else {
_cached_version = raw;
}
return _cached_version;
}
async function getTarballName() {
const version = await getVersion();
const arch = {
arm: 'armv7a',
arm64: 'aarch64',
ppc64: 'powerpc64',
riscv64: 'riscv64',
x64: 'x86_64',
}[os.arch()];
return {
linux: `zig-linux-${arch}-${version}`,
darwin: `zig-macos-${arch}-${version}`,
win32: `zig-windows-${arch}-${version}`,
}[os.platform()];
}
async function getTarballExt() {
return {
linux: '.tar.xz',
darwin: '.tar.xz',
win32: '.zip',
}[os.platform()];
}
async function getCachePrefix() {
const tarball_name = await getTarballName();
const job_name = github.context.job.replaceAll(/[^\w]/g, "_");
return `setup-zig-cache-${job_name}-${tarball_name}-`;
}
async function getZigCachePath() {
let env_output = '';
await exec.exec('zig', ['env'], {
listeners: {
stdout: (data) => {
env_output += data.toString();
},
},
});
return JSON.parse(env_output)['global_cache_dir'];
}
async function getTarballCachePath() {
return path.join(process.env['RUNNER_TEMP'], await getTarballName());
}
module.exports = {
getVersion,
getTarballName,
getTarballExt,
getCachePrefix,
getZigCachePath,
getTarballCachePath,
};