Skip to content

Commit

Permalink
Add local cache and actions/cache support
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <[email protected]>

Signed-off-by: Dmitry Volodin <[email protected]>
  • Loading branch information
dmvolod committed May 9, 2023
1 parent 02a85cd commit efeacec
Show file tree
Hide file tree
Showing 8 changed files with 1,413 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ jspm_packages/
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

5 changes: 5 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ describe('installer', () => {
expect(fs.existsSync(mage)).toBe(true);
}, 100000);

it('acquires v1.8.0 version of Mage from local cache', async () => {
const mage = await installer.getMage('v1.8.0');
expect(fs.existsSync(mage)).toBe(true);
}, 100000);

it('acquires latest version of Mage', async () => {
const mage = await installer.getMage('latest');
expect(fs.existsSync(mage)).toBe(true);
Expand Down
16 changes: 15 additions & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

991 changes: 966 additions & 25 deletions dist/licenses.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"license": "MIT",
"packageManager": "[email protected]",
"dependencies": {
"@actions/cache": "^3.2.1",
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/http-client": "^2.1.0",
Expand Down
41 changes: 37 additions & 4 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as util from 'util';
import * as core from '@actions/core';
import * as httpm from '@actions/http-client';
import * as tc from '@actions/tool-cache';
import * as cache from '@actions/cache';

const osPlat: string = os.platform();
const osArch: string = os.arch();
Expand Down Expand Up @@ -37,6 +38,22 @@ export async function getMage(version: string): Promise<string> {
core.info(`Mage version found: ${release.tag_name}`);

const filename: string = getFilename(semver);
const magePath = tc.find('mage-action', semver);
if (magePath) {
core.info(`Mage binary found in local cache @ ${magePath}`);
return getExePath(magePath);
}

if (cache.isFeatureAvailable()) {
core.debug(`GitHub actions cache feature available`);
const cacheKey = await cache.restoreCache([getExePath(mageLocalPath())], getCacheKey(semver));
if (cacheKey) {
core.info(`Restored ${cacheKey} from GitHub actions cache`);
const cachePath: string = await tc.cacheDir(mageLocalPath(), 'mage-action', semver);
return getExePath(cachePath);
}
}

const downloadUrl: string = util.format(
'https://github.com/magefile/mage/releases/download/%s/%s',
release.tag_name,
Expand All @@ -50,20 +67,36 @@ export async function getMage(version: string): Promise<string> {
core.info('Extracting Mage...');
let extPath: string;
if (osPlat == 'win32') {
extPath = await tc.extractZip(downloadPath);
extPath = await tc.extractZip(downloadPath, mageLocalPath());
} else {
extPath = await tc.extractTar(downloadPath);
extPath = await tc.extractTar(downloadPath, mageLocalPath());
}
core.debug(`Extracted to ${extPath}`);

const cachePath: string = await tc.cacheDir(extPath, 'mage-action', semver);
core.debug(`Cached to ${cachePath}`);
if (cache.isFeatureAvailable()) {
core.debug(`Caching to GitHub actions cache`);
await cache.saveCache([getExePath(mageLocalPath())], getCacheKey(semver));
}

return getExePath(cachePath);
}

const exePath: string = path.join(cachePath, osPlat == 'win32' ? 'mage.exe' : 'mage');
const getCacheKey = (semver: string): string => {
return util.format('mage-action-cache-%s', semver);
};

const mageLocalPath = (): string => {
return path.join(`${process.env.HOME}`, '.mage');
};

const getExePath = (basePath: string): string => {
const exePath: string = path.join(basePath, osPlat == 'win32' ? 'mage.exe' : 'mage');
core.debug(`Exe path is ${exePath}`);

return exePath;
}
};

const getFilename = (semver: string): string => {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'macOS' : 'Linux';
Expand Down
Loading

0 comments on commit efeacec

Please sign in to comment.