Skip to content

Commit 0a2a743

Browse files
committed
fix(@angular-devkit/schematics): support filtering a HostTree
1 parent dd7052a commit 0a2a743

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

packages/angular_devkit/schematics/src/rules/base.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { Observable, of as observableOf } from 'rxjs';
99
import { concatMap, last, map } from 'rxjs/operators';
1010
import { FileOperator, Rule, SchematicContext, Source } from '../engine/interface';
11-
import { FilteredTree } from '../tree/filtered';
11+
import { FilterTree, FilteredTree } from '../tree/filtered';
1212
import { FileEntry, FilePredicate, MergeStrategy, Tree } from '../tree/interface';
1313
import {
1414
branch,
@@ -92,7 +92,13 @@ export function noop(): Rule {
9292

9393

9494
export function filter(predicate: FilePredicate<boolean>): Rule {
95-
return (tree: Tree) => new FilteredTree(tree, predicate);
95+
return ((tree: Tree) => {
96+
if (tree instanceof VirtualTree) {
97+
return new FilteredTree(tree, predicate);
98+
} else {
99+
return new FilterTree(tree, predicate);
100+
}
101+
});
96102
}
97103

98104

packages/angular_devkit/schematics/src/rules/base_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// tslint:disable:non-null-operator
1010
import { Path, virtualFs } from '@angular-devkit/core';
1111
import {
12-
FileSystemTree,
12+
HostTree,
1313
MergeStrategy,
1414
partitionApplyMerge,
1515
} from '@angular-devkit/schematics';
@@ -138,7 +138,7 @@ describe('partitionApplyMerge', () => {
138138
'/test1': '',
139139
'/test2': '',
140140
});
141-
const tree = new FileSystemTree(host);
141+
const tree = new HostTree(host);
142142
const predicate = (path: Path) => path.indexOf('1') != -1;
143143

144144
const ruleYes: Rule = (tree: Tree) => {

packages/angular_devkit/schematics/src/tree/filtered.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { DelegateTree } from './delegate';
89
import { FilePredicate, Tree } from './interface';
910
import { VirtualTree } from './virtual';
1011

@@ -38,3 +39,15 @@ export class FilteredTree extends VirtualTree {
3839
});
3940
}
4041
}
42+
43+
export class FilterTree extends DelegateTree {
44+
constructor(tree: Tree, filter: FilePredicate<boolean> = () => true) {
45+
super(tree.branch());
46+
47+
tree.visit(path => {
48+
if (!filter(path)) {
49+
this.delete(path);
50+
}
51+
});
52+
}
53+
}

packages/angular_devkit/schematics/src/tree/filtered_spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { normalize } from '@angular-devkit/core';
9-
import { FilteredTree } from './filtered';
9+
import { FilterTree, FilteredTree } from './filtered';
10+
import { HostTree } from './host-tree';
1011
import { VirtualTree } from './virtual';
1112

1213

@@ -21,3 +22,30 @@ describe('FilteredTree', () => {
2122
expect(filtered.files.sort()).toEqual(['/file1', '/file3'].map(normalize));
2223
});
2324
});
25+
26+
describe('FilterTree', () => {
27+
it('works', () => {
28+
const tree = new HostTree();
29+
tree.create('/file1', '');
30+
tree.create('/file2', '');
31+
tree.create('/file3', '');
32+
33+
const filtered = new FilterTree(tree, p => p != '/file2');
34+
const filteredFiles: string[] = [];
35+
filtered.visit(path => filteredFiles.push(path));
36+
expect(filteredFiles.sort()).toEqual(['/file1', '/file3'].map(normalize));
37+
});
38+
39+
it('works with two filters', () => {
40+
const tree = new HostTree();
41+
tree.create('/file1', '');
42+
tree.create('/file2', '');
43+
tree.create('/file3', '');
44+
45+
const filtered = new FilterTree(tree, p => p != '/file2');
46+
const filtered2 = new FilterTree(filtered, p => p != '/file3');
47+
const filteredFiles: string[] = [];
48+
filtered2.visit(path => filteredFiles.push(path));
49+
expect(filteredFiles.sort()).toEqual(['/file1'].map(normalize));
50+
});
51+
});

packages/angular_devkit/schematics/src/tree/static.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { FilteredTree } from './filtered';
8+
import { FilterTree, FilteredTree } from './filtered';
99
import { HostTree } from './host-tree';
1010
import { FilePredicate, MergeStrategy, Tree } from './interface';
1111
import { VirtualTree } from './virtual';
@@ -16,34 +16,45 @@ export function empty() {
1616
}
1717

1818
export function branch(tree: Tree) {
19-
if (tree instanceof HostTree) {
20-
return tree.branch();
19+
// TODO: Remove VirtualTree usage in 7.0
20+
if (tree instanceof VirtualTree) {
21+
return VirtualTree.branch(tree);
2122
}
2223

23-
return VirtualTree.branch(tree);
24+
return tree.branch();
2425
}
2526

2627
export function merge(tree: Tree, other: Tree, strategy: MergeStrategy = MergeStrategy.Default) {
27-
if (tree instanceof HostTree) {
28-
tree.merge(other, strategy);
29-
30-
return tree;
28+
// TODO: Remove VirtualTree usage in 7.0
29+
if (tree instanceof VirtualTree) {
30+
return VirtualTree.merge(tree, other, strategy);
3131
}
3232

33-
return VirtualTree.merge(tree, other, strategy);
33+
tree.merge(other, strategy);
34+
35+
return tree;
3436
}
3537

3638
export function partition(tree: Tree, predicate: FilePredicate<boolean>): [Tree, Tree] {
37-
return [
38-
new FilteredTree(tree, predicate),
39-
new FilteredTree(tree, (path, entry) => !predicate(path, entry)),
40-
];
39+
// TODO: Remove VirtualTree usage in 7.0
40+
if (tree instanceof VirtualTree) {
41+
return [
42+
new FilteredTree(tree, predicate),
43+
new FilteredTree(tree, (path, entry) => !predicate(path, entry)),
44+
];
45+
} else {
46+
return [
47+
new FilterTree(tree, predicate),
48+
new FilterTree(tree, (path, entry) => !predicate(path, entry)),
49+
];
50+
}
4151
}
4252

4353
export function optimize(tree: Tree) {
44-
if (tree instanceof HostTree) {
45-
return tree;
54+
// TODO: Remove VirtualTree usage in 7.0
55+
if (tree instanceof VirtualTree) {
56+
return VirtualTree.optimize(tree);
4657
}
4758

48-
return VirtualTree.optimize(tree);
59+
return tree;
4960
}

0 commit comments

Comments
 (0)