Skip to content

Commit

Permalink
Better handling of platforms when downloading
Browse files Browse the repository at this point in the history
Check whether platform is supported before moving on

Add some tests to ensure it's the case. Also that we support
all said platform (file can actually be downloaded). This test suite
is slow, but I think it's worth it

Note: in future releases, windows support will need to be reworked.
in some instances, the `exe` is gone and replaced with a zip that'll
likely need to unpack
  • Loading branch information
daddykotex committed Aug 9, 2022
1 parent 78f484f commit 7a1d861
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/coursier/coursier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export function getCoursierExecutable(extensionPath: string): Promise<string> {
if (paths.length > 0) {
return paths[0];
} else {
return downloadCoursierIfRequired(extensionPath, "v2.0.6");
return downloadCoursierIfRequired(
extensionPath,
process.platform,
"v2.0.13"
);
}
});
}
42 changes: 25 additions & 17 deletions src/coursier/download-coursier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { access, mkdir } from "fs/promises";

export function downloadCoursierIfRequired(
extensionPath: string,
platform: string,
versionPath: string
): Promise<string> {
function binPath(filename: string) {
Expand All @@ -20,25 +21,32 @@ export function downloadCoursierIfRequired(
});
}

const urls = {
darwin: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-apple-darwin`,
linux: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-linux`,
win32: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-win32.exe`,
};
const targets = {
darwin: binPath("coursier"),
linux: binPath("coursier"),
win32: binPath("coursier.exe"),
const supportedTargets = {
darwin: {
url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-apple-darwin`,
bin: binPath("coursier"),
},
linux: {
url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-linux`,
bin: binPath("coursier"),
},
win32: {
url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-win32.exe`,
bin: binPath("coursier.exe"),
},
};

const targetFile = targets[process.platform];
return validBinFileExists(targetFile).then((valid) => {
return valid
? targetFile
: createDir().then(() =>
downloadFile(urls[process.platform], targetFile)
);
});
const target = supportedTargets[platform];
if (target === undefined) {
return Promise.reject(`Unsupported platform ${platform}.`);
} else {
const targetFile = target.bin;
return validBinFileExists(targetFile).then((valid) => {
return valid
? targetFile
: createDir().then(() => downloadFile(target.url, targetFile));
});
}
}

function validBinFileExists(file: string): Promise<boolean> {
Expand Down
26 changes: 26 additions & 0 deletions tests/coursier/download-coursier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { downloadCoursierIfRequired } from "../../src/coursier/download-coursier";
import { tmpdir } from "os";
import { existsSync, mkdirSync, accessSync, constants, rmSync } from "fs";
import { join } from "path";

["darwin", "linux", "win32"].forEach((p) => {
test(
`download on platform: ${p}`,
() => {
const dir = join(tmpdir(), p);
rmSync(dir, { recursive: true, force: true });
mkdirSync(dir, { recursive: true });
return downloadCoursierIfRequired(dir, p, "v2.0.13").then((x) => {
expect(existsSync(x)).toBeTruthy();
accessSync(x, constants.X_OK);
});
},
25 * 1000
);
});

test(`fails on unknown platform`, () => {
return expect(
downloadCoursierIfRequired(tmpdir(), "unsupported", "v2.0.13")
).rejects.toEqual("Unsupported platform unsupported.");
});

0 comments on commit 7a1d861

Please sign in to comment.