chore(broccoli): implement diffing LodashRenderer plugin

Closes #2438
This commit is contained in:
Caitlin Potter 2015-06-09 13:35:58 -04:00
parent 902759e1c7
commit 002101521c
3 changed files with 52 additions and 13 deletions

View File

@ -0,0 +1,45 @@
import fs = require('fs');
import fse = require('fs-extra');
import path = require('path');
var _ = require('lodash');
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
interface LodashRendererOptions {
encoding?: string;
context?: any;
// files option unsupported --- use Funnel on inputTree instead.
files?: string[];
}
const kDefaultOptions: LodashRendererOptions = {encoding: 'utf-8', context: {}, files: []};
/**
* Intercepts each changed file and replaces its contents with
* the associated changes.
*/
export class LodashRenderer implements DiffingBroccoliPlugin {
constructor(private inputPath, private cachePath,
private options: LodashRendererOptions = kDefaultOptions) {}
rebuild(treeDiff: DiffResult) {
let{encoding = 'utf-8', context = {}} = this.options;
let processFile = (relativePath) => {
let sourceFilePath = path.join(this.inputPath, relativePath);
let destFilePath = path.join(this.cachePath, relativePath);
let content = fs.readFileSync(sourceFilePath, {encoding});
let transformedContent = _.template(content)(context);
fse.outputFileSync(destFilePath, transformedContent);
};
let removeFile = (relativePath) => {
let destFilePath = path.join(this.cachePath, relativePath);
fs.unlinkSync(destFilePath);
};
treeDiff.changedPaths.forEach(processFile);
treeDiff.removedPaths.forEach(removeFile);
}
}
export default wrapDiffingPlugin(LodashRenderer);

View File

@ -7,7 +7,7 @@ import destCopy from '../broccoli-dest-copy';
var Funnel = require('broccoli-funnel'); var Funnel = require('broccoli-funnel');
import mergeTrees from '../broccoli-merge-trees'; import mergeTrees from '../broccoli-merge-trees';
var path = require('path'); var path = require('path');
var renderLodashTemplate = require('broccoli-lodash'); import renderLodashTemplate from '../broccoli-lodash';
var stew = require('broccoli-stew'); var stew = require('broccoli-stew');
import ts2dart from '../broccoli-ts2dart'; import ts2dart from '../broccoli-ts2dart';
import dartfmt from '../broccoli-dartfmt'; import dartfmt from '../broccoli-dartfmt';
@ -124,13 +124,9 @@ function getTemplatedPubspecsTree() {
} }
}; };
// Generate pubspec.yaml from templates. // Generate pubspec.yaml from templates.
// Lodash insists on dropping one level of extension, so first artificially rename the yaml var pubspecs = modulesFunnel(['**/pubspec.yaml']);
// files to .yaml.template.
var pubspecs = stew.rename(modulesFunnel(['**/pubspec.yaml']), '.yaml', '.yaml.template');
// Then render the templates. // Then render the templates.
return renderLodashTemplate( return renderLodashTemplate(pubspecs, {context: {'packageJson': COMMON_PACKAGE_JSON}});
pubspecs,
{files: ['**/pubspec.yaml.template'], context: {'packageJson': COMMON_PACKAGE_JSON}});
} }
function getDocsTree() { function getDocsTree() {

View File

@ -6,7 +6,7 @@ import transpileWithTraceur from '../traceur/index';
var Funnel = require('broccoli-funnel'); var Funnel = require('broccoli-funnel');
import mergeTrees from '../broccoli-merge-trees'; import mergeTrees from '../broccoli-merge-trees';
var path = require('path'); var path = require('path');
var renderLodashTemplate = require('broccoli-lodash'); import renderLodashTemplate from '../broccoli-lodash';
import replace from '../broccoli-replace'; import replace from '../broccoli-replace';
var stew = require('broccoli-stew'); var stew = require('broccoli-stew');
@ -73,11 +73,9 @@ module.exports = function makeNodeTree(destinationPath) {
} }
}; };
// Add a .template extension since renderLodashTemplate strips one extension var packageJsons = new Funnel(modulesTree, {include: ['**/package.json']});
var packageJsons = stew.rename(new Funnel(modulesTree, {include: ['**/package.json']}), '.json', packageJsons =
'.json.template'); renderLodashTemplate(packageJsons, {context: {'packageJson': COMMON_PACKAGE_JSON}});
packageJsons = renderLodashTemplate(
packageJsons, {files: ["**/**"], context: {'packageJson': COMMON_PACKAGE_JSON}});
// HACK: workaround for Traceur behavior. // HACK: workaround for Traceur behavior.
// It expects all transpiled modules to contain this marker. // It expects all transpiled modules to contain this marker.