Skip to content

Commit ba3bb41

Browse files
committed
feat(@angular-devkit/schematics): returning a rule chains it
Instead of having to call the rule (which is an anti-pattern), the system now understands that returning a Rule should chain it to this current Rule. There is no way to do some pattern (like read a file to select between two Rules) without this feature.
1 parent c2ae7b1 commit ba3bb41

File tree

24 files changed

+58
-68
lines changed

24 files changed

+58
-68
lines changed

packages/_/devkit/package/factory.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { JsonAstObject, JsonValue, parseJsonAst } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
Tree,
1312
UpdateRecorder,
1413
apply,
@@ -88,18 +87,16 @@ export default function (options: Schema): Rule {
8887
.replace(/-/g, '_');
8988

9089
// Verify if we need to create a full project, or just add a new schematic.
91-
return (tree: Tree, context: SchematicContext) => {
92-
const source = apply(url('./project-files'), [
93-
template({
94-
...options as object,
95-
dot: '.',
96-
path,
97-
}),
98-
]);
90+
const source = apply(url('./project-files'), [
91+
template({
92+
...options as object,
93+
dot: '.',
94+
path,
95+
}),
96+
]);
9997

100-
return chain([
101-
mergeWith(source),
102-
addPackageToMonorepo(options, path),
103-
])(tree, context);
104-
};
98+
return chain([
99+
mergeWith(source),
100+
addPackageToMonorepo(options, path),
101+
]);
105102
}

packages/angular/pwa/pwa/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function addServiceWorker(options: PwaOptions): Rule {
3333
};
3434
delete swOptions.title;
3535

36-
return externalSchematic('@schematics/angular', 'service-worker', swOptions)(host, context);
36+
return externalSchematic('@schematics/angular', 'service-worker', swOptions);
3737
};
3838
}
3939

@@ -153,7 +153,7 @@ function addManifestToAssetsConfig(options: PwaOptions) {
153153
}
154154

155155
export default function (options: PwaOptions): Rule {
156-
return (host: Tree, context: SchematicContext) => {
156+
return (host: Tree) => {
157157
const workspace = getWorkspace(host);
158158
if (!options.project) {
159159
throw new SchematicsException('Option "project" is required.');
@@ -186,6 +186,6 @@ export default function (options: PwaOptions): Rule {
186186
])),
187187
updateIndexFile(options),
188188
addManifestToAssetsConfig(options),
189-
])(host, context);
189+
]);
190190
};
191191
}

packages/angular_devkit/schematics/src/engine/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null
217217
* know which types is the schematic or collection metadata, as they are both tooling specific.
218218
*/
219219
export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
220-
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | void;
220+
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export function callRule(rule: Rule,
8383
return observableOf(inputTree);
8484
} else if (TreeSymbol in result) {
8585
return observableOf(result as Tree);
86+
} else if (typeof result == 'function') {
87+
// This is considered a Rule, chain the rule and return its output.
88+
return callRule(result, input, context);
8689
} else if (isObservable(result)) {
8790
const obs = result as Observable<Tree>;
8891

packages/schematics/angular/app-shell/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function addUniversalTarget(options: AppShellOptions): Rule {
176176
delete universalOptions.index;
177177
delete universalOptions.sourceDir;
178178

179-
return schematic('universal', universalOptions)(host, context);
179+
return schematic('universal', universalOptions);
180180
};
181181
}
182182

packages/schematics/angular/application/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,6 @@ export default function (options: ApplicationOptions): Rule {
350350
move(sourceDir),
351351
]), MergeStrategy.Overwrite),
352352
schematic('e2e', e2eOptions),
353-
])(host, context);
353+
]);
354354
};
355355
}

packages/schematics/angular/class/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export default function (options: ClassOptions): Rule {
5252
move(parsedPath.path),
5353
]);
5454

55-
return branchAndMerge(mergeWith(templateSource))(host, context);
55+
return branchAndMerge(mergeWith(templateSource));
5656
};
5757
}

packages/schematics/angular/component/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -127,7 +126,7 @@ function buildSelector(options: ComponentOptions, projectPrefix: string) {
127126

128127

129128
export default function(options: ComponentOptions): Rule {
130-
return (host: Tree, context: SchematicContext) => {
129+
return (host: Tree) => {
131130
const workspace = getWorkspace(host);
132131
if (!options.project) {
133132
throw new SchematicsException('Option (project) is required.');
@@ -165,6 +164,6 @@ export default function(options: ComponentOptions): Rule {
165164
addDeclarationToNgModule(options),
166165
mergeWith(templateSource),
167166
])),
168-
])(host, context);
167+
]);
169168
};
170169
}

packages/schematics/angular/directive/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -103,7 +102,7 @@ function buildSelector(options: DirectiveOptions, projectPrefix: string) {
103102
}
104103

105104
export default function (options: DirectiveOptions): Rule {
106-
return (host: Tree, context: SchematicContext) => {
105+
return (host: Tree) => {
107106
const workspace = getWorkspace(host);
108107
if (!options.project) {
109108
throw new SchematicsException('Option (project) is required.');
@@ -138,6 +137,6 @@ export default function (options: DirectiveOptions): Rule {
138137
addDeclarationToNgModule(options),
139138
mergeWith(templateSource),
140139
])),
141-
])(host, context);
140+
]);
142141
};
143142
}

packages/schematics/angular/e2e/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function validateProjectName(projectName: string) {
146146
}
147147

148148
export default function (options: E2eOptions): Rule {
149-
return (host: Tree, context: SchematicContext) => {
149+
return (host: Tree) => {
150150
validateProjectName(options.name);
151151

152152
const workspace = getWorkspace(host);
@@ -171,6 +171,6 @@ export default function (options: E2eOptions): Rule {
171171
}),
172172
move(appDir),
173173
])),
174-
])(host, context);
174+
]);
175175
};
176176
}

packages/schematics/angular/enum/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ export default function (options: EnumOptions): Rule {
5353
branchAndMerge(chain([
5454
mergeWith(templateSource),
5555
])),
56-
])(host, context);
56+
]);
5757
};
5858
}

packages/schematics/angular/guard/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -28,7 +27,7 @@ import { Schema as GuardOptions } from './schema';
2827

2928

3029
export default function (options: GuardOptions): Rule {
31-
return (host: Tree, context: SchematicContext) => {
30+
return (host: Tree) => {
3231
const workspace = getWorkspace(host);
3332
if (!options.project) {
3433
throw new SchematicsException('Option (project) is required.');
@@ -56,6 +55,6 @@ export default function (options: GuardOptions): Rule {
5655
branchAndMerge(chain([
5756
mergeWith(templateSource),
5857
])),
59-
])(host, context);
58+
]);
6059
};
6160
}

packages/schematics/angular/interface/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -26,7 +25,7 @@ import { Schema as InterfaceOptions } from './schema';
2625

2726

2827
export default function (options: InterfaceOptions): Rule {
29-
return (host: Tree, context: SchematicContext) => {
28+
return (host: Tree) => {
3029
const workspace = getWorkspace(host);
3130
if (!options.project) {
3231
throw new SchematicsException('Option (project) is required.');
@@ -56,6 +55,6 @@ export default function (options: InterfaceOptions): Rule {
5655
branchAndMerge(chain([
5756
mergeWith(templateSource),
5857
])),
59-
])(host, context);
58+
]);
6059
};
6160
}

packages/schematics/angular/library/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,6 @@ export default function (options: LibraryOptions): Rule {
257257
context.addTask(new NodePackageInstallTask());
258258
}
259259
},
260-
])(host, context);
260+
]);
261261
};
262262
}

packages/schematics/angular/migrations/update-6/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,6 @@ export default function (): Rule {
786786

787787
return host;
788788
},
789-
])(host, context);
789+
]);
790790
};
791791
}

packages/schematics/angular/module/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { basename, dirname, normalize, relative, strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -72,7 +71,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
7271
}
7372

7473
export default function (options: ModuleOptions): Rule {
75-
return (host: Tree, context: SchematicContext) => {
74+
return (host: Tree) => {
7675
const workspace = getWorkspace(host);
7776
if (!options.project) {
7877
throw new SchematicsException('Option (project) is required.');
@@ -106,6 +105,6 @@ export default function (options: ModuleOptions): Rule {
106105
addDeclarationToNgModule(options),
107106
mergeWith(templateSource),
108107
])),
109-
])(host, context);
108+
]);
110109
};
111110
}

packages/schematics/angular/pipe/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -87,7 +86,7 @@ function addDeclarationToNgModule(options: PipeOptions): Rule {
8786
}
8887

8988
export default function (options: PipeOptions): Rule {
90-
return (host: Tree, context: SchematicContext) => {
89+
return (host: Tree) => {
9190
const workspace = getWorkspace(host);
9291
if (!options.project) {
9392
throw new SchematicsException('Option (project) is required.');
@@ -114,9 +113,11 @@ export default function (options: PipeOptions): Rule {
114113
move(parsedPath.path),
115114
]);
116115

117-
return branchAndMerge(chain([
116+
return branchAndMerge(
117+
chain([
118118
addDeclarationToNgModule(options),
119119
mergeWith(templateSource),
120-
]))(host, context);
120+
]),
121+
);
121122
};
122123
}

packages/schematics/angular/service-worker/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,6 @@ export default function (options: ServiceWorkerOptions): Rule {
187187
updateConfigFile(options),
188188
addDependencies(),
189189
updateAppModule(options),
190-
])(host, context);
190+
]);
191191
};
192192
}

packages/schematics/angular/service/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -25,7 +24,7 @@ import { buildDefaultPath } from '../utility/project';
2524
import { Schema as ServiceOptions } from './schema';
2625

2726
export default function (options: ServiceOptions): Rule {
28-
return (host: Tree, context: SchematicContext) => {
27+
return (host: Tree) => {
2928
const workspace = getWorkspace(host);
3029
if (!options.project) {
3130
throw new SchematicsException('Option (project) is required.');
@@ -50,6 +49,6 @@ export default function (options: ServiceOptions): Rule {
5049
move(parsedPath.path),
5150
]);
5251

53-
return mergeWith(templateSource)(host, context);
52+
return mergeWith(templateSource);
5453
};
5554
}

packages/schematics/angular/universal/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,6 @@ export default function (options: UniversalOptions): Rule {
251251
updateConfigFile(options, tsConfigDirectory),
252252
wrapBootstrapCall(options),
253253
addServerTransition(options),
254-
])(host, context);
254+
]);
255255
};
256256
}

packages/schematics/angular/workspace/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
12-
Tree,
1311
apply,
1412
mergeWith,
1513
template,
@@ -19,15 +17,12 @@ import { latestVersions } from '../utility/latest-versions';
1917
import { Schema as WorkspaceOptions } from './schema';
2018

2119
export default function (options: WorkspaceOptions): Rule {
22-
return (host: Tree, context: SchematicContext) => {
23-
24-
return mergeWith(apply(url('./files'), [
25-
template({
26-
utils: strings,
27-
...options,
28-
'dot': '.',
29-
latestVersions,
30-
}),
31-
]))(host, context);
32-
};
20+
return mergeWith(apply(url('./files'), [
21+
template({
22+
utils: strings,
23+
...options,
24+
'dot': '.',
25+
latestVersions,
26+
}),
27+
]));
3328
}

packages/schematics/package_update/all/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { SchematicsUpdateSchema } from '../schema';
1010

1111

1212
export default function(options: SchematicsUpdateSchema): Rule {
13-
return (tree: Tree, context: SchematicContext) => {
13+
return (_tree: Tree, context: SchematicContext) => {
1414
return chain(
1515
context.schematic.collection.listSchematicNames()
1616
.filter(name => name != context.schematic.description.name)
1717
.map(name => schematic(name, options)),
18-
)(tree, context);
18+
);
1919
};
2020
}

0 commit comments

Comments
 (0)