forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
214 lines (184 loc) · 6.16 KB
/
base.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import { logging, schema, virtualFs } from '@angular-devkit/core';
import {
EMPTY,
Observable,
Subject,
concat,
concatMap,
defaultIfEmpty,
from,
ignoreElements,
last,
of,
tap,
throwError,
} from 'rxjs';
import { Engine, EngineHost, SchematicEngine } from '../engine';
import { UnsuccessfulWorkflowExecution } from '../exception/exception';
import { standardFormats } from '../formats';
import { DryRunEvent, DryRunSink } from '../sink/dryrun';
import { HostSink } from '../sink/host';
import { Sink } from '../sink/sink';
import { HostTree } from '../tree/host-tree';
import { Tree } from '../tree/interface';
import {
LifeCycleEvent,
RequiredWorkflowExecutionContext,
Workflow,
WorkflowExecutionContext,
} from './interface';
export interface BaseWorkflowOptions {
host: virtualFs.Host;
engineHost: EngineHost<{}, {}>;
registry?: schema.CoreSchemaRegistry;
force?: boolean;
dryRun?: boolean;
}
/**
* Base class for workflows. Even without abstract methods, this class should not be used without
* surrounding some initialization for the registry and host. This class only adds life cycle and
* dryrun/force support. You need to provide any registry and task executors that you need to
* support.
* See {@see NodeWorkflow} implementation for how to make a specialized subclass of this.
* TODO: add default set of CoreSchemaRegistry transforms. Once the job refactor is done, use that
* as the support for tasks.
*
* @public
*/
export abstract class BaseWorkflow implements Workflow {
protected _engine: Engine<{}, {}>;
protected _engineHost: EngineHost<{}, {}>;
protected _registry: schema.CoreSchemaRegistry;
protected _host: virtualFs.Host;
protected _reporter: Subject<DryRunEvent> = new Subject();
protected _lifeCycle: Subject<LifeCycleEvent> = new Subject();
protected _context: WorkflowExecutionContext[];
protected _force: boolean;
protected _dryRun: boolean;
constructor(options: BaseWorkflowOptions) {
this._host = options.host;
this._engineHost = options.engineHost;
if (options.registry) {
this._registry = options.registry;
} else {
this._registry = new schema.CoreSchemaRegistry(standardFormats);
this._registry.addPostTransform(schema.transforms.addUndefinedDefaults);
}
this._engine = new SchematicEngine(this._engineHost, this);
this._context = [];
this._force = options.force || false;
this._dryRun = options.dryRun || false;
}
get context(): Readonly<WorkflowExecutionContext> {
const maybeContext = this._context[this._context.length - 1];
if (!maybeContext) {
throw new Error('Cannot get context when workflow is not executing...');
}
return maybeContext;
}
get engine(): Engine<{}, {}> {
return this._engine;
}
get engineHost(): EngineHost<{}, {}> {
return this._engineHost;
}
get registry(): schema.SchemaRegistry {
return this._registry;
}
get reporter(): Observable<DryRunEvent> {
return this._reporter.asObservable();
}
get lifeCycle(): Observable<LifeCycleEvent> {
return this._lifeCycle.asObservable();
}
protected _createSinks(): Sink[] {
let error = false;
const dryRunSink = new DryRunSink(this._host, this._force);
const dryRunSubscriber = dryRunSink.reporter.subscribe((event) => {
this._reporter.next(event);
error = error || event.kind == 'error';
});
// We need two sinks if we want to output what will happen, and actually do the work.
return [
dryRunSink,
// Add a custom sink that clean ourselves and throws an error if an error happened.
{
commit() {
dryRunSubscriber.unsubscribe();
if (error) {
return throwError(new UnsuccessfulWorkflowExecution());
}
return of();
},
},
// Only add a HostSink if this is not a dryRun.
...(!this._dryRun ? [new HostSink(this._host, this._force)] : []),
];
}
execute(
options: Partial<WorkflowExecutionContext> & RequiredWorkflowExecutionContext,
): Observable<void> {
const parentContext = this._context[this._context.length - 1];
if (!parentContext) {
this._lifeCycle.next({ kind: 'start' });
}
/** Create the collection and the schematic. */
const collection = this._engine.createCollection(options.collection);
// Only allow private schematics if called from the same collection.
const allowPrivate =
options.allowPrivate || (parentContext && parentContext.collection === options.collection);
const schematic = collection.createSchematic(options.schematic, allowPrivate);
const sinks = this._createSinks();
this._lifeCycle.next({ kind: 'workflow-start' });
const context = {
...options,
debug: options.debug || false,
logger: options.logger || (parentContext && parentContext.logger) || new logging.NullLogger(),
parentContext,
};
this._context.push(context);
return schematic
.call(options.options, of(new HostTree(this._host)), { logger: context.logger })
.pipe(
concatMap((tree: Tree) => {
// Process all sinks.
return concat(
from(sinks).pipe(
concatMap((sink) => sink.commit(tree)),
ignoreElements(),
),
of(tree),
);
}),
concatMap(() => {
if (this._dryRun) {
return EMPTY;
}
this._lifeCycle.next({ kind: 'post-tasks-start' });
return this._engine
.executePostTasks()
.pipe(
tap({ complete: () => this._lifeCycle.next({ kind: 'post-tasks-end' }) }),
defaultIfEmpty(undefined),
last(),
);
}),
tap({
complete: () => {
this._lifeCycle.next({ kind: 'workflow-end' });
this._context.pop();
if (this._context.length == 0) {
this._lifeCycle.next({ kind: 'end' });
}
},
}),
);
}
}