Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 7f2a3dd

Browse files
committed
MVP
1 parent acd39b1 commit 7f2a3dd

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ jobs:
1616
- run: npm run lint
1717
- run: npm run build
1818
- run: npm run test
19+
20+
install:
21+
runs-on: ubuntu-latest
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
crate:
26+
- cross # Available in the tool cache
27+
- ripgrep # Not available in the tool cache
28+
steps:
29+
- uses: actions/checkout@v2
30+
- uses: ./
31+
with:
32+
crate: ${{ matrix.crate }}

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"homepage": "https://github.com/actions-rs/install",
3030
"dependencies": {
3131
"@actions-rs/core": "0.0.9",
32-
"@actions/core": "^1.2.3"
32+
"@actions/core": "^1.2.3",
33+
"@actions/http-client": "^1.0.6",
34+
"@actions/tool-cache": "^1.3.3"
3335
},
3436
"devDependencies": {
3537
"@types/jest": "^25.1.4",

src/download.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os from "os";
2+
import fs from "fs";
3+
import path from "path";
4+
5+
import * as core from "@actions/core";
6+
import * as tc from "@actions/tool-cache";
7+
import * as http from "@actions/http-client";
8+
9+
function getRunner(): string {
10+
const platform = os.platform() as string;
11+
switch (platform) {
12+
case "windows":
13+
return "windows-2019";
14+
case "darwin":
15+
return "macos-10.15";
16+
case "linux":
17+
// TODO: Handle `ubuntu-16.04`
18+
return "ubuntu-18.04";
19+
default:
20+
throw new Error("Unsupported OS");
21+
}
22+
}
23+
24+
function getExt(): string {
25+
const platform = os.platform() as string;
26+
switch (platform) {
27+
case "windows":
28+
return ".exe";
29+
default:
30+
return "";
31+
}
32+
}
33+
34+
async function resolveVersion(crate: string): Promise<string> {
35+
// TODO: Better user-agent
36+
const url = `https://crates.io/api/v1/crates/${crate}`;
37+
const client = new http.HttpClient("actions-rs/install");
38+
39+
const resp: any = await client.getJson(url);
40+
if (resp.result == null) {
41+
throw new Error("Unable to fetch latest crate version");
42+
}
43+
44+
return resp.result["crate"]["newest_version"];
45+
}
46+
47+
function buildUrl(crate: string, version: string): string {
48+
const s3Region = "us-east-2";
49+
const s3Bucket = "actions-rs.install.binary-cache";
50+
const runner = getRunner();
51+
const ext = getExt();
52+
53+
return `https://s3.${s3Region}.amazonaws.com/${s3Bucket}/${crate}/${runner}/${crate}-${version}${ext}`;
54+
}
55+
56+
function targetPath(crate: string): string {
57+
const ext = getExt();
58+
const filename = `${crate}${ext}`;
59+
return path.join(os.homedir(), ".cargo", "bin", filename);
60+
}
61+
62+
export async function downloadFromCache(
63+
crate: string,
64+
version: string
65+
): Promise<void> {
66+
if (version == "latest") {
67+
version = await resolveVersion(crate);
68+
}
69+
const url = buildUrl(crate, version);
70+
const path = targetPath(crate);
71+
72+
if (fs.existsSync(path)) {
73+
core.warning(`Crate ${crate} already exist at ${path}`);
74+
return;
75+
}
76+
77+
await tc.downloadTool(url, path);
78+
}

src/main.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import * as core from "@actions/core";
2+
import { Cargo } from "@actions-rs/core";
23

3-
export async function run(): Promise<void> {
4-
core.info("All done");
4+
import * as input from "./input";
5+
import * as download from "./download";
6+
7+
export async function run(crate: string, version: string): Promise<void> {
8+
try {
9+
await download.downloadFromCache(crate, version);
10+
} catch (error) {
11+
core.warning(
12+
`Unable to download ${crate} == ${version} from the tool cache: ${error}`
13+
);
14+
core.info("Falling back to the `cargo install` command");
15+
const cargo = await Cargo.get();
16+
await cargo.installCached(crate, version);
17+
}
518
}
619

720
async function main(): Promise<void> {
821
try {
9-
await run();
22+
const actionInput = input.get();
23+
24+
await run(actionInput.crate, actionInput.version);
1025
} catch (error) {
1126
core.setFailed(error.message);
1227
}

0 commit comments

Comments
 (0)