Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"url": "https://github.com/actions-rs/core/issues"
},
"dependencies": {
"@actions/cache": "^0.2.1",
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4",
"@actions/github": "^2.2.0",
Expand Down
61 changes: 61 additions & 0 deletions src/commands/cargo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import * as io from '@actions/io';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import * as http from '@actions/http-client';
import * as path from 'path';

export async function resolveVersion(crate: string): Promise<string> {
const url = `https://crates.io/api/v1/crates/${crate}`;
const client = new http.HttpClient(
'@actions-rs (https://github.com/actions-rs/)',
);

const resp: any = await client.getJson(url);
if (resp.result == null) {
throw new Error('Unable to fetch latest crate version');
}

return resp.result['crate']['newest_version'];
}
Comment on lines +8 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was moved from actions-rs/install, since it is required in this repo to resolve the crate version, when latest is chosen. Otherwise it would not be possible to update the cache, since the name <crate>-latest-<key> would always be the same


export class Cargo {
private readonly path: string;
Expand Down Expand Up @@ -51,7 +68,51 @@ see https://help.github.com/en/articles/software-in-virtual-environments-for-git
public async installCached(
program: string,
version?: string,
primaryKey?: string,
restoreKeys?: string[],
): Promise<string> {
if (version == 'latest') {
version = await resolveVersion(program);
}
if (primaryKey) {
restoreKeys = restoreKeys || [];
const paths = [path.join(path.dirname(this.path), program)];
const programKey = program + '-' + version + '-' + primaryKey;
const programRestoreKeys = restoreKeys.map(
(key) => program + '-' + version + '-' + key,
);
const cacheKey = await cache.restoreCache(
paths,
programKey,
programRestoreKeys,
);
if (cacheKey) {
core.info(
`Using cached \`${program}\` with version ${version}`,
);
return program;
} else {
const res = await this.install(program, version);
try {
core.info(`Caching \`${program}\` with key ${programKey}`);
await cache.saveCache(paths, programKey);
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error;
} else if (error.name === cache.ReserveCacheError.name) {
core.info(error.message);
} else {
core.info('[warning]' + error.message);
}
}
Comment on lines +96 to +107
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error handling code is copied from the actions/cache repository.

return res;
}
} else {
return await this.install(program, version);
}
}

async install(program: string, version?: string): Promise<string> {
const args = ['install'];
if (version && version != 'latest') {
args.push('--version');
Expand Down
10 changes: 10 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ export function getInputList(
.map((item: string) => item.trim())
.filter((item: string) => item.length > 0);
}

export function getInputAsArray(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used in actions-rs/install and copied from @actions/core, but using the getInput wrapper function of this package.

name: string,
options?: core.InputOptions,
): string[] {
return getInput(name, options)
.split('\n')
.map((s) => s.trim())
.filter((x) => x !== '');
}