2015-04-26 03:31:53 -04:00
|
|
|
var broccoli = require('broccoli');
|
2015-06-08 21:56:24 -04:00
|
|
|
var fs = require('fs');
|
2015-04-26 03:31:53 -04:00
|
|
|
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');
|
|
|
|
|
2015-11-13 04:58:17 -05:00
|
|
|
type ProjectMap = {
|
|
|
|
[key: string]: boolean
|
|
|
|
};
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
type Options = {
|
|
|
|
projects: ProjectMap;
|
|
|
|
noTypeChecks: boolean;
|
|
|
|
generateEs6: boolean;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2015-04-26 03:31:53 -04:00
|
|
|
/**
|
|
|
|
* BroccoliBuilder facade for all of our build pipelines.
|
|
|
|
*/
|
|
|
|
export class AngularBuilder {
|
|
|
|
private nodeBuilder: BroccoliBuilder;
|
|
|
|
private browserDevBuilder: BroccoliBuilder;
|
|
|
|
private browserProdBuilder: BroccoliBuilder;
|
|
|
|
private dartBuilder: BroccoliBuilder;
|
2015-05-28 18:54:54 -04:00
|
|
|
private outputPath: string;
|
2015-06-08 21:56:24 -04:00
|
|
|
private firstResult: BuildResult;
|
2015-04-26 03:31:53 -04:00
|
|
|
|
2015-05-28 18:54:54 -04:00
|
|
|
constructor(public options: AngularBuilderOptions) { this.outputPath = options.outputPath; }
|
2015-04-26 03:31:53 -04:00
|
|
|
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
public rebuildBrowserDevTree(opts: Options): Promise<BuildResult> {
|
|
|
|
this.browserDevBuilder = this.browserDevBuilder || this.makeBrowserDevBuilder(opts);
|
2015-06-08 21:56:24 -04:00
|
|
|
return this.rebuild(this.browserDevBuilder, 'js.dev');
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
public rebuildBrowserProdTree(opts: Options): Promise<BuildResult> {
|
|
|
|
this.browserProdBuilder = this.browserProdBuilder || this.makeBrowserProdBuilder(opts);
|
2015-06-08 21:56:24 -04:00
|
|
|
return this.rebuild(this.browserProdBuilder, 'js.prod');
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
public rebuildNodeTree(opts: Options): Promise<BuildResult> {
|
|
|
|
this.nodeBuilder = this.nodeBuilder || this.makeNodeBuilder(opts.projects);
|
2015-06-08 21:56:24 -04:00
|
|
|
return this.rebuild(this.nodeBuilder, 'js.cjs');
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 04:58:17 -05:00
|
|
|
public rebuildDartTree(projects: ProjectMap): Promise<BuildResult> {
|
|
|
|
this.dartBuilder = this.dartBuilder || this.makeDartBuilder(projects);
|
2015-06-08 21:56:24 -04:00
|
|
|
return this.rebuild(this.dartBuilder, 'dart');
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cleanup(): Promise<any> {
|
|
|
|
return Q.all([
|
|
|
|
this.nodeBuilder && this.nodeBuilder.cleanup(),
|
|
|
|
this.browserDevBuilder && this.browserDevBuilder.cleanup(),
|
|
|
|
this.browserProdBuilder && this.browserProdBuilder.cleanup()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
private makeBrowserDevBuilder(opts: Options): BroccoliBuilder {
|
|
|
|
let tree = makeBrowserTree(
|
|
|
|
{
|
|
|
|
name: 'dev',
|
|
|
|
typeAssertions: true,
|
2015-11-23 17:58:18 -05:00
|
|
|
sourceMaps: true,
|
2015-11-16 19:18:59 -05:00
|
|
|
projects: opts.projects,
|
|
|
|
noTypeChecks: opts.noTypeChecks,
|
|
|
|
generateEs6: opts.generateEs6
|
|
|
|
},
|
|
|
|
path.join(this.outputPath, 'js', 'dev'));
|
2015-04-26 03:31:53 -04:00
|
|
|
return new broccoli.Builder(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-16 19:18:59 -05:00
|
|
|
private makeBrowserProdBuilder(opts: Options): BroccoliBuilder {
|
|
|
|
let tree = makeBrowserTree(
|
|
|
|
{
|
|
|
|
name: 'prod',
|
|
|
|
typeAssertions: false,
|
2015-11-23 17:58:18 -05:00
|
|
|
sourceMaps: false,
|
2015-11-16 19:18:59 -05:00
|
|
|
projects: opts.projects,
|
|
|
|
noTypeChecks: opts.noTypeChecks,
|
|
|
|
generateEs6: opts.generateEs6
|
|
|
|
},
|
|
|
|
path.join(this.outputPath, 'js', 'prod'));
|
2015-04-26 03:31:53 -04:00
|
|
|
return new broccoli.Builder(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 04:58:17 -05:00
|
|
|
private makeNodeBuilder(projects: ProjectMap): BroccoliBuilder {
|
|
|
|
let tree = makeNodeTree(projects, path.join(this.outputPath, 'js', 'cjs'));
|
2015-04-26 03:31:53 -04:00
|
|
|
return new broccoli.Builder(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-13 04:58:17 -05:00
|
|
|
private makeDartBuilder(projects: ProjectMap): BroccoliBuilder {
|
2015-05-28 18:54:54 -04:00
|
|
|
let options = {
|
|
|
|
outputPath: path.join(this.outputPath, 'dart'),
|
|
|
|
dartSDK: this.options.dartSDK,
|
2015-11-13 04:58:17 -05:00
|
|
|
logs: this.options.logs,
|
|
|
|
projects: projects
|
2015-05-28 18:54:54 -04:00
|
|
|
};
|
|
|
|
let tree = makeDartTree(options);
|
2015-04-26 03:31:53 -04:00
|
|
|
return new broccoli.Builder(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-08 21:56:24 -04:00
|
|
|
private rebuild(builder, name) {
|
|
|
|
return builder.build().then(
|
|
|
|
(result) => {
|
|
|
|
if (!this.firstResult) {
|
|
|
|
this.firstResult = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
printSlowTrees(result.graph);
|
|
|
|
writeBuildLog(result, name);
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
// the build tree is the same during rebuilds, only leaf properties of the nodes change
|
|
|
|
// so let's traverse it and get updated values for input/cache/output paths
|
|
|
|
if (this.firstResult) {
|
|
|
|
writeBuildLog(this.firstResult, name);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function writeBuildLog(result: BuildResult, name: string) {
|
|
|
|
let logPath = `tmp/build.${name}.log`;
|
|
|
|
let prevLogPath = logPath + '.previous';
|
|
|
|
let formattedLogContent = JSON.stringify(broccoliNodeToBuildNode(result.graph), null, 2);
|
|
|
|
|
|
|
|
if (fs.existsSync(prevLogPath)) fs.unlinkSync(prevLogPath);
|
|
|
|
if (fs.existsSync(logPath)) fs.renameSync(logPath, prevLogPath);
|
|
|
|
fs.writeFileSync(logPath, formattedLogContent, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function broccoliNodeToBuildNode(broccoliNode) {
|
|
|
|
let tree = broccoliNode.tree.newStyleTree || broccoliNode.tree;
|
|
|
|
|
2015-06-08 22:01:22 -04:00
|
|
|
return new BuildNode(tree.description || tree.constructor.name,
|
|
|
|
tree.inputPath ? [tree.inputPath] : tree.inputPaths, tree.cachePath,
|
2015-11-16 19:18:59 -05:00
|
|
|
tree.outputPath, broccoliNode.selfTime / (1000 * 1000 * 1000),
|
|
|
|
broccoliNode.totalTime / (1000 * 1000 * 1000),
|
|
|
|
broccoliNode.subtrees.map(broccoliNodeToBuildNode));
|
2015-06-08 21:56:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BuildNode {
|
2015-06-08 22:01:22 -04:00
|
|
|
constructor(public pluginName: string, public inputPaths: string[], public cachePath: string,
|
2015-11-16 19:18:59 -05:00
|
|
|
public outputPath: string, public selfTime: number, public totalTime: number,
|
|
|
|
public inputNodes: BroccoliNode[]) {}
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|