ghaction-upx/src/installer.ts

51 lines
1.8 KiB
TypeScript
Raw 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
let osPlat: string = os.platform();
let osArch: string = os.arch();
export async function getUPX(version: string): Promise<string> {
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.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);
2020-01-08 14:26:34 -07:00
2020-01-17 01:36:42 -07:00
core.info(`⬇️ Downloading ${downloadUrl}...`);
2020-05-06 14:14:50 -06:00
const downloadPath: string = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
2020-01-08 14:26:34 -07:00
2020-01-17 01:36:42 -07:00
core.info('📦 Extracting UPX...');
2020-05-06 14:14:50 -06:00
const extPath: string =
osPlat == 'win32' ? await tc.extractZip(downloadPath) : await tc.extractTar(downloadPath, undefined, 'x');
core.debug(`Extracted to ${extPath}`);
2020-01-08 14:26:34 -07:00
2020-05-06 14:14:50 -06:00
const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', version);
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}`);
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
}