mirror of
https://github.com/crazy-max/ghaction-upx.git
synced 2025-12-09 14:30:42 -07:00
Initial commit
This commit is contained in:
38
src/decompress-tarxz.d.ts
vendored
Normal file
38
src/decompress-tarxz.d.ts
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
export = decompresstarxz;
|
||||
|
||||
declare function decompresstarxz(
|
||||
input: string | Buffer,
|
||||
output?: string | decompresstarxz.DecompressOptions,
|
||||
opts?: decompresstarxz.DecompressOptions
|
||||
): Promise<decompresstarxz.File[]>;
|
||||
|
||||
declare namespace decompresstarxz {
|
||||
interface File {
|
||||
data: Buffer;
|
||||
mode: number;
|
||||
mtime: string;
|
||||
path: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface DecompressOptions {
|
||||
/**
|
||||
* Filter out files before extracting
|
||||
*/
|
||||
filter?(file: File): boolean;
|
||||
/**
|
||||
* Map files before extracting
|
||||
*/
|
||||
map?(file: File): File;
|
||||
/**
|
||||
* Array of plugins to use.
|
||||
* Default: [decompressTar(), decompressTarbz2(), decompressTargz(), decompressUnzip()]
|
||||
*/
|
||||
plugins?: any[];
|
||||
/**
|
||||
* Remove leading directory components from extracted files.
|
||||
* Default: 0
|
||||
*/
|
||||
strip?: number;
|
||||
}
|
||||
}
|
||||
69
src/installer.ts
Normal file
69
src/installer.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import decompress = require('decompress');
|
||||
import decompresstarxz = require('decompress-tarxz');
|
||||
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';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
console.log(`✅ 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);
|
||||
|
||||
console.log(`⬇️ Downloading ${downloadUrl}...`);
|
||||
await download.default(downloadUrl, tmpdir, {filename: fileName});
|
||||
|
||||
console.log('📦 Extracting UPX...');
|
||||
if (osPlat == 'win32') {
|
||||
await decompress(`${tmpdir}/${fileName}`, tmpdir, {strip: 1});
|
||||
} else {
|
||||
await decompresstarxz(`${tmpdir}/${fileName}`, tmpdir, {strip: 1});
|
||||
}
|
||||
|
||||
return path.join(tmpdir, osPlat == 'win32' ? 'upx.exe' : 'upx');
|
||||
}
|
||||
|
||||
function getFileName(version: string): string {
|
||||
let platform: string = '';
|
||||
if (osPlat == 'win32') {
|
||||
platform = osArch == 'x64' ? 'win64' : 'win32';
|
||||
} else if (osPlat == 'linux') {
|
||||
platform = osArch == 'x64' ? 'amd64_linux' : 'i386_linux';
|
||||
}
|
||||
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.xz';
|
||||
return util.format('upx-%s-%s.%s', version, platform, ext);
|
||||
}
|
||||
|
||||
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/, '');
|
||||
}
|
||||
26
src/main.ts
Normal file
26
src/main.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import * as installer from './installer';
|
||||
import * as fs from 'fs';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
export async function run(silent?: boolean) {
|
||||
try {
|
||||
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}`);
|
||||
}
|
||||
|
||||
console.log('🏃 Running UPX...');
|
||||
await exec.exec(`${upx} ${args} ${file}`, undefined, {
|
||||
silent: silent
|
||||
});
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
Reference in New Issue
Block a user