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-05-21 12:07:23 -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-03 16:42:57 -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-05-21 12:07:23 -04:00
|
|
|
private calculateDiff(firstRun: boolean): (DiffResult | DiffResult[]) {
|
|
|
|
if (this.treeDiffer) {
|
|
|
|
let diffResult = this.treeDiffer.diffTree();
|
|
|
|
diffResult.log(!firstRun);
|
|
|
|
return diffResult;
|
|
|
|
} else if (this.treeDiffers) {
|
|
|
|
return this.treeDiffers.map((treeDiffer) => treeDiffer.diffTree())
|
|
|
|
.map((diffResult) => {
|
|
|
|
diffResult.log(!firstRun);
|
|
|
|
return diffResult;
|
|
|
|
});
|
|
|
|
} 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-05-21 12:07:23 -04:00
|
|
|
let diffResult = this.calculateDiff(firstRun);
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-05-04 11:27:14 -04:00
|
|
|
var rebuildPromise = this.wrappedPlugin.rebuild(diffResult);
|
2015-05-04 11:19:25 -04:00
|
|
|
|
2015-05-04 11:27:14 -04:00
|
|
|
if (rebuildPromise) {
|
|
|
|
return (<Promise<any>>rebuildPromise).then(this.relinkOutputAndCachePaths.bind(this));
|
|
|
|
}
|
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[]) {
|
|
|
|
return trees.map((tree) => this.stabilizeTree(tree));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
return (tree.constructor['name'] === 'DiffingPluginWrapper') ? tree : stabilizeTree(tree);
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
|
|
|
}
|