Allow multiple files (#140)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2021-03-27 19:31:20 +01:00 committed by GitHub
parent 925acc7207
commit 1ebf61db85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 3750 additions and 152 deletions

BIN
.github/ghaction-upx.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

@ -28,33 +28,28 @@ jobs:
version: version:
- latest - latest
- v3.95 - v3.95
include:
- os: ubuntu-latest
file: https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-linux-amd64
- os: windows-latest
file: https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-windows-4.0-amd64.exe
steps: steps:
- -
name: Checkout name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- -
name: Download file name: Download files
id: download shell: bash
run: | run: |
mkdir ./bin mkdir ./bin
if [ "${{ matrix.os }}" == "windows-latest" ]; then if [ "${{ matrix.os }}" = "windows-latest" ]; then
curl -sSLk ${{ matrix.file }} -o ./bin/firefox-history-merger.exe curl -sSLk https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-windows-4.0-386.exe -o ./bin/firefox-history-merger-windows-4.0-386.exe
echo ::set-output name=filename::./bin/firefox-history-merger.exe curl -sSLk https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-windows-4.0-amd64.exe -o ./bin/firefox-history-merger-windows-4.0-amd64.exe
else else
curl -sSLk ${{ matrix.file }} -o ./bin/firefox-history-merger curl -sSLk https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-linux-386 -o ./bin/firefox-history-merger-linux-386
chmod +x ./bin/firefox-history-merger curl -sSLk https://github.com/crazy-max/firefox-history-merger/releases/download/2.4.0/firefox-history-merger-linux-amd64 -o ./bin/firefox-history-merger-linux-amd64
echo ::set-output name=filename::./bin/firefox-history-merger
fi fi
shell: bash chmod +x ./bin/firefox-history-merger*
- -
name: UPX name: UPX
uses: ./ uses: ./
with: with:
version: ${{ matrix.version }} version: ${{ matrix.version }}
file: ${{ steps.download.outputs.filename }} files: |
./bin/firefox-history-merger*
args: -fq args: -fq

View File

@ -9,6 +9,8 @@
GitHub Action for [UPX](https://github.com/upx/upx), the Ultimate Packer for eXecutables. GitHub Action for [UPX](https://github.com/upx/upx), the Ultimate Packer for eXecutables.
![Screenshot](.github/ghaction-upx.png)
___ ___
* [Usage](#usage) * [Usage](#usage)
@ -25,7 +27,6 @@ ___
name: upx name: upx
on: on:
pull_request:
push: push:
jobs: jobs:
@ -40,7 +41,8 @@ jobs:
uses: crazy-max/ghaction-upx@v1 uses: crazy-max/ghaction-upx@v1
with: with:
version: latest version: latest
file: ./bin/mybinary files: |
./bin/*.exe
args: -fq args: -fq
``` ```
@ -53,7 +55,7 @@ Following inputs can be used as `step.with` keys
| Name | Type | Default | Description | | Name | Type | Default | Description |
|---------------|---------|-----------|---------------------------------| |---------------|---------|-----------|---------------------------------|
| `version` | String | `latest` | UPX version. Example: `v3.95` | | `version` | String | `latest` | UPX version. Example: `v3.95` |
| `file` | String | | File to compress (**required**) | | `files` | String | | Newline-delimited list of path globs for files to compress (**required**) |
| `args` | String | | Arguments to pass to UPX | | `args` | String | | Arguments to pass to UPX |
## Keep up-to-date with GitHub Dependabot ## Keep up-to-date with GitHub Dependabot
@ -78,7 +80,10 @@ This action is only available for Linux and Windows [virtual environments](https
## How can I help? ## How can I help?
All kinds of contributions are welcome :raised_hands:! The most basic way to show your support is to star :star2: the project, or to raise issues :speech_balloon: You can also support this project by [**becoming a sponsor on GitHub**](https://github.com/sponsors/crazy-max) :clap: or by making a [Paypal donation](https://www.paypal.me/crazyws) to ensure this journey continues indefinitely! :rocket: All kinds of contributions are welcome :raised_hands:! The most basic way to show your support is to star :star2:
the project, or to raise issues :speech_balloon: You can also support this project by
[**becoming a sponsor on GitHub**](https://github.com/sponsors/crazy-max) :clap: or by making a
[Paypal donation](https://www.paypal.me/crazyws) to ensure this journey continues indefinitely! :rocket:
Thanks again for your support, it is much appreciated! :pray: Thanks again for your support, it is much appreciated! :pray:

98
__tests__/context.test.ts Normal file
View File

@ -0,0 +1,98 @@
import * as context from '../src/context';
import * as core from '@actions/core';
import * as path from 'path';
describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar']);
});
it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty lines correctly', async () => {
setInput('foo', 'bar\n\nbaz');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('remove empty result correctly', async () => {
setInput('foo', 'bar,baz,');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = await context.getInputList(core.getInput('foo'));
console.log(res);
expect(res).toEqual(['bar', 'baz', 'bat']);
});
it('handles multiple lines and ignoring comma correctly', async () => {
setInput('files', './bin/binary.exe\n./bin/binary2.exe');
const res = await context.getInputList(core.getInput('files'), true);
console.log(res);
expect(res).toEqual(['./bin/binary.exe', './bin/binary2.exe']);
});
it('handles different new lines and ignoring comma correctly', async () => {
setInput('driver-opts', './bin/binary.exe\r\n./bin/binary2.exe');
const res = await context.getInputList(core.getInput('files'), true);
console.log(res);
expect(res).toEqual(['./bin/binary.exe', './bin/binary2.exe']);
});
});
describe('asyncForEach', () => {
it('executes async tasks sequentially', async () => {
const testValues = [1, 2, 3, 4, 5];
const results: number[] = [];
await context.asyncForEach(testValues, async value => {
results.push(value);
});
expect(results).toEqual(testValues);
});
});
describe('resolvePaths', () => {
it('resolve files given a set of paths', async () => {
expect(
context.resolvePaths([path.join(__dirname, 'fixtures/data/**/*').split(path.sep).join(path.posix.sep)])
).toEqual([path.join(__dirname, 'fixtures/data/foo/bar.txt').split(path.sep).join(path.posix.sep)]);
});
});
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}
function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}

View File

@ -0,0 +1 @@
scan me

View File

@ -11,12 +11,16 @@ inputs:
description: 'UPX version. Example: 3.95' description: 'UPX version. Example: 3.95'
default: 'latest' default: 'latest'
required: false required: false
file: files:
description: 'File to compress' description: 'Newline-delimited list of path globs for files to compress'
required: true required: false
args: args:
description: 'Arguments to pass to UPX' description: 'Arguments to pass to UPX'
required: false required: false
file:
deprecationMessage: 'file is deprecated. Please use files input instead.'
description: 'File to compress'
required: false
runs: runs:
using: 'node12' using: 'node12'

3677
dist/index.js generated vendored

File diff suppressed because it is too large Load Diff

42
src/context.ts Normal file
View 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()));
}, []);
};

View File

@ -9,34 +9,37 @@ const osPlat: string = os.platform();
const osArch: string = os.arch(); const osArch: string = os.arch();
export async function getUPX(version: string): Promise<string> { export async function getUPX(version: string): Promise<string> {
core.startGroup(`Checking UPX ${version} release...`);
const release: github.GitHubRelease | null = await github.getRelease(version); const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) { if (!release) {
throw new Error(`Cannot find UPX ${version} release`); throw new Error(`Cannot find UPX ${version} release`);
} }
const semver: string = release.tag_name.replace(/^v/, ''); 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 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); const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', semver, filename);
core.info(`⬇️ Downloading ${downloadUrl}...`); core.startGroup(`Downloading ${downloadUrl}...`);
const downloadPath: string = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`); const downloadPath: string = await tc.downloadTool(downloadUrl);
core.info(`Downloaded to ${downloadPath}`);
core.info('📦 Extracting UPX...');
let extPath: string; let extPath: string;
if (osPlat == 'win32') { if (osPlat == 'win32') {
extPath = await tc.extractZip(downloadPath); extPath = await tc.extractZip(downloadPath);
} else { } else {
extPath = await tc.extractTar(downloadPath, undefined, 'x'); 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); const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', semver);
core.debug(`Cached to ${cachePath}`); core.debug(`Cached to ${cachePath}`);
const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx'); const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx');
core.debug(`Exe path is ${exePath}`); core.debug(`Exe path is ${exePath}`);
core.endGroup();
return exePath; return exePath;
} }

View File

@ -1,5 +1,5 @@
import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import * as context from './context';
import * as installer from './installer'; import * as installer from './installer';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
@ -11,19 +11,20 @@ async function run(): Promise<void> {
return; return;
} }
const version = core.getInput('version') || 'latest'; const inputs: context.Inputs = await context.getInputs();
const file = core.getInput('file', {required: true}); const upx = await installer.getUPX(inputs.version);
const args = core.getInput('args');
if (!fs.existsSync(file)) { const files: string[] = await context.resolvePaths(inputs.files);
core.setFailed(`File to compress not found: ${file}`); if (files.length == 0) {
core.warning(`No files were found. Please check the 'files' input.`);
return; return;
} }
const upx = await installer.getUPX(version); await context.asyncForEach(files, async filepath => {
core.startGroup(`Compressing ${filepath}...`);
core.info('🏃 Running UPX...'); await exec.exec(`${upx} ${inputs.args} ${filepath}`);
await exec.exec(`${upx} ${args} ${file}`); core.endGroup();
});
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} }