Skip to content

Commit ad0fe57

Browse files
committed
Generalize filename suffixes to regexes
1 parent 094c154 commit ad0fe57

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ async function subprocess(command, options) {
113113

114114
async function installCrystalForLinux({crystal, shards, arch = getArch(), path}) {
115115
checkVersion(crystal, [Latest, Nightly, NumericVersion]);
116-
const suffixes = {"x86_64": "linux-x86_64", "x86": "linux-i686"};
117-
checkArch(arch, Object.keys(suffixes));
116+
const filePatterns = {"x86_64": /-linux-x86_64\.tar\.gz$/, "x86": /-linux-i686\.tar\.gz$/};
117+
checkArch(arch, Object.keys(filePatterns));
118118

119119
const depsTask = installAptPackages(
120120
"libevent-dev libgmp-dev libpcre3-dev libssl-dev libxml2-dev libyaml-dev".split(" "),
121121
);
122-
await installBinaryRelease({crystal, shards, suffix: suffixes[arch], path});
122+
await installBinaryRelease({crystal, shards, filePattern: filePatterns[arch], path});
123123

124124
Core.info("Setting up environment for Crystal");
125125
Core.addPath(Path.join(path, "bin"));
@@ -136,11 +136,11 @@ async function installCrystalForMac({crystal, shards, arch = "x86_64", path}) {
136136
checkVersion(crystal, [Latest, Nightly, NumericVersion]);
137137
if (crystal === Latest || crystal === Nightly || cmpTags(crystal, "1.2") >= 0) {
138138
checkArch(arch, ["universal", "x86_64", "aarch64"]);
139-
await installBinaryRelease({crystal, shards, suffix: "darwin-universal", path});
140139
} else {
141140
checkArch(arch, ["x86_64"]);
142-
await installBinaryRelease({crystal, shards, suffix: "darwin-x86_64", path});
143141
}
142+
const filePattern = /-darwin-(universal|x86_64)\.tar\.gz$/;
143+
await installBinaryRelease({crystal, shards, filePattern, path});
144144

145145
Core.info("Setting up environment for Crystal");
146146
Core.addPath(Path.join(path, "embedded", "bin"));
@@ -176,14 +176,14 @@ async function installAptPackages(packages) {
176176
Core.endGroup();
177177
}
178178

179-
async function installBinaryRelease({crystal, suffix, path}) {
179+
async function installBinaryRelease({crystal, filePattern, path}) {
180180
if (crystal === Nightly) {
181-
await IO.mv(await downloadCrystalNightly(suffix), path);
181+
await IO.mv(await downloadCrystalNightly(filePattern), path);
182182
} else {
183183
if (crystal === Latest) {
184184
crystal = null;
185185
}
186-
await IO.mv(await downloadCrystalRelease(suffix, crystal), path);
186+
await IO.mv(await downloadCrystalRelease(filePattern, crystal), path);
187187
}
188188
}
189189

@@ -318,11 +318,11 @@ async function findLatestCommit({name, repo, branch = "master"}) {
318318
return commit["sha"];
319319
}
320320

321-
async function downloadCrystalRelease(suffix, version = null) {
321+
async function downloadCrystalRelease(filePattern, version = null) {
322322
const release = await findRelease({name: "Crystal", repo: RepoCrystal, tag: version});
323323
Core.setOutput("crystal", release["tag_name"]);
324324

325-
const asset = release["assets"].find((a) => a["name"].endsWith([`-${suffix}.tar.gz`]));
325+
const asset = release["assets"].find((a) => filePattern.test(a["name"]));
326326

327327
Core.info(`Downloading Crystal build from ${asset["url"]}`);
328328
const resp = await github.request({
@@ -364,7 +364,7 @@ async function downloadSource({name, repo, ref}) {
364364
return onlySubdir(await ToolCache.extractZip(downloadedPath));
365365
}
366366

367-
async function downloadCrystalNightly(suffix) {
367+
async function downloadCrystalNightly(filePattern) {
368368
Core.info("Looking for latest Crystal build");
369369

370370
let build;
@@ -387,7 +387,7 @@ async function downloadCrystalNightly(suffix) {
387387
const req = `/${build["build_num"]}/artifacts`;
388388
const resp = await fetch(CircleApiBase + req);
389389
const artifacts = await resp.json();
390-
const artifact = artifacts.find((a) => a["path"].endsWith(`-${suffix}.tar.gz`));
390+
const artifact = artifacts.find((a) => filePattern.test(a["path"]));
391391

392392
Core.info(`Downloading Crystal build from ${artifact["url"]}`);
393393
const downloadedPath = await ToolCache.downloadTool(artifact["url"]);

0 commit comments

Comments
 (0)