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

feat(): add preflight before main commands run #260

Draft
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ target/
.mvn/
temp/
*.tgz
mvnw/bin
57 changes: 57 additions & 0 deletions src/commands/preflight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


const {spawnSync} = require('child_process');
const path = require('path');
const colors = require('colors');
const {flags, shell} = require('../mvnw');

module.exports = (opts) => {
console.info('Starting preflight check ');

const params = ['eo:help'].concat(flags(opts)).filter((t) => t !== '');

const home = path.resolve(__dirname, '../../mvnw');
const bin = path.resolve(home, 'mvnw') + (process.platform == 'win32' ? '.cmd' : '');

const result = spawnSync(
bin,
process.platform == 'win32' ? params.map((p) => `"${p}"`) : params,
{
cwd: home,
stdio: 'inherit',
shell: shell(),
}
);

if (result.status !== 0) {
throw new Error(`Preflight check failed. Parser version could be the problem.` +
` Current version of parser is : ${opts.parser}`);
}

process.stdout.write(
colors.green('Preflight check finished successfully\n')
);
};
13 changes: 13 additions & 0 deletions src/eoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const tinted = require('./tinted-console');
const audit = require('./commands/audit');
const clean = require('./commands/clean');
const parse = require('./commands/parse');
const preflight = require('./commands/preflight');
const assemble = require('./commands/assemble');
const sodg = require('./commands/sodg');
const phi = require('./commands/phi');
Expand Down Expand Up @@ -107,13 +108,15 @@ program
program.command('register')
.description('Register all visible EO source files')
.action((str, opts) => {
preflight(program.opts());
register(program.opts());
});

program.command('parse')
.description('Parse EO files into XMIR')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => parse(program.opts()));
Expand All @@ -126,6 +129,7 @@ program.command('assemble')
.description('Parse EO files into XMIR and join them with required dependencies')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()));
Expand All @@ -144,6 +148,7 @@ program.command('sodg')
.option('--exclude <names>', 'Don\'t generate SODG for these objects')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -157,6 +162,7 @@ program.command('phi')
.description('Generate PHI files from XMIR')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -171,6 +177,7 @@ program.command('unphi')
.description('Generate XMIR files from PHI files')
.action((str, opts) => {
clear(str);
preflight(program.opts());
unphi({...program.opts(), ...str});
});

Expand All @@ -185,6 +192,7 @@ program.command('verify')
.description('Verify XMIR files and fail if any issues inside')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -198,6 +206,7 @@ program.command('transpile')
.description('Convert EO files into target language')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -212,6 +221,7 @@ program.command('compile')
.description('Compile target language sources into binaries')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -227,6 +237,7 @@ program.command('link')
.description('Link together all binaries into a single executable binary')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -244,6 +255,7 @@ program.command('dataize')
.option('--stack <size>', 'Change stack size', '1M')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand All @@ -261,6 +273,7 @@ program.command('test')
.description('Run all visible unit tests')
.action((str, opts) => {
clear(str);
preflight(program.opts());
if (program.opts().alone == undefined) {
register(program.opts())
.then((r) => assemble(program.opts()))
Expand Down
2 changes: 2 additions & 0 deletions src/mvnw.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ let target;
let running = false;
let beginning;

module.exports.shell = shell;

/**
* Prepare options for Maven.
* @param {Hash} opts - Opts provided to the "eoc"
Expand Down
52 changes: 52 additions & 0 deletions test/commands/test_preflight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {runSync, homeHash} = require('../helpers');

describe('preflight', () => {
it('run parses with wrong version', (done) => {
home = path.resolve('temp/test-parse/simple');
fs.rmSync(home, {recursive: true, force: true});

let thrown = false;

try {
runSync([
'parse',
'--parser=WRONG_VERSION',
'--hash=' + homeHash,
'-s', path.resolve(home, 'src'),
'-t', path.resolve(home, 'target'),
]);
} catch (e) {
thrown = true;
}
assert(thrown);

done();
});
});
Loading