Skip to content

Commit

Permalink
feat: Added param 'with-cache'
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Jun 2, 2022
1 parent c4effdc commit f4656ac
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 78 deletions.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
62 changes: 38 additions & 24 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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');
}
});
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
54 changes: 34 additions & 20 deletions dist/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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');
}
});
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
51 changes: 31 additions & 20 deletions src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ok } from 'assert';
import * as path from 'path';

export class Cache {
private _withCache: boolean;
private _key: string = '';
private _dir;

Expand All @@ -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<boolean> {
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<void> {
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');
}
}
}
6 changes: 3 additions & 3 deletions src/Installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -27,4 +27,4 @@ export class Installer {
core.endGroup();
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lazarus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ async function run(): Promise<void> {
// `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) {
Expand Down
15 changes: 9 additions & 6 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { Cache } from './Cache';
import { _version } from './constants';

async function run(): Promise<void> {
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();
Expand Down

0 comments on commit f4656ac

Please sign in to comment.