ghaction-upx/src/installer.ts

56 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

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> {
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/, '');
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
core.startGroup(`Downloading ${downloadUrl}...`);
2020-05-06 14:14:50 -06:00
const downloadPath: string = await tc.downloadTool(downloadUrl);
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');
}
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}`);
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 {
let platform = '';
2020-01-08 14:26:34 -07:00
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
}