2020-01-08 14:26:34 -07:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as util from 'util';
|
2020-05-06 14:14:50 -06:00
|
|
|
import * as github from './github';
|
2020-01-17 01:36:42 -07:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as tc from '@actions/tool-cache';
|
2020-01-08 14:26:34 -07:00
|
|
|
|
2020-05-07 04:11:19 -06:00
|
|
|
const osPlat: string = os.platform();
|
|
|
|
const osArch: string = os.arch();
|
2020-01-08 14:26:34 -07:00
|
|
|
|
|
|
|
export async function getUPX(version: string): Promise<string> {
|
2021-03-27 12:31:20 -06:00
|
|
|
core.startGroup(`Checking UPX ${version} release...`);
|
2020-05-06 14:14:50 -06:00
|
|
|
const release: github.GitHubRelease | null = await github.getRelease(version);
|
|
|
|
if (!release) {
|
|
|
|
throw new Error(`Cannot find UPX ${version} release`);
|
2020-01-08 14:26:34 -07:00
|
|
|
}
|
2020-05-06 14:14:50 -06:00
|
|
|
const semver: string = release.tag_name.replace(/^v/, '');
|
2021-03-27 12:31:20 -06:00
|
|
|
core.info(`UPX ${semver} found`);
|
|
|
|
core.endGroup();
|
2020-05-06 14:14:50 -06:00
|
|
|
|
|
|
|
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);
|
2020-01-08 14:26:34 -07:00
|
|
|
|
2021-03-27 12:31:20 -06:00
|
|
|
core.startGroup(`Downloading ${downloadUrl}...`);
|
|
|
|
|
2020-05-06 14:14:50 -06:00
|
|
|
const downloadPath: string = await tc.downloadTool(downloadUrl);
|
2021-03-27 12:31:20 -06:00
|
|
|
core.info(`Downloaded to ${downloadPath}`);
|
2020-01-08 14:26:34 -07:00
|
|
|
|
2020-05-07 04:11:19 -06:00
|
|
|
let extPath: string;
|
|
|
|
if (osPlat == 'win32') {
|
|
|
|
extPath = await tc.extractZip(downloadPath);
|
|
|
|
} else {
|
|
|
|
extPath = await tc.extractTar(downloadPath, undefined, 'x');
|
|
|
|
}
|
2021-03-27 12:31:20 -06:00
|
|
|
core.info(`Extracted to ${extPath}`);
|
2020-01-08 14:26:34 -07:00
|
|
|
|
2020-05-07 04:11:19 -06:00
|
|
|
const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', semver);
|
2020-05-06 14:14:50 -06:00
|
|
|
core.debug(`Cached to ${cachePath}`);
|
2020-01-08 14:26:34 -07:00
|
|
|
|
2020-05-06 14:14:50 -06:00
|
|
|
const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx');
|
|
|
|
core.debug(`Exe path is ${exePath}`);
|
2021-03-27 12:31:20 -06:00
|
|
|
core.endGroup();
|
2020-05-06 14:14:50 -06:00
|
|
|
|
|
|
|
return exePath;
|
2020-01-17 01:36:42 -07:00
|
|
|
}
|
|
|
|
|
2020-05-06 14:14:50 -06:00
|
|
|
function getName(version: string): string {
|
2020-01-08 14:26:34 -07:00
|
|
|
let platform: string = '';
|
|
|
|
if (osPlat == 'win32') {
|
|
|
|
platform = osArch == 'x64' ? 'win64' : 'win32';
|
|
|
|
} else if (osPlat == 'linux') {
|
|
|
|
platform = osArch == 'x64' ? 'amd64_linux' : 'i386_linux';
|
|
|
|
}
|
2020-01-17 01:36:42 -07:00
|
|
|
return util.format('upx-%s-%s', version, platform);
|
2020-01-08 14:26:34 -07:00
|
|
|
}
|