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 path = require('path');
|
|
|
|
var printSlowTrees = require('broccoli-slow-trees');
|
|
|
|
var Q = require('q');
|
|
|
|
|
2016-03-24 13:00:19 -04:00
|
|
|
export type ProjectMap = {
|
2015-11-13 04:58:17 -05:00
|
|
|
[key: string]: boolean
|
|
|
|
};
|
|
|
|
|
2016-03-24 13:00:19 -04:00
|
|
|
export type Options = {
|
2016-04-12 12:40:37 -04:00
|
|
|
projects: ProjectMap;
|
|
|
|
noTypeChecks: boolean;
|
|
|
|
generateEs6: boolean;
|
|
|
|
useBundles: boolean;
|
2016-03-24 13:00:19 -04:00
|
|
|
};
|
2015-11-16 19:18:59 -05:00
|
|
|
|
2016-04-07 13:14:38 -04:00
|
|
|
export interface AngularBuilderOptions {
|
|
|
|
outputPath: string;
|
|
|
|
dartSDK?: any;
|
|
|
|
logs?: any;
|
|
|
|
}
|
|
|
|
|
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,
|
2015-12-29 20:01:10 -05:00
|
|
|
generateEs6: opts.generateEs6,
|
|
|
|
useBundles: opts.useBundles
|
2015-11-16 19:18:59 -05:00
|
|
|
},
|
|
|
|
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,
|
2015-12-29 20:01:10 -05:00
|
|
|
generateEs6: opts.generateEs6,
|
|
|
|
useBundles: opts.useBundles
|
2015-11-16 19:18:59 -05:00
|
|
|
},
|
|
|
|
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
|
|
|
};
|
2016-02-12 17:21:15 -05:00
|
|
|
// Workaround for https://github.com/dart-lang/dart_style/issues/493
|
|
|
|
var makeDartTree = require('./trees/dart_tree');
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
private rebuild(builder: BroccoliBuilder, name: string): Promise<BuildResult> {
|
|
|
|
return builder.build().then<BuildResult>(
|
2015-06-08 21:56:24 -04:00
|
|
|
(result) => {
|
|
|
|
if (!this.firstResult) {
|
|
|
|
this.firstResult = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
printSlowTrees(result.graph);
|
|
|
|
writeBuildLog(result, name);
|
2016-01-22 13:51:16 -05:00
|
|
|
return result;
|
2015-06-08 21:56:24 -04:00
|
|
|
},
|
2016-01-22 13:51:16 -05:00
|
|
|
(error): any => {
|
2015-06-08 21:56:24 -04:00
|
|
|
// 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'});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function broccoliNodeToBuildNode(broccoliNode: BroccoliNode): BuildNode {
|
2015-06-08 21:56:24 -04:00
|
|
|
let tree = broccoliNode.tree.newStyleTree || broccoliNode.tree;
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
return new BuildNode(tree.description || (<any>tree.constructor).name,
|
2016-04-12 12:40:37 -04:00
|
|
|
tree.inputPath ? [tree.inputPath] : tree.inputPaths, tree.cachePath,
|
|
|
|
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 {
|
2016-04-12 12:40:37 -04:00
|
|
|
constructor(public pluginName: string, public inputPaths: string[], public cachePath: string,
|
|
|
|
public outputPath: string, public selfTime: number, public totalTime: number,
|
2016-01-22 13:51:16 -05:00
|
|
|
public inputNodes: BuildNode[]) {}
|
2015-04-26 03:31:53 -04:00
|
|
|
}
|