Skip to content

Commit adc0faf

Browse files
committed
refactor: use @actions/tool-cache instead of @actions/cache
Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent 6604e09 commit adc0faf

File tree

2,735 files changed

+1312
-200328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,735 files changed

+1312
-200328
lines changed

dist/const.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.TMP_DIR = exports.shell = exports.path = exports.os = exports.io = exports.https = exports.fs = exports.core = exports.cache = void 0;
4-
exports.cache = require('@actions/cache');
3+
exports.TMP_DIR = exports.tc = exports.shell = exports.path = exports.os = exports.io = exports.https = exports.fs = exports.core = void 0;
54
exports.core = require('@actions/core');
65
exports.fs = require('fs');
76
exports.https = require('https');
87
exports.io = require('@actions/io');
98
exports.os = require('os');
109
exports.path = require('path');
1110
exports.shell = require('@actions/exec');
11+
exports.tc = require('@actions/tool-cache');
1212
exports.TMP_DIR = exports.fs.mkdtempSync(exports.path.join(exports.os.tmpdir(), 'slim-'));

dist/index.js

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const inputOverwrite = const_1.core.getBooleanInput('overwrite', { required: fal
1414
const inputTarget = const_1.core.getInput('target', { required: true });
1515
const inputVersion = const_1.core.getInput('version', { required: false });
1616
let inputTag = const_1.core.getInput('tag', { required: true });
17-
let SLIM_PATH = '';
1817
function get_slim() {
1918
return __awaiter(this, void 0, void 0, function* () {
2019
let DIST = '';
@@ -54,7 +53,6 @@ function get_slim() {
5453
catch (_a) {
5554
throw new Error(`Could not get the Slim version ${VER}.`);
5655
}
57-
URL = `https://github.com/slimtoolkit/slim/releases/download/${VER}`;
5856
// Get kernel name and machine architecture.
5957
KERNEL = const_1.os.platform();
6058
MACHINE = const_1.os.arch();
@@ -89,62 +87,41 @@ function get_slim() {
8987
}
9088
// Derive the filename
9189
FILENAME = `dist_${DIST}.${EXT}`;
92-
// Get the bin from cache
93-
const cachePrefix = `slim-${VER}`;
94-
const cacheKey = `${cachePrefix}-${DIST}`;
95-
const cachePath = const_1.path.join('/tmp', cacheKey);
96-
try {
97-
const_1.core.debug('Restoring cache');
98-
const cacheResult = yield const_1.cache.restoreCache([cachePath], cacheKey, [`${cachePrefix}-`]);
99-
if (typeof cacheResult === 'undefined') {
100-
throw new Error(`Cache miss: ${cacheKey} was not found in the cache.`);
101-
}
102-
const_1.core.debug(`${cacheKey} cache was restored.`);
103-
SLIM_PATH = cachePath;
90+
URL = `https://github.com/slimtoolkit/slim/releases/download/${VER}/${FILENAME}`;
91+
const_1.core.debug(`Checking cache for slim...`);
92+
let slimPath = const_1.tc.find('slim', VER, MACHINE);
93+
if (slimPath) {
94+
const_1.core.notice(`slim ${VER} found in cache`);
10495
}
105-
catch (e) {
106-
const_1.core.error(e);
107-
const file = const_1.fs.createWriteStream(const_1.path.join(const_1.TMP_DIR, FILENAME));
108-
yield new Promise((resolve, reject) => {
109-
const_1.https.get(`${URL}/${FILENAME}`, (response) => {
110-
response.pipe(file);
111-
file.on('finish', () => {
112-
file.close();
113-
resolve(file);
114-
});
115-
}).on('error', (error) => {
116-
const_1.fs.unlinkSync(const_1.path.join(const_1.TMP_DIR, FILENAME));
117-
reject(error);
118-
});
119-
});
120-
const_1.core.debug(`Unpacking ${const_1.path.join(const_1.TMP_DIR, FILENAME)}`);
121-
if (EXT === 'zip') {
122-
const extract = require('extract-zip');
123-
yield extract(const_1.path.join(const_1.TMP_DIR, FILENAME), {
124-
dir: const_1.TMP_DIR
125-
});
96+
else {
97+
slimPath = const_1.path.join(process.env.GITHUB_WORKSPACE, '../', 'slim');
98+
let srcPath;
99+
try {
100+
const_1.core.debug(`Downloading slim ${VER} for ${KERNEL}/${MACHINE}...`);
101+
srcPath = yield const_1.tc.downloadTool(URL);
126102
}
127-
else if (EXT === 'tar.gz') {
128-
const tar = require('tar');
129-
yield tar.x({
130-
file: const_1.path.join(const_1.TMP_DIR, FILENAME),
131-
cwd: const_1.TMP_DIR,
132-
gzip: true
133-
});
103+
catch (error) {
104+
throw new Error(`Could not download slim: ${error.message}`);
134105
}
135-
else {
136-
throw new Error('Unexpected file extension.');
106+
let extractedPath;
107+
try {
108+
const_1.core.debug(`Extracting slim ${VER}...`);
109+
if (EXT === 'zip') {
110+
extractedPath = yield const_1.tc.extractZip(srcPath, slimPath);
111+
}
112+
else { // tar.gz
113+
extractedPath = yield const_1.tc.extractTar(srcPath, slimPath);
114+
}
137115
}
138-
SLIM_PATH = const_1.path.join(const_1.TMP_DIR, `dist_${DIST}`);
139-
const_1.core.debug(`Copying ${SLIM_PATH} -> (${cachePath})`);
140-
yield const_1.io.cp(SLIM_PATH, cachePath, { recursive: true, force: true });
141-
const cacheId = yield const_1.cache.saveCache([cachePath], cacheKey);
142-
const_1.core.debug(`${cacheKey} cache saved (ID: ${cacheId})`);
143-
}
144-
finally {
145-
const_1.core.addPath(SLIM_PATH);
146-
const_1.core.info(`Using slim version ${VER}`);
116+
catch (error) {
117+
throw new Error(`Could not extract slim: ${error.message}`);
118+
}
119+
const_1.core.debug('Caching slim...');
120+
slimPath = yield const_1.tc.cacheDir(extractedPath, 'slim', VER, MACHINE);
147121
}
122+
const_1.core.debug('Adding slim to PATH...');
123+
const_1.core.addPath(slimPath);
124+
const_1.core.info(`slim ${VER} has been installed successfully`);
148125
});
149126
}
150127
function run() {
@@ -157,8 +134,8 @@ function run() {
157134
if (typeof process.env['DSLIM_CONTINUE_AFTER'] === 'undefined')
158135
args.push('--continue-after', '1');
159136
}
160-
yield const_1.shell.exec('slim', args, { cwd: SLIM_PATH });
161-
const data = const_1.fs.readFileSync(const_1.path.join(SLIM_PATH, 'slim.report.json'));
137+
yield const_1.shell.exec('slim', args, { cwd: const_1.TMP_DIR });
138+
const data = const_1.fs.readFileSync(const_1.path.join(const_1.TMP_DIR, 'slim.report.json'));
162139
const report = JSON.parse(data);
163140
const_1.core.setOutput('report', report);
164141
if (report.state == 'error') {

0 commit comments

Comments
 (0)