5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { Observable , of as observableOf } from 'rxjs' ;
9
- import { concatMap , last , map } from 'rxjs/operators' ;
10
- import { FileOperator , Rule , SchematicContext , Source } from '../engine/interface' ;
8
+ import { Observable , concat } from 'rxjs' ;
9
+ import { map , mapTo , toArray } from 'rxjs/operators' ;
10
+ import { FileOperator , Rule , Source } from '../engine/interface' ;
11
11
import { SchematicsException } from '../exception/exception' ;
12
12
import { FilterHostTree , HostTree } from '../tree/host-tree' ;
13
13
import { FileEntry , FilePredicate , MergeStrategy , Tree } from '../tree/interface' ;
14
14
import { ScopedTree } from '../tree/scoped' ;
15
- import {
16
- branch ,
17
- empty as staticEmpty ,
18
- merge as staticMerge ,
19
- partition as staticPartition ,
20
- } from '../tree/static' ;
15
+ import { empty as staticEmpty , partition } from '../tree/static' ;
21
16
import { callRule , callSource } from './call' ;
22
17
23
-
24
18
/**
25
19
* A Source that returns an tree as its single value.
26
20
*/
27
21
export function source ( tree : Tree ) : Source {
28
22
return ( ) => tree ;
29
23
}
30
24
31
-
32
25
/**
33
26
* A source that returns an empty tree.
34
27
*/
35
28
export function empty ( ) : Source {
36
29
return ( ) => staticEmpty ( ) ;
37
30
}
38
31
39
-
40
32
/**
41
33
* Chain multiple rules into a single rule.
42
34
*/
43
35
export function chain ( rules : Rule [ ] ) : Rule {
44
- return ( tree : Tree , context : SchematicContext ) => {
45
- return rules . reduce ( ( acc : Observable < Tree > , curr : Rule ) => {
46
- return callRule ( curr , acc , context ) ;
47
- } , observableOf ( tree ) ) ;
36
+ return ( tree , context ) => {
37
+ return rules . reduce (
38
+ ( acc : Tree | Observable < Tree > , curr : Rule ) => callRule ( curr , acc , context ) ,
39
+ tree ,
40
+ ) ;
48
41
} ;
49
42
}
50
43
51
-
52
44
/**
53
45
* Apply multiple rules to a source, and returns the source transformed.
54
46
*/
55
47
export function apply ( source : Source , rules : Rule [ ] ) : Source {
56
48
return context => callRule ( chain ( rules ) , callSource ( source , context ) , context ) ;
57
49
}
58
50
59
-
60
51
/**
61
52
* Merge an input tree with the source passed in.
62
53
*/
63
54
export function mergeWith ( source : Source , strategy : MergeStrategy = MergeStrategy . Default ) : Rule {
64
- return ( tree : Tree , context : SchematicContext ) => {
65
- const result = callSource ( source , context ) ;
66
-
67
- return result . pipe ( map ( other => staticMerge ( tree , other , strategy || context . strategy ) ) ) ;
55
+ return ( tree , context ) => {
56
+ return callSource ( source , context ) . pipe (
57
+ map ( sourceTree => tree . merge ( sourceTree , strategy || context . strategy ) ) ,
58
+ mapTo ( tree ) ,
59
+ ) ;
68
60
} ;
69
61
}
70
62
71
-
72
63
export function noop ( ) : Rule {
73
64
return ( ) => { } ;
74
65
}
75
66
76
-
77
67
export function filter ( predicate : FilePredicate < boolean > ) : Rule {
78
68
return ( ( tree : Tree ) => {
79
69
if ( HostTree . isHostTree ( tree ) ) {
@@ -84,25 +74,19 @@ export function filter(predicate: FilePredicate<boolean>): Rule {
84
74
} ) ;
85
75
}
86
76
87
-
88
77
export function asSource ( rule : Rule ) : Source {
89
- return apply ( empty ( ) , [ rule ] ) ;
78
+ return context => callRule ( rule , staticEmpty ( ) , context ) ;
90
79
}
91
80
92
-
93
81
export function branchAndMerge ( rule : Rule , strategy = MergeStrategy . Default ) : Rule {
94
- return ( tree : Tree , context : SchematicContext ) => {
95
- const branchedTree = branch ( tree ) ;
96
-
97
- return callRule ( rule , observableOf ( branchedTree ) , context )
98
- . pipe (
99
- last ( ) ,
100
- map ( t => staticMerge ( tree , t , strategy ) ) ,
101
- ) ;
82
+ return ( tree , context ) => {
83
+ return callRule ( rule , tree . branch ( ) , context ) . pipe (
84
+ map ( branch => tree . merge ( branch , strategy || context . strategy ) ) ,
85
+ mapTo ( tree ) ,
86
+ ) ;
102
87
} ;
103
88
}
104
89
105
-
106
90
export function when ( predicate : FilePredicate < boolean > , operator : FileOperator ) : FileOperator {
107
91
return ( entry : FileEntry ) => {
108
92
if ( predicate ( entry . path , entry ) ) {
@@ -113,30 +97,28 @@ export function when(predicate: FilePredicate<boolean>, operator: FileOperator):
113
97
} ;
114
98
}
115
99
116
-
117
100
export function partitionApplyMerge (
118
101
predicate : FilePredicate < boolean > ,
119
102
ruleYes : Rule ,
120
103
ruleNo ?: Rule ,
121
104
) : Rule {
122
- return ( tree : Tree , context : SchematicContext ) => {
123
- const [ yes , no ] = staticPartition ( tree , predicate ) ;
105
+ return ( tree , context ) => {
106
+ const [ yes , no ] = partition ( tree , predicate ) ;
124
107
125
- if ( ! ruleNo ) {
126
- // Shortcut.
127
- return callRule ( ruleYes , observableOf ( staticPartition ( tree , predicate ) [ 0 ] ) , context )
128
- . pipe ( map ( yesTree => staticMerge ( yesTree , no , context . strategy ) ) ) ;
129
- }
108
+ return concat (
109
+ callRule ( ruleYes , yes , context ) ,
110
+ callRule ( ruleNo || noop ( ) , no , context ) ,
111
+ ) . pipe (
112
+ toArray ( ) ,
113
+ map ( ( [ yesTree , noTree ] ) => {
114
+ yesTree . merge ( noTree , context . strategy ) ;
130
115
131
- return callRule ( ruleYes , observableOf ( yes ) , context )
132
- . pipe ( concatMap ( yesTree => {
133
- return callRule ( ruleNo , observableOf ( no ) , context )
134
- . pipe ( map ( noTree => staticMerge ( yesTree , noTree , context . strategy ) ) ) ;
135
- } ) ) ;
116
+ return yesTree ;
117
+ } ) ,
118
+ ) ;
136
119
} ;
137
120
}
138
121
139
-
140
122
export function forEach ( operator : FileOperator ) : Rule {
141
123
return ( tree : Tree ) => {
142
124
tree . visit ( ( path , entry ) => {
@@ -182,7 +164,7 @@ export function applyToSubtree(path: string, rules: Rule[]): Rule {
182
164
return ( tree , context ) => {
183
165
const scoped = new ScopedTree ( tree , path ) ;
184
166
185
- return callRule ( chain ( rules ) , observableOf ( scoped ) , context ) . pipe (
167
+ return callRule ( chain ( rules ) , scoped , context ) . pipe (
186
168
map ( result => {
187
169
if ( result === scoped ) {
188
170
return tree ;
0 commit comments