2014-09-18 17:56:38 -04:00
|
|
|
var gulp = require('gulp');
|
|
|
|
var rename = require('gulp-rename');
|
|
|
|
var watch = require('gulp-watch');
|
|
|
|
var shell = require('gulp-shell');
|
|
|
|
var mergeStreams = require('event-stream').merge;
|
|
|
|
var connect = require('gulp-connect');
|
|
|
|
var clean = require('gulp-rimraf');
|
|
|
|
var runSequence = require('run-sequence');
|
|
|
|
var glob = require('glob');
|
|
|
|
var ejs = require('gulp-ejs');
|
|
|
|
var path = require('path');
|
2014-09-25 20:55:46 -04:00
|
|
|
var through2 = require('through2');
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
// import js2dart build tasks
|
2014-09-22 01:02:28 -04:00
|
|
|
var js2dartTasks = require('./tools/js2dart/gulp-tasks');
|
|
|
|
js2dartTasks.install(gulp);
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
var js2es5Options = {
|
2014-09-18 17:56:38 -04:00
|
|
|
annotations: true, // parse annotations
|
|
|
|
types: true, // parse types
|
|
|
|
script: false, // parse as a module
|
|
|
|
modules: 'register',
|
|
|
|
typeAssertionModule: 'assert',
|
|
|
|
typeAssertions: true,
|
2014-09-22 01:02:28 -04:00
|
|
|
moduleName: true
|
2014-09-18 17:56:38 -04:00
|
|
|
};
|
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
var js2dartOptions = {
|
|
|
|
annotations: true, // parse annotations
|
|
|
|
types: true, // parse types
|
|
|
|
script: false, // parse as a module
|
|
|
|
outputLanguage: 'dart',
|
|
|
|
moduleName: true
|
|
|
|
};
|
|
|
|
|
2014-09-18 17:56:38 -04:00
|
|
|
var traceur = require('./tools/js2dart/gulp-traceur');
|
2014-09-24 23:20:59 -04:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// rtts-assert and traceur runtime
|
|
|
|
|
|
|
|
gulp.task('jsRuntime/build', function() {
|
2014-09-25 20:55:46 -04:00
|
|
|
return createJsRuntimeTask(false);
|
2014-09-24 23:20:59 -04:00
|
|
|
});
|
|
|
|
|
2014-09-25 20:55:46 -04:00
|
|
|
function createJsRuntimeTask(isWatch) {
|
2014-09-24 23:20:59 -04:00
|
|
|
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
|
|
|
|
var rttsAssert = srcFn('tools/rtts-assert/src/assert.js')
|
|
|
|
.pipe(traceur(js2es5Options))
|
|
|
|
.pipe(gulp.dest('build/js'));
|
|
|
|
var traceurRuntime = srcFn('tools/js2dart/node_modules/traceur/bin/traceur-runtime.js')
|
|
|
|
.pipe(gulp.dest('build/js'));
|
|
|
|
return mergeStreams(rttsAssert, traceurRuntime);
|
|
|
|
}
|
2014-09-18 17:56:38 -04:00
|
|
|
|
|
|
|
// -----------------------
|
|
|
|
// modules
|
|
|
|
var sourceTypeConfigs = {
|
|
|
|
dart: {
|
|
|
|
compiler: function() {
|
2014-09-24 23:20:59 -04:00
|
|
|
return traceur(js2dartOptions, true);
|
2014-09-18 17:56:38 -04:00
|
|
|
},
|
2014-09-19 19:38:37 -04:00
|
|
|
transpileSrc: ['modules/**/*.js'],
|
2014-09-18 17:56:38 -04:00
|
|
|
htmlSrc: ['modules/*/src/**/*.html'],
|
2014-09-19 19:38:37 -04:00
|
|
|
copySrc: ['modules/**/*.dart', 'modules/**/*.yaml'],
|
2014-09-18 17:56:38 -04:00
|
|
|
outputDir: 'build/dart',
|
|
|
|
outputExt: 'dart',
|
2014-09-25 20:55:46 -04:00
|
|
|
mimeType: 'application/dart',
|
|
|
|
postProcess: function(file, done) {
|
|
|
|
if (file.path.match(/pubspec\.yaml/)) {
|
|
|
|
console.log(file.path);
|
|
|
|
shell.task(['pub get'], {
|
|
|
|
cwd: path.dirname(file.path)
|
|
|
|
})().on('end', done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 17:56:38 -04:00
|
|
|
},
|
|
|
|
js: {
|
|
|
|
compiler: function() {
|
2014-09-24 23:20:59 -04:00
|
|
|
return traceur(js2es5Options, true);
|
2014-09-18 17:56:38 -04:00
|
|
|
},
|
2014-09-19 19:38:37 -04:00
|
|
|
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
|
2014-09-18 17:56:38 -04:00
|
|
|
htmlSrc: ['modules/*/src/**/*.html'],
|
2014-09-19 19:38:37 -04:00
|
|
|
copySrc: ['modules/**/*.es5'],
|
2014-09-18 17:56:38 -04:00
|
|
|
outputDir: 'build/js',
|
2014-09-25 20:55:46 -04:00
|
|
|
outputExt: 'js',
|
|
|
|
postProcess: function() {
|
|
|
|
|
|
|
|
}
|
2014-09-18 17:56:38 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-25 20:55:46 -04:00
|
|
|
|
2014-09-18 17:56:38 -04:00
|
|
|
gulp.task('modules/clean', function() {
|
|
|
|
return gulp.src('build', {read: false})
|
|
|
|
.pipe(clean());
|
|
|
|
});
|
|
|
|
|
2014-09-25 20:55:46 -04:00
|
|
|
gulp.task('modules/build.dart/src', function() {
|
|
|
|
return createModuleTask(sourceTypeConfigs.dart, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('modules/build.dart/analyzer', function() {
|
|
|
|
var baseDir = sourceTypeConfigs.dart.outputDir;
|
|
|
|
var files = [].slice.call(glob.sync('*/lib/*.dart', {
|
|
|
|
cwd: baseDir
|
|
|
|
}));
|
|
|
|
files = files.filter(function(fileName) {
|
|
|
|
return fileName.match(/(\w+)\/lib\/\1/);
|
|
|
|
});
|
|
|
|
var commands = files.map(function(fileName) {
|
|
|
|
return 'dartanalyzer '+baseDir+'/'+fileName
|
|
|
|
});
|
|
|
|
return shell.task(commands)();
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('modules/build.dart', function(done) {
|
|
|
|
runSequence('modules/build.dart/src', 'modules/build.dart/analyzer', done);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('modules/build.js', function() {
|
|
|
|
return createModuleTask(sourceTypeConfigs.js, false);
|
|
|
|
});
|
|
|
|
|
2014-09-19 19:38:37 -04:00
|
|
|
function renameSrcToLib(file) {
|
|
|
|
file.dirname = file.dirname.replace(/\bsrc\b/, 'lib');
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:56:38 -04:00
|
|
|
function createModuleTask(sourceTypeConfig, isWatch) {
|
|
|
|
var start = isWatch ? watch : gulp.src.bind(gulp);
|
2014-09-25 20:55:46 -04:00
|
|
|
var transpile = start(sourceTypeConfig.transpileSrc)
|
|
|
|
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
|
|
|
|
.pipe(rename(renameSrcToLib))
|
|
|
|
.pipe(sourceTypeConfig.compiler())
|
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
|
|
|
var copy = start(sourceTypeConfig.copySrc)
|
|
|
|
.pipe(rename(renameSrcToLib))
|
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
|
|
|
// TODO: provide the list of files to the template
|
|
|
|
// automatically!
|
|
|
|
var html = start(sourceTypeConfig.htmlSrc)
|
|
|
|
.pipe(rename(renameSrcToLib))
|
|
|
|
.pipe(ejs({
|
|
|
|
type: sourceTypeConfig.outputExt
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
|
|
|
|
|
|
|
var s = mergeStreams(transpile, copy, html);
|
|
|
|
return s.pipe(through2.obj(function(file, enc, done) {
|
|
|
|
sourceTypeConfig.postProcess(file, done);
|
|
|
|
}));
|
2014-09-18 17:56:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------
|
|
|
|
// WEB SERVER
|
|
|
|
gulp.task('serve', connect.server({
|
|
|
|
root: [__dirname+'/build'],
|
|
|
|
port: 8000,
|
|
|
|
livereload: false,
|
|
|
|
open: false,
|
|
|
|
middleware: function() {
|
|
|
|
return [function(req, resp, next){
|
|
|
|
if (req.url.match(/\.dart$/)) {
|
|
|
|
resp.setHeader("Content-Type", "application/dart");
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// --------------
|
|
|
|
// general targets
|
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
gulp.task('clean', ['js2dart/clean', 'modules/clean']);
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-09-22 01:02:28 -04:00
|
|
|
gulp.task('build', function() {
|
|
|
|
return runSequence(
|
|
|
|
// sequential
|
2014-09-24 23:20:59 -04:00
|
|
|
'js2dart/build',
|
2014-09-22 01:02:28 -04:00
|
|
|
// parallel
|
2014-09-24 23:20:59 -04:00
|
|
|
['jsRuntime/build', 'modules/build.dart', 'modules/build.js']
|
2014-09-18 17:56:38 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2014-09-22 01:02:28 -04:00
|
|
|
gulp.task('watch', function() {
|
2014-09-24 23:20:59 -04:00
|
|
|
runSequence('js2dart/test/watch');
|
2014-09-22 01:02:28 -04:00
|
|
|
var js2dartWatch = watch(js2dartTasks.paths.js2dartSrc, function(_, done) {
|
|
|
|
runSequence(
|
|
|
|
// sequential
|
|
|
|
'js2dart/build', 'js2dart/test',
|
|
|
|
// parallel
|
2014-09-24 23:20:59 -04:00
|
|
|
['jsRuntime/build', 'modules/build.dart', 'modules/build.js'],
|
2014-09-22 01:02:28 -04:00
|
|
|
done);
|
|
|
|
});
|
2014-09-25 20:55:46 -04:00
|
|
|
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
|
|
|
|
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
|
|
|
|
return mergeStreams(js2dartWatch, dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
|
2014-09-18 17:56:38 -04:00
|
|
|
});
|