Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 2b04bd4

Browse files
committed
.
1 parent 6c93339 commit 2b04bd4

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

packages/schematics/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ The tooling is responsible for the following tasks:
3131
1. Create a Sink and commit the result of the schematics to the Sink. Many sinks are provided by the library; FileSystemSink and DryRunSink are examples.
3232
1. Output any logs propagated by the library, including debugging information.
3333

34+
The tooling API is composed of the following pieces:
35+
36+
## Engine
37+
The `SchematicEngine` is responsible for loading and constructing `Collection`s and `Schematics`'. When creating an engine, the tooling provides an `EngineHost` interface that understands how to create a `CollectionDescription` from a name, and how to create a `Schematic
38+
3439
# Examples
3540

3641
## Simple
@@ -50,7 +55,7 @@ export default function MySchematic(options: any) {
5055
A few things from this example:
5156

5257
1. The function receives the list of options from the tooling.
53-
2. It returns a [`Rule`](src/engine/interface.ts#L73), which is a transformation from a `Tree` to another `Tree`.
58+
1. It returns a [`Rule`](src/engine/interface.ts#L73), which is a transformation from a `Tree` to another `Tree`.
5459

5560

5661

packages/schematics/src/engine/engine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class UnknownUrlSourceProtocol extends BaseException {
3232
export class SchematicEngine implements Engine {
3333
private _protocolMap = new Map<string, ProtocolHandler>();
3434

35-
constructor(private _options: EngineHost) {
35+
constructor(private _host: EngineHost) {
3636
// Default implementations.
3737
this._protocolMap.set('null', () => {
3838
return () => empty();
@@ -46,7 +46,7 @@ export class SchematicEngine implements Engine {
4646
}
4747

4848
createCollection(name: string): Collection | null {
49-
const description = this._options.loadCollection(name);
49+
const description = this._host.loadCollection(name);
5050
if (!description) {
5151
return null;
5252
}
@@ -55,7 +55,7 @@ export class SchematicEngine implements Engine {
5555
}
5656

5757
createSchematic<T>(name: string, collection: Collection, options: T): Schematic | null {
58-
const description = this._options.loadSchematic<T>(name, collection, options);
58+
const description = this._host.loadSchematic<T>(name, collection, options);
5959
if (!description) {
6060
return null;
6161
}

packages/schematics/src/engine/interface.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,25 @@ import {Url} from 'url';
1212

1313

1414
export interface EngineHost {
15-
loadCollection(name: string): CollectionDescription | null;
16-
loadSchematic<T>(name: string,
17-
collection: Collection,
18-
options: T): ResolvedSchematicDescription | null;
15+
loadCollection<T>(name: string): CollectionDescription<T> | null;
16+
loadSchematic<CT, ST>(name: string,
17+
collection: CollectionDescription<CT>): SchematicDescription<ST> | null;
1918
}
2019

2120

22-
export interface Schematic {
23-
readonly name: string;
24-
readonly description: string;
25-
readonly path: string;
26-
readonly collection: Collection;
21+
export interface Engine {
22+
createCollection<T>(name: string): Collection<T> | null;
23+
createSchematic<CT, ST>(name: string, collection: Collection<CT>): Schematic<CT, ST> | null;
24+
registerUrlProtocolHandler(protocol: string, handler: ProtocolHandler): void;
25+
createSourceFromUrl(url: Url): Source;
26+
}
27+
2728

28-
call(host: Observable<Tree>, parentContext: Partial<SchematicContext>): Observable<Tree>;
29+
export interface Schematic<CT, ST> {
30+
readonly description: SchematicDescription<ST>;
31+
readonly collection: Collection<CT>;
32+
33+
call(host: Observable<Tree>, parentContext: SchematicContext | null): Observable<Tree>;
2934
}
3035

3136

@@ -35,22 +40,13 @@ export interface ProtocolHandler {
3540

3641

3742

38-
export interface Engine {
39-
createCollection(name: string): Collection | null;
40-
createSchematic<T>(name: string, collection: Collection, options: T): Schematic | null;
41-
registerUrlProtocolHandler(protocol: string, handler: ProtocolHandler): void;
42-
createSourceFromUrl(url: Url): Source;
43-
}
44-
45-
46-
export interface Collection {
47-
readonly engine: Engine;
43+
export interface Collection<T> {
4844
readonly name: string;
49-
readonly path: string;
45+
readonly description: CollectionDescription<T>;
5046

5147
listSchematicNames(): string[];
52-
getSchematicDescription(name: string): SchematicDescription | null;
53-
createSchematic<T>(name: string, options: T): Schematic;
48+
getSchematicDescription<T>(name: string): SchematicDescription<T> | null;
49+
createSchematic(name: string): Schematic;
5450
}
5551

5652

@@ -62,25 +58,14 @@ export interface SchematicContext {
6258
}
6359

6460

65-
export interface CollectionDescription {
66-
readonly path: string;
67-
readonly name?: string;
68-
readonly version?: string;
69-
readonly schematics: { [name: string]: SchematicDescription };
70-
}
71-
72-
export interface SchematicDescription {
73-
readonly factory: string;
74-
readonly description: string;
75-
readonly schema?: string;
76-
}
77-
78-
79-
export interface ResolvedSchematicDescription extends SchematicDescription {
61+
export type CollectionDescription<T> = T & {
8062
readonly name: string;
81-
readonly path: string;
82-
readonly rule: Rule;
83-
}
63+
};
64+
export type SchematicDescription<T, OT> = T & {
65+
readonly name: string;
66+
readonly factory: RuleFactory<OT>;
67+
};
68+
8469

8570
export type RuleFactory<T> = (options: T) => Rule;
8671

packages/schematics_cli/src/simple-cli-engine.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,28 @@ import {
66
Collection,
77
CollectionDescription,
88
EngineHost,
9-
ResolvedSchematicDescription,
9+
SchematicDescription,
1010
RuleFactory,
1111
} from '@angular/schematics';
1212

1313

14+
export interface CliCollectionDescription extends CollectionDescription {
15+
readonly path: string;
16+
readonly name?: string;
17+
readonly version?: string;
18+
readonly schematics: { [name: string]: CliSchematicDescription };
19+
}
20+
21+
22+
export interface CliSchematicDescription {
23+
readonly factory: string;
24+
readonly description: string;
25+
readonly schema?: string;
26+
}
27+
28+
29+
30+
1431
/**
1532
* A simple EngineHost used by the CLI. When loading for a collection, it looks through the
1633
*/
@@ -35,7 +52,7 @@ export class SimpleCliEngineHost implements EngineHost {
3552

3653
loadSchematic<T>(name: string,
3754
collection: Collection,
38-
options: T): ResolvedSchematicDescription | null {
55+
options: T): SchematicDescription<T> | null {
3956
const collectionPath = dirname(collection.path);
4057
const description = collection.getSchematicDescription(name);
4158

0 commit comments

Comments
 (0)