2015-05-04 11:19:25 -04:00
|
|
|
/// <reference path="broccoli.d.ts" />
|
|
|
|
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
|
|
|
|
/// <reference path="../typings/node/node.d.ts" />
|
|
|
|
|
|
|
|
import fs = require('fs');
|
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
|
|
|
import {TreeDiffer, DiffResult} from './tree-differ';
|
2015-05-24 20:27:38 -04:00
|
|
|
import stabilizeTree from './broccoli-tree-stabilizer';
|
2015-05-04 11:19:25 -04:00
|
|
|
let symlinkOrCopy = require('symlink-or-copy');
|
|
|
|
|
|
|
|
|
|
|
|
export {DiffResult} from './tree-differ';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes writing diffing plugins easy.
|
|
|
|
*
|
|
|
|
* Factory method that takes a class that implements the DiffingBroccoliPlugin interface and returns
|
|
|
|
* an instance of BroccoliTree.
|
|
|
|
*
|
|
|
|
* @param pluginClass
|
|
|
|
* @returns {DiffingPlugin}
|
|
|
|
*/
|
|
|
|
export function wrapDiffingPlugin(pluginClass): DiffingPluginWrapperFactory {
|
|
|
|
return function() { return new DiffingPluginWrapper(pluginClass, arguments); };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface DiffingBroccoliPlugin {
|
2015-06-23 16:21:17 -04:00
|
|
|
rebuild(diff: (DiffResult | DiffResult[])): (Promise<any>| void);
|
2015-05-04 11:19:25 -04:00
|
|
|
cleanup ? () : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DiffingPluginWrapperFactory = (inputTrees: (BroccoliTree | BroccoliTree[]), options?) =>
|
2015-06-12 10:50:45 -04:00
|
|
|
BroccoliTree;
|
2015-05-04 11:19:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DiffingPluginWrapper implements BroccoliTree {
|
2015-05-21 12:07:23 -04:00
|
|
|
treeDiffer: TreeDiffer = null;
|
|
|
|
treeDiffers: TreeDiffer[] = null;
|
2015-05-04 11:19:25 -04:00
|
|
|
initialized = false;
|
|
|
|
wrappedPlugin: DiffingBroccoliPlugin = null;
|
|
|
|
inputTree = null;
|
|
|
|
inputTrees = null;
|
|
|
|
description = null;
|
|
|
|
|
|
|
|
// props monkey-patched by broccoli builder:
|
|
|
|
inputPath = null;
|
2015-05-21 12:07:23 -04:00
|
|
|
inputPaths = null;
|
2015-05-04 11:19:25 -04:00
|
|
|
cachePath = null;
|
|
|
|
outputPath = null;
|
|
|
|
|
|
|
|
constructor(private pluginClass, private wrappedPluginArguments) {
|
|
|
|
if (Array.isArray(wrappedPluginArguments[0])) {
|
2015-05-24 20:27:38 -04:00
|
|
|
this.inputTrees = this.stabilizeTrees(wrappedPluginArguments[0]);
|
2015-05-04 11:19:25 -04:00
|
|
|
} else {
|
2015-05-24 20:27:38 -04:00
|
|
|
this.inputTree = this.stabilizeTree(wrappedPluginArguments[0]);
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.description = this.pluginClass.name;
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:21:17 -04:00
|
|
|
private calculateDiff(firstRun: boolean): (DiffResult | DiffResult[]) {
|
|
|
|
// TODO(caitp): optionally log trees based on environment variable or
|
|
|
|
// command line option. It may be worth logging for trees where elapsed
|
|
|
|
// time exceeds some threshold, like 10ms.
|
|
|
|
if (this.treeDiffer) {
|
|
|
|
return this.treeDiffer.diffTree();
|
|
|
|
} else if (this.treeDiffers) {
|
|
|
|
return this.treeDiffers.map((treeDiffer) => treeDiffer.diffTree());
|
2015-05-21 12:07:23 -04:00
|
|
|
} else {
|
|
|
|
throw new Error("Missing TreeDiffer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
rebuild() {
|
2015-05-04 11:27:14 -04:00
|
|
|
try {
|
|
|
|
let firstRun = !this.initialized;
|
|
|
|
this.init();
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-06-23 16:21:17 -04:00
|
|
|
let diffResult = this.calculateDiff(firstRun);
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-06-23 16:21:17 -04:00
|
|
|
var rebuildPromise = this.wrappedPlugin.rebuild(diffResult);
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-06-23 16:21:17 -04:00
|
|
|
if (rebuildPromise) {
|
|
|
|
return (<Promise<any>>rebuildPromise).then(this.relinkOutputAndCachePaths.bind(this));
|
2015-05-04 11:27:14 -04:00
|
|
|
}
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-05-04 11:27:14 -04:00
|
|
|
this.relinkOutputAndCachePaths();
|
|
|
|
} catch (e) {
|
|
|
|
e.message = `[${this.description}]: ${e.message}`;
|
|
|
|
throw e;
|
|
|
|
}
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-24 20:27:38 -04:00
|
|
|
cleanup() {
|
|
|
|
if (this.wrappedPlugin.cleanup) {
|
|
|
|
this.wrappedPlugin.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
private relinkOutputAndCachePaths() {
|
|
|
|
// just symlink the cache and output tree
|
|
|
|
fs.rmdirSync(this.outputPath);
|
|
|
|
symlinkOrCopy.sync(this.cachePath, this.outputPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private init() {
|
|
|
|
if (!this.initialized) {
|
2015-05-06 19:24:10 -04:00
|
|
|
let includeExtensions = this.pluginClass.includeExtensions || [];
|
|
|
|
let excludeExtensions = this.pluginClass.excludeExtensions || [];
|
2015-05-21 12:07:23 -04:00
|
|
|
let description = this.description;
|
2015-05-04 11:19:25 -04:00
|
|
|
this.initialized = true;
|
2015-05-21 12:07:23 -04:00
|
|
|
if (this.inputPaths) {
|
|
|
|
this.treeDiffers =
|
|
|
|
this.inputPaths.map((inputPath) => new TreeDiffer(
|
|
|
|
description, inputPath, includeExtensions, excludeExtensions));
|
|
|
|
} else if (this.inputPath) {
|
|
|
|
this.treeDiffer =
|
|
|
|
new TreeDiffer(description, this.inputPath, includeExtensions, excludeExtensions);
|
|
|
|
}
|
|
|
|
this.wrappedPlugin = new this.pluginClass(this.inputPaths || this.inputPath, this.cachePath,
|
|
|
|
this.wrappedPluginArguments[1]);
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-24 20:27:38 -04:00
|
|
|
private stabilizeTrees(trees: BroccoliTree[]) {
|
2015-06-05 03:19:34 -04:00
|
|
|
// Prevent extensions to prevent array from being mutated from the outside.
|
|
|
|
// For-loop used to avoid re-allocating a new array.
|
|
|
|
for (let i = 0; i < trees.length; ++i) {
|
|
|
|
trees[i] = this.stabilizeTree(trees[i]);
|
|
|
|
}
|
|
|
|
return Object.freeze(trees);
|
2015-05-24 20:27:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private stabilizeTree(tree: BroccoliTree) {
|
|
|
|
// Ignore all DiffingPlugins as they are already stable, for others we don't know for sure
|
|
|
|
// so we need to stabilize them.
|
|
|
|
// Since it's not safe to use instanceof operator in node, we are checking the constructor.name.
|
2015-06-07 17:56:35 -04:00
|
|
|
//
|
|
|
|
// New-styler/rebuild trees should always be stable.
|
|
|
|
let isNewStyleTree = !!(tree['newStyleTree'] || typeof tree.rebuild === 'function' ||
|
|
|
|
tree['isReadAPICompatTree'] || tree.constructor['name'] === 'Funnel');
|
|
|
|
|
|
|
|
return isNewStyleTree ? tree : stabilizeTree(tree);
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
|
|
|
}
|