11import { Command , flags } from '@oclif/command'
2+ import assert from 'assert'
3+ import { spawnSync } from 'child_process'
24import { promises as fs } from 'fs'
5+ import path from 'path'
36
4- import { DEVICE_CONFIG_FLAGS , loadDeviceConfigs } from '../config/device'
5- import { COLLECTED_SYSTEM_STATE_DIR } from '../config/paths'
7+ import { DEVICE_CONFIG_FLAGS , DeviceBuildId , getDeviceBuildId , loadDeviceConfigs } from '../config/device'
8+ import { ADEVTOOL_DIR , COLLECTED_SYSTEM_STATE_DIR , OS_CHECKOUT_DIR } from '../config/paths'
69import { collectSystemState , serializeSystemState } from '../config/system-state'
710import { forEachDevice } from '../frontend/devices'
11+ import { DeviceImages , prepareFactoryImages } from '../frontend/source'
12+ import { loadBuildIndex } from '../images/build-index'
13+ import { spawnAsync } from '../util/process'
14+ import { generatePrep } from './generate-prep'
815
916export default class CollectState extends Command {
1017 static description = 'collect built system state for use with other commands'
@@ -28,21 +35,46 @@ export default class CollectState extends Command {
2835 required : true ,
2936 default : COLLECTED_SYSTEM_STATE_DIR ,
3037 } ) ,
38+ rebuild : flags . boolean ( {
39+ description : 'generate prep vendor module (same as generate-prep) and make an OS build before collecting state' ,
40+ default : false ,
41+ } ) ,
42+ allowOutReuse : flags . boolean ( {
43+ description : 'if --rebuild is specified, do not remove out/ dir before making an OS build' ,
44+ default : false ,
45+ } ) ,
3146 ...DEVICE_CONFIG_FLAGS ,
3247 }
3348
3449 async run ( ) {
3550 let {
36- flags : { aapt2 : aapt2Path , devices, outRoot, parallel, outPath } ,
51+ flags : { aapt2 : aapt2Path , devices, outRoot, parallel, outPath, rebuild , allowOutReuse } ,
3752 } = this . parse ( CollectState )
3853
3954 let configs = await loadDeviceConfigs ( devices )
4055
56+ let deviceImagesMap : Map < DeviceBuildId , DeviceImages >
57+ if ( rebuild ) {
58+ deviceImagesMap = await prepareFactoryImages ( await loadBuildIndex ( ) , configs )
59+ }
60+
4161 let isDir = ( await fs . stat ( outPath ) ) . isDirectory ( )
4262 await forEachDevice (
4363 configs ,
4464 parallel ,
4565 async config => {
66+ if ( rebuild ) {
67+ let deviceImages = deviceImagesMap . get ( getDeviceBuildId ( config ) )
68+ assert ( deviceImages !== undefined )
69+ await generatePrep ( config , deviceImages . unpackedFactoryImageDir , config . device . build_id )
70+ if ( ! allowOutReuse ) {
71+ await spawnAsync ( 'rm' , [ '-rf' , path . join ( OS_CHECKOUT_DIR , 'out' ) ] )
72+ }
73+ let res = spawnSync ( path . join ( ADEVTOOL_DIR , 'scripts/make-prep-build.sh' ) ,
74+ [ config . device . name ] , { stdio : 'inherit' } )
75+ console . assert ( res . status === 0 , `make-prep-build.sh failed, exit code ${ res . status } ` )
76+ }
77+
4678 let state = await collectSystemState ( config . device . name , outRoot , aapt2Path )
4779
4880 // Write
0 commit comments