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

Don't require a key to install cached program #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 44 additions & 47 deletions src/commands/cargo.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -45,19 +46,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
*
Expand All @@ -68,51 +64,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<string> {
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 = 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);
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<string> {
/**
* 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<string> {
const args = ['install'];
if (version && version != 'latest') {
args.push('--version');
Expand Down
10 changes: 0 additions & 10 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '');
}