Skip to content

Commit

Permalink
Gulpfile should belong in main project, not in mbed-js
Browse files Browse the repository at this point in the history
  • Loading branch information
janjongboom committed Dec 13, 2017
1 parent f936c9b commit e4181e6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/

2 changes: 2 additions & 0 deletions .mbedignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/

5 changes: 5 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const gulp = require('gulp');

require('./mbed-js/jerryscript-mbed-gulp/main')(gulp);
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
5 changes: 0 additions & 5 deletions mbed-js/Gulpfile.js

This file was deleted.

80 changes: 40 additions & 40 deletions mbed-js/jerryscript-mbed-gulp/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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 */ }
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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']);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pycparser<=2.17 # generate_pins.py does not work with 2.18 version
simpleeval
pycparserext
gen_c_source
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "mbed-js-v2-example",
"description": "Mbed.js example application",
"author": "Jan Jongboom <[email protected]>",
"repository": "https://github.com/ARMmbed/mbed-js-v2",
"dependencies": {
"gulp": "^3.9.1",
"mbed-js": "file:mbed-js"
}
}
2 changes: 2 additions & 0 deletions source/js/main.js → source/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var led = new DigitalOut(LED1);

blabalabl;

setInterval(function() {
led.write(led.read() === 0 ? 1 : 0);
}, 500);

0 comments on commit e4181e6

Please sign in to comment.