2014-10-28 06:45:48 -04:00
|
|
|
var benchpress = require('angular-benchpress/lib/cli');
|
2014-09-18 17:56:38 -04:00
|
|
|
var clean = require('gulp-rimraf');
|
2014-10-28 06:45:48 -04:00
|
|
|
var connect = require('gulp-connect');
|
2014-09-18 17:56:38 -04:00
|
|
|
var ejs = require('gulp-ejs');
|
2014-10-28 06:45:48 -04:00
|
|
|
var es = require('event-stream');
|
2014-09-28 16:55:01 -04:00
|
|
|
var file2moduleName = require('./file2modulename');
|
2014-10-02 15:27:01 -04:00
|
|
|
var fs = require('fs');
|
2014-10-28 06:45:48 -04:00
|
|
|
var glob = require('glob');
|
|
|
|
var gulp = require('gulp');
|
|
|
|
var merge = require('merge');
|
|
|
|
var mergeStreams = require('event-stream').merge;
|
2014-10-02 15:27:01 -04:00
|
|
|
var path = require('path');
|
2014-09-29 17:20:23 -04:00
|
|
|
var Q = require('q');
|
2014-10-28 06:45:48 -04:00
|
|
|
var readline = require('readline');
|
|
|
|
var rename = require('gulp-rename');
|
|
|
|
var runSequence = require('run-sequence');
|
|
|
|
var shell = require('gulp-shell');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var through2 = require('through2');
|
|
|
|
var watch = require('gulp-watch');
|
2014-11-04 19:37:28 -05:00
|
|
|
var changed = require('gulp-changed');
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
var js2es5Options = {
|
2014-10-30 13:52:32 -04:00
|
|
|
sourceMaps: true,
|
2014-09-18 17:56:38 -04:00
|
|
|
annotations: true, // parse annotations
|
|
|
|
types: true, // parse types
|
|
|
|
script: false, // parse as a module
|
2014-10-08 16:15:38 -04:00
|
|
|
modules: 'instantiate'
|
|
|
|
};
|
|
|
|
|
|
|
|
var js2es5OptionsProd = merge(true, js2es5Options, {
|
|
|
|
typeAssertions: false
|
|
|
|
});
|
|
|
|
|
|
|
|
var js2es5OptionsDev = merge(true, js2es5Options, {
|
2014-09-26 20:36:57 -04:00
|
|
|
typeAssertionModule: 'rtts_assert/rtts_assert',
|
2014-09-25 17:04:46 -04:00
|
|
|
typeAssertions: true
|
2014-10-08 16:15:38 -04:00
|
|
|
});
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
var js2dartOptions = {
|
2014-10-30 13:52:32 -04:00
|
|
|
sourceMaps: true,
|
2014-09-24 23:20:59 -04:00
|
|
|
annotations: true, // parse annotations
|
|
|
|
types: true, // parse types
|
|
|
|
script: false, // parse as a module
|
2014-09-25 17:04:46 -04:00
|
|
|
outputLanguage: 'dart'
|
2014-09-24 23:20:59 -04:00
|
|
|
};
|
|
|
|
|
2014-11-04 19:37:28 -05:00
|
|
|
var PUB_CMD = process.platform === 'win32' ? 'pub.bat' : 'pub';
|
|
|
|
|
2014-09-26 20:39:27 -04:00
|
|
|
var gulpTraceur = require('./tools/transpiler/gulp-traceur');
|
2014-09-25 17:04:46 -04:00
|
|
|
|
2014-09-24 23:20:59 -04:00
|
|
|
// ---------
|
2014-09-28 16:55:01 -04:00
|
|
|
// traceur runtime
|
2014-09-24 23:20:59 -04:00
|
|
|
|
|
|
|
gulp.task('jsRuntime/build', function() {
|
2014-10-08 16:15:38 -04:00
|
|
|
var traceurRuntime = gulp.src([
|
|
|
|
gulpTraceur.RUNTIME_PATH,
|
|
|
|
"node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js",
|
2014-11-04 18:05:33 -05:00
|
|
|
"node_modules/systemjs/dist/system.src.js",
|
2014-10-08 16:15:38 -04:00
|
|
|
"node_modules/systemjs/lib/extension-register.js"
|
|
|
|
]).pipe(gulp.dest('build/js'));
|
2014-09-26 20:36:57 -04:00
|
|
|
return traceurRuntime;
|
2014-09-28 16:55:01 -04:00
|
|
|
});
|
2014-09-18 17:56:38 -04:00
|
|
|
|
|
|
|
// -----------------------
|
|
|
|
// modules
|
|
|
|
var sourceTypeConfigs = {
|
|
|
|
dart: {
|
2014-09-19 19:38:37 -04:00
|
|
|
transpileSrc: ['modules/**/*.js'],
|
2014-09-18 17:56:38 -04:00
|
|
|
htmlSrc: ['modules/*/src/**/*.html'],
|
2014-10-02 15:27:01 -04:00
|
|
|
copySrc: ['modules/**/*.dart'],
|
2014-09-18 17:56:38 -04:00
|
|
|
outputDir: 'build/dart',
|
|
|
|
outputExt: 'dart',
|
2014-10-02 15:27:01 -04:00
|
|
|
mimeType: 'application/dart'
|
2014-09-18 17:56:38 -04:00
|
|
|
},
|
|
|
|
js: {
|
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-10-02 15:27:01 -04:00
|
|
|
outputExt: 'js'
|
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() {
|
2014-10-08 16:15:38 -04:00
|
|
|
return createModuleTask(merge(sourceTypeConfigs.dart, {compilerOptions: js2dartOptions}));
|
2014-09-25 20:55:46 -04:00
|
|
|
});
|
|
|
|
|
2014-10-02 15:27:01 -04:00
|
|
|
gulp.task('modules/build.dart/pubspec', function(done) {
|
|
|
|
var outputDir = sourceTypeConfigs.dart.outputDir;
|
|
|
|
return gulp.src('modules/*/pubspec.yaml')
|
2014-11-04 19:37:28 -05:00
|
|
|
.pipe(changed(outputDir)) // Only forward files that changed.
|
|
|
|
.pipe(gulpDestWithForward(outputDir))
|
2014-10-02 15:27:01 -04:00
|
|
|
.pipe(through2.obj(function(file, enc, done) {
|
2014-11-04 19:37:28 -05:00
|
|
|
// After a `pubspec.yml` is copied, we run `pub install`.
|
|
|
|
spawn(PUB_CMD, ['get'], {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
cwd: path.dirname(file.path)
|
|
|
|
}).on('close', done);
|
2014-10-02 15:27:01 -04:00
|
|
|
}));
|
2014-09-25 20:55:46 -04:00
|
|
|
});
|
|
|
|
|
2014-10-02 15:27:01 -04:00
|
|
|
gulp.task('modules/build.dart', ['modules/build.dart/src', 'modules/build.dart/pubspec']);
|
2014-09-25 20:55:46 -04:00
|
|
|
|
2014-10-08 16:15:38 -04:00
|
|
|
gulp.task('modules/build.dev.js', function() {
|
|
|
|
return createModuleTask(merge(true, sourceTypeConfigs.js, {compilerOptions: js2es5OptionsDev}));
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('modules/build.prod.js', function() {
|
|
|
|
return createModuleTask(merge(true, sourceTypeConfigs.js, {compilerOptions: js2es5OptionsProd}));
|
2014-09-25 20:55:46 -04:00
|
|
|
});
|
|
|
|
|
2014-09-19 19:38:37 -04:00
|
|
|
function renameSrcToLib(file) {
|
|
|
|
file.dirname = file.dirname.replace(/\bsrc\b/, 'lib');
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:38 -04:00
|
|
|
function renameEs5ToJs(file) {
|
|
|
|
if (file.extname == '.es5') {
|
|
|
|
file.extname = '.js';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
function createModuleTask(sourceTypeConfig) {
|
|
|
|
var transpile = gulp.src(sourceTypeConfig.transpileSrc)
|
2014-09-25 20:55:46 -04:00
|
|
|
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
|
|
|
|
.pipe(rename(renameSrcToLib))
|
2014-09-28 16:55:01 -04:00
|
|
|
.pipe(gulpTraceur(sourceTypeConfig.compilerOptions, file2moduleName))
|
2014-09-25 20:55:46 -04:00
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
2014-09-28 16:55:01 -04:00
|
|
|
var copy = gulp.src(sourceTypeConfig.copySrc)
|
2014-09-25 20:55:46 -04:00
|
|
|
.pipe(rename(renameSrcToLib))
|
2014-10-08 16:15:38 -04:00
|
|
|
.pipe(rename(renameEs5ToJs))
|
2014-09-25 20:55:46 -04:00
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
|
|
|
// TODO: provide the list of files to the template
|
|
|
|
// automatically!
|
2014-09-28 16:55:01 -04:00
|
|
|
var html = gulp.src(sourceTypeConfig.htmlSrc)
|
2014-09-25 20:55:46 -04:00
|
|
|
.pipe(rename(renameSrcToLib))
|
|
|
|
.pipe(ejs({
|
|
|
|
type: sourceTypeConfig.outputExt
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(sourceTypeConfig.outputDir));
|
|
|
|
|
2014-10-02 15:27:01 -04:00
|
|
|
return mergeStreams(transpile, copy, html);
|
2014-09-18 17:56:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 19:37:28 -05:00
|
|
|
|
|
|
|
// Act as `gulp.dest()` but does forward the files to the next stream.
|
|
|
|
// I believe this is how `gulp.dest` should work.
|
|
|
|
function gulpDestWithForward(destDir) {
|
|
|
|
var stream = gulp.dest(destDir);
|
|
|
|
var originalTransform = stream._transform;
|
|
|
|
|
|
|
|
stream._transform = function(file, enc, done) {
|
|
|
|
return originalTransform.call(this, file, enc, function() {
|
|
|
|
stream.push(file);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2014-10-02 15:27:01 -04:00
|
|
|
// ------------------
|
|
|
|
// ANALYZE
|
|
|
|
|
|
|
|
gulp.task('analyze/dartanalyzer', function(done) {
|
|
|
|
var pubSpecs = [].slice.call(glob.sync('build/dart/*/pubspec.yaml', {
|
|
|
|
cwd: __dirname
|
|
|
|
}));
|
|
|
|
var tempFile = '_analyzer.dart';
|
|
|
|
// analyze in parallel!
|
|
|
|
return Q.all(pubSpecs.map(function(pubSpecFile) {
|
|
|
|
var dir = path.dirname(pubSpecFile);
|
|
|
|
var srcFiles = [].slice.call(glob.sync('lib/**/*.dart', {
|
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var testFiles = [].slice.call(glob.sync('test/**/*_spec.dart', {
|
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var analyzeFile = ['library _analyzer;'];
|
|
|
|
srcFiles.concat(testFiles).forEach(function(fileName, index) {
|
|
|
|
if (fileName !== tempFile) {
|
|
|
|
analyzeFile.push('import "./'+fileName+'" as mod'+index+';');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fs.writeFileSync(path.join(dir, tempFile), analyzeFile.join('\n'));
|
|
|
|
var defer = Q.defer();
|
|
|
|
analyze(dir, defer.makeNodeResolver());
|
|
|
|
return defer.promise;
|
|
|
|
}));
|
|
|
|
|
|
|
|
function analyze(dirName, done) {
|
2014-10-27 12:32:55 -04:00
|
|
|
var dartanalyzerCmd = (process.platform === "win32" ? "dartanalyzer.bat" : "dartanalyzer");
|
|
|
|
var stream = spawn(dartanalyzerCmd, ['--fatal-warnings', tempFile], {
|
2014-10-02 15:27:01 -04:00
|
|
|
// inherit stdin and stderr, but filter stdout
|
|
|
|
stdio: [process.stdin, 'pipe', process.stderr],
|
|
|
|
cwd: dirName
|
|
|
|
});
|
|
|
|
// Filter out unused imports from our generated file.
|
|
|
|
// We don't reexports from the generated file
|
|
|
|
// as this could lead to name clashes when two files
|
|
|
|
// export the same thing.
|
2014-10-28 06:45:48 -04:00
|
|
|
var rl = readline.createInterface({
|
2014-10-02 15:27:01 -04:00
|
|
|
input: stream.stdout,
|
|
|
|
output: process.stdout,
|
|
|
|
terminal: false
|
|
|
|
});
|
|
|
|
var hintCount = 0;
|
|
|
|
rl.on('line', function(line) {
|
|
|
|
if (line.match(/Unused import .*_analyzer\.dart/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (line.match(/\[hint\]/)) {
|
|
|
|
hintCount++;
|
|
|
|
}
|
|
|
|
console.log(dirName + ':' + line);
|
|
|
|
});
|
|
|
|
stream.on('close', function(code) {
|
|
|
|
var error;
|
|
|
|
if (code !== 0) {
|
|
|
|
error = new Error('Dartanalyzer failed with exit code ' + code);
|
|
|
|
}
|
|
|
|
if (hintCount > 0) {
|
|
|
|
error = new Error('Dartanalyzer showed hints');
|
|
|
|
}
|
|
|
|
done(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-10-08 16:15:38 -04:00
|
|
|
|
|
|
|
// ------------------
|
2014-10-14 16:00:35 -04:00
|
|
|
// BENCHMARKS JS
|
2014-10-08 16:15:38 -04:00
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
gulp.task('benchmarks/build.benchpress.js', function () {
|
2014-10-08 16:15:38 -04:00
|
|
|
benchpress.build({
|
2014-10-14 16:00:35 -04:00
|
|
|
benchmarksPath: 'build/js/benchmarks/lib',
|
|
|
|
buildPath: 'build/benchpress/js'
|
2014-10-08 16:15:38 -04:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
gulp.task('benchmarks/build.js', function() {
|
2014-10-08 16:15:38 -04:00
|
|
|
runSequence(
|
|
|
|
['jsRuntime/build', 'modules/build.prod.js'],
|
2014-10-14 16:00:35 -04:00
|
|
|
'benchmarks/build.benchpress.js'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------
|
|
|
|
// BENCHMARKS DART
|
|
|
|
|
|
|
|
gulp.task('benchmarks/build.dart2js.dart', function () {
|
|
|
|
return gulp.src([
|
|
|
|
"build/dart/benchmarks/lib/**/benchmark.dart"
|
|
|
|
]).pipe(shell(['dart2js --package-root="build/dart/benchmarks/packages" -o "<%= file.path %>.js" <%= file.path %>']));
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('benchmarks/create-bpconf.dart', function () {
|
|
|
|
var bpConfContent = "module.exports = function(c) {c.set({scripts: [{src: 'benchmark.dart.js'}]});}";
|
|
|
|
var createBpConfJs = es.map(function(file, cb) {
|
|
|
|
var dir = path.dirname(file.path);
|
|
|
|
fs.writeFileSync(path.join(dir, "bp.conf.js"), bpConfContent);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
|
|
|
|
return gulp.src([
|
|
|
|
"build/dart/benchmarks/lib/**/benchmark.dart"
|
|
|
|
]).pipe(createBpConfJs);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('benchmarks/build.benchpress.dart', function () {
|
|
|
|
benchpress.build({
|
|
|
|
benchmarksPath: 'build/dart/benchmarks/lib',
|
|
|
|
buildPath: 'build/benchpress/dart'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('benchmarks/build.dart', function() {
|
|
|
|
runSequence(
|
|
|
|
'modules/build.dart',
|
|
|
|
'benchmarks/build.dart2js.dart',
|
|
|
|
'benchmarks/create-bpconf.dart',
|
|
|
|
'benchmarks/build.benchpress.dart'
|
2014-10-08 16:15:38 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-18 17:56:38 -04:00
|
|
|
// ------------------
|
|
|
|
// WEB SERVER
|
2014-10-08 16:15:38 -04:00
|
|
|
|
|
|
|
gulp.task('serve', function() {
|
|
|
|
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();
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-09-18 17:56:38 -04:00
|
|
|
|
|
|
|
// --------------
|
|
|
|
// general targets
|
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
gulp.task('clean', ['modules/clean']);
|
2014-09-18 17:56:38 -04:00
|
|
|
|
2014-10-02 15:27:01 -04:00
|
|
|
gulp.task('build', function(done) {
|
|
|
|
runSequence(
|
|
|
|
// parallel
|
2014-10-08 16:15:38 -04:00
|
|
|
['jsRuntime/build', 'modules/build.dart', 'modules/build.dev.js'],
|
2014-10-02 15:27:01 -04:00
|
|
|
// sequential
|
|
|
|
'analyze/dartanalyzer'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('analyze', function(done) {
|
|
|
|
runSequence('analyze/dartanalyzer');
|
|
|
|
});
|