Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different architectures #104

Merged
merged 10 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ It’s much easier to work on this project if your editor can run these tools fo
There’s a script that can help you do it.

```
npx ts-node scripts/HashUrls.ts LINUX_URL MAC_URL WINDOWS_URL
npx ts-node scripts/HashUrls.ts URL...
```

Example:
Expand All @@ -31,7 +31,7 @@ It’s much easier to work on this project if your editor can run these tools fo
npx ts-node scripts/HashUrls.ts https://github.com/mpizenberg/elm-test-rs/releases/download/v1.0/elm-test-rs_{linux.tar.gz,macos.tar.gz,windows.zip}
```

The above example assumes your shell has brace expansion. Either way, it expects 3 URLs: One for linux, one for mac and one for windows (in that order). It downloads the files to memory (verifying that they exist) and hashes them. When done, it prints some JSON that you can paste into the code. Run Prettier afterwards.
The above example assumes your shell has brace expansion. Either way, it expects several URLs: One for each platform you support. It downloads the files to memory (verifying that they exist) and hashes them. When done, it prints some JSON that you can paste into the code. Run Prettier afterwards.

2. Run `npx jest -u` to update snapshots and look them through quickly to see if they seem legit.

Expand Down
63 changes: 39 additions & 24 deletions scripts/HashUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import * as crypto from "crypto";

import { downloadFile } from "../src/commands/Install";
import { fromEntries } from "../src/Helpers";
import type { Asset, AssetType, OSName } from "../src/KnownTools";

const OS_LIST: Array<OSName> = ["linux", "mac", "windows"];
import type { Asset, AssetType } from "../src/KnownTools";

async function run(urls: Array<string>): Promise<string> {
const progress: Array<number> = urls.map(() => 0);

const assets: Array<[OSName, Asset]> = await Promise.all(
urls.map((url, index): Promise<[OSName, Asset]> => {
const assets: Array<[string, Asset]> = await Promise.all(
urls.map((url, index): Promise<[string, Asset]> => {
const hash = crypto.createHash("sha256");
let fileSize = 0;
return new Promise((resolve, reject) => {
Expand All @@ -32,35 +30,61 @@ async function run(urls: Array<string>): Promise<string> {
},
onSuccess: () => {
progress[index] = 1;
const osName = OS_LIST[index] ?? ("UNKNOWN" as OSName);
const platform = guessPlatform(url);
const asset: Asset = {
hash: hash.digest("hex"),
url,
fileSize,
fileName: guessFileName(url, osName),
fileName: guessFileName(url, platform),
type: guessAssetType(url),
};
resolve([osName, asset]);
resolve([platform, asset]);
},
});
});
})
);

process.stderr.write("\r100%");
return JSON.stringify(fromEntries(assets), null, 2);
return JSON.stringify(
fromEntries(assets.sort(([a], [b]) => a.localeCompare(b))),
null,
2
);
}

function guessFileName(url: string, osName: OSName): string {
function guessPlatform(url: string): string {
return `${guessPlatformName(url)}-${guessPlatformArch(url)}`;
}

function guessPlatformName(passedUrl: string): string {
const url = passedUrl.toLowerCase();
return url.includes("mac") || url.includes("darwin")
? "darwin"
: url.includes("linux")
? "linux"
: url.includes("win")
? "win32"
: "UNKNOWN";
}

function guessPlatformArch(passedUrl: string): string {
const url = passedUrl.toLowerCase();
return url.includes("arm") || url.includes("aarch")
? url.includes("32")
? "arm"
: "arm64"
: "x64";
}

function guessFileName(url: string, platform: string): string {
const match = /github\.com\/[^/]+\/([^/]+)/.exec(url);
const name = match === null || match[1] === undefined ? "UNKNOWN" : match[1];
return osName === "windows" ? `${name}.exe` : name;
return platform.startsWith("win32-") ? `${name}.exe` : name;
}

function guessAssetType(url: string): AssetType {
return url.endsWith(".tgz")
? "tgz"
: url.endsWith(".tar.gz")
return url.endsWith(".tgz") || url.endsWith(".tar.gz")
? "tgz"
: url.endsWith(".gz")
? "gz"
Expand All @@ -70,16 +94,7 @@ function guessAssetType(url: string): AssetType {
}

if (require.main === module) {
const urls = process.argv.slice(2);
if (urls.length !== 3) {
process.stderr.write(
`\nExpected 3 urls (${OS_LIST.join(", ")}, in that order) but got ${
urls.length
}\n`
);
process.exit(1);
}
run(urls)
run(process.argv.slice(2))
.then((json) => {
process.stdout.write(`\n${json}\n`);
process.exit(0);
Expand Down
31 changes: 0 additions & 31 deletions src/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,6 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

export type Either<Left, Right> =
| { tag: "Left"; value: Left }
| { tag: "Right"; value: Right };

export function partitionMap<T, Left, Right>(
items: Array<T>,
f: (
item: T,
index: number,
leftSoFar: Array<Left>,
rightSoFar: Array<Right>
) => Either<Left, Right>
): [Array<Left>, Array<Right>] {
const left: Array<Left> = [];
const right: Array<Right> = [];

for (const [index, item] of items.entries()) {
const either = f(item, index, left, right);
switch (either.tag) {
case "Left":
left.push(either.value);
break;
case "Right":
right.push(either.value);
break;
}
}

return [left, right];
}

export const HIDE_CURSOR = "\x1B[?25l";
export const SHOW_CURSOR = "\x1B[?25h";
export const RESET_COLOR = "\x1B[0m";
Expand Down
Loading