From e26b8e93168a7ce138d9d966f92497f234cbbbd3 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 10 Sep 2020 21:40:54 +0200 Subject: [PATCH 1/2] Don't require a key to install cached program With the runner name and the program version a unique key can already be generated --- src/commands/cargo.ts | 90 +++++++++++++++++++++---------------------- src/input.ts | 10 ----- 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/src/commands/cargo.ts b/src/commands/cargo.ts index 9d2f6022..95bc6bab 100644 --- a/src/commands/cargo.ts +++ b/src/commands/cargo.ts @@ -45,19 +45,14 @@ see https://help.github.com/en/articles/software-in-virtual-environments-for-git } /** - * Executes `cargo install ${program}`. - * - * TODO: Caching ability implementation is blocked, - * see https://github.com/actions-rs/core/issues/31 - * As for now it acts just like an stub and simply installs the program - * on each call. + * Executes `cargo install ${program}` or uses the cached ${program} from + * the GitHub Actions cache. * - * `version` argument could be either actual program version or `"latest"` string, - * which can be provided by user input. + * `version` argument could either be the actual program version or + * `"latest"`, which can be provided by user input. * - * If `version` is `undefined` or `"latest"`, this method could call the Crates.io API, - * fetch the latest version and search for it in cache. - * TODO: Actually implement this. + * If `version` is `undefined` or `"latest"`, this method calls the + * Crates.io API, fetches the latest version and search for it in the cache. * * ## Returns * @@ -68,51 +63,52 @@ 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 { + version = version || (await resolveVersion(program)); 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); - } + const runner = ''; // TODO + const paths = [path.join(path.dirname(this.path), program)]; + const programKey = program + '-' + version + '-' + runner; + const cacheKey = await cache.restoreCache(paths, programKey); + 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); } - return res; } - } else { - return await this.install(program, version); + return res; } } - async install(program: string, version?: string): Promise { + /** + * Executes `cargo install ${program}`. + * + * `version` argument could either be the actual program version or + * `"latest"`, which can be provided by user input. + * + * If `version` is `undefined` or `"latest"`, this method calls the + * Crates.io API, fetches the latest version and search for it in the cache. + * + * ## Returns + * + * Path to the installed program. + * As the $PATH should be already tuned properly at this point, + * returned value at the moment is simply equal to the `program` argument. + */ + public async install(program: string, version?: string): Promise { const args = ['install']; if (version && version != 'latest') { args.push('--version'); diff --git a/src/input.ts b/src/input.ts index bd913eb9..9634b839 100644 --- a/src/input.ts +++ b/src/input.ts @@ -41,13 +41,3 @@ export function getInputList( .map((item: string) => item.trim()) .filter((item: string) => item.length > 0); } - -export function getInputAsArray( - name: string, - options?: core.InputOptions, -): string[] { - return getInput(name, options) - .split('\n') - .map((s) => s.trim()) - .filter((x) => x !== ''); -} From be732302d14a9029fa0f6e28afc64133621f508c Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 10 Sep 2020 22:57:20 +0200 Subject: [PATCH 2/2] Get runner name from os.platform/release/arch --- src/commands/cargo.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/cargo.ts b/src/commands/cargo.ts index 95bc6bab..18b39e5b 100644 --- a/src/commands/cargo.ts +++ b/src/commands/cargo.ts @@ -1,3 +1,4 @@ +import * as os from 'os'; import * as io from '@actions/io'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; @@ -68,7 +69,7 @@ see https://help.github.com/en/articles/software-in-virtual-environments-for-git if (version == 'latest') { version = await resolveVersion(program); } - const runner = ''; // TODO + const runner = os.platform() + '-' + os.release() + '-' + os.arch(); const paths = [path.join(path.dirname(this.path), program)]; const programKey = program + '-' + version + '-' + runner; const cacheKey = await cache.restoreCache(paths, programKey);