Skip to content

Commit

Permalink
Implement interactive menu.
Browse files Browse the repository at this point in the history
Merge pull request #42 from haroldo-ok/menu
  • Loading branch information
haroldo-ok committed Sep 25, 2022
2 parents 5844adf + fc2b553 commit 2192188
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/test/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void VN_init() {
XGM_setLoopNumber(-1);
XGM_setForceDelayDMA(TRUE);

VDP_drawText("choice4genesis v0.3.0", 18, 27);
VDP_drawText("choice4genesis v0.4.0", 18, 27);
}


Expand Down
2 changes: 2 additions & 0 deletions generator/commandline.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const readCommandLine = () => yargs
type: 'string'
})
})
.command('menu', 'show an interactive menu')
.options({
'project-dir': {
alias: 'pd',
Expand Down Expand Up @@ -56,6 +57,7 @@ const readCommandLine = () => yargs
['$0 transpile test', 'Transpiles the project called "test"'],
['$0 compile test', 'Compiles, without transpiling, the project called "test"'],
['$0 emulate test', 'Executes the existing ROM of the project called "test"'],
['$0 menu', 'Calls the interactive menu'],
['$0 transpile test -- compile', 'Transpiles and compiles the project called "test"'],
['$0 transpile test -- compile emulate', 'Transpiles, compiles and runs the project called "test"']
])
Expand Down
4 changes: 4 additions & 0 deletions generator/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
const { existsSync } = require('fs');
const { normalize } = require('path');

const chalk = require('chalk');

const { execute } = require('./executor');

const compile = async commandLine =>
new Promise((resolve, reject) => {
console.log(chalk.blue('Calling the compiler...'));

const projectFolder = normalize(`${commandLine.projectDir}/${commandLine.project}/`);
if (!existsSync(projectFolder)) {
resolve({ errors: [{ message: 'Directory does not exist: ' + projectFolder }] });
Expand Down
4 changes: 3 additions & 1 deletion generator/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

const { spawn } = require('child_process');

const chalk = require('chalk');

const execute = async (execName, parameters, { appName, cwd }) =>
new Promise((resolve, reject) => {
const proc = spawn(execName, parameters, { cwd });
proc.stdout.on('data', data => console.log(`${data}`.trimRight()))
proc.stderr.on('data', data => console.error(`${data}`.trimRight()))
proc.on('error', data => console.error(`${data}`.trimRight()))
proc.on('error', data => console.error(chalk.red(`${data}`.trimRight())))
proc.on('close', code => {
if (!code) {
resolve({});
Expand Down
44 changes: 44 additions & 0 deletions generator/ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const { existsSync, lstatSync, readdir } = require('fs');
const { normalize } = require('path');
const { textSync } = require('figlet');
const chalk = require('chalk');

const menu = require('node-menu');

const pjson = require('./../package.json');

// TODO: Use async file access
const showMenu = (commandLine, executeCommands) => {

const projectsFolder = normalize(commandLine.projectDir);
if (!existsSync(projectsFolder)) {
return { errors: [{ message: 'Directory does not exist: ' + projectsFolder }] };
}

readdir(projectsFolder, (err, files) => {
if (err) {
console.error('Error while listing projects: ' + err);
return;
}

menu.customHeader(function() {
console.log(chalk.blue(textSync(pjson.name, { font: 'Rectangles' })));
console.log(`v${pjson.version}`);
console.log('');
})
.addDelimiter('-', 40, 'Which project do you want to compile?');

files
.filter(fileName => lstatSync(`${projectsFolder}/${fileName}`).isDirectory())
.forEach(projectName => menu.addItem(projectName, () => {
commandLine.project = projectName;
return executeCommands();
}));

menu.start();
});
};

module.exports = { showMenu };
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs');
const { normalize } = require('path');
const { compact } = require('lodash');
const chalk = require('chalk');

const { transpile } = require('./generator/transpiler');
const { compile } = require('./generator/compiler');
const { emulate } = require('./generator/emulator');
const { readCommandLine } = require('./generator/commandline');
const { watchProject } = require('./generator/watcher');
const { showMenu } = require('./generator/ui')


const commandLine = readCommandLine();
Expand All @@ -17,11 +19,11 @@ const handleErrors = result => {
}

result.errors.forEach(({sourceName, line, message}) => {
console.error(compact([
console.error(chalk.red(compact([
sourceName && `${sourceName}.choice`,
line && `Error at line ${line}`,
message
]).join(': '));
]).join(': ')));
});

return -1;
Expand All @@ -31,7 +33,9 @@ const handleErrors = result => {
const COMMANDS = { transpile, compile, emulate };

const executeCommands = async () => {
const commandsToExecute = compact(commandLine._.map(command => COMMANDS[command]));
const commandNames = commandLine._.filter(command => command !== 'menu');
const commandsToExecute = compact((commandNames.length ? commandNames : ['transpile', 'compile', 'emulate'])
.map(command => COMMANDS[command]));

for (execute of commandsToExecute) {
const result = await execute(commandLine);
Expand All @@ -42,7 +46,9 @@ const executeCommands = async () => {
}
}

if (commandLine.watch) {
if (commandLine._.includes('menu')) {
showMenu(commandLine, executeCommands);
} else if (commandLine.watch) {
console.warn('The "watch" option is a bit unstable, right now.');
watchProject(commandLine, executeCommands);
} else {
Expand Down
1 change: 1 addition & 0 deletions menu.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node . menu
53 changes: 46 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "choice4genesis",
"version": "0.3.0",
"version": "0.4.0",
"description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ",
"main": "index.js",
"scripts": {
"test": "jest",
"test_watch": "jest --watch",
"transpile_test": "node . transpile test",
"transpile_compile_test": "node . transpile test -- compile",
"compile_run_test": "node . transpile test -- compile emulate"
"transpile_test": "node . transpile test",
"transpile_compile_test": "node . transpile test -- compile",
"compile_run_test": "node . transpile test -- compile emulate"
},
"repository": {
"type": "git",
Expand All @@ -32,7 +32,10 @@
"jest": "^29.0.1"
},
"dependencies": {
"chalk": "^4.1.2",
"figlet": "^1.5.2",
"lodash": "^4.17.21",
"node-menu": "^1.3.2",
"parsimmon": "^1.18.1",
"yargs": "^17.5.1"
}
Expand Down

0 comments on commit 2192188

Please sign in to comment.