Skip to content

Commit 0021015

Browse files
committed
chore(broccoli): implement diffing LodashRenderer plugin
Closes angular#2438
1 parent 902759e commit 0021015

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

tools/broccoli/broccoli-lodash.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import fs = require('fs');
2+
import fse = require('fs-extra');
3+
import path = require('path');
4+
var _ = require('lodash');
5+
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
6+
7+
interface LodashRendererOptions {
8+
encoding?: string;
9+
context?: any;
10+
// files option unsupported --- use Funnel on inputTree instead.
11+
files?: string[];
12+
}
13+
14+
const kDefaultOptions: LodashRendererOptions = {encoding: 'utf-8', context: {}, files: []};
15+
16+
17+
/**
18+
* Intercepts each changed file and replaces its contents with
19+
* the associated changes.
20+
*/
21+
export class LodashRenderer implements DiffingBroccoliPlugin {
22+
constructor(private inputPath, private cachePath,
23+
private options: LodashRendererOptions = kDefaultOptions) {}
24+
25+
rebuild(treeDiff: DiffResult) {
26+
let{encoding = 'utf-8', context = {}} = this.options;
27+
let processFile = (relativePath) => {
28+
let sourceFilePath = path.join(this.inputPath, relativePath);
29+
let destFilePath = path.join(this.cachePath, relativePath);
30+
let content = fs.readFileSync(sourceFilePath, {encoding});
31+
let transformedContent = _.template(content)(context);
32+
fse.outputFileSync(destFilePath, transformedContent);
33+
};
34+
35+
let removeFile = (relativePath) => {
36+
let destFilePath = path.join(this.cachePath, relativePath);
37+
fs.unlinkSync(destFilePath);
38+
};
39+
40+
treeDiff.changedPaths.forEach(processFile);
41+
treeDiff.removedPaths.forEach(removeFile);
42+
}
43+
}
44+
45+
export default wrapDiffingPlugin(LodashRenderer);

tools/broccoli/trees/dart_tree.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import destCopy from '../broccoli-dest-copy';
77
var Funnel = require('broccoli-funnel');
88
import mergeTrees from '../broccoli-merge-trees';
99
var path = require('path');
10-
var renderLodashTemplate = require('broccoli-lodash');
10+
import renderLodashTemplate from '../broccoli-lodash';
1111
var stew = require('broccoli-stew');
1212
import ts2dart from '../broccoli-ts2dart';
1313
import dartfmt from '../broccoli-dartfmt';
@@ -124,13 +124,9 @@ function getTemplatedPubspecsTree() {
124124
}
125125
};
126126
// Generate pubspec.yaml from templates.
127-
// Lodash insists on dropping one level of extension, so first artificially rename the yaml
128-
// files to .yaml.template.
129-
var pubspecs = stew.rename(modulesFunnel(['**/pubspec.yaml']), '.yaml', '.yaml.template');
127+
var pubspecs = modulesFunnel(['**/pubspec.yaml']);
130128
// Then render the templates.
131-
return renderLodashTemplate(
132-
pubspecs,
133-
{files: ['**/pubspec.yaml.template'], context: {'packageJson': COMMON_PACKAGE_JSON}});
129+
return renderLodashTemplate(pubspecs, {context: {'packageJson': COMMON_PACKAGE_JSON}});
134130
}
135131

136132
function getDocsTree() {

tools/broccoli/trees/node_tree.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import transpileWithTraceur from '../traceur/index';
66
var Funnel = require('broccoli-funnel');
77
import mergeTrees from '../broccoli-merge-trees';
88
var path = require('path');
9-
var renderLodashTemplate = require('broccoli-lodash');
9+
import renderLodashTemplate from '../broccoli-lodash';
1010
import replace from '../broccoli-replace';
1111
var stew = require('broccoli-stew');
1212

@@ -73,11 +73,9 @@ module.exports = function makeNodeTree(destinationPath) {
7373
}
7474
};
7575

76-
// Add a .template extension since renderLodashTemplate strips one extension
77-
var packageJsons = stew.rename(new Funnel(modulesTree, {include: ['**/package.json']}), '.json',
78-
'.json.template');
79-
packageJsons = renderLodashTemplate(
80-
packageJsons, {files: ["**/**"], context: {'packageJson': COMMON_PACKAGE_JSON}});
76+
var packageJsons = new Funnel(modulesTree, {include: ['**/package.json']});
77+
packageJsons =
78+
renderLodashTemplate(packageJsons, {context: {'packageJson': COMMON_PACKAGE_JSON}});
8179

8280
// HACK: workaround for Traceur behavior.
8381
// It expects all transpiled modules to contain this marker.

0 commit comments

Comments
 (0)