mirror of
https://github.com/crazy-max/ghaction-upx.git
synced 2025-12-09 14:30:42 -07:00
Allow multiple files (#140)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
42
src/context.ts
Normal file
42
src/context.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import * as glob from 'glob';
|
||||
import {lstatSync} from 'fs';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
export interface Inputs {
|
||||
version: string;
|
||||
files: string[];
|
||||
args: string;
|
||||
}
|
||||
|
||||
export async function getInputs(): Promise<Inputs> {
|
||||
return {
|
||||
version: core.getInput('version') || 'latest',
|
||||
files: getInputList(core.getInput('files') || core.getInput('file'), true),
|
||||
args: core.getInput('args')
|
||||
};
|
||||
}
|
||||
|
||||
export function getInputList(items: string, ignoreComma?: boolean): string[] {
|
||||
if (items == '') {
|
||||
return [];
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter(x => x)
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
export const asyncForEach = async (array, callback) => {
|
||||
for (let index = 0; index < array.length; index++) {
|
||||
await callback(array[index], index, array);
|
||||
}
|
||||
};
|
||||
|
||||
export const resolvePaths = (patterns: string[]): string[] => {
|
||||
return patterns.reduce((acc: string[], pattern: string): string[] => {
|
||||
return acc.concat(glob.sync(pattern).filter(path => lstatSync(path).isFile()));
|
||||
}, []);
|
||||
};
|
||||
@@ -9,34 +9,37 @@ const osPlat: string = os.platform();
|
||||
const osArch: string = os.arch();
|
||||
|
||||
export async function getUPX(version: string): Promise<string> {
|
||||
core.startGroup(`Checking UPX ${version} release...`);
|
||||
const release: github.GitHubRelease | null = await github.getRelease(version);
|
||||
if (!release) {
|
||||
throw new Error(`Cannot find UPX ${version} release`);
|
||||
}
|
||||
const semver: string = release.tag_name.replace(/^v/, '');
|
||||
core.info(`UPX ${semver} found`);
|
||||
core.endGroup();
|
||||
|
||||
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}...`);
|
||||
const downloadPath: string = await tc.downloadTool(downloadUrl);
|
||||
core.debug(`Downloaded to ${downloadPath}`);
|
||||
core.startGroup(`Downloading ${downloadUrl}...`);
|
||||
|
||||
const downloadPath: string = await tc.downloadTool(downloadUrl);
|
||||
core.info(`Downloaded to ${downloadPath}`);
|
||||
|
||||
core.info('📦 Extracting UPX...');
|
||||
let extPath: string;
|
||||
if (osPlat == 'win32') {
|
||||
extPath = await tc.extractZip(downloadPath);
|
||||
} else {
|
||||
extPath = await tc.extractTar(downloadPath, undefined, 'x');
|
||||
}
|
||||
core.debug(`Extracted to ${extPath}`);
|
||||
core.info(`Extracted to ${extPath}`);
|
||||
|
||||
const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', semver);
|
||||
core.debug(`Cached to ${cachePath}`);
|
||||
|
||||
const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx');
|
||||
core.debug(`Exe path is ${exePath}`);
|
||||
core.endGroup();
|
||||
|
||||
return exePath;
|
||||
}
|
||||
|
||||
21
src/main.ts
21
src/main.ts
@@ -1,5 +1,5 @@
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as context from './context';
|
||||
import * as installer from './installer';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
@@ -11,19 +11,20 @@ async function run(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
const version = core.getInput('version') || 'latest';
|
||||
const file = core.getInput('file', {required: true});
|
||||
const args = core.getInput('args');
|
||||
const inputs: context.Inputs = await context.getInputs();
|
||||
const upx = await installer.getUPX(inputs.version);
|
||||
|
||||
if (!fs.existsSync(file)) {
|
||||
core.setFailed(`File to compress not found: ${file}`);
|
||||
const files: string[] = await context.resolvePaths(inputs.files);
|
||||
if (files.length == 0) {
|
||||
core.warning(`No files were found. Please check the 'files' input.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const upx = await installer.getUPX(version);
|
||||
|
||||
core.info('🏃 Running UPX...');
|
||||
await exec.exec(`${upx} ${args} ${file}`);
|
||||
await context.asyncForEach(files, async filepath => {
|
||||
core.startGroup(`Compressing ${filepath}...`);
|
||||
await exec.exec(`${upx} ${inputs.args} ${filepath}`);
|
||||
core.endGroup();
|
||||
});
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user