-
Notifications
You must be signed in to change notification settings - Fork 19
Use GitHub Actions cache in installCached #92
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
export class Cargo { | ||
private readonly path: string; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error handling code is copied from the |
||
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'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,13 @@ export function getInputList( | |
.map((item: string) => item.trim()) | ||
.filter((item: string) => item.length > 0); | ||
} | ||
|
||
export function getInputAsArray( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is used in |
||
name: string, | ||
options?: core.InputOptions, | ||
): string[] { | ||
return getInput(name, options) | ||
.split('\n') | ||
.map((s) => s.trim()) | ||
.filter((x) => x !== ''); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.