diff --git a/tools/broccoli/broccoli-lodash.ts b/tools/broccoli/broccoli-lodash.ts new file mode 100644 index 0000000000..43ca661736 --- /dev/null +++ b/tools/broccoli/broccoli-lodash.ts @@ -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); diff --git a/tools/broccoli/trees/dart_tree.ts b/tools/broccoli/trees/dart_tree.ts index ed30bc910b..8c2d42ecaf 100644 --- a/tools/broccoli/trees/dart_tree.ts +++ b/tools/broccoli/trees/dart_tree.ts @@ -7,7 +7,7 @@ import destCopy from '../broccoli-dest-copy'; var Funnel = require('broccoli-funnel'); import mergeTrees from '../broccoli-merge-trees'; var path = require('path'); -var renderLodashTemplate = require('broccoli-lodash'); +import renderLodashTemplate from '../broccoli-lodash'; var stew = require('broccoli-stew'); import ts2dart from '../broccoli-ts2dart'; import dartfmt from '../broccoli-dartfmt'; @@ -124,13 +124,9 @@ function getTemplatedPubspecsTree() { } }; // Generate pubspec.yaml from templates. - // Lodash insists on dropping one level of extension, so first artificially rename the yaml - // files to .yaml.template. - var pubspecs = stew.rename(modulesFunnel(['**/pubspec.yaml']), '.yaml', '.yaml.template'); + var pubspecs = modulesFunnel(['**/pubspec.yaml']); // Then render the templates. - return renderLodashTemplate( - pubspecs, - {files: ['**/pubspec.yaml.template'], context: {'packageJson': COMMON_PACKAGE_JSON}}); + return renderLodashTemplate(pubspecs, {context: {'packageJson': COMMON_PACKAGE_JSON}}); } function getDocsTree() { diff --git a/tools/broccoli/trees/node_tree.ts b/tools/broccoli/trees/node_tree.ts index 13fa8449a1..5bee8a8367 100644 --- a/tools/broccoli/trees/node_tree.ts +++ b/tools/broccoli/trees/node_tree.ts @@ -6,7 +6,7 @@ import transpileWithTraceur from '../traceur/index'; var Funnel = require('broccoli-funnel'); import mergeTrees from '../broccoli-merge-trees'; var path = require('path'); -var renderLodashTemplate = require('broccoli-lodash'); +import renderLodashTemplate from '../broccoli-lodash'; import replace from '../broccoli-replace'; 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 = stew.rename(new Funnel(modulesTree, {include: ['**/package.json']}), '.json', - '.json.template'); - packageJsons = renderLodashTemplate( - packageJsons, {files: ["**/**"], context: {'packageJson': COMMON_PACKAGE_JSON}}); + var packageJsons = new Funnel(modulesTree, {include: ['**/package.json']}); + packageJsons = + renderLodashTemplate(packageJsons, {context: {'packageJson': COMMON_PACKAGE_JSON}}); // HACK: workaround for Traceur behavior. // It expects all transpiled modules to contain this marker.