From 360a49c14318086dcb926e63d79134b1f61d5776 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 25 Jun 2020 13:33:25 +0200 Subject: [PATCH 1/3] Add actions/cache dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a3c09838..fc5f90af 100644 --- a/package.json +++ b/package.json @@ -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", From 33eec396c2d552ef5ce6ab6fda77801cfd1fdf7d Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 25 Jun 2020 13:34:03 +0200 Subject: [PATCH 2/3] Use GitHub Actions cache in installCached --- src/commands/cargo.ts | 61 +++++++++++++++++++++++++++++++++++++++++++ src/input.ts | 10 +++++++ 2 files changed, 71 insertions(+) diff --git a/src/commands/cargo.ts b/src/commands/cargo.ts index 9842a758..9d2f6022 100644 --- a/src/commands/cargo.ts +++ b/src/commands/cargo.ts @@ -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 { + 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']; +} 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 { + 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); + } + } + return res; + } + } else { + return await this.install(program, version); + } + } + + 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 9634b839..bd913eb9 100644 --- a/src/input.ts +++ b/src/input.ts @@ -41,3 +41,13 @@ 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 43bfe530b3501cf39b9b273e4f2a5a51548d45ae Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 25 Jun 2020 16:33:34 +0200 Subject: [PATCH 3/3] Update package-lock.json --- package-lock.json | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index c18c5756..b363ae07 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,27 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@actions/cache": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-0.2.1.tgz", + "integrity": "sha512-CV2D9zp+d+nL7o6XK/I7vVh350JglPMp/jHi9ppRUkdBHPUeP0UHVUfygZaAs8WmxhhWY1MI0gWah+t0QYu3Fg==", + "requires": { + "@actions/core": "^1.2.4", + "@actions/exec": "^1.0.1", + "@actions/glob": "^0.1.0", + "@actions/http-client": "^1.0.8", + "@actions/io": "^1.0.1", + "semver": "^6.1.0", + "uuid": "^3.3.3" + }, + "dependencies": { + "semver": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" + } + } + }, "@actions/core": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz", @@ -70,6 +91,15 @@ } } }, + "@actions/glob": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@actions/glob/-/glob-0.1.0.tgz", + "integrity": "sha512-lx8SzyQ2FE9+UUvjqY1f28QbTJv+w8qP7kHHbfQRhphrlcx0Mdmm1tZdGJzfxv1jxREa/sLW4Oy8CbGQKCJySA==", + "requires": { + "@actions/core": "^1.2.0", + "minimatch": "^3.0.4" + } + }, "@actions/http-client": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.8.tgz", @@ -1771,8 +1801,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "base": { "version": "0.11.2", @@ -1863,7 +1892,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2229,8 +2257,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "configstore": { "version": "5.0.1", @@ -5625,7 +5652,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" }