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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
"use strict";
/**
* @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.io/license
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Architect = void 0;
const core_1 = require("@angular-devkit/core");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const api_1 = require("./api");
const schedule_by_name_1 = require("./schedule-by-name");
const inputSchema = require('./input-schema.json');
const outputSchema = require('./output-schema.json');
function _createJobHandlerFromBuilderInfo(info, target, host, registry, baseOptions) {
const jobDescription = {
name: target ? `{${(0, api_1.targetStringFromTarget)(target)}}` : info.builderName,
argument: { type: 'object' },
input: inputSchema,
output: outputSchema,
info,
};
function handler(argument, context) {
// Add input validation to the inbound bus.
const inboundBusWithInputValidation = context.inboundBus.pipe((0, operators_1.concatMap)((message) => {
if (message.kind === core_1.experimental.jobs.JobInboundMessageKind.Input) {
const v = message.value;
const options = {
...baseOptions,
...v.options,
};
// Validate v against the options schema.
return registry.compile(info.optionSchema).pipe((0, operators_1.concatMap)((validation) => validation(options)), (0, operators_1.map)((validationResult) => {
const { data, success, errors } = validationResult;
if (success) {
return { ...v, options: data };
}
throw new core_1.json.schema.SchemaValidationException(errors);
}), (0, operators_1.map)((value) => ({ ...message, value })));
}
else {
return (0, rxjs_1.of)(message);
}
}),
// Using a share replay because the job might be synchronously sending input, but
// asynchronously listening to it.
(0, operators_1.shareReplay)(1));
// Make an inboundBus that completes instead of erroring out.
// We'll merge the errors into the output instead.
const inboundBus = (0, rxjs_1.onErrorResumeNext)(inboundBusWithInputValidation);
const output = (0, rxjs_1.from)(host.loadBuilder(info)).pipe((0, operators_1.concatMap)((builder) => {
if (builder === null) {
throw new Error(`Cannot load builder for builderInfo ${JSON.stringify(info, null, 2)}`);
}
return builder.handler(argument, { ...context, inboundBus }).pipe((0, operators_1.map)((output) => {
if (output.kind === core_1.experimental.jobs.JobOutboundMessageKind.Output) {
// Add target to it.
return {
...output,
value: {
...output.value,
...(target ? { target } : 0),
},
};
}
else {
return output;
}
}));
}),
// Share subscriptions to the output, otherwise the the handler will be re-run.
(0, operators_1.shareReplay)());
// Separate the errors from the inbound bus into their own observable that completes when the
// builder output does.
const inboundBusErrors = inboundBusWithInputValidation.pipe((0, operators_1.ignoreElements)(), (0, operators_1.takeUntil)((0, rxjs_1.onErrorResumeNext)(output.pipe((0, operators_1.last)()))));
// Return the builder output plus any input errors.
return (0, rxjs_1.merge)(inboundBusErrors, output);
}
return (0, rxjs_1.of)(Object.assign(handler, { jobDescription }));
}
/**
* A JobRegistry that resolves builder targets from the host.
*/
class ArchitectBuilderJobRegistry {
constructor(_host, _registry, _jobCache, _infoCache) {
this._host = _host;
this._registry = _registry;
this._jobCache = _jobCache;
this._infoCache = _infoCache;
}
_resolveBuilder(name) {
const cache = this._infoCache;
if (cache) {
const maybeCache = cache.get(name);
if (maybeCache !== undefined) {
return maybeCache;
}
const info = (0, rxjs_1.from)(this._host.resolveBuilder(name)).pipe((0, operators_1.shareReplay)(1));
cache.set(name, info);
return info;
}
return (0, rxjs_1.from)(this._host.resolveBuilder(name));
}
_createBuilder(info, target, options) {
const cache = this._jobCache;
if (target) {
const maybeHit = cache && cache.get((0, api_1.targetStringFromTarget)(target));
if (maybeHit) {
return maybeHit;
}
}
else {
const maybeHit = cache && cache.get(info.builderName);
if (maybeHit) {
return maybeHit;
}
}
const result = _createJobHandlerFromBuilderInfo(info, target, this._host, this._registry, options || {});
if (cache) {
if (target) {
cache.set((0, api_1.targetStringFromTarget)(target), result.pipe((0, operators_1.shareReplay)(1)));
}
else {
cache.set(info.builderName, result.pipe((0, operators_1.shareReplay)(1)));
}
}
return result;
}
get(name) {
const m = name.match(/^([^:]+):([^:]+)$/i);
if (!m) {
return (0, rxjs_1.of)(null);
}
return (0, rxjs_1.from)(this._resolveBuilder(name)).pipe((0, operators_1.concatMap)((builderInfo) => (builderInfo ? this._createBuilder(builderInfo) : (0, rxjs_1.of)(null))), (0, operators_1.first)(null, null));
}
}
/**
* A JobRegistry that resolves targets from the host.
*/
class ArchitectTargetJobRegistry extends ArchitectBuilderJobRegistry {
get(name) {
const m = name.match(/^{([^:]+):([^:]+)(?::([^:]*))?}$/i);
if (!m) {
return (0, rxjs_1.of)(null);
}
const target = {
project: m[1],
target: m[2],
configuration: m[3],
};
return (0, rxjs_1.from)(Promise.all([
this._host.getBuilderNameForTarget(target),
this._host.getOptionsForTarget(target),
])).pipe((0, operators_1.concatMap)(([builderStr, options]) => {
if (builderStr === null || options === null) {
return (0, rxjs_1.of)(null);
}
return this._resolveBuilder(builderStr).pipe((0, operators_1.concatMap)((builderInfo) => {
if (builderInfo === null) {
return (0, rxjs_1.of)(null);
}
return this._createBuilder(builderInfo, target, options);
}));
}), (0, operators_1.first)(null, null));
}
}
function _getTargetOptionsFactory(host) {
return core_1.experimental.jobs.createJobHandler((target) => {
return host.getOptionsForTarget(target).then((options) => {
if (options === null) {
throw new Error(`Invalid target: ${JSON.stringify(target)}.`);
}
return options;
});
}, {
name: '..getTargetOptions',
output: { type: 'object' },
argument: inputSchema.properties.target,
});
}
function _getProjectMetadataFactory(host) {
return core_1.experimental.jobs.createJobHandler((target) => {
return host.getProjectMetadata(target).then((options) => {
if (options === null) {
throw new Error(`Invalid target: ${JSON.stringify(target)}.`);
}
return options;
});
}, {
name: '..getProjectMetadata',
output: { type: 'object' },
argument: {
oneOf: [{ type: 'string' }, inputSchema.properties.target],
},
});
}
function _getBuilderNameForTargetFactory(host) {
return core_1.experimental.jobs.createJobHandler(async (target) => {
const builderName = await host.getBuilderNameForTarget(target);
if (!builderName) {
throw new Error(`No builder were found for target ${(0, api_1.targetStringFromTarget)(target)}.`);
}
return builderName;
}, {
name: '..getBuilderNameForTarget',
output: { type: 'string' },
argument: inputSchema.properties.target,
});
}
function _validateOptionsFactory(host, registry) {
return core_1.experimental.jobs.createJobHandler(async ([builderName, options]) => {
// Get option schema from the host.
const builderInfo = await host.resolveBuilder(builderName);
if (!builderInfo) {
throw new Error(`No builder info were found for builder ${JSON.stringify(builderName)}.`);
}
return registry
.compile(builderInfo.optionSchema)
.pipe((0, operators_1.concatMap)((validation) => validation(options)), (0, operators_1.switchMap)(({ data, success, errors }) => {
if (success) {
return (0, rxjs_1.of)(data);
}
throw new core_1.json.schema.SchemaValidationException(errors);
}))
.toPromise();
}, {
name: '..validateOptions',
output: { type: 'object' },
argument: {
type: 'array',
items: [{ type: 'string' }, { type: 'object' }],
},
});
}
class Architect {
constructor(_host, registry = new core_1.json.schema.CoreSchemaRegistry(), additionalJobRegistry) {
this._host = _host;
this._jobCache = new Map();
this._infoCache = new Map();
const privateArchitectJobRegistry = new core_1.experimental.jobs.SimpleJobRegistry();
// Create private jobs.
privateArchitectJobRegistry.register(_getTargetOptionsFactory(_host));
privateArchitectJobRegistry.register(_getBuilderNameForTargetFactory(_host));
privateArchitectJobRegistry.register(_validateOptionsFactory(_host, registry));
privateArchitectJobRegistry.register(_getProjectMetadataFactory(_host));
const jobRegistry = new core_1.experimental.jobs.FallbackRegistry([
new ArchitectTargetJobRegistry(_host, registry, this._jobCache, this._infoCache),
new ArchitectBuilderJobRegistry(_host, registry, this._jobCache, this._infoCache),
privateArchitectJobRegistry,
...(additionalJobRegistry ? [additionalJobRegistry] : []),
]);
this._scheduler = new core_1.experimental.jobs.SimpleScheduler(jobRegistry, registry);
}
has(name) {
return this._scheduler.has(name);
}
scheduleBuilder(name, options, scheduleOptions = {}) {
// The below will match 'project:target:configuration'
if (!/^[^:]+:[^:]+(:[^:]+)?$/.test(name)) {
throw new Error('Invalid builder name: ' + JSON.stringify(name));
}
return (0, schedule_by_name_1.scheduleByName)(name, options, {
scheduler: this._scheduler,
logger: scheduleOptions.logger || new core_1.logging.NullLogger(),
currentDirectory: this._host.getCurrentDirectory(),
workspaceRoot: this._host.getWorkspaceRoot(),
analytics: scheduleOptions.analytics,
});
}
scheduleTarget(target, overrides = {}, scheduleOptions = {}) {
return (0, schedule_by_name_1.scheduleByTarget)(target, overrides, {
scheduler: this._scheduler,
logger: scheduleOptions.logger || new core_1.logging.NullLogger(),
currentDirectory: this._host.getCurrentDirectory(),
workspaceRoot: this._host.getWorkspaceRoot(),
analytics: scheduleOptions.analytics,
});
}
}
exports.Architect = Architect;
|