1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.Architect = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const rxjs_1 = require("rxjs");
|
---|
13 | const operators_1 = require("rxjs/operators");
|
---|
14 | const api_1 = require("./api");
|
---|
15 | const schedule_by_name_1 = require("./schedule-by-name");
|
---|
16 | const inputSchema = require('./input-schema.json');
|
---|
17 | const outputSchema = require('./output-schema.json');
|
---|
18 | function _createJobHandlerFromBuilderInfo(info, target, host, registry, baseOptions) {
|
---|
19 | const jobDescription = {
|
---|
20 | name: target ? `{${api_1.targetStringFromTarget(target)}}` : info.builderName,
|
---|
21 | argument: { type: 'object' },
|
---|
22 | input: inputSchema,
|
---|
23 | output: outputSchema,
|
---|
24 | info,
|
---|
25 | };
|
---|
26 | function handler(argument, context) {
|
---|
27 | // Add input validation to the inbound bus.
|
---|
28 | const inboundBusWithInputValidation = context.inboundBus.pipe(operators_1.concatMap((message) => {
|
---|
29 | if (message.kind === core_1.experimental.jobs.JobInboundMessageKind.Input) {
|
---|
30 | const v = message.value;
|
---|
31 | const options = {
|
---|
32 | ...baseOptions,
|
---|
33 | ...v.options,
|
---|
34 | };
|
---|
35 | // Validate v against the options schema.
|
---|
36 | return registry.compile(info.optionSchema).pipe(operators_1.concatMap((validation) => validation(options)), operators_1.map((validationResult) => {
|
---|
37 | const { data, success, errors } = validationResult;
|
---|
38 | if (success) {
|
---|
39 | return { ...v, options: data };
|
---|
40 | }
|
---|
41 | throw new core_1.json.schema.SchemaValidationException(errors);
|
---|
42 | }), operators_1.map((value) => ({ ...message, value })));
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | return rxjs_1.of(message);
|
---|
46 | }
|
---|
47 | }),
|
---|
48 | // Using a share replay because the job might be synchronously sending input, but
|
---|
49 | // asynchronously listening to it.
|
---|
50 | operators_1.shareReplay(1));
|
---|
51 | // Make an inboundBus that completes instead of erroring out.
|
---|
52 | // We'll merge the errors into the output instead.
|
---|
53 | const inboundBus = rxjs_1.onErrorResumeNext(inboundBusWithInputValidation);
|
---|
54 | const output = rxjs_1.from(host.loadBuilder(info)).pipe(operators_1.concatMap((builder) => {
|
---|
55 | if (builder === null) {
|
---|
56 | throw new Error(`Cannot load builder for builderInfo ${JSON.stringify(info, null, 2)}`);
|
---|
57 | }
|
---|
58 | return builder.handler(argument, { ...context, inboundBus }).pipe(operators_1.map((output) => {
|
---|
59 | if (output.kind === core_1.experimental.jobs.JobOutboundMessageKind.Output) {
|
---|
60 | // Add target to it.
|
---|
61 | return {
|
---|
62 | ...output,
|
---|
63 | value: {
|
---|
64 | ...output.value,
|
---|
65 | ...(target ? { target } : 0),
|
---|
66 | },
|
---|
67 | };
|
---|
68 | }
|
---|
69 | else {
|
---|
70 | return output;
|
---|
71 | }
|
---|
72 | }));
|
---|
73 | }),
|
---|
74 | // Share subscriptions to the output, otherwise the the handler will be re-run.
|
---|
75 | operators_1.shareReplay());
|
---|
76 | // Separate the errors from the inbound bus into their own observable that completes when the
|
---|
77 | // builder output does.
|
---|
78 | const inboundBusErrors = inboundBusWithInputValidation.pipe(operators_1.ignoreElements(), operators_1.takeUntil(rxjs_1.onErrorResumeNext(output.pipe(operators_1.last()))));
|
---|
79 | // Return the builder output plus any input errors.
|
---|
80 | return rxjs_1.merge(inboundBusErrors, output);
|
---|
81 | }
|
---|
82 | return rxjs_1.of(Object.assign(handler, { jobDescription }));
|
---|
83 | }
|
---|
84 | /**
|
---|
85 | * A JobRegistry that resolves builder targets from the host.
|
---|
86 | */
|
---|
87 | class ArchitectBuilderJobRegistry {
|
---|
88 | constructor(_host, _registry, _jobCache, _infoCache) {
|
---|
89 | this._host = _host;
|
---|
90 | this._registry = _registry;
|
---|
91 | this._jobCache = _jobCache;
|
---|
92 | this._infoCache = _infoCache;
|
---|
93 | }
|
---|
94 | _resolveBuilder(name) {
|
---|
95 | const cache = this._infoCache;
|
---|
96 | if (cache) {
|
---|
97 | const maybeCache = cache.get(name);
|
---|
98 | if (maybeCache !== undefined) {
|
---|
99 | return maybeCache;
|
---|
100 | }
|
---|
101 | const info = rxjs_1.from(this._host.resolveBuilder(name)).pipe(operators_1.shareReplay(1));
|
---|
102 | cache.set(name, info);
|
---|
103 | return info;
|
---|
104 | }
|
---|
105 | return rxjs_1.from(this._host.resolveBuilder(name));
|
---|
106 | }
|
---|
107 | _createBuilder(info, target, options) {
|
---|
108 | const cache = this._jobCache;
|
---|
109 | if (target) {
|
---|
110 | const maybeHit = cache && cache.get(api_1.targetStringFromTarget(target));
|
---|
111 | if (maybeHit) {
|
---|
112 | return maybeHit;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | const maybeHit = cache && cache.get(info.builderName);
|
---|
117 | if (maybeHit) {
|
---|
118 | return maybeHit;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | const result = _createJobHandlerFromBuilderInfo(info, target, this._host, this._registry, options || {});
|
---|
122 | if (cache) {
|
---|
123 | if (target) {
|
---|
124 | cache.set(api_1.targetStringFromTarget(target), result.pipe(operators_1.shareReplay(1)));
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | cache.set(info.builderName, result.pipe(operators_1.shareReplay(1)));
|
---|
128 | }
|
---|
129 | }
|
---|
130 | return result;
|
---|
131 | }
|
---|
132 | get(name) {
|
---|
133 | const m = name.match(/^([^:]+):([^:]+)$/i);
|
---|
134 | if (!m) {
|
---|
135 | return rxjs_1.of(null);
|
---|
136 | }
|
---|
137 | return rxjs_1.from(this._resolveBuilder(name)).pipe(operators_1.concatMap((builderInfo) => (builderInfo ? this._createBuilder(builderInfo) : rxjs_1.of(null))), operators_1.first(null, null));
|
---|
138 | }
|
---|
139 | }
|
---|
140 | /**
|
---|
141 | * A JobRegistry that resolves targets from the host.
|
---|
142 | */
|
---|
143 | class ArchitectTargetJobRegistry extends ArchitectBuilderJobRegistry {
|
---|
144 | get(name) {
|
---|
145 | const m = name.match(/^{([^:]+):([^:]+)(?::([^:]*))?}$/i);
|
---|
146 | if (!m) {
|
---|
147 | return rxjs_1.of(null);
|
---|
148 | }
|
---|
149 | const target = {
|
---|
150 | project: m[1],
|
---|
151 | target: m[2],
|
---|
152 | configuration: m[3],
|
---|
153 | };
|
---|
154 | return rxjs_1.from(Promise.all([
|
---|
155 | this._host.getBuilderNameForTarget(target),
|
---|
156 | this._host.getOptionsForTarget(target),
|
---|
157 | ])).pipe(operators_1.concatMap(([builderStr, options]) => {
|
---|
158 | if (builderStr === null || options === null) {
|
---|
159 | return rxjs_1.of(null);
|
---|
160 | }
|
---|
161 | return this._resolveBuilder(builderStr).pipe(operators_1.concatMap((builderInfo) => {
|
---|
162 | if (builderInfo === null) {
|
---|
163 | return rxjs_1.of(null);
|
---|
164 | }
|
---|
165 | return this._createBuilder(builderInfo, target, options);
|
---|
166 | }));
|
---|
167 | }), operators_1.first(null, null));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | function _getTargetOptionsFactory(host) {
|
---|
171 | return core_1.experimental.jobs.createJobHandler((target) => {
|
---|
172 | return host.getOptionsForTarget(target).then((options) => {
|
---|
173 | if (options === null) {
|
---|
174 | throw new Error(`Invalid target: ${JSON.stringify(target)}.`);
|
---|
175 | }
|
---|
176 | return options;
|
---|
177 | });
|
---|
178 | }, {
|
---|
179 | name: '..getTargetOptions',
|
---|
180 | output: { type: 'object' },
|
---|
181 | argument: inputSchema.properties.target,
|
---|
182 | });
|
---|
183 | }
|
---|
184 | function _getProjectMetadataFactory(host) {
|
---|
185 | return core_1.experimental.jobs.createJobHandler((target) => {
|
---|
186 | return host.getProjectMetadata(target).then((options) => {
|
---|
187 | if (options === null) {
|
---|
188 | throw new Error(`Invalid target: ${JSON.stringify(target)}.`);
|
---|
189 | }
|
---|
190 | return options;
|
---|
191 | });
|
---|
192 | }, {
|
---|
193 | name: '..getProjectMetadata',
|
---|
194 | output: { type: 'object' },
|
---|
195 | argument: {
|
---|
196 | oneOf: [{ type: 'string' }, inputSchema.properties.target],
|
---|
197 | },
|
---|
198 | });
|
---|
199 | }
|
---|
200 | function _getBuilderNameForTargetFactory(host) {
|
---|
201 | return core_1.experimental.jobs.createJobHandler(async (target) => {
|
---|
202 | const builderName = await host.getBuilderNameForTarget(target);
|
---|
203 | if (!builderName) {
|
---|
204 | throw new Error(`No builder were found for target ${api_1.targetStringFromTarget(target)}.`);
|
---|
205 | }
|
---|
206 | return builderName;
|
---|
207 | }, {
|
---|
208 | name: '..getBuilderNameForTarget',
|
---|
209 | output: { type: 'string' },
|
---|
210 | argument: inputSchema.properties.target,
|
---|
211 | });
|
---|
212 | }
|
---|
213 | function _validateOptionsFactory(host, registry) {
|
---|
214 | return core_1.experimental.jobs.createJobHandler(async ([builderName, options]) => {
|
---|
215 | // Get option schema from the host.
|
---|
216 | const builderInfo = await host.resolveBuilder(builderName);
|
---|
217 | if (!builderInfo) {
|
---|
218 | throw new Error(`No builder info were found for builder ${JSON.stringify(builderName)}.`);
|
---|
219 | }
|
---|
220 | return registry
|
---|
221 | .compile(builderInfo.optionSchema)
|
---|
222 | .pipe(operators_1.concatMap((validation) => validation(options)), operators_1.switchMap(({ data, success, errors }) => {
|
---|
223 | if (success) {
|
---|
224 | return rxjs_1.of(data);
|
---|
225 | }
|
---|
226 | throw new core_1.json.schema.SchemaValidationException(errors);
|
---|
227 | }))
|
---|
228 | .toPromise();
|
---|
229 | }, {
|
---|
230 | name: '..validateOptions',
|
---|
231 | output: { type: 'object' },
|
---|
232 | argument: {
|
---|
233 | type: 'array',
|
---|
234 | items: [{ type: 'string' }, { type: 'object' }],
|
---|
235 | },
|
---|
236 | });
|
---|
237 | }
|
---|
238 | class Architect {
|
---|
239 | constructor(_host, registry = new core_1.json.schema.CoreSchemaRegistry(), additionalJobRegistry) {
|
---|
240 | this._host = _host;
|
---|
241 | this._jobCache = new Map();
|
---|
242 | this._infoCache = new Map();
|
---|
243 | const privateArchitectJobRegistry = new core_1.experimental.jobs.SimpleJobRegistry();
|
---|
244 | // Create private jobs.
|
---|
245 | privateArchitectJobRegistry.register(_getTargetOptionsFactory(_host));
|
---|
246 | privateArchitectJobRegistry.register(_getBuilderNameForTargetFactory(_host));
|
---|
247 | privateArchitectJobRegistry.register(_validateOptionsFactory(_host, registry));
|
---|
248 | privateArchitectJobRegistry.register(_getProjectMetadataFactory(_host));
|
---|
249 | const jobRegistry = new core_1.experimental.jobs.FallbackRegistry([
|
---|
250 | new ArchitectTargetJobRegistry(_host, registry, this._jobCache, this._infoCache),
|
---|
251 | new ArchitectBuilderJobRegistry(_host, registry, this._jobCache, this._infoCache),
|
---|
252 | privateArchitectJobRegistry,
|
---|
253 | ...(additionalJobRegistry ? [additionalJobRegistry] : []),
|
---|
254 | ]);
|
---|
255 | this._scheduler = new core_1.experimental.jobs.SimpleScheduler(jobRegistry, registry);
|
---|
256 | }
|
---|
257 | has(name) {
|
---|
258 | return this._scheduler.has(name);
|
---|
259 | }
|
---|
260 | scheduleBuilder(name, options, scheduleOptions = {}) {
|
---|
261 | // The below will match 'project:target:configuration'
|
---|
262 | if (!/^[^:]+:[^:]+(:[^:]+)?$/.test(name)) {
|
---|
263 | throw new Error('Invalid builder name: ' + JSON.stringify(name));
|
---|
264 | }
|
---|
265 | return schedule_by_name_1.scheduleByName(name, options, {
|
---|
266 | scheduler: this._scheduler,
|
---|
267 | logger: scheduleOptions.logger || new core_1.logging.NullLogger(),
|
---|
268 | currentDirectory: this._host.getCurrentDirectory(),
|
---|
269 | workspaceRoot: this._host.getWorkspaceRoot(),
|
---|
270 | analytics: scheduleOptions.analytics,
|
---|
271 | });
|
---|
272 | }
|
---|
273 | scheduleTarget(target, overrides = {}, scheduleOptions = {}) {
|
---|
274 | return schedule_by_name_1.scheduleByTarget(target, overrides, {
|
---|
275 | scheduler: this._scheduler,
|
---|
276 | logger: scheduleOptions.logger || new core_1.logging.NullLogger(),
|
---|
277 | currentDirectory: this._host.getCurrentDirectory(),
|
---|
278 | workspaceRoot: this._host.getWorkspaceRoot(),
|
---|
279 | analytics: scheduleOptions.analytics,
|
---|
280 | });
|
---|
281 | }
|
---|
282 | }
|
---|
283 | exports.Architect = Architect;
|
---|