X Tutup
Skip to content

Commit dc45559

Browse files
committed
build(broccoli): add support for DiffResult#addedPaths
Some plugins want to explicitly know of new paths, so we need to distinguish them from changed paths.
1 parent efab032 commit dc45559

13 files changed

+211
-113
lines changed

tools/broccoli/broccoli-dartfmt.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class DartFormatter implements DiffingBroccoliPlugin {
3030

3131
rebuild(treeDiff: DiffResult): Promise<any> {
3232
let args = ['-w'];
33-
treeDiff.changedPaths.forEach((changedFile) => {
34-
let sourcePath = path.join(this.inputPath, changedFile);
35-
let destPath = path.join(this.cachePath, changedFile);
36-
if (/\.dart$/.test(changedFile)) args.push(destPath);
37-
fse.copySync(sourcePath, destPath);
38-
});
33+
treeDiff.addedPaths.concat(treeDiff.changedPaths)
34+
.forEach((changedFile) => {
35+
let sourcePath = path.join(this.inputPath, changedFile);
36+
let destPath = path.join(this.cachePath, changedFile);
37+
if (/\.dart$/.test(changedFile)) args.push(destPath);
38+
fse.copySync(sourcePath, destPath);
39+
});
3940
treeDiff.removedPaths.forEach((removedFile) => {
4041
let destPath = path.join(this.cachePath, removedFile);
4142
fse.removeSync(destPath);

tools/broccoli/broccoli-dest-copy.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ class DestCopy implements DiffingBroccoliPlugin {
1515

1616

1717
rebuild(treeDiff: DiffResult) {
18-
treeDiff.changedPaths.forEach((changedFilePath) => {
19-
var destFilePath = path.join(this.outputRoot, changedFilePath);
20-
21-
var destDirPath = path.dirname(destFilePath);
22-
fse.mkdirsSync(destDirPath);
23-
fse.copySync(path.join(this.inputPath, changedFilePath), destFilePath);
24-
});
18+
treeDiff.addedPaths.concat(treeDiff.changedPaths)
19+
.forEach((changedFilePath) => {
20+
var destFilePath = path.join(this.outputRoot, changedFilePath);
21+
22+
var destDirPath = path.dirname(destFilePath);
23+
fse.mkdirsSync(destDirPath);
24+
fse.copySync(path.join(this.inputPath, changedFilePath), destFilePath);
25+
});
2526

2627
treeDiff.removedPaths.forEach((removedFilePath) => {
2728
var destFilePath = path.join(this.outputRoot, removedFilePath);

tools/broccoli/broccoli-flatten.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,26 @@ describe('Flatten', () => {
5252

5353
expect(fs.readdirSync('output')).toEqual(['file-1.1.txt', 'file-2.txt', 'file-3.txt']);
5454
});
55+
56+
57+
it('should throw an exception if duplicates are found', () => {
58+
let testDir = {
59+
'input': {
60+
'dir1': {
61+
'file-1.txt': mockfs.file({content: 'file-1.txt content', mtime: new Date(1000)}),
62+
'subdir-1': {
63+
'file-1.txt': mockfs.file({content: 'file-1.1.txt content', mtime: new Date(1000)})
64+
},
65+
'empty-dir': {}
66+
},
67+
},
68+
'output': {}
69+
};
70+
mockfs(testDir);
71+
72+
let differ = new TreeDiffer('testLabel', 'input');
73+
let flattenedTree = flatten('input');
74+
expect(() => flattenedTree.rebuild(differ.diffTree())).
75+
toThrowError("Duplicate file 'file-1.txt' found in path 'dir1/subdir-1/file-1.txt'");
76+
});
5577
});

tools/broccoli/broccoli-flatten.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import fs = require('fs');
22
import fse = require('fs-extra');
33
import path = require('path');
44
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
5+
var symlinkOrCopy = require('symlink-or-copy').sync;
6+
7+
var isWindows = process.platform === 'win32';
58

69

710
/**
@@ -11,8 +14,17 @@ import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-br
1114
export class DiffingFlatten implements DiffingBroccoliPlugin {
1215
constructor(private inputPath, private cachePath, private options) {}
1316

17+
1418
rebuild(treeDiff: DiffResult) {
15-
treeDiff.changedPaths.forEach((changedFilePath) => {
19+
let pathsToUpdate = treeDiff.addedPaths;
20+
21+
// since we need to run on Windows as well we can't rely on symlinks being available,
22+
// which means that we need to respond to both added and changed paths
23+
if (isWindows) {
24+
pathsToUpdate = pathsToUpdate.concat(treeDiff.changedPaths);
25+
}
26+
27+
pathsToUpdate.forEach((changedFilePath) => {
1628
var sourceFilePath = path.join(this.inputPath, changedFilePath);
1729
var destFilePath = path.join(this.cachePath, path.basename(changedFilePath));
1830
var destDirPath = path.dirname(destFilePath);
@@ -21,9 +33,11 @@ export class DiffingFlatten implements DiffingBroccoliPlugin {
2133
fse.mkdirpSync(destDirPath);
2234
}
2335

24-
// TODO: once we have addedPaths support, we should throw dupes are found
2536
if (!fs.existsSync(destFilePath)) {
26-
fs.symlinkSync(sourceFilePath, destFilePath);
37+
symlinkOrCopy(sourceFilePath, destFilePath);
38+
} else {
39+
throw new Error(`Duplicate file '${path.basename(changedFilePath)}' ` +
40+
`found in path '${changedFilePath}'`);
2741
}
2842
});
2943

tools/broccoli/broccoli-lodash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class LodashRenderer implements DiffingBroccoliPlugin {
3737
fs.unlinkSync(destFilePath);
3838
};
3939

40-
treeDiff.changedPaths.forEach(processFile);
40+
treeDiff.addedPaths.concat(treeDiff.changedPaths).forEach(processFile);
4141
treeDiff.removedPaths.forEach(removeFile);
4242
}
4343
}

tools/broccoli/broccoli-merge-trees.spec.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('MergeTrees', () => {
4545
expect(read('dest/foo.js')).toBe('tree2/foo.js content');
4646
});
4747

48-
it('should throw if duplicates are used by default', () => {
48+
it('should throw if duplicates are found during the initial build', () => {
4949
let testDir: any = {
5050
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
5151
'tree2': {'foo.js': mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)})},
@@ -54,15 +54,34 @@ describe('MergeTrees', () => {
5454
mockfs(testDir);
5555
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
5656
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {});
57-
expect(() => treeMerger.rebuild(treeDiffer.diffTrees())).toThrow();
57+
expect(() => treeMerger.rebuild(treeDiffer.diffTrees())).
58+
toThrowError("`overwrite` option is required for handling duplicates.");
5859

59-
delete testDir.tree2['foo.js'];
60-
delete testDir.tree3['foo.js'];
60+
testDir = {
61+
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
62+
'tree2': {},
63+
'tree3': {}
64+
};
65+
mockfs(testDir);
66+
});
67+
68+
69+
it('should throw if duplicates are found during rebuild', () => {
70+
let testDir = {
71+
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
72+
'tree2': {},
73+
'tree3': {}
74+
};
6175
mockfs(testDir);
76+
77+
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
78+
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {});
6279
expect(() => treeMerger.rebuild(treeDiffer.diffTrees())).not.toThrow();
6380

81+
6482
testDir.tree2['foo.js'] = mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)});
6583
mockfs(testDir);
66-
expect(() => treeMerger.rebuild(treeDiffer.diffTrees())).toThrow();
84+
expect(() => treeMerger.rebuild(treeDiffer.diffTrees())).
85+
toThrowError("`overwrite` option is required for handling duplicates.");
6786
});
6887
});

tools/broccoli/broccoli-merge-trees.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path = require('path');
44
var symlinkOrCopySync = require('symlink-or-copy').sync;
55
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
66

7+
var isWindows = process.platform === 'win32';
8+
79
interface MergeTreesOptions {
810
overwrite?: boolean;
911
}
@@ -43,10 +45,12 @@ export class MergeTrees implements DiffingBroccoliPlugin {
4345
};
4446

4547
if (this.firstBuild) {
48+
this.firstBuild = false;
49+
4650
// Build initial cache
4751
treeDiffs.reverse().forEach((treeDiff: DiffResult, index) => {
4852
index = treeDiffs.length - 1 - index;
49-
treeDiff.changedPaths.forEach((changedPath) => {
53+
treeDiff.addedPaths.forEach((changedPath) => {
5054
let cache = this.pathCache[changedPath];
5155
if (cache === undefined) {
5256
this.pathCache[changedPath] = [index];
@@ -59,7 +63,7 @@ export class MergeTrees implements DiffingBroccoliPlugin {
5963
}
6064
});
6165
});
62-
this.firstBuild = false;
66+
6367
} else {
6468
// Update cache
6569
treeDiffs.reverse().forEach((treeDiff: DiffResult, index) => {
@@ -81,7 +85,14 @@ export class MergeTrees implements DiffingBroccoliPlugin {
8185
}
8286
}
8387
});
84-
treeDiff.changedPaths.forEach((changedPath) => {
88+
89+
let pathsToUpdate = treeDiff.addedPaths;
90+
91+
if (isWindows) {
92+
pathsToUpdate = pathsToUpdate.concat(treeDiff.changedPaths);
93+
}
94+
95+
pathsToUpdate.forEach((changedPath) => {
8596
let cache = this.pathCache[changedPath];
8697
if (cache === undefined) {
8798
// File was added

tools/broccoli/broccoli-replace.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,33 @@ class DiffingReplace implements DiffingBroccoliPlugin {
1717
var patterns = this.options.patterns;
1818
var files = this.options.files;
1919

20-
treeDiff.changedPaths.forEach((changedFilePath) => {
21-
var sourceFilePath = path.join(this.inputPath, changedFilePath);
22-
var destFilePath = path.join(this.cachePath, changedFilePath);
23-
var destDirPath = path.dirname(destFilePath);
24-
25-
if (!fs.existsSync(destDirPath)) {
26-
fse.mkdirpSync(destDirPath);
27-
}
28-
29-
var fileMatches = files.some((filePath) => minimatch(changedFilePath, filePath));
30-
if (fileMatches) {
31-
var content = fs.readFileSync(sourceFilePath, FILE_ENCODING);
32-
patterns.forEach((pattern) => {
33-
var replacement = pattern.replacement;
34-
if (typeof replacement === 'function') {
35-
replacement = function(content) {
36-
return pattern.replacement(content, changedFilePath);
37-
};
20+
treeDiff.addedPaths.concat(treeDiff.changedPaths)
21+
.forEach((changedFilePath) => {
22+
var sourceFilePath = path.join(this.inputPath, changedFilePath);
23+
var destFilePath = path.join(this.cachePath, changedFilePath);
24+
var destDirPath = path.dirname(destFilePath);
25+
26+
if (!fs.existsSync(destDirPath)) {
27+
fse.mkdirpSync(destDirPath);
28+
}
29+
30+
var fileMatches = files.some((filePath) => minimatch(changedFilePath, filePath));
31+
if (fileMatches) {
32+
var content = fs.readFileSync(sourceFilePath, FILE_ENCODING);
33+
patterns.forEach((pattern) => {
34+
var replacement = pattern.replacement;
35+
if (typeof replacement === 'function') {
36+
replacement = function(content) {
37+
return pattern.replacement(content, changedFilePath);
38+
};
39+
}
40+
content = content.replace(pattern.match, replacement);
41+
});
42+
fs.writeFileSync(destFilePath, content, FILE_ENCODING);
43+
} else if (!fs.existsSync(destFilePath)) {
44+
fs.symlinkSync(sourceFilePath, destFilePath);
3845
}
39-
content = content.replace(pattern.match, replacement);
4046
});
41-
fs.writeFileSync(destFilePath, content, FILE_ENCODING);
42-
} else if (!fs.existsSync(destFilePath)) {
43-
fs.symlinkSync(sourceFilePath, destFilePath);
44-
}
45-
});
4647

4748
treeDiff.removedPaths.forEach((removedFilePath) => {
4849
var destFilePath = path.join(this.cachePath, removedFilePath);

tools/broccoli/broccoli-ts2dart.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ class TSToDartTranspiler implements DiffingBroccoliPlugin {
2323
rebuild(treeDiff: DiffResult) {
2424
let toEmit = [];
2525
let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
26-
treeDiff.changedPaths.forEach((changedPath) => {
27-
let inputFilePath = path.resolve(this.inputPath, changedPath);
26+
treeDiff.addedPaths.concat(treeDiff.changedPaths)
27+
.forEach((changedPath) => {
28+
let inputFilePath = path.resolve(this.inputPath, changedPath);
2829

29-
// Ignore files which don't need to be transpiled to Dart
30-
let dartInputFilePath = getDartFilePath(inputFilePath);
31-
if (fs.existsSync(dartInputFilePath)) return;
30+
// Ignore files which don't need to be transpiled to Dart
31+
let dartInputFilePath = getDartFilePath(inputFilePath);
32+
if (fs.existsSync(dartInputFilePath)) return;
3233

33-
// Prepare to rebuild
34-
toEmit.push(path.resolve(this.inputPath, changedPath));
35-
});
34+
// Prepare to rebuild
35+
toEmit.push(path.resolve(this.inputPath, changedPath));
36+
});
3637

3738
treeDiff.removedPaths.forEach((removedPath) => {
3839
let absolutePath = path.resolve(this.inputPath, removedPath);

tools/broccoli/broccoli-typescript.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
5050
let pathsToEmit = [];
5151
let pathsWithErrors = [];
5252

53-
treeDiff.changedPaths.forEach((tsFilePath) => {
54-
if (!this.fileRegistry[tsFilePath]) {
55-
this.fileRegistry[tsFilePath] = {version: 0};
56-
this.rootFilePaths.push(tsFilePath);
57-
} else {
58-
this.fileRegistry[tsFilePath].version++;
59-
}
53+
treeDiff.addedPaths.concat(treeDiff.changedPaths)
54+
.forEach((tsFilePath) => {
55+
if (!this.fileRegistry[tsFilePath]) {
56+
this.fileRegistry[tsFilePath] = {version: 0};
57+
this.rootFilePaths.push(tsFilePath);
58+
} else {
59+
this.fileRegistry[tsFilePath].version++;
60+
}
6061

61-
pathsToEmit.push(tsFilePath);
62-
});
62+
pathsToEmit.push(tsFilePath);
63+
});
6364

6465
treeDiff.removedPaths.forEach((tsFilePath) => {
6566
console.log('removing outputs for', tsFilePath);

0 commit comments

Comments
 (0)
X Tutup