From f4656acf0d22f88cd8b505f4d0dcfa36145812b9 Mon Sep 17 00:00:00 2001 From: Gustavo Carreno Date: Thu, 2 Jun 2022 19:38:22 +0100 Subject: [PATCH] feat: Added param 'with-cache' --- action.yml | 6 ++++- dist/main/index.js | 62 ++++++++++++++++++++++++++++------------------ dist/post/index.js | 54 +++++++++++++++++++++++++--------------- package.json | 2 +- src/Cache.ts | 51 +++++++++++++++++++++++--------------- src/Installer.ts | 6 ++--- src/Lazarus.ts | 4 +-- src/main.ts | 5 +++- src/post.ts | 15 ++++++----- 9 files changed, 127 insertions(+), 78 deletions(-) diff --git a/action.yml b/action.yml index 57b2e6c..6b73021 100644 --- a/action.yml +++ b/action.yml @@ -7,13 +7,17 @@ branding: color: 'blue' inputs: - lazarus-version: # id of input + lazarus-version: description: 'Version of Lazarus' required: true default: 'dist' include-packages: description: 'Include packages that the project needs' required: false + with-cache: + description: 'Use cache for installer files' + required: false + default: true runs: using: 'node16' diff --git a/dist/main/index.js b/dist/main/index.js index 6236791..aeb13b4 100644 --- a/dist/main/index.js +++ b/dist/main/index.js @@ -42,10 +42,11 @@ const core = __importStar(__webpack_require__(2186)); const assert_1 = __webpack_require__(2357); const path = __importStar(__webpack_require__(5622)); class Cache { - constructor() { + constructor(WithCache) { this._key = ''; let tempDirectory = process.env['RUNNER_TEMP'] || ''; assert_1.ok(tempDirectory, 'Expected RUNNER_TEMP to be defined'); + this._withCache = WithCache; this._dir = path.join(tempDirectory, 'installers'); } get Key() { @@ -56,34 +57,45 @@ class Cache { } restore() { return __awaiter(this, void 0, void 0, function* () { - core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); - let cacheLoaded = (yield cache.restoreCache([this._dir], this._key)) != null; - if (!cacheLoaded) { - core.exportVariable('SAVE_CACHE_DIR', this._dir); - core.exportVariable('SAVE_CACHE_KEY', this._key); - core.info('Cache.restore -- no hit'); + if (this._withCache === true) { + core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); + let cacheLoaded = (yield cache.restoreCache([this._dir], this._key)) != null; + if (!cacheLoaded) { + core.exportVariable('SAVE_CACHE_DIR', this._dir); + core.exportVariable('SAVE_CACHE_KEY', this._key); + core.info('Cache.restore -- no hit'); + } + else { + core.info('Cache.restore -- hit'); + } + return cacheLoaded; } else { - core.info('Cache.restore -- hit'); + core.info('Cache.restore -- Cache is disabled'); + return false; } - return cacheLoaded; }); } save() { return __awaiter(this, void 0, void 0, function* () { - try { - let key = process.env['SAVE_CACHE_KEY'] || ''; - let dir = process.env['SAVE_CACHE_DIR'] || ''; - if (key != '' && dir != '') { - core.info(`Cache.save -- Key: ${key} dir: ${dir}`); - yield cache.saveCache([dir], key); + if (this._withCache === true) { + try { + let key = process.env['SAVE_CACHE_KEY'] || ''; + let dir = process.env['SAVE_CACHE_DIR'] || ''; + if (key != '' && dir != '') { + core.info(`Cache.save -- Key: ${key} dir: ${dir}`); + yield cache.saveCache([dir], key); + } + else { + core.info('Cache.save -- nothing to save'); + } } - else { - core.info(`Cache.save -- nothing to save`); + catch (error) { + core.info(error.message); } } - catch (error) { - core.info(error.message); + else { + core.info('Cache.save -- Cache is disabled'); } }); } @@ -134,8 +146,8 @@ const pkgs = __importStar(__webpack_require__(925)); const RepoBaseURL = 'https://packages.lazarus-ide.org'; const ParamJSON = 'packagelist.json'; class Installer { - constructor(LazarusVerzion, PackageList) { - this._Lazarus = new laz.Lazarus(LazarusVerzion); + constructor(LazarusVerzion, PackageList, WithCache) { + this._Lazarus = new laz.Lazarus(LazarusVerzion, WithCache); this._IncludePackages = PackageList; this._Packages = new pkgs.Packages(LazarusVerzion, RepoBaseURL, ParamJSON); } @@ -406,12 +418,12 @@ const pkgs = { } }; class Lazarus { - constructor(LazarusVersion) { + constructor(LazarusVersion, WithCache) { this._Platform = os.platform(); this._Arch = os.arch(); this._LazarusVersion = ''; this._LazarusVersion = LazarusVersion; - this._Cache = new Cache_1.Cache(); + this._Cache = new Cache_1.Cache(WithCache); this._Cache.Key = this._LazarusVersion + '-' + this._Arch + '-' + this._Platform; } installLazarus() { @@ -1201,10 +1213,12 @@ function run() { let lazarusVersion = core.getInput('lazarus-version'); // `include-packages` input defined in action metadata file let includePackages = core.getInput('include-packages'); + // `with-cache` input defined in action metadata file + let withCache = core.getInput('with-cache') == 'true'; // Get the JSON webhook payload for the event that triggered the workflow //const payload = JSON.stringify(github.context.payload, undefined, 2) //console.log(`The event payload: ${payload}`); - let Installer = new inst.Installer(lazarusVersion, includePackages.split(',')); + let Installer = new inst.Installer(lazarusVersion, includePackages.split(','), withCache); yield Installer.install(); } catch (error) { diff --git a/dist/post/index.js b/dist/post/index.js index bf8d48c..ad14662 100644 --- a/dist/post/index.js +++ b/dist/post/index.js @@ -42,10 +42,11 @@ const core = __importStar(__webpack_require__(2186)); const assert_1 = __webpack_require__(2357); const path = __importStar(__webpack_require__(5622)); class Cache { - constructor() { + constructor(WithCache) { this._key = ''; let tempDirectory = process.env['RUNNER_TEMP'] || ''; assert_1.ok(tempDirectory, 'Expected RUNNER_TEMP to be defined'); + this._withCache = WithCache; this._dir = path.join(tempDirectory, 'installers'); } get Key() { @@ -56,34 +57,45 @@ class Cache { } restore() { return __awaiter(this, void 0, void 0, function* () { - core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); - let cacheLoaded = (yield cache.restoreCache([this._dir], this._key)) != null; - if (!cacheLoaded) { - core.exportVariable('SAVE_CACHE_DIR', this._dir); - core.exportVariable('SAVE_CACHE_KEY', this._key); - core.info('Cache.restore -- no hit'); + if (this._withCache === true) { + core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); + let cacheLoaded = (yield cache.restoreCache([this._dir], this._key)) != null; + if (!cacheLoaded) { + core.exportVariable('SAVE_CACHE_DIR', this._dir); + core.exportVariable('SAVE_CACHE_KEY', this._key); + core.info('Cache.restore -- no hit'); + } + else { + core.info('Cache.restore -- hit'); + } + return cacheLoaded; } else { - core.info('Cache.restore -- hit'); + core.info('Cache.restore -- Cache is disabled'); + return false; } - return cacheLoaded; }); } save() { return __awaiter(this, void 0, void 0, function* () { - try { - let key = process.env['SAVE_CACHE_KEY'] || ''; - let dir = process.env['SAVE_CACHE_DIR'] || ''; - if (key != '' && dir != '') { - core.info(`Cache.save -- Key: ${key} dir: ${dir}`); - yield cache.saveCache([dir], key); + if (this._withCache === true) { + try { + let key = process.env['SAVE_CACHE_KEY'] || ''; + let dir = process.env['SAVE_CACHE_DIR'] || ''; + if (key != '' && dir != '') { + core.info(`Cache.save -- Key: ${key} dir: ${dir}`); + yield cache.saveCache([dir], key); + } + else { + core.info('Cache.save -- nothing to save'); + } } - else { - core.info(`Cache.save -- nothing to save`); + catch (error) { + core.info(error.message); } } - catch (error) { - core.info(error.message); + else { + core.info('Cache.save -- Cache is disabled'); } }); } @@ -131,8 +143,10 @@ const core = __importStar(__webpack_require__(2186)); const Cache_1 = __webpack_require__(3123); function run() { return __awaiter(this, void 0, void 0, function* () { + // `with-cache` input defined in action metadata file + let withCache = core.getInput('with-cache') == 'true'; try { - let installCache = new Cache_1.Cache(); + let installCache = new Cache_1.Cache(withCache); yield installCache.save(); } catch (error) { diff --git a/package.json b/package.json index bf50d9f..b451a4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "setup-lazarus", - "version": "3.1", + "version": "3.1.1", "description": "Set up your GitHub Actions workflow with a specific version of Lazarus", "main": "lib/main.js", "scripts": { diff --git a/src/Cache.ts b/src/Cache.ts index 2ee0903..795448a 100644 --- a/src/Cache.ts +++ b/src/Cache.ts @@ -4,6 +4,7 @@ import { ok } from 'assert'; import * as path from 'path'; export class Cache { + private _withCache: boolean; private _key: string = ''; private _dir; @@ -15,40 +16,50 @@ export class Cache { this._key = Value; } - constructor () { + constructor (WithCache: boolean) { let tempDirectory = process.env['RUNNER_TEMP'] || ''; ok(tempDirectory, 'Expected RUNNER_TEMP to be defined'); + this._withCache = WithCache; this._dir = path.join(tempDirectory, 'installers'); } async restore(): Promise { - core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); - let cacheLoaded = await cache.restoreCache([this._dir], this._key) != null; - if (!cacheLoaded) { - core.exportVariable('SAVE_CACHE_DIR', this._dir); - core.exportVariable('SAVE_CACHE_KEY', this._key); - core.info('Cache.restore -- no hit'); + if (this._withCache === true) { + core.info(`Cache.restore -- Key: ${this._key} dir: ${this._dir}`); + let cacheLoaded = await cache.restoreCache([this._dir], this._key) != null; + if (!cacheLoaded) { + core.exportVariable('SAVE_CACHE_DIR', this._dir); + core.exportVariable('SAVE_CACHE_KEY', this._key); + core.info('Cache.restore -- no hit'); + } else { + core.info('Cache.restore -- hit'); + } + + return cacheLoaded; } else { - core.info('Cache.restore -- hit'); + core.info('Cache.restore -- Cache is disabled'); + return false; } - - return cacheLoaded; } async save(): Promise { - try { - let key = process.env['SAVE_CACHE_KEY'] || ''; - let dir = process.env['SAVE_CACHE_DIR'] || ''; + if (this._withCache === true) { + try { + let key = process.env['SAVE_CACHE_KEY'] || ''; + let dir = process.env['SAVE_CACHE_DIR'] || ''; - if (key !='' && dir != '') { - core.info(`Cache.save -- Key: ${key} dir: ${dir}`); - await cache.saveCache([dir], key); - } else { - core.info(`Cache.save -- nothing to save`); + if (key !='' && dir != '') { + core.info(`Cache.save -- Key: ${key} dir: ${dir}`); + await cache.saveCache([dir], key); + } else { + core.info('Cache.save -- nothing to save'); + } + } catch (error) { + core.info(error.message); } - } catch (error) { - core.info(error.message); + } else { + core.info('Cache.save -- Cache is disabled'); } } } diff --git a/src/Installer.ts b/src/Installer.ts index 7df8f92..f0bf3f1 100644 --- a/src/Installer.ts +++ b/src/Installer.ts @@ -10,8 +10,8 @@ export class Installer { private _IncludePackages: string[]; private _Packages: pkgs.Packages; - constructor(LazarusVerzion: string, PackageList: string[]) { - this._Lazarus = new laz.Lazarus(LazarusVerzion); + constructor(LazarusVerzion: string, PackageList: string[], WithCache: boolean) { + this._Lazarus = new laz.Lazarus(LazarusVerzion, WithCache); this._IncludePackages = PackageList; this._Packages = new pkgs.Packages(LazarusVerzion, RepoBaseURL, ParamJSON); } @@ -27,4 +27,4 @@ export class Installer { core.endGroup(); } } -} \ No newline at end of file +} diff --git a/src/Lazarus.ts b/src/Lazarus.ts index a627710..9a92697 100644 --- a/src/Lazarus.ts +++ b/src/Lazarus.ts @@ -222,9 +222,9 @@ export class Lazarus{ private _LazarusVersion: string = ''; private _Cache: Cache; - constructor(LazarusVersion: string) { + constructor(LazarusVersion: string, WithCache: boolean) { this._LazarusVersion = LazarusVersion; - this._Cache = new Cache(); + this._Cache = new Cache(WithCache); this._Cache.Key = this._LazarusVersion + '-' + this._Arch + '-' + this._Platform; } diff --git a/src/main.ts b/src/main.ts index 426960c..a2f8ecd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,11 +11,14 @@ async function run(): Promise { // `include-packages` input defined in action metadata file let includePackages = core.getInput('include-packages'); + // `with-cache` input defined in action metadata file + let withCache = core.getInput('with-cache') == 'true'; + // Get the JSON webhook payload for the event that triggered the workflow //const payload = JSON.stringify(github.context.payload, undefined, 2) //console.log(`The event payload: ${payload}`); - let Installer = new inst.Installer(lazarusVersion, includePackages.split(',')); + let Installer = new inst.Installer(lazarusVersion, includePackages.split(','), withCache); await Installer.install(); } catch (error) { diff --git a/src/post.ts b/src/post.ts index 9428a92..d59b5e2 100644 --- a/src/post.ts +++ b/src/post.ts @@ -6,13 +6,16 @@ import { Cache } from './Cache'; import { _version } from './constants'; async function run(): Promise { - try { - let installCache = new Cache(); - await installCache.save(); - } catch (error) { - core.setFailed(error.message); - } + // `with-cache` input defined in action metadata file + let withCache = core.getInput('with-cache') == 'true'; + + try { + let installCache = new Cache(withCache); + await installCache.save(); + } catch (error) { + core.setFailed(error.message); + } } run();