-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathschedule-by-name.ts
154 lines (140 loc) · 3.95 KB
/
schedule-by-name.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
/**
* @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
*/
import { analytics, experimental, json, logging } from '@angular-devkit/core';
import { EMPTY, Subscription } from 'rxjs';
import { catchError, first, ignoreElements, map, shareReplay } from 'rxjs/operators';
import {
BuilderInfo,
BuilderInput,
BuilderOutput,
BuilderProgressReport,
BuilderRun,
Target,
targetStringFromTarget,
} from './api';
const progressSchema = require('./progress-schema.json');
let _uniqueId = 0;
export async function scheduleByName(
name: string,
buildOptions: json.JsonObject,
options: {
target?: Target;
scheduler: experimental.jobs.Scheduler;
logger: logging.LoggerApi;
workspaceRoot: string | Promise<string>;
currentDirectory: string | Promise<string>;
analytics?: analytics.Analytics;
},
): Promise<BuilderRun> {
const childLoggerName = options.target ? `{${targetStringFromTarget(options.target)}}` : name;
const logger = options.logger.createChild(childLoggerName);
const job = options.scheduler.schedule<{}, BuilderInput, BuilderOutput>(name, {});
let stateSubscription: Subscription;
const workspaceRoot = await options.workspaceRoot;
const currentDirectory = await options.currentDirectory;
const description = await job.description.toPromise();
const info = description.info as BuilderInfo;
const id = ++_uniqueId;
const message = {
id,
currentDirectory,
workspaceRoot,
info: info,
options: buildOptions,
...(options.target ? { target: options.target } : {}),
};
// Wait for the job to be ready.
if (job.state !== experimental.jobs.JobState.Started) {
stateSubscription = job.outboundBus.subscribe(
(event) => {
if (event.kind === experimental.jobs.JobOutboundMessageKind.Start) {
job.input.next(message);
}
},
() => {},
);
} else {
job.input.next(message);
}
const logChannelSub = job.getChannel<logging.LogEntry>('log').subscribe(
(entry) => {
logger.next(entry);
},
() => {},
);
const s = job.outboundBus.subscribe({
error() {},
complete() {
s.unsubscribe();
logChannelSub.unsubscribe();
if (stateSubscription) {
stateSubscription.unsubscribe();
}
},
});
const output = job.output.pipe(
map(
(output) =>
({
...output,
...(options.target ? { target: options.target } : 0),
info,
} as BuilderOutput),
),
shareReplay(),
);
// If there's an analytics object, take the job channel and report it to the analytics.
if (options.analytics) {
const reporter = new analytics.AnalyticsReporter(options.analytics);
job
.getChannel<analytics.AnalyticsReport>('analytics')
.subscribe((report) => reporter.report(report));
}
// Start the builder.
output.pipe(first()).subscribe({
error() {},
});
return {
id,
info,
// This is a getter so that it always returns the next output, and not the same one.
get result() {
return output.pipe(first()).toPromise();
},
output,
progress: job
.getChannel<BuilderProgressReport>('progress', progressSchema)
.pipe(shareReplay(1)),
stop() {
job.stop();
return job.outboundBus
.pipe(
ignoreElements(),
catchError(() => EMPTY),
)
.toPromise();
},
};
}
export async function scheduleByTarget(
target: Target,
overrides: json.JsonObject,
options: {
scheduler: experimental.jobs.Scheduler;
logger: logging.LoggerApi;
workspaceRoot: string | Promise<string>;
currentDirectory: string | Promise<string>;
analytics?: analytics.Analytics;
},
): Promise<BuilderRun> {
return scheduleByName(`{${targetStringFromTarget(target)}}`, overrides, {
...options,
target,
logger: options.logger,
});
}