5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import {
9
- Architect ,
10
- BuilderConfiguration ,
11
- BuilderContext ,
12
- TargetSpecifier ,
13
- } from '@angular-devkit/architect' ;
8
+ import { index2 } from '@angular-devkit/architect' ;
9
+ import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node' ;
14
10
import { experimental , json , schema , tags } from '@angular-devkit/core' ;
15
11
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
16
12
import { BepJsonWriter } from '../utilities/bep' ;
@@ -31,8 +27,8 @@ export interface ArchitectCommandOptions extends BaseCommandOptions {
31
27
export abstract class ArchitectCommand <
32
28
T extends ArchitectCommandOptions = ArchitectCommandOptions ,
33
29
> extends Command < ArchitectCommandOptions > {
34
- private _host = new NodeJsSyncHost ( ) ;
35
- protected _architect : Architect ;
30
+ protected _architect : index2 . Architect ;
31
+ protected _architectHost : WorkspaceNodeModulesArchitectHost ;
36
32
protected _workspace : experimental . workspace . Workspace ;
37
33
protected _registry : json . schema . SchemaRegistry ;
38
34
@@ -47,7 +43,13 @@ export abstract class ArchitectCommand<
47
43
this . _registry = new json . schema . CoreSchemaRegistry ( ) ;
48
44
this . _registry . addPostTransform ( json . schema . transforms . addUndefinedDefaults ) ;
49
45
50
- await this . _loadWorkspaceAndArchitect ( ) ;
46
+ const workspaceLoader = new WorkspaceLoader ( new NodeJsSyncHost ( ) ) ;
47
+
48
+ const workspace = await workspaceLoader . loadWorkspace ( this . workspace . root ) ;
49
+ this . _workspace = workspace ;
50
+
51
+ this . _architectHost = new WorkspaceNodeModulesArchitectHost ( workspace , this . workspace . root ) ;
52
+ this . _architect = new index2 . Architect ( this . _architectHost , this . _registry ) ;
51
53
52
54
if ( ! this . target ) {
53
55
if ( options . help ) {
@@ -67,7 +69,7 @@ export abstract class ArchitectCommand<
67
69
let projectName = options . project ;
68
70
const targetProjectNames : string [ ] = [ ] ;
69
71
for ( const name of this . _workspace . listProjectNames ( ) ) {
70
- if ( this . _architect . listProjectTargets ( name ) . includes ( this . target ) ) {
72
+ if ( this . _workspace . getProjectTargets ( name ) [ this . target ] ) {
71
73
targetProjectNames . push ( name ) ;
72
74
}
73
75
}
@@ -85,17 +87,20 @@ export abstract class ArchitectCommand<
85
87
const leftoverMap = new Map < string , { optionDefs : Option [ ] , parsedOptions : Arguments } > ( ) ;
86
88
let potentialProjectNames = new Set < string > ( targetProjectNames ) ;
87
89
for ( const name of targetProjectNames ) {
88
- const builderConfig = this . _architect . getBuilderConfiguration ( {
90
+ const builderName = await this . _architectHost . getBuilderNameForTarget ( {
89
91
project : name ,
90
92
target : this . target ,
91
93
} ) ;
92
94
93
95
if ( this . multiTarget ) {
94
- builderNames . add ( builderConfig . builder ) ;
96
+ builderNames . add ( builderName ) ;
95
97
}
96
98
97
- const builderDesc = await this . _architect . getBuilderDescription ( builderConfig ) . toPromise ( ) ;
98
- const optionDefs = await parseJsonSchemaToOptions ( this . _registry , builderDesc . schema ) ;
99
+ const builderDesc = await this . _architectHost . resolveBuilder ( builderName ) ;
100
+ const optionDefs = await parseJsonSchemaToOptions (
101
+ this . _registry ,
102
+ builderDesc . optionSchema as json . JsonObject ,
103
+ ) ;
99
104
const parsedOptions = parseArguments ( [ ...commandLeftovers ] , optionDefs ) ;
100
105
const builderLeftovers = parsedOptions [ '--' ] || [ ] ;
101
106
leftoverMap . set ( name , { optionDefs, parsedOptions } ) ;
@@ -157,20 +162,20 @@ export abstract class ArchitectCommand<
157
162
158
163
options . project = projectName ;
159
164
160
- const builderConf = this . _architect . getBuilderConfiguration ( {
165
+ const builderConf = await this . _architectHost . getBuilderNameForTarget ( {
161
166
project : projectName || ( targetProjectNames . length > 0 ? targetProjectNames [ 0 ] : '' ) ,
162
167
target : this . target ,
163
168
} ) ;
164
- const builderDesc = await this . _architect . getBuilderDescription ( builderConf ) . toPromise ( ) ;
169
+ const builderDesc = await this . _architectHost . resolveBuilder ( builderConf ) ;
165
170
166
171
this . description . options . push ( ...(
167
- await parseJsonSchemaToOptions ( this . _registry , builderDesc . schema )
172
+ await parseJsonSchemaToOptions ( this . _registry , builderDesc . optionSchema as json . JsonObject )
168
173
) ) ;
169
174
170
175
// Update options to remove analytics from options if the builder isn't safelisted.
171
176
for ( const o of this . description . options ) {
172
177
if ( o . userAnalytics ) {
173
- if ( ! isPackageNameSafeForAnalytics ( builderDesc . name ) ) {
178
+ if ( ! isPackageNameSafeForAnalytics ( builderConf ) ) {
174
179
o . userAnalytics = undefined ;
175
180
}
176
181
}
@@ -183,7 +188,8 @@ export abstract class ArchitectCommand<
183
188
184
189
protected async runBepTarget < T > (
185
190
command : string ,
186
- configuration : BuilderConfiguration < T > ,
191
+ configuration : index2 . Target ,
192
+ overrides : json . JsonObject ,
187
193
buildEventLog : string ,
188
194
) : Promise < number > {
189
195
const bep = new BepJsonWriter ( buildEventLog ) ;
@@ -193,7 +199,12 @@ export abstract class ArchitectCommand<
193
199
194
200
let last = 1 ;
195
201
let rebuild = false ;
196
- await this . _architect . run ( configuration , { logger : this . logger } ) . forEach ( event => {
202
+ const run = await this . _architect . scheduleTarget (
203
+ configuration ,
204
+ overrides ,
205
+ { logger : this . logger } ,
206
+ ) ;
207
+ await run . output . forEach ( event => {
197
208
last = event . success ? 0 : 1 ;
198
209
199
210
if ( rebuild ) {
@@ -207,19 +218,25 @@ export abstract class ArchitectCommand<
207
218
bep . writeBuildFinished ( last ) ;
208
219
} ) ;
209
220
221
+ await run . stop ( ) ;
222
+
210
223
return last ;
211
224
}
212
225
213
226
protected async runSingleTarget (
214
- targetSpec : TargetSpecifier ,
227
+ target : index2 . Target ,
215
228
targetOptions : string [ ] ,
216
- commandOptions : ArchitectCommandOptions & Arguments ) {
229
+ commandOptions : ArchitectCommandOptions & Arguments ,
230
+ ) {
217
231
// We need to build the builderSpec twice because architect does not understand
218
232
// overrides separately (getting the configuration builds the whole project, including
219
233
// overrides).
220
- const builderConf = this . _architect . getBuilderConfiguration ( targetSpec ) ;
221
- const builderDesc = await this . _architect . getBuilderDescription ( builderConf ) . toPromise ( ) ;
222
- const targetOptionArray = await parseJsonSchemaToOptions ( this . _registry , builderDesc . schema ) ;
234
+ const builderConf = await this . _architectHost . getBuilderNameForTarget ( target ) ;
235
+ const builderDesc = await this . _architectHost . resolveBuilder ( builderConf ) ;
236
+ const targetOptionArray = await parseJsonSchemaToOptions (
237
+ this . _registry ,
238
+ builderDesc . optionSchema as json . JsonObject ,
239
+ ) ;
223
240
const overrides = parseArguments ( targetOptions , targetOptionArray , this . logger ) ;
224
241
225
242
if ( overrides [ '--' ] ) {
@@ -229,25 +246,26 @@ export abstract class ArchitectCommand<
229
246
230
247
return 1 ;
231
248
}
232
- const realBuilderConf = this . _architect . getBuilderConfiguration ( { ...targetSpec , overrides } ) ;
233
- const builderContext : Partial < BuilderContext > = {
234
- logger : this . logger ,
235
- targetSpecifier : targetSpec ,
236
- } ;
237
249
238
250
if ( commandOptions . buildEventLog && [ 'build' , 'serve' ] . includes ( this . description . name ) ) {
239
251
// The build/serve commands supports BEP messaging
240
252
this . logger . warn ( 'BEP support is experimental and subject to change.' ) ;
241
253
242
254
return this . runBepTarget (
243
255
this . description . name ,
244
- realBuilderConf ,
256
+ target ,
257
+ overrides as json . JsonObject ,
245
258
commandOptions . buildEventLog as string ,
246
259
) ;
247
260
} else {
248
- const result = await this . _architect
249
- . run ( realBuilderConf , builderContext )
250
- . toPromise ( ) ;
261
+ const run = await this . _architect . scheduleTarget (
262
+ target ,
263
+ overrides as json . JsonObject ,
264
+ { logger : this . logger } ,
265
+ ) ;
266
+
267
+ const result = await run . output . toPromise ( ) ;
268
+ await run . stop ( ) ;
251
269
252
270
return result . success ? 0 : 1 ;
253
271
}
@@ -265,7 +283,11 @@ export abstract class ArchitectCommand<
265
283
// Running them in parallel would jumble the log messages.
266
284
let result = 0 ;
267
285
for ( const project of this . getProjectNamesByTarget ( this . target ) ) {
268
- result |= await this . runSingleTarget ( { ...targetSpec , project } , extra , options ) ;
286
+ result |= await this . runSingleTarget (
287
+ { ...targetSpec , project } as index2 . Target ,
288
+ extra ,
289
+ options ,
290
+ ) ;
269
291
}
270
292
271
293
return result ;
@@ -300,7 +322,7 @@ export abstract class ArchitectCommand<
300
322
301
323
private getProjectNamesByTarget ( targetName : string ) : string [ ] {
302
324
const allProjectsForTargetName = this . _workspace . listProjectNames ( ) . map ( projectName =>
303
- this . _architect . listProjectTargets ( projectName ) . includes ( targetName ) ? projectName : null ,
325
+ this . _workspace . getProjectTargets ( projectName ) [ targetName ] ? projectName : null ,
304
326
) . filter ( x => ! ! x ) as string [ ] ;
305
327
306
328
if ( this . multiTarget ) {
@@ -322,16 +344,7 @@ export abstract class ArchitectCommand<
322
344
}
323
345
}
324
346
325
- private async _loadWorkspaceAndArchitect ( ) {
326
- const workspaceLoader = new WorkspaceLoader ( this . _host ) ;
327
-
328
- const workspace = await workspaceLoader . loadWorkspace ( this . workspace . root ) ;
329
-
330
- this . _workspace = workspace ;
331
- this . _architect = await new Architect ( workspace ) . loadArchitect ( ) . toPromise ( ) ;
332
- }
333
-
334
- private _makeTargetSpecifier ( commandOptions : ArchitectCommandOptions ) : TargetSpecifier {
347
+ private _makeTargetSpecifier ( commandOptions : ArchitectCommandOptions ) : index2 . Target {
335
348
let project , target , configuration ;
336
349
337
350
if ( commandOptions . target ) {
@@ -358,7 +371,7 @@ export abstract class ArchitectCommand<
358
371
359
372
return {
360
373
project,
361
- configuration,
374
+ configuration : configuration || '' ,
362
375
target,
363
376
} ;
364
377
}
0 commit comments