From 15ae11ce935d1700280ad2b758c4425cc5d7a682 Mon Sep 17 00:00:00 2001 From: Anton Bashmakov Date: Fri, 5 Apr 2024 10:52:23 +0200 Subject: [PATCH] feat(): add preflight before main commands run --- .gitignore | 1 + src/commands/preflight.js | 57 +++++++++++++++++++++++++++++++++ src/eoc.js | 13 ++++++++ src/mvnw.js | 2 ++ test/commands/test_preflight.js | 52 ++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 src/commands/preflight.js create mode 100644 test/commands/test_preflight.js diff --git a/.gitignore b/.gitignore index bd95645e..fbf8cc70 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ target/ .mvn/ temp/ *.tgz +mvnw/bin diff --git a/src/commands/preflight.js b/src/commands/preflight.js new file mode 100644 index 00000000..968e4d77 --- /dev/null +++ b/src/commands/preflight.js @@ -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') + ); +}; diff --git a/src/eoc.js b/src/eoc.js index bf18ac78..3b385b41 100755 --- a/src/eoc.js +++ b/src/eoc.js @@ -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'); @@ -107,6 +108,7 @@ program program.command('register') .description('Register all visible EO source files') .action((str, opts) => { + preflight(program.opts()); register(program.opts()); }); @@ -114,6 +116,7 @@ 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())); @@ -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())); @@ -144,6 +148,7 @@ program.command('sodg') .option('--exclude ', '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())) @@ -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())) @@ -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}); }); @@ -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())) @@ -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())) @@ -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())) @@ -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())) @@ -244,6 +255,7 @@ program.command('dataize') .option('--stack ', '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())) @@ -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())) diff --git a/src/mvnw.js b/src/mvnw.js index 84d0c59d..4b531fea 100644 --- a/src/mvnw.js +++ b/src/mvnw.js @@ -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" diff --git a/test/commands/test_preflight.js b/test/commands/test_preflight.js new file mode 100644 index 00000000..0758dded --- /dev/null +++ b/test/commands/test_preflight.js @@ -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(); + }); +});