forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schematic.ts
87 lines (77 loc) · 2.64 KB
/
schematic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException } from '@angular-devkit/core';
import { Observable, of as observableOf } from 'rxjs';
import { concatMap, first, map } from 'rxjs/operators';
import { callRule } from '../rules/call';
import { Tree } from '../tree/interface';
import { ScopedTree } from '../tree/scoped';
import {
Collection,
Engine,
ExecutionOptions,
RuleFactory,
Schematic,
SchematicDescription,
TypedSchematicContext,
} from './interface';
export class InvalidSchematicsNameException extends BaseException {
constructor(name: string) {
super(`Schematics has invalid name: "${name}".`);
}
}
export class SchematicImpl<CollectionT extends object, SchematicT extends object>
implements Schematic<CollectionT, SchematicT> {
constructor(private _description: SchematicDescription<CollectionT, SchematicT>,
private _factory: RuleFactory<{}>,
private _collection: Collection<CollectionT, SchematicT>,
private _engine: Engine<CollectionT, SchematicT>) {
if (!_description.name.match(/^[-@/_.a-zA-Z0-9]+$/)) {
throw new InvalidSchematicsNameException(_description.name);
}
}
get description() { return this._description; }
get collection() { return this._collection; }
call<OptionT extends object>(
options: OptionT,
host: Observable<Tree>,
parentContext?: Partial<TypedSchematicContext<CollectionT, SchematicT>>,
executionOptions?: Partial<ExecutionOptions>,
): Observable<Tree> {
const context = this._engine.createContext(this, parentContext, executionOptions);
return host
.pipe(
first(),
concatMap(tree => this._engine.transformOptions(this, options, context).pipe(
map(o => [tree, o]),
)),
concatMap(([tree, transformedOptions]: [Tree, OptionT]) => {
let input: Tree;
let scoped = false;
if (executionOptions && executionOptions.scope) {
scoped = true;
input = new ScopedTree(tree, executionOptions.scope);
} else {
input = tree;
}
return callRule(this._factory(transformedOptions), observableOf(input), context).pipe(
map(output => {
if (output === input) {
return tree;
} else if (scoped) {
tree.merge(output);
return tree;
} else {
return output;
}
}),
);
}),
);
}
}