refactor(build): introduce AngularBuilder facade and clean up many things

This commit is contained in:
Igor Minar
2015-04-26 00:31:53 -07:00
parent 4c9b8ebb0c
commit 1cbdb9cd17
8 changed files with 172 additions and 164 deletions

View File

@ -0,0 +1,98 @@
var broccoli = require('broccoli');
var fse = require('fs-extra');
var makeBrowserTree = require('./trees/browser_tree');
var makeNodeTree = require('./trees/node_tree');
var makeDartTree = require('./trees/dart_tree');
var path = require('path');
var printSlowTrees = require('broccoli-slow-trees');
var Q = require('q');
/**
* BroccoliBuilder facade for all of our build pipelines.
*/
export class AngularBuilder {
private nodeBuilder: BroccoliBuilder;
private browserDevBuilder: BroccoliBuilder;
private browserProdBuilder: BroccoliBuilder;
private dartBuilder: BroccoliBuilder;
constructor(private outputPath: string) {}
public rebuildBrowserDevTree(): Promise<BuildResult> {
this.browserDevBuilder = this.browserDevBuilder || this.makeBrowserDevBuilder();
return this.rebuild(this.browserDevBuilder);
}
public rebuildBrowserProdTree(): Promise<BuildResult> {
this.browserProdBuilder = this.browserProdBuilder || this.makeBrowserProdBuilder();
return this.rebuild(this.browserProdBuilder);
}
public rebuildNodeTree(): Promise<BuildResult> {
this.nodeBuilder = this.nodeBuilder || this.makeNodeBuilder();
return this.rebuild(this.nodeBuilder);
}
public rebuildDartTree(): Promise<BuildResult> {
this.dartBuilder = this.dartBuilder || this.makeDartBuilder();
return this.rebuild(this.dartBuilder);
}
cleanup(): Promise<any> {
return Q.all([
this.nodeBuilder && this.nodeBuilder.cleanup(),
this.browserDevBuilder && this.browserDevBuilder.cleanup(),
this.browserProdBuilder && this.browserProdBuilder.cleanup()
]);
}
private makeBrowserDevBuilder(): BroccoliBuilder {
let tree = makeBrowserTree({name: 'dev', typeAssertions: true},
path.join(this.outputPath, 'js', 'dev'));
return new broccoli.Builder(tree);
}
private makeBrowserProdBuilder(): BroccoliBuilder {
let tree = makeBrowserTree({name: 'prod', typeAssertions: false},
path.join(this.outputPath, 'js', 'prod'));
return new broccoli.Builder(tree);
}
private makeNodeBuilder(): BroccoliBuilder {
let tree = makeNodeTree(path.join(this.outputPath, 'js', 'cjs'));
return new broccoli.Builder(tree);
}
private makeDartBuilder(): BroccoliBuilder {
let tree = makeDartTree(path.join(this.outputPath, 'dart'));
return new broccoli.Builder(tree);
}
private rebuild(builder) {
return builder.build()
.then((result) => { printSlowTrees(result.graph); })
.catch((err) => {
console.error(err.toString());
// Should show file and line/col if present
if (err.file) {
console.error('File: ' + err.file);
}
if (err.stack) {
console.error(err.stack);
}
throw err;
});
}
}

View File

@ -73,7 +73,7 @@ interface BroccoliBuilder {
* Cleans up the whole build tree by calling `.cleanup()` method on all trees that are part of the
* pipeline.
*/
cleanup();
cleanup(): Promise<any>;
}

View File

@ -1,89 +0,0 @@
var broccoli = require('broccoli');
var destCopy = require('./broccoli-dest-copy');
var fse = require('fs-extra');
var makeBrowserTree = require('./trees/browser_tree');
var makeNodeTree = require('./trees/node_tree');
var makeDartTree = require('./trees/dart_tree');
var path = require('path');
var printSlowTrees = require('broccoli-slow-trees');
/**
* Helper for building with broccoli.
*/
export class BroccoliBuilder {
private builder;
private broccoliExecuted: {[s: string]: boolean} = {};
// Named constructors
static forDevTree(): BroccoliBuilder {
return new BroccoliBuilder(makeBrowserTree({name: 'dev', typeAssertions: true}),
path.join('js', 'dev'));
}
static forNodeTree(): BroccoliBuilder {
return new BroccoliBuilder(makeNodeTree(), path.join('js', 'cjs'));
}
static forProdTree(): BroccoliBuilder {
return new BroccoliBuilder(makeBrowserTree({name: 'prod', typeAssertions: false}),
path.join('js', 'prod'));
}
static forDartTree(): BroccoliBuilder { return new BroccoliBuilder(makeDartTree(), 'dart'); }
constructor(tree, private outputRoot: string) {
if (this.broccoliExecuted[outputRoot]) {
throw new Error('The broccoli task can be called only once for outputRoot ' + outputRoot);
}
this.broccoliExecuted[outputRoot] = true;
var distPath = path.join('dist', outputRoot);
// We do a clean build every time to avoid stale outputs.
// Broccoli's cache folders allow it to remain incremental without reading this dir.
fse.removeSync(distPath);
tree = destCopy(tree, 'dist');
this.builder = new broccoli.Builder(tree);
}
doBuild(): Promise<any> {
return this.builder.build()
.then(hash =>
{
console.log('=== Stats for %s (total: %ds) ===', this.outputRoot,
Math.round(hash.totalTime / 1000000) / 1000);
printSlowTrees(hash.graph);
})
.catch(err => {
console.log(err.toString());
// Should show file and line/col if present
if (err.file) {
console.error('File: ' + err.file);
}
if (err.stack) {
console.error(err.stack);
}
throw err;
});
}
buildOnce(): Promise<any> {
// es6-promise doesn't have finally()
return (<any>this.doBuild())
.finally(() =>
{
this.time('Build cleanup', () => this.builder.cleanup());
console.log('=== Done building %s ===', this.outputRoot);
})
.catch(err => {
console.error('\nBuild failed');
process.exit(1);
});
}
time(label, work) {
var start = Date.now();
work();
var duration = Date.now() - start;
console.log("%s: %dms", label, duration);
}
}

View File

@ -1,5 +1,5 @@
/// <reference path="../../typings/node/node.d.ts" />
/// <reference path="../../typings/fs-extra/fs-extra.d.ts" />
/// <reference path="../typings/node/node.d.ts" />
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
import Writer = require('broccoli-writer');
import fs = require('fs');
import fsx = require('fs-extra');

View File

@ -1,5 +1,6 @@
'use strict';
var destCopy = require('../broccoli-dest-copy');
var Funnel = require('broccoli-funnel');
var flatten = require('broccoli-flatten');
var htmlReplace = require('../html-replace');
@ -10,10 +11,11 @@ var stew = require('broccoli-stew');
var ts2dart = require('../broccoli-ts2dart');
var traceurCompiler = require('../traceur');
var projectRootDir = path.normalize(path.join(__dirname, '..', '..', '..'));
module.exports = function makeBrowserTree(options) {
module.exports = function makeBrowserTree(options, destinationPath) {
var modulesTree = new Funnel(
'modules',
{include: ['**/**'], exclude: ['**/*.cjs', 'benchmarks/e2e_test/**'], destDir: '/'});
@ -113,8 +115,7 @@ module.exports = function makeBrowserTree(options) {
es5Tree = mergeTrees([es5Tree, htmlTree]);
return mergeTrees([
stew.mv(es6Tree, 'js/' + options.name + '/es6'),
stew.mv(es5Tree, 'js/' + options.name + '/es5')
]);
var mergedTree = mergeTrees([stew.mv(es6Tree, '/es6'), stew.mv(es5Tree, '/es5')]);
return destCopy(mergedTree, destinationPath);
};

View File

@ -1,7 +1,8 @@
/// <reference path="../../typings/node/node.d.ts" />
'use strict';
import {MultiCopy} from './multi_copy';
import {MultiCopy} from './../multi_copy';
var destCopy = require('../broccoli-dest-copy');
var Funnel = require('broccoli-funnel');
var glob = require('glob');
var mergeTrees = require('broccoli-merge-trees');
@ -137,12 +138,11 @@ function getDocsTree() {
return mergeTrees([licenses, mdTree, docs]);
}
module.exports = function makeDartTree() {
module.exports = function makeDartTree(destinationPath) {
var sourceTree = mergeTrees([getSourceTree(), getHtmlSourcesTree()]);
sourceTree = fixDartFolderLayout(sourceTree);
var mergedResult = mergeTrees([sourceTree, getTemplatedPubspecsTree(), getDocsTree()]);
var dartTree = mergeTrees([sourceTree, getTemplatedPubspecsTree(), getDocsTree()]);
// Move the tree under the 'dart' folder.
return stew.mv(mergedResult, 'dart');
return destCopy(dartTree, destinationPath);
};

View File

@ -1,5 +1,6 @@
'use strict';
var destCopy = require('../broccoli-dest-copy');
var Funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var path = require('path');
@ -13,7 +14,7 @@ var TypescriptCompiler = require('../typescript');
var projectRootDir = path.normalize(path.join(__dirname, '..', '..', '..'));
module.exports = function makeNodeTree() {
module.exports = function makeNodeTree(destinationPath) {
// list of npm packages that this build will create
var outputPackages = ['angular2', 'benchpress', 'rtts_assert'];
@ -112,5 +113,5 @@ module.exports = function makeNodeTree() {
nodeTree = mergeTrees([nodeTree, typescriptTree], { overwrite: true });
nodeTree = mergeTrees([nodeTree, docs, packageJsons]);
return stew.mv(nodeTree, 'js/cjs');
return destCopy(nodeTree, destinationPath);
};