Skip to content

Commit 1cbdb9c

Browse files
committed
refactor(build): introduce AngularBuilder facade and clean up many things
1 parent 4c9b8eb commit 1cbdb9c

File tree

8 files changed

+172
-164
lines changed

8 files changed

+172
-164
lines changed

gulpfile.js

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var madge = require('madge');
1212
var merge = require('merge');
1313
var merge2 = require('merge2');
1414
var path = require('path');
15-
var Q = require('q');
1615

1716
var gulpTraceur = require('./tools/transpiler/gulp-traceur');
1817
var clean = require('./tools/build/clean');
@@ -35,11 +34,19 @@ var bundler = require('./tools/build/bundle');
3534
var replace = require('gulp-replace');
3635
var insert = require('gulp-insert');
3736

38-
// dynamic require in build.broccoli.tools so we can bootstrap TypeScript compilation
39-
function missingDynamicBroccoli() {
40-
throw new Error('ERROR: build.broccoli.tools task should have been run before using broccoli');
37+
38+
// dynamic require in build.tools so we can bootstrap TypeScript compilation
39+
function throwToolsBuildMissingError() {
40+
throw new Error('ERROR: build.tools task should have been run before using angularBuilder');
4141
}
42-
var getBroccoli = missingDynamicBroccoli;
42+
43+
var angularBuilder = {
44+
rebuildBrowserDevTree: throwToolsBuildMissingError,
45+
rebuildBrowserProdTree: throwToolsBuildMissingError,
46+
rebuildNodeTree: throwToolsBuildMissingError,
47+
rebuildDartTree: throwToolsBuildMissingError,
48+
cleanup: function() {}
49+
};
4350

4451
// Note: when DART_SDK is not found, all gulp tasks ending with `.dart` will be skipped.
4552

@@ -240,7 +247,7 @@ gulp.task('build/clean.docs', clean(gulp, gulpPlugins, {
240247
// transpile
241248

242249
gulp.task('build/tree.dart', ['build.broccoli.tools'], function() {
243-
return getBroccoli().forDartTree().buildOnce();
250+
return angularBuilder.rebuildDartTree();
244251
});
245252

246253
// ------------
@@ -255,13 +262,6 @@ gulp.task('build/pubspec.dart', pubget.subDir(gulp, gulpPlugins, {
255262
command: DART_SDK.PUB
256263
}));
257264

258-
// ------------
259-
// linknodemodules
260-
261-
gulp.task('build/linknodemodules.js.cjs', linknodemodules(gulp, gulpPlugins, {
262-
dir: CONFIG.dest.js.cjs
263-
}));
264-
265265
// ------------
266266
// dartanalyzer
267267

@@ -464,44 +464,19 @@ gulp.task('test.unit.dart/ci', function (done) {
464464
karma.start({configFile: __dirname + '/karma-dart.conf.js',
465465
singleRun: true, reporters: ['dots'], browsers: getBrowsersFromCLI()}, done);
466466
});
467-
gulp.task('test.unit.cjs/ci', function () {
468-
return gulp.src(CONFIG.test.js.cjs).pipe(jasmine({includeStackTrace: true, timeout: 1000}));
469-
});
470-
gulp.task('test.unit.cjs', ['build.js.cjs'], function () {
471-
//Run tests once
472-
runSequence('test.unit.cjs/ci', function() {});
473-
});
474467

475-
function runNodeJasmineTests() {
476-
var doneDeferred = Q.defer();
477-
var jasmineProcess = fork('./tools/traceur-jasmine', ['dist/js/cjs/angular2/test/**/*_spec.js'], {
478-
stdio: 'inherit'
479-
});
480468

481-
jasmineProcess.on('close', function (code) {
482-
doneDeferred.resolve();
469+
gulp.task('test.unit.cjs/ci', function(done) {
470+
fork('./tools/traceur-jasmine', ['dist/js/cjs/angular2/test/**/*_spec.js'], {
471+
stdio: 'inherit'
472+
}).on('close', function (exitCode) {
473+
done(exitCode);
483474
});
484-
485-
return doneDeferred.promise;
486-
}
487-
488-
gulp.task('test.unit.cjs/ci', runNodeJasmineTests);
489-
490-
gulp.task('test.unit.cjs', ['build.broccoli.tools'], function (done) {
491-
//Run tests once
492-
var nodeBroccoliBuilder = getBroccoli().forNodeTree();
475+
});
493476

494477

495-
nodeBroccoliBuilder.doBuild().then(function() {
496-
gulp.start('build/linknodemodules.js.cjs');
497-
return runNodeJasmineTests();
498-
}).then(function() {
499-
//Watcher to transpile file changed
500-
gulp.watch('modules/**', function(event) {
501-
console.log("fs changes detected", event);
502-
nodeBroccoliBuilder.doBuild().then(runNodeJasmineTests);
503-
});
504-
});
478+
gulp.task('test.unit.cjs', ['test.unit.cjs/ci'], function () {
479+
gulp.watch('modules/**', ['test.unit.cjs/ci']);
505480
});
506481

507482

@@ -636,12 +611,9 @@ gulp.task('build.broccoli.tools', function() {
636611
});
637612

638613
gulp.task('broccoli.js.dev', ['build.broccoli.tools'], function() {
639-
return getBroccoli().forDevTree().buildOnce();
614+
return angularBuilder.rebuildBrowserDevTree();
640615
});
641616

642-
gulp.task('broccoli.js.prod', ['build.broccoli.tools'], function() {
643-
return getBroccoli().forProdTree().buildOnce();
644-
});
645617

646618
gulp.task('build.js.dev', function(done) {
647619
runSequence(
@@ -652,19 +624,27 @@ gulp.task('build.js.dev', function(done) {
652624
);
653625
});
654626

655-
gulp.task('build.js.prod', ['broccoli.js.prod']);
656-
657-
gulp.task('broccoli.js.cjs', ['build.broccoli.tools'], function() {
658-
return getBroccoli().forNodeTree().buildOnce();
627+
gulp.task('build.js.prod', ['build.broccoli.tools'], function() {
628+
return angularBuilder.rebuildBrowserProdTree();
659629
});
660-
gulp.task('build.js.cjs', function(done) {
661-
runSequence(
662-
'broccoli.js.cjs',
663-
'build/linknodemodules.js.cjs',
664-
done
665-
);
630+
631+
632+
var firstBuildJsCjs = true;
633+
634+
gulp.task('build.js.cjs', ['build.broccoli.tools'], function() {
635+
return angularBuilder.rebuildNodeTree().then(function() {
636+
if (firstBuildJsCjs) {
637+
firstBuildJsCjs = false;
638+
console.log('creating node_modules symlink hack');
639+
// linknodemodules is all sync
640+
linknodemodules(gulp, gulpPlugins, {
641+
dir: CONFIG.dest.js.cjs
642+
})();
643+
}
644+
});
666645
});
667646

647+
668648
var bundleConfig = {
669649
paths: {
670650
"*": "dist/js/prod/es6/*.es6",
@@ -789,3 +769,20 @@ gulp.task('build/css.dart', function() {
789769
});
790770

791771
gulp.task('build.material', ['build.js.dev', 'build/css.js.dev']);
772+
773+
774+
gulp.task('cleanup.builder', function() {
775+
angularBuilder.cleanup();
776+
});
777+
778+
779+
// register cleanup listener for ctrl+c/kill used to quit any persistent task (autotest or serve tasks)
780+
process.on('SIGINT', function() {
781+
gulp.start('cleanup.builder');
782+
process.exit();
783+
});
784+
785+
// register cleanup listener for all non-persistent tasks
786+
process.on('beforeExit', function() {
787+
gulp.start('cleanup.builder');
788+
});

tools/broccoli/angular_builder.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
var broccoli = require('broccoli');
2+
var fse = require('fs-extra');
3+
var makeBrowserTree = require('./trees/browser_tree');
4+
var makeNodeTree = require('./trees/node_tree');
5+
var makeDartTree = require('./trees/dart_tree');
6+
var path = require('path');
7+
var printSlowTrees = require('broccoli-slow-trees');
8+
var Q = require('q');
9+
10+
11+
/**
12+
* BroccoliBuilder facade for all of our build pipelines.
13+
*/
14+
export class AngularBuilder {
15+
private nodeBuilder: BroccoliBuilder;
16+
private browserDevBuilder: BroccoliBuilder;
17+
private browserProdBuilder: BroccoliBuilder;
18+
private dartBuilder: BroccoliBuilder;
19+
20+
21+
constructor(private outputPath: string) {}
22+
23+
24+
public rebuildBrowserDevTree(): Promise<BuildResult> {
25+
this.browserDevBuilder = this.browserDevBuilder || this.makeBrowserDevBuilder();
26+
return this.rebuild(this.browserDevBuilder);
27+
}
28+
29+
30+
public rebuildBrowserProdTree(): Promise<BuildResult> {
31+
this.browserProdBuilder = this.browserProdBuilder || this.makeBrowserProdBuilder();
32+
return this.rebuild(this.browserProdBuilder);
33+
}
34+
35+
36+
public rebuildNodeTree(): Promise<BuildResult> {
37+
this.nodeBuilder = this.nodeBuilder || this.makeNodeBuilder();
38+
return this.rebuild(this.nodeBuilder);
39+
}
40+
41+
42+
public rebuildDartTree(): Promise<BuildResult> {
43+
this.dartBuilder = this.dartBuilder || this.makeDartBuilder();
44+
return this.rebuild(this.dartBuilder);
45+
}
46+
47+
48+
cleanup(): Promise<any> {
49+
return Q.all([
50+
this.nodeBuilder && this.nodeBuilder.cleanup(),
51+
this.browserDevBuilder && this.browserDevBuilder.cleanup(),
52+
this.browserProdBuilder && this.browserProdBuilder.cleanup()
53+
]);
54+
}
55+
56+
57+
private makeBrowserDevBuilder(): BroccoliBuilder {
58+
let tree = makeBrowserTree({name: 'dev', typeAssertions: true},
59+
path.join(this.outputPath, 'js', 'dev'));
60+
return new broccoli.Builder(tree);
61+
}
62+
63+
64+
private makeBrowserProdBuilder(): BroccoliBuilder {
65+
let tree = makeBrowserTree({name: 'prod', typeAssertions: false},
66+
path.join(this.outputPath, 'js', 'prod'));
67+
return new broccoli.Builder(tree);
68+
}
69+
70+
71+
private makeNodeBuilder(): BroccoliBuilder {
72+
let tree = makeNodeTree(path.join(this.outputPath, 'js', 'cjs'));
73+
return new broccoli.Builder(tree);
74+
}
75+
76+
77+
private makeDartBuilder(): BroccoliBuilder {
78+
let tree = makeDartTree(path.join(this.outputPath, 'dart'));
79+
return new broccoli.Builder(tree);
80+
}
81+
82+
83+
private rebuild(builder) {
84+
return builder.build()
85+
.then((result) => { printSlowTrees(result.graph); })
86+
.catch((err) => {
87+
console.error(err.toString());
88+
// Should show file and line/col if present
89+
if (err.file) {
90+
console.error('File: ' + err.file);
91+
}
92+
if (err.stack) {
93+
console.error(err.stack);
94+
}
95+
throw err;
96+
});
97+
}
98+
}

tools/broccoli/broccoli.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ interface BroccoliBuilder {
7373
* Cleans up the whole build tree by calling `.cleanup()` method on all trees that are part of the
7474
* pipeline.
7575
*/
76-
cleanup();
76+
cleanup(): Promise<any>;
7777
}
7878

7979

tools/broccoli/broccoli_builder.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

tools/broccoli/trees/multi_copy.ts renamed to tools/broccoli/multi_copy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// <reference path="../../typings/node/node.d.ts" />
2-
/// <reference path="../../typings/fs-extra/fs-extra.d.ts" />
1+
/// <reference path="../typings/node/node.d.ts" />
2+
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
33
import Writer = require('broccoli-writer');
44
import fs = require('fs');
55
import fsx = require('fs-extra');

0 commit comments

Comments
 (0)