Use native tools

This commit is contained in:
CrazyMax 2020-05-06 22:14:50 +02:00
parent 232b78f307
commit 6395a3aa2f
6 changed files with 75 additions and 875 deletions

9
__tests__/github.test.ts Normal file
View File

@ -0,0 +1,9 @@
import * as github from '../src/github';
describe('github', () => {
it('returns 3.96 GitHub release', async () => {
const release = await github.getRelease('3.96');
expect(release).not.toBeNull();
expect(release.tag_name).toEqual('v3.96');
});
});

837
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,14 +22,12 @@
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4",
"@actions/tool-cache": "^1.3.4",
"download": "^8.0.0",
"typed-rest-client": "^1.7.3"
"@actions/http-client": "^1.0.8",
"@actions/tool-cache": "^1.3.4"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/download": "^6.2.4",
"@zeit/ncc": "^0.22.1",
"jest": "^25.5.0",
"jest-circus": "^25.5.0",

12
src/github.ts Normal file
View File

@ -0,0 +1,12 @@
import * as httpm from '@actions/http-client';
export interface GitHubRelease {
id: number;
tag_name: string;
}
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/upx/upx/releases/${version !== 'latest' ? `v${version}` : version}`;
const http: httpm.HttpClient = new httpm.HttpClient('ghaction-upx');
return (await http.getJson<GitHubRelease>(url)).result;
};

View File

@ -1,9 +1,7 @@
import * as download from 'download';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as restm from 'typed-rest-client/RestClient';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
@ -11,36 +9,37 @@ let osPlat: string = os.platform();
let osArch: string = os.arch();
export async function getUPX(version: string): Promise<string> {
const selected = await determineVersion(version);
if (selected) {
version = selected;
const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) {
throw new Error(`Cannot find UPX ${version} release`);
}
core.info(`✅ UPX version found: ${version}`);
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'upx-'));
const fileName = getFileName(version);
const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', version, fileName);
const semver: string = release.tag_name.replace(/^v/, '');
core.debug(`Semver is ${semver}`);
core.info(`✅ UPX version found: ${semver}`);
const filename = util.format('%s.%s', getName(semver), osPlat == 'win32' ? 'zip' : 'tar.xz');
const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', semver, filename);
core.info(`⬇️ Downloading ${downloadUrl}...`);
await download.default(downloadUrl, tmpdir, {filename: fileName});
const downloadPath: string = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
core.info('📦 Extracting UPX...');
let extPath: string = tmpdir;
if (osPlat == 'win32') {
extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
} else {
extPath = await tc.extractTar(`${tmpdir}/${fileName}`, undefined, 'x');
const extPath: string =
osPlat == 'win32' ? await tc.extractZip(downloadPath) : await tc.extractTar(downloadPath, undefined, 'x');
core.debug(`Extracted to ${extPath}`);
const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', version);
core.debug(`Cached to ${cachePath}`);
const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx');
core.debug(`Exe path is ${exePath}`);
return exePath;
}
return path.join(extPath, getFileNameWithoutExt(version), osPlat == 'win32' ? 'upx.exe' : 'upx');
}
function getFileName(version: string): string {
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.xz';
return util.format('%s.%s', getFileNameWithoutExt(version), ext);
}
function getFileNameWithoutExt(version: string): string {
function getName(version: string): string {
let platform: string = '';
if (osPlat == 'win32') {
platform = osArch == 'x64' ? 'win64' : 'win32';
@ -49,26 +48,3 @@ function getFileNameWithoutExt(version: string): string {
}
return util.format('upx-%s-%s', version, platform);
}
interface GitHubRelease {
tag_name: string;
}
async function determineVersion(version: string): Promise<string> {
let rest: restm.RestClient = new restm.RestClient('ghaction-upx', 'https://github.com', undefined, {
headers: {
Accept: 'application/json'
}
});
if (version !== 'latest') {
version = `v${version}`;
}
let res: restm.IRestResponse<GitHubRelease> = await rest.get<GitHubRelease>(`/upx/upx/releases/${version}`);
if (res.statusCode != 200 || res.result === null) {
throw new Error(`Cannot find UPX ${version} release (http ${res.statusCode})`);
}
return res.result.tag_name.replace(/^v/, '');
}

View File

@ -1,10 +1,10 @@
import * as installer from './installer';
import * as fs from 'fs';
import * as os from 'os';
import * as installer from './installer';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
export async function run(silent?: boolean) {
export async function run() {
try {
if (os.platform() == 'darwin') {
core.setFailed('Not supported on darwin platform');
@ -14,16 +14,16 @@ export async function run(silent?: boolean) {
const version = core.getInput('version') || 'latest';
const file = core.getInput('file', {required: true});
const args = core.getInput('args');
const upx = await installer.getUPX(version);
if (!fs.existsSync(file)) {
core.setFailed(`⛔ File to compress not found: ${file}`);
core.setFailed(`File to compress not found: ${file}`);
return;
}
console.log('🏃 Running UPX...');
await exec.exec(`${upx} ${args} ${file}`, undefined, {
silent: silent
});
const upx = await installer.getUPX(version);
core.info('🏃 Running UPX...');
await exec.exec(`${upx} ${args} ${file}`);
} catch (error) {
core.setFailed(error.message);
}