|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { json } from '@angular-devkit/core'; |
| 9 | +import { EMPTY, from, of } from 'rxjs'; |
| 10 | +import { map, mergeMap } from 'rxjs/operators'; |
| 11 | +import { BuilderOutput, BuilderRun, createBuilder } from '../src/index2'; |
| 12 | +import { Schema as OperatorSchema } from './operator-schema'; |
| 13 | + |
| 14 | +export default createBuilder<json.JsonObject & OperatorSchema>((options, context) => { |
| 15 | + const allRuns: Promise<[number, BuilderRun]>[] = []; |
| 16 | + |
| 17 | + context.reportProgress(0, |
| 18 | + (options.targets ? options.targets.length : 0) |
| 19 | + + (options.builders ? options.builders.length : 0), |
| 20 | + ); |
| 21 | + |
| 22 | + if (options.targets) { |
| 23 | + allRuns.push(...options.targets.map(({ target: targetStr, overrides }, i) => { |
| 24 | + const [project, target, configuration] = targetStr.split(/:/g, 3); |
| 25 | + |
| 26 | + return context.scheduleTarget({ project, target, configuration }, overrides || {}) |
| 27 | + .then(run => [i, run] as [number, BuilderRun]); |
| 28 | + })); |
| 29 | + } |
| 30 | + |
| 31 | + if (options.builders) { |
| 32 | + allRuns.push(...options.builders.map(({ builder, options }, i) => { |
| 33 | + return context.scheduleBuilder(builder, options || {}) |
| 34 | + .then(run => [i, run] as [number, BuilderRun]); |
| 35 | + })); |
| 36 | + } |
| 37 | + |
| 38 | + const allResults: (BuilderOutput | null)[] = allRuns.map(() => null); |
| 39 | + let n = 0; |
| 40 | + context.reportProgress(n++, allRuns.length); |
| 41 | + |
| 42 | + return from(allRuns).pipe( |
| 43 | + mergeMap(runPromise => from(runPromise)), |
| 44 | + mergeMap(([i, run]: [number, BuilderRun]) => run.output.pipe(map(output => [i, output]))), |
| 45 | + mergeMap<[number, BuilderOutput], BuilderOutput>(([i, output]) => { |
| 46 | + allResults[i] = output; |
| 47 | + context.reportProgress(n++, allRuns.length); |
| 48 | + |
| 49 | + if (allResults.some(x => x === null)) { |
| 50 | + // Some builders aren't done running yet. |
| 51 | + return EMPTY; |
| 52 | + } else { |
| 53 | + return of({ |
| 54 | + success: allResults.every(x => x ? x.success : false), |
| 55 | + }); |
| 56 | + } |
| 57 | + }), |
| 58 | + ); |
| 59 | +}); |
0 commit comments