From e4181e6ce48cb9b37ba2d627274cf5b3e761d820 Mon Sep 17 00:00:00 2001 From: Jan Jongboom Date: Wed, 13 Dec 2017 16:24:46 +0700 Subject: [PATCH] Gulpfile should belong in main project, not in mbed-js --- .gitignore | 2 + .mbedignore | 2 + Gulpfile.js | 5 ++ README.md | 27 +++++-- mbed-js/Gulpfile.js | 5 -- mbed-js/jerryscript-mbed-gulp/main.js | 80 +++++++++---------- .../source/launcher.cpp | 4 +- mbed-js/{ => tools}/requirements.txt | 1 - package.json | 10 +++ source/{js => }/main.js | 2 + 10 files changed, 84 insertions(+), 54 deletions(-) create mode 100644 .gitignore create mode 100644 .mbedignore create mode 100644 Gulpfile.js delete mode 100644 mbed-js/Gulpfile.js rename mbed-js/{ => tools}/requirements.txt (87%) create mode 100644 package.json rename source/{js => }/main.js (90%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d570088 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ + diff --git a/.mbedignore b/.mbedignore new file mode 100644 index 0000000..d570088 --- /dev/null +++ b/.mbedignore @@ -0,0 +1,2 @@ +node_modules/ + diff --git a/Gulpfile.js b/Gulpfile.js new file mode 100644 index 0000000..a392cb5 --- /dev/null +++ b/Gulpfile.js @@ -0,0 +1,5 @@ +'use strict'; + +const gulp = require('gulp'); + +require('./mbed-js/jerryscript-mbed-gulp/main')(gulp); diff --git a/README.md b/README.md index fc9f84d..6d56f47 100644 --- a/README.md +++ b/README.md @@ -8,25 +8,40 @@ Work in progress. ## How to build -1. Install Mbed CLI. +1. Install Mbed CLI, Python 2.7 and node.js 8.x. 1. Import this project: ``` $ mbed import https://github.com/janjongboom/mbed-js-v2 ``` -1. Turn your JavaScript into C: +1. Install dependencies: ``` - $ cd mbed-js - $ pip install -r requirements.txt $ npm install - $ gulp --target=K64F --js=../source/js/main.js ``` 1. Build the project for your target: ``` - $ mbed compile -m K64F -t GCC_ARM + $ gulp build --js ./source/main.js --target=K64F ``` +1. Drag the `.bin` (or `.hex`) file to your board to flash. + +**Changing build options** + +To change the build options, invoke Mbed CLI by hand. + +First turn your JS into C++ code via: + +``` +$ gulp --js ./source/main.js --target=K64F +``` + +Then, compile manually: + +``` +$ mbed compile -m K64F -t GCC_ARM --profile=debug +``` + diff --git a/mbed-js/Gulpfile.js b/mbed-js/Gulpfile.js deleted file mode 100644 index 0ce434c..0000000 --- a/mbed-js/Gulpfile.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -const gulp = require('gulp'); - -require('./jerryscript-mbed-gulp/main')(gulp); diff --git a/mbed-js/jerryscript-mbed-gulp/main.js b/mbed-js/jerryscript-mbed-gulp/main.js index 3f71408..b557450 100644 --- a/mbed-js/jerryscript-mbed-gulp/main.js +++ b/mbed-js/jerryscript-mbed-gulp/main.js @@ -20,7 +20,7 @@ module.exports = function(gulp) { const fs = require('fs'); const del = require('del'); - const exec = require('child-process-promise').exec; + const spawn = require('child-process-promise').spawn; const Path = require('path'); @@ -29,11 +29,14 @@ module.exports = function(gulp) { const node_package = JSON.parse(fs.readFileSync(Path.join(__dirname, '..', 'package.json'))); const buildDir = Path.join(__dirname, '..', 'build'); - const jsBuildDir = Path.join(buildDir, 'out'); gulp.task('bundle', function() { var noParse = []; + if (!util.env.js || !fs.existsSync(util.env.js)) { + throw 'No main.js specified: Call `gulp --js path/to/main.js`'; + } + try { noParse.push(require.resolve('bleno')); } catch (e) { /* empty */ } @@ -62,55 +65,41 @@ module.exports = function(gulp) { .pipe(gulp.dest(buildDir)); }); - // function cpp_name_sanitise(name) { - // let out_name = name.replace(new RegExp('-', 'g'), '_') - // .replace(new RegExp('\\\\', 'g'), '_') - // .replace(new RegExp('\\?', 'g'), '_') - // .replace(new RegExp('\'', 'g'), '_') - // .replace(new RegExp('"', 'g'), '_'); - - // if ("0123456789".indexOf(out_name[0]) != -1) { - // out_name = '_' + out_name; - // } - - // return out_name; - // } + gulp.task('pip-requirements', function() { + return spawn( + 'pip', + [ 'install', '-r', 'requirements.txt' ], + { cwd: Path.join(__dirname, '..', 'tools')/*, stdio: 'inherit' */ }); + }); - // function cpp_string_sanitise(string) { - // let out_str = string.replace(new RegExp('\\\\', 'g'), '\\\\') - // .replace(new RegExp("\n", 'g'), "\\n") - // .replace(new RegExp("\"", 'g'), '\\"'); + gulp.task('generate-pins', ['make-build-dir', 'pip-requirements'], function() { + let script = Path.join(__dirname, '..', 'tools', 'generate_pins.py'); - // return out_str; - // } + if (!util.env.target) { + throw 'No target specified. Use `gulp --target=YOUR_TARGET`' + } - gulp.task('generate-pins', ['make-build-dir'], function() { - // @todo: properly spawn this. This breaks with a space in the path - let script = Path.join(__dirname, '..', 'tools', 'generate_pins.py'); - return exec('python ' + script + ' ' + util.env.target, - { cwd: jsBuildDir }); + return spawn('python', [ script, util.env.target ], { cwd: buildDir }); }); - gulp.task('cppify', ['bundle'], function() { - // @todo: properly spawn this. This breaks with a space in the path + gulp.task('cppify', ['bundle', 'pip-requirements'], function() { let script = Path.join(__dirname, '..', 'jerryscript', 'tools', 'js2c.py'); - return exec([ - 'python ' + script, - ' --ignore pins.js', + + let p = spawn('python', [ + script, + '--ignore', 'pins.js', '--no-main', - '--dest ' + buildDir, - '--js-source ' + buildDir - ].join(' '), { cwd: buildDir }); + '--dest', buildDir, + '--js-source', buildDir + ], { cwd: buildDir/*, stdio: 'inherit' */ }); + + return p; }); gulp.task('make-build-dir', function() { if (!fs.existsSync(buildDir)) { fs.mkdirSync(buildDir); } - - if (!fs.existsSync(jsBuildDir)) { - fs.mkdirSync(jsBuildDir); - } }); function dependencies(obj) { @@ -133,7 +122,7 @@ module.exports = function(gulp) { for (let i = 0; i < keys.length; i++) { if (list[keys[i]] && !list[keys[i]].missing) { // check for mbedjs.json - var path = list[keys[i]].path + '/mbedjs.json'; + var path = Path.join(list[keys[i]].path, '/mbedjs.json'); try { fs.statSync(path); @@ -204,5 +193,16 @@ module.exports = function(gulp) { }); }); - gulp.task('default', ['generate-cpp']); + gulp.task('build', ['generate-cpp'], function() { + let p = spawn('mbed', [ 'compile', '-m', util.env.target, '-t', 'GCC_ARM' ], { stdio: 'inherit' }); + + return p; + }); + + if (util.env.build) { + gulp.task('default', ['build']); + } + else { + gulp.task('default', ['generate-cpp']); + } }; diff --git a/mbed-js/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp b/mbed-js/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp index edeca99..716754c 100644 --- a/mbed-js/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp +++ b/mbed-js/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp @@ -83,8 +83,8 @@ void jsmbed_js_exit() { void jsmbed_js_launch() { jsmbed_js_init(); - puts(" JerryScript in mbed\r\n"); - puts(" build date: " __DATE__ " \r\n"); + puts(" Mbed.js\r\n"); + puts(" Build date: " __DATE__ " \r\n"); if (load_javascript() == 0) { mbed::js::event_loop(); diff --git a/mbed-js/requirements.txt b/mbed-js/tools/requirements.txt similarity index 87% rename from mbed-js/requirements.txt rename to mbed-js/tools/requirements.txt index 22b6eb4..8a55736 100644 --- a/mbed-js/requirements.txt +++ b/mbed-js/tools/requirements.txt @@ -1,4 +1,3 @@ pycparser<=2.17 # generate_pins.py does not work with 2.18 version simpleeval pycparserext -gen_c_source diff --git a/package.json b/package.json new file mode 100644 index 0000000..eae3fdd --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "mbed-js-v2-example", + "description": "Mbed.js example application", + "author": "Jan Jongboom ", + "repository": "https://github.com/ARMmbed/mbed-js-v2", + "dependencies": { + "gulp": "^3.9.1", + "mbed-js": "file:mbed-js" + } +} diff --git a/source/js/main.js b/source/main.js similarity index 90% rename from source/js/main.js rename to source/main.js index d9fd45b..7b9b5e4 100644 --- a/source/js/main.js +++ b/source/main.js @@ -1,5 +1,7 @@ var led = new DigitalOut(LED1); +blabalabl; + setInterval(function() { led.write(led.read() === 0 ? 1 : 0); }, 500);