Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds input for defines #30

Draft
wants to merge 1 commit into
base: publish
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Arguments to makensis.exe. Default: `""`.

Newline-delimited list of paths to load plugins from. Default `""`.

### `defines`

Newline-delimited list of key-value pairs to define variables. Default `""`.

## Example usage

```yml
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ inputs:
required: false
default: ''

defines:
description: 'Newline-delimited list of key-value pairs to define variables'
required: false
default: ''

runs:
using: 'node16'
main: 'dist/index.cjs'
Expand Down
10 changes: 5 additions & 5 deletions dist/index.cjs

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const getBoolean = (value) => {
};

/**
* @returns {Promise.<void>}
* @returns {Promise<void>}
*/
const run = async () => {
try {
Expand All @@ -29,13 +29,18 @@ const run = async () => {
customArguments,
additionalPluginPaths,
scriptFile,
defines,
} = getInput();
const installer = new Installer(debugMode);
installer.setCustomArguments(customArguments);

additionalPluginPaths
.forEach(pluginPath => installer.addPluginPath(pluginPath.trim()));

for (const define of defines) {
installer.setDefine(define);
}

await installer.createInstallerAsync(
scriptFile
);
Expand Down
23 changes: 19 additions & 4 deletions src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

import { getInput as coreGetInput } from '@actions/core';

/**
* @param {string} input
*/
export const getMultilineInput = (input) => {
return input
.split(/\n|\r/)
.map(x => x.trim())
.filter(x => !!x);
};

/**
* @typedef {{
* customArguments: string,
* additionalPluginPaths: string[],
* scriptFile: string,
* defines: string[],
* }} Input
* @returns {Input}
*/
export const getInput = () => {
const customArguments = getInput.coreGetInput('arguments');

const additionalPluginPaths = getInput.coreGetInput('additional-plugin-paths')
.split(/\n|\r/)
.map(pluginPath => pluginPath.trim())
.filter(pluginPath => !!pluginPath);
const additionalPluginPaths = getMultilineInput(
getInput.coreGetInput('additional-plugin-paths'),
);

const scriptFile = getInput.coreGetInput('script-file');

const defines = getMultilineInput(
getInput.coreGetInput('defines'),
);

return {
customArguments,
additionalPluginPaths,
scriptFile,
defines,
}
};
getInput.coreGetInput = coreGetInput;
29 changes: 26 additions & 3 deletions src/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { F_OK } = constants;

/**
* @param {string} item
* @returns {Promise.<boolean>}
* @returns {Promise<boolean>}
*/
const isDirectoryAsync = async (item) => {
const stats = await stat(item);
Expand All @@ -27,7 +27,7 @@ const isDirectoryAsync = async (item) => {

/**
* @param {string} item
* @returns {Promise.<boolean>}
* @returns {Promise<boolean>}
*/
const fileExistsAsync = async (item) => {
try {
Expand All @@ -42,7 +42,7 @@ const fileExistsAsync = async (item) => {
/**
* @param {string} src
* @param {string} dest
* @returns {Promise.<void>}
* @returns {Promise<void>}
*/
const copyDirectoryAsync = async (src, dest) => {
console.log('copyDirectory', src, dest);
Expand Down Expand Up @@ -122,6 +122,29 @@ export class Installer {
this.pluginPaths.push(pluginPath);
}

getDefines() {
const exp = /-D([^ ]+)/g;
let res;
const defines = [];
while (res = exp.exec(this.customArguments)) {
defines.push(res[1]);
}

return defines;
}

/**
* @param {string} define
*/
setDefine(define) {
this.debugLog(`Adding define ${define}`);
const customArg = `-D${define}`;
if (this.customArguments.length) {
this.customArguments += ' ';
}
this.customArguments += customArg;
}

/**
* @param {string} scriptPath
* @returns {string[]}
Expand Down
4 changes: 2 additions & 2 deletions src/makensis.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Makensis {
/**
* Executes the makensis program, and returns its stdout.
* @param {string} args The arguments passed onto the makensis program.
* @returns {string}
* @returns {Promise<string>}
*/
async execAsync(args) {
const result = await execAsync(`"${this.path}" ${args}`);
Expand All @@ -71,7 +71,7 @@ class Makensis {
* @typedef {{
* [name: string]: string | undefined
* }} Symbols
* @returns {Promise.<Symbols>}
* @returns {Promise<Symbols>}
* @throws {Error} Given no symbols were output from the makensis -HDRINFO command.
*/
async getSymbolsAsync() {
Expand Down
31 changes: 23 additions & 8 deletions test/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ describe('e2e', () => {

/**
* @typedef {{
* customArguments?: string,
* additionalPluginPaths?: string[],
* scriptFile?: string,
* customArguments: string,
* additionalPluginPaths: string[],
* scriptFile: string,
* defines: string[],
* outfile: string,
* }} RunOptions
* @param {RunOptions} options
* @param {Partial<RunOptions>} options
*/
const run = async (options = {}) => {
const {
customArguments,
additionalPluginPaths,
scriptFile
scriptFile,
defines,
} = options;

const args = [];
Expand All @@ -63,6 +66,9 @@ describe('e2e', () => {
if (scriptFile) {
args.push('INPUT_SCRIPT-FILE', scriptFile);
}
if (defines) {
args.push('INPUT_DEFINES', defines.join('\n'));
}

// Call bootstrap.js do avoid a problem where hyphenated environment
// variables are unable to be assigned on non-windows platforms.
Expand Down Expand Up @@ -90,10 +96,11 @@ describe('e2e', () => {

/**
* @param {string} script
* @param {(options: RunOptions) => void} fn
* @param {(options: Partial<RunOptions>) => void} fn
*/
const test = (script, fn) => {
it(`should create installer for ${script}.nsi`, async () => {
/** @type {Partial<RunOptions>} */
const options = {
scriptFile: `./test/${script}.nsi`
};
Expand All @@ -103,11 +110,13 @@ describe('e2e', () => {

await run(options);

const actual = await exists(`./test/${script}.exe`);
const outfile = options.outfile || script;

const actual = await exists(`./test/${outfile}.exe`);

assert(
actual,
`Installer \`./test/${script}.exe\` should exist`
`Installer \`./test/${outfile}.exe\` should exist`
);
});
};
Expand All @@ -116,6 +125,12 @@ describe('e2e', () => {
test('with-plugins', options => options.additionalPluginPaths = [
'./test/EnVar'
]);
test('with-defines', options => {
options.defines = [
'OUT_FILE=foo',
];
options.outfile = 'foo';
});

// Skip for windows due to inconsistency
if (platform() !== 'win32') {
Expand Down
27 changes: 27 additions & 0 deletions test/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,32 @@ describe('input', () => {
const { scriptFile: actual } = getInput()
assert.strictEqual(actual, expected);
});

it('should assign defines, given \'defines\'', () => {
inputs = {
defines: 'abc\ndef\rghi',
};
const expected = ['abc', 'def', 'ghi'];
const { defines: actual } = getInput()
assert.deepStrictEqual(actual, expected);
});

it('should not include defines, given whitespace in \'defines\'', () => {
inputs = {
defines: 'abc\ndef\rghi\n \t\r\njkl',
};
const expected = ['abc', 'def', 'ghi', 'jkl'];
const { defines: actual } = getInput()
assert.deepStrictEqual(actual, expected);
});

it('should trim defines, given whitespace padding in \'defines\'', () => {
inputs = {
defines: 'abc\n def \r\t\tghi\t\t\nsomething with spaces',
};
const expected = ['abc', 'def', 'ghi', 'something with spaces'];
const { defines: actual } = getInput()
assert.deepStrictEqual(actual, expected);
});
});
});
43 changes: 43 additions & 0 deletions test/installer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ describe('Installer', () => {
});
});

describe('getDefines', () => {
it('should return empty, given no defines', () => {
const debugMode = false;
const target = new Installer(debugMode);

const expected = [];
const actual = target.getDefines();

assert.deepStrictEqual(actual, expected);

assert.strictEqual(target.getCustomArguments(), '');
});

it('should return some, given one setDefine call', () => {
const debugMode = false;
const target = new Installer(debugMode);

target.setDefine('foo=bar');

const expected = ['foo=bar'];
const actual = target.getDefines();

assert.deepStrictEqual(actual, expected);

assert.strictEqual(target.getCustomArguments(), '-Dfoo=bar');
});

it('should return some, given two setDefine calls', () => {
const debugMode = false;
const target = new Installer(debugMode);

target.setDefine('foo=bar');
target.setDefine('lorem=ipsum');

const expected = ['foo=bar', 'lorem=ipsum'];
const actual = target.getDefines();

assert.deepStrictEqual(actual, expected);

assert.strictEqual(target.getCustomArguments(), '-Dfoo=bar -Dlorem=ipsum');
});
});

describe('getProcessArguments', () => {
it('should default to no warnings verbosity, given no debug mode', () => {
const debugMode = false;
Expand Down
Loading