-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcreate-builder.ts
204 lines (185 loc) · 6.67 KB
/
create-builder.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
/**
* @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 { experimental, isPromise, json, logging } from '@angular-devkit/core';
import { Observable, Subscription, from, isObservable, of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import {
BuilderContext,
BuilderHandlerFn,
BuilderInfo,
BuilderInput,
BuilderOutput,
BuilderOutputLike,
BuilderProgressState,
ScheduleOptions,
Target,
TypedBuilderProgress,
targetStringFromTarget,
} from './api';
import { Builder, BuilderSymbol, BuilderVersionSymbol } from './internal';
import { scheduleByName, scheduleByTarget } from './schedule-by-name';
export function createBuilder<
OptT extends json.JsonObject,
OutT extends BuilderOutput = BuilderOutput,
>(
fn: BuilderHandlerFn<OptT>,
): Builder<OptT> {
const cjh = experimental.jobs.createJobHandler;
const handler = cjh<json.JsonObject, BuilderInput, OutT>((options, context) => {
const scheduler = context.scheduler;
const progressChannel = context.createChannel('progress');
const logChannel = context.createChannel('log');
let currentState: BuilderProgressState = BuilderProgressState.Stopped;
let current = 0;
let status = '';
let total = 1;
function log(entry: logging.LogEntry) {
logChannel.next(entry);
}
function progress(progress: TypedBuilderProgress, context: BuilderContext) {
currentState = progress.state;
if (progress.state === BuilderProgressState.Running) {
current = progress.current;
total = progress.total !== undefined ? progress.total : total;
if (progress.status === undefined) {
progress.status = status;
} else {
status = progress.status;
}
}
progressChannel.next({
...progress as json.JsonObject,
...(context.target && { target: context.target }),
...(context.builder && { builder: context.builder }),
id: context.id,
});
}
return new Observable<OutT>(observer => {
const subscriptions: Subscription[] = [];
const inputSubscription = context.inboundBus.subscribe(
i => {
switch (i.kind) {
case experimental.jobs.JobInboundMessageKind.Stop:
observer.complete();
break;
case experimental.jobs.JobInboundMessageKind.Input:
onInput(i.value);
break;
}
},
);
function onInput(i: BuilderInput) {
const builder = i.info as BuilderInfo;
const loggerName = i.target
? targetStringFromTarget(i.target as Target)
: builder.builderName;
const logger = new logging.Logger(loggerName);
subscriptions.push(logger.subscribe(entry => log(entry)));
const context: BuilderContext = {
builder,
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
target: i.target as Target,
logger: logger,
id: i.id,
async scheduleTarget(
target: Target,
overrides: json.JsonObject = {},
scheduleOptions: ScheduleOptions = {},
) {
const run = await scheduleByTarget(target, overrides, {
scheduler,
logger: scheduleOptions.logger || logger.createChild(''),
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
});
// We don't want to subscribe errors and complete.
subscriptions.push(run.progress.subscribe(event => progressChannel.next(event)));
return run;
},
async scheduleBuilder(
builderName: string,
options: json.JsonObject = {},
scheduleOptions: ScheduleOptions = {},
) {
const run = await scheduleByName(builderName, options, {
scheduler,
logger: scheduleOptions.logger || logger.createChild(''),
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
});
// We don't want to subscribe errors and complete.
subscriptions.push(run.progress.subscribe(event => progressChannel.next(event)));
return run;
},
async getTargetOptions(target: Target) {
return scheduler.schedule<Target, json.JsonValue, json.JsonObject>(
'..getTargetOptions', target).output.toPromise();
},
reportRunning() {
switch (currentState) {
case BuilderProgressState.Waiting:
case BuilderProgressState.Stopped:
progress({ state: BuilderProgressState.Running, current: 0, total }, context);
break;
}
},
reportStatus(status: string) {
switch (currentState) {
case BuilderProgressState.Running:
progress({ state: currentState, status, current, total }, context);
break;
case BuilderProgressState.Waiting:
progress({ state: currentState, status }, context);
break;
}
},
reportProgress(current: number, total?: number, status?: string) {
switch (currentState) {
case BuilderProgressState.Running:
progress({ state: currentState, current, total, status }, context);
}
},
};
context.reportRunning();
let result: BuilderOutputLike;
try {
result = fn(i.options as OptT, context);
} catch (e) {
result = throwError(e);
}
if (isPromise(result)) {
result = from(result);
} else if (!isObservable(result)) {
result = of(result);
}
// Manage some state automatically.
progress({ state: BuilderProgressState.Running, current: 0, total: 1 }, context);
subscriptions.push(result.pipe(
tap(() => {
progress({ state: BuilderProgressState.Running, current: total }, context);
progress({ state: BuilderProgressState.Stopped }, context);
}),
).subscribe(
message => observer.next(message as OutT),
error => observer.error(error),
() => observer.complete(),
));
}
return () => {
subscriptions.forEach(x => x.unsubscribe());
inputSubscription.unsubscribe();
};
});
});
return {
handler,
[BuilderSymbol]: true,
[BuilderVersionSymbol]: require('../package.json').version,
};
}