-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelpers.ts
More file actions
262 lines (198 loc) · 6.28 KB
/
Copy pathhelpers.ts
File metadata and controls
262 lines (198 loc) · 6.28 KB
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
import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import execa from 'execa';
import yaml from 'yaml';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as glob from '@actions/glob';
import * as tc from '@actions/tool-cache';
export const WINDOWS = process.platform === 'win32';
export function getProtoHome() {
if (process.env.PROTO_HOME) {
return process.env.PROTO_HOME;
}
if (process.env.PROTO_ROOT) {
return process.env.PROTO_ROOT;
}
return path.join(os.homedir(), '.proto');
}
export function getBinDir() {
return path.join(getProtoHome(), 'bin');
}
export function getPluginsDir() {
return path.join(getProtoHome(), 'plugins');
}
export function getShimsDir() {
return path.join(getProtoHome(), 'shims');
}
export function getToolsDir() {
return path.join(getProtoHome(), 'tools');
}
export function getUidFile() {
return path.join(getProtoHome(), 'id');
}
export function getWorkingDir() {
return process.env.GITHUB_WORKSPACE ?? process.cwd();
}
export function getWorkspaceRoot() {
return path.join(getWorkingDir(), core.getInput('workspace-root'));
}
export function isCacheEnabled() {
return core.getBooleanInput('cache') && cache.isFeatureAvailable();
}
export function isUsingMoon() {
return (
!!core.getInput('moon-version') ||
fs.existsSync(path.join(getWorkspaceRoot(), '.moon')) ||
fs.existsSync(path.join(getWorkspaceRoot(), '.config', 'moon'))
);
}
export function shouldInstallMoon() {
// Not installing with proto, so need to install with the action
if (!core.getBooleanInput('auto-install')) {
return true;
}
const prototools = path.join(getWorkspaceRoot(), '.prototools');
if (fs.existsSync(prototools)) {
const lines = fs.readFileSync(prototools, 'utf8').split('\n');
for (const line of lines) {
// If we encountered a table, then we are out of the versions mapping
if (line.startsWith('[')) {
break;
}
// If we find a `moon = "1.2.3"` version string, then we shouldn't install
// moon with the action, and instead install through proto
if (line.match(/^moon(\s*)=(\s*)('|")/)) {
return false;
}
}
}
return true;
}
export function extractMajorMinor(version: string) {
const [major, minor] = version.split('.');
return `${major}.${minor}`;
}
export function getCacheKeyPrefix() {
// v2 - Before proto v0.24 changes
// v3 - proto v0.51 lockfile changes
return 'moonrepo-toolchain-v3';
}
export async function getToolchainCacheKey() {
const hasher = crypto.createHash('sha1');
const files = ['.prototools', '.protolock'];
if (isUsingMoon()) {
const root = core.getInput('workspace-root');
files.push(
// v1
'.moon/toolchain.yml',
// v2
'.moon/toolchains.yml',
'.config/moon/toolchains.yml',
);
if (root) {
const moreFiles: string[] = [];
for (const file of files) {
moreFiles.push(path.join(root, file));
}
files.push(...moreFiles);
}
}
core.debug(`Hashing files: ${files.join(', ')}`);
hasher.update(await glob.hashFiles(files.join('\n')));
const protoVersion = process.env.PROTO_CLI_VERSION ?? core.getState('PROTO_CLI_VERSION');
if (protoVersion) {
core.debug(`Hashing proto version: ${protoVersion}`);
hasher.update(extractMajorMinor(protoVersion));
}
const moonVersion = process.env.MOON_CLI_VERSION ?? core.getState('MOON_CLI_VERSION');
if (moonVersion) {
core.debug(`Hashing moon version: ${moonVersion}`);
hasher.update(extractMajorMinor(moonVersion));
}
const configVersion = core.getInput('cache-version');
if (configVersion) {
core.debug(`Hashing configured version: ${configVersion}`);
hasher.update(configVersion);
}
return `${getCacheKeyPrefix()}-${process.platform}-${process.arch}-${hasher.digest('hex')}`;
}
export function getProtoVersion(): string {
const version = core.getInput('proto-version');
if (version) {
return version;
}
if (isUsingMoon()) {
for (const file of [
'.moon/toolchain.yml',
'.moon/toolchains.yml',
'.config/moon/toolchains.yml',
]) {
const toolchainPath = path.join(getWorkspaceRoot(), file);
if (fs.existsSync(toolchainPath)) {
const toolchain = yaml.parse(fs.readFileSync(toolchainPath, 'utf8')) as {
proto?: { version?: string };
};
const protoVersion = toolchain?.proto?.version;
// Only fully-qualified is allowed
if (
protoVersion &&
typeof protoVersion === 'string' &&
protoVersion.split('.').length >= 3
) {
return protoVersion;
}
}
}
}
return 'latest';
}
export function getMoonVersion(): string {
return core.getInput('moon-version') || 'latest';
}
export async function installBin(bin: string) {
core.info(`Installing \`${bin}\` globally`);
const version =
// eslint-disable-next-line no-nested-ternary
bin === 'proto' ? getProtoVersion() : bin === 'moon' ? getMoonVersion() : 'latest';
const scriptName = WINDOWS ? `${bin}.ps1` : `${bin}.sh`;
const scriptPath = path.join(getProtoHome(), 'temp', scriptName);
// If the installer already exists, delete it and ensure were using the latest
if (fs.existsSync(scriptPath)) {
fs.unlinkSync(scriptPath);
}
core.info('Downloading installation script');
const script = await tc.downloadTool(
`https://raw.githubusercontent.com/moonrepo/moon/refs/heads/master/website/static/install/${scriptName}`,
scriptPath,
);
// eslint-disable-next-line no-magic-numbers
await fs.promises.chmod(script, 0o755);
core.info(`Downloaded script to ${script}`);
core.info('Executing installation script');
const binDir = getBinDir();
const binPath = path.join(binDir, WINDOWS ? `${bin}.exe` : bin);
const envPrefix = bin.toUpperCase();
await execa(script, version === 'latest' ? [] : [version], {
env: {
[`${envPrefix}_INSTALL_DIR`]: binDir,
},
stdio: core.isDebug() || !!process.env[`${envPrefix}_DEBUG`] ? 'inherit' : 'pipe',
});
core.info(`Installed binary to ${binPath}`);
core.info('Checking version');
try {
const result = await execa(binPath, ['--version'], { stdio: 'pipe' });
if (result.stdout) {
const v = result.stdout.replace(bin, '').trim();
// eslint-disable-next-line require-atomic-updates
process.env[`${envPrefix}_CLI_VERSION`] = v;
core.saveState(`${envPrefix}_CLI_VERSION`, v);
core.info(result.stdout);
}
} catch (error) {
core.error(String(error));
}
}