Skip to content

Commit

Permalink
feat(@angular-devkit/schematics): add applyToSubTree rule
Browse files Browse the repository at this point in the history
This rule allows a group of rules to be applied to a scoped subdirectory of the current tree.
  • Loading branch information
clydin authored and mgechev committed Jan 18, 2019
1 parent 4913316 commit 0563e96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions etc/api/angular_devkit/schematics/src/_golden-api.d.ts
Expand Up @@ -30,6 +30,8 @@ export declare function applyPathTemplate<T extends PathTemplateData>(data: T, o

export declare function applyTemplates<T>(options: T): Rule;

export declare function applyToSubtree(path: string, rules: Rule[]): Rule;

export declare function asSource(rule: Rule): Source;

export declare type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null>;
Expand Down
19 changes: 19 additions & 0 deletions packages/angular_devkit/schematics/src/rules/base.ts
Expand Up @@ -12,6 +12,7 @@ import { SchematicsException } from '../exception/exception';
import { FilteredTree } from '../tree/filtered';
import { FilterHostTree, HostTree } from '../tree/host-tree';
import { FileEntry, FilePredicate, MergeStrategy, Tree } from '../tree/interface';
import { ScopedTree } from '../tree/scoped';
import {
branch,
empty as staticEmpty,
Expand Down Expand Up @@ -202,3 +203,21 @@ export function composeFileOperators(operators: FileOperator[]): FileOperator {
return current;
};
}

export function applyToSubtree(path: string, rules: Rule[]): Rule {
return (tree, context) => {
const scoped = new ScopedTree(tree, path);

return callRule(chain(rules), observableOf(scoped), context).pipe(
map(result => {
if (result === scoped) {
return tree;
} else {
throw new SchematicsException(
'Original tree must be returned from all rules when using "applyToSubtree".',
);
}
}),
);
};
}
21 changes: 20 additions & 1 deletion packages/angular_devkit/schematics/src/rules/base_spec.ts
Expand Up @@ -17,8 +17,9 @@ import { of as observableOf } from 'rxjs';
import { Rule, SchematicContext, Source } from '../engine/interface';
import { Tree } from '../tree/interface';
import { empty } from '../tree/static';
import { apply, chain } from './base';
import { apply, applyToSubtree, chain } from './base';
import { callRule, callSource } from './call';
import { move } from './move';


const context: SchematicContext = {
Expand Down Expand Up @@ -163,3 +164,21 @@ describe('partitionApplyMerge', () => {
.then(done, done.fail);
});
});

describe('applyToSubtree', () => {
it('works', done => {
const tree = new HostTree();
tree.create('a/b/file1', 'hello world');
tree.create('a/b/file2', 'hello world');
tree.create('a/c/file3', 'hello world');

callRule(applyToSubtree('a/b', [move('x')]), observableOf(tree), context)
.toPromise()
.then(result => {
expect(result.exists('a/b/x/file1')).toBe(true);
expect(result.exists('a/b/x/file2')).toBe(true);
expect(result.exists('a/c/file3')).toBe(true);
})
.then(done, done.fail);
});
});

0 comments on commit 0563e96

Please sign in to comment.