-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathrun-controller.ts
435 lines (362 loc) · 21.2 KB
/
run-controller.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { HmrConstants, DeviceDiscoveryEventNames } from "../common/constants";
import { PREPARE_READY_EVENT_NAME, TrackActionNames, DEBUGGER_DETACHED_EVENT_NAME, RunOnDeviceEvents, USER_INTERACTION_NEEDED_EVENT_NAME } from "../constants";
import { cache, performanceLog } from "../common/decorators";
import { EventEmitter } from "events";
export class RunController extends EventEmitter implements IRunController {
private rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
constructor(
protected $analyticsService: IAnalyticsService,
private $buildController: IBuildController,
private $debugController: IDebugController,
private $deviceInstallAppService: IDeviceInstallAppService,
protected $devicesService: Mobile.IDevicesService,
protected $errors: IErrors,
protected $injector: IInjector,
private $hmrStatusService: IHmrStatusService,
public $hooksService: IHooksService,
private $liveSyncServiceResolver: ILiveSyncServiceResolver,
private $liveSyncProcessDataService: ILiveSyncProcessDataService,
protected $logger: ILogger,
protected $mobileHelper: Mobile.IMobileHelper,
private $platformsDataService: IPlatformsDataService,
private $pluginsService: IPluginsService,
private $prepareController: IPrepareController,
private $prepareDataService: IPrepareDataService,
private $prepareNativePlatformService: IPrepareNativePlatformService,
protected $projectDataService: IProjectDataService
) {
super();
}
public async run(runData: IRunData): Promise<void> {
const { liveSyncInfo, deviceDescriptors } = runData;
const { projectDir } = liveSyncInfo;
const projectData = this.$projectDataService.getProjectData(projectDir);
await this.initializeSetup(projectData);
const platforms = this.$devicesService.getPlatformsFromDeviceDescriptors(deviceDescriptors);
const deviceDescriptorsForInitialSync = this.getDeviceDescriptorsForInitialSync(projectDir, deviceDescriptors);
this.$liveSyncProcessDataService.persistData(projectDir, deviceDescriptors, platforms);
const shouldStartWatcher = !liveSyncInfo.skipWatcher && this.$liveSyncProcessDataService.hasDeviceDescriptors(projectDir);
if (shouldStartWatcher && liveSyncInfo.useHotModuleReload) {
this.$hmrStatusService.attachToHmrStatusEvent();
}
this.$prepareController.on(PREPARE_READY_EVENT_NAME, async data => {
await this.syncChangedDataOnDevices(data, projectData, liveSyncInfo, deviceDescriptors);
});
await this.syncInitialDataOnDevices(projectData, liveSyncInfo, deviceDescriptorsForInitialSync);
this.attachDeviceLostHandler();
}
public async stop(data: IStopRunData): Promise<void> {
const { projectDir, deviceIdentifiers, stopOptions } = data;
const liveSyncProcessInfo = this.$liveSyncProcessDataService.getPersistedData(projectDir);
if (liveSyncProcessInfo && !liveSyncProcessInfo.isStopped) {
// In case we are coming from error during livesync, the current action is the one that erred (but we are still executing it),
// so we cannot await it as this will cause infinite loop.
const shouldAwaitPendingOperation = !stopOptions || stopOptions.shouldAwaitAllActions;
const deviceIdentifiersToRemove = deviceIdentifiers || _.map(liveSyncProcessInfo.deviceDescriptors, d => d.identifier);
const removedDeviceIdentifiers = _.remove(liveSyncProcessInfo.deviceDescriptors, descriptor => _.includes(deviceIdentifiersToRemove, descriptor.identifier))
.map(descriptor => descriptor.identifier);
// Handle the case when no more devices left for any of the persisted platforms
_.each(liveSyncProcessInfo.platforms, platform => {
const devices = this.$devicesService.getDevicesForPlatform(platform);
if (!devices || !devices.length) {
this.$prepareController.stopWatchers(projectDir, platform);
}
});
// In case deviceIdentifiers are not passed, we should stop the whole LiveSync.
if (!deviceIdentifiers || !deviceIdentifiers.length || !liveSyncProcessInfo.deviceDescriptors || !liveSyncProcessInfo.deviceDescriptors.length) {
if (liveSyncProcessInfo.timer) {
clearTimeout(liveSyncProcessInfo.timer);
}
_.each(liveSyncProcessInfo.platforms, platform => {
this.$prepareController.stopWatchers(projectDir, platform);
});
liveSyncProcessInfo.isStopped = true;
if (liveSyncProcessInfo.actionsChain && shouldAwaitPendingOperation) {
await liveSyncProcessInfo.actionsChain;
}
liveSyncProcessInfo.deviceDescriptors = [];
const projectData = this.$projectDataService.getProjectData(projectDir);
await this.$hooksService.executeAfterHooks('watch', {
hookArgs: {
projectData
}
});
} else if (liveSyncProcessInfo.currentSyncAction && shouldAwaitPendingOperation) {
await liveSyncProcessInfo.currentSyncAction;
}
// Emit RunOnDevice stopped when we've really stopped.
_.each(removedDeviceIdentifiers, deviceIdentifier => {
this.emitCore(RunOnDeviceEvents.runOnDeviceStopped, {
projectDir,
deviceIdentifier
});
});
}
}
public getDeviceDescriptors(data: { projectDir: string }): ILiveSyncDeviceDescriptor[] {
return this.$liveSyncProcessDataService.getDeviceDescriptors(data.projectDir);
}
protected async refreshApplication(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo, filesChangeEventData: IFilesChangeEventData, deviceDescriptor: ILiveSyncDeviceDescriptor, settings?: IRefreshApplicationSettings): Promise<IRestartApplicationInfo> {
const result = deviceDescriptor.debuggingEnabled ?
await this.refreshApplicationWithDebug(projectData, liveSyncResultInfo, filesChangeEventData, deviceDescriptor, settings) :
await this.refreshApplicationWithoutDebug(projectData, liveSyncResultInfo, filesChangeEventData, deviceDescriptor, settings);
return result;
}
protected async refreshApplicationWithDebug(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo, filesChangeEventData: IFilesChangeEventData, deviceDescriptor: ILiveSyncDeviceDescriptor, settings?: IRefreshApplicationSettings): Promise<IRestartApplicationInfo> {
const debugOptions = deviceDescriptor.debugOptions || {};
liveSyncResultInfo.waitForDebugger = !!debugOptions.debugBrk;
const refreshInfo = await this.refreshApplicationWithoutDebug(projectData, liveSyncResultInfo, filesChangeEventData, deviceDescriptor, settings);
// we do not stop the application when debugBrk is false, so we need to attach, instead of launch
// if we try to send the launch request, the debugger port will not be printed and the command will timeout
debugOptions.start = !debugOptions.debugBrk;
debugOptions.forceDebuggerAttachedEvent = refreshInfo.didRestart;
await this.$debugController.enableDebuggingCoreWithoutWaitingCurrentAction(projectData.projectDir, deviceDescriptor.identifier, debugOptions);
return refreshInfo;
}
@performanceLog()
protected async refreshApplicationWithoutDebug(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo, filesChangeEventData: IFilesChangeEventData, deviceDescriptor: ILiveSyncDeviceDescriptor, settings?: IRefreshApplicationSettings): Promise<IRestartApplicationInfo> {
const result = { didRestart: false };
const platform = liveSyncResultInfo.deviceAppData.platform;
const applicationIdentifier = projectData.projectIdentifiers[platform.toLowerCase()];
const platformLiveSyncService = this.$liveSyncServiceResolver.resolveLiveSyncService(platform);
try {
let shouldRestart = filesChangeEventData && (filesChangeEventData.hasNativeChanges || !filesChangeEventData.hasOnlyHotUpdateFiles);
if (!shouldRestart) {
shouldRestart = await platformLiveSyncService.shouldRestart(projectData, liveSyncResultInfo);
}
if (!shouldRestart) {
shouldRestart = !await platformLiveSyncService.tryRefreshApplication(projectData, liveSyncResultInfo);
}
if (shouldRestart) {
this.emit(DEBUGGER_DETACHED_EVENT_NAME, { deviceIdentifier: liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier });
await platformLiveSyncService.restartApplication(projectData, liveSyncResultInfo);
result.didRestart = true;
}
} catch (err) {
this.$logger.info(`Error while trying to start application ${applicationIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}. Error is: ${err.message || err}`);
const msg = `Unable to start application ${applicationIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}. Try starting it manually.`;
this.$logger.warn(msg);
const device = liveSyncResultInfo.deviceAppData.device;
const deviceIdentifier = device.deviceInfo.identifier;
if (!settings || !settings.shouldSkipEmitLiveSyncNotification) {
this.emitCore(RunOnDeviceEvents.runOnDeviceNotification, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
notification: msg
});
}
if (settings && settings.shouldCheckDeveloperDiscImage && (err.message || err) === "Could not find developer disk image") {
const attachDebuggerOptions: IAttachDebuggerData = {
platform: device.deviceInfo.platform,
isEmulator: device.isEmulator,
projectDir: projectData.projectDir,
deviceIdentifier,
debugOptions: deviceDescriptor.debugOptions,
outputPath: deviceDescriptor.buildData.outputPath
};
this.emit(USER_INTERACTION_NEEDED_EVENT_NAME, attachDebuggerOptions);
}
}
return result;
}
private getDeviceDescriptorsForInitialSync(projectDir: string, deviceDescriptors: ILiveSyncDeviceDescriptor[]) {
const currentRunData = this.$liveSyncProcessDataService.getPersistedData(projectDir);
const isAlreadyLiveSyncing = currentRunData && !currentRunData.isStopped;
// Prevent cases where liveSync is called consecutive times with the same device, for example [ A, B, C ] and then [ A, B, D ] - we want to execute initialSync only for D.
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, currentRunData.deviceDescriptors, "identifier") : deviceDescriptors;
return deviceDescriptorsForInitialSync;
}
private async initializeSetup(projectData: IProjectData): Promise<void> {
try {
await this.$pluginsService.ensureAllDependenciesAreInstalled(projectData);
} catch (err) {
this.$logger.trace(err);
this.$errors.failWithoutHelp(`Unable to install dependencies. Make sure your package.json is valid and all dependencies are correct. Error is: ${err.message}`);
}
}
@cache()
private attachDeviceLostHandler(): void {
this.$devicesService.on(DeviceDiscoveryEventNames.DEVICE_LOST, async (device: Mobile.IDevice) => {
this.$logger.trace(`Received ${DeviceDiscoveryEventNames.DEVICE_LOST} event in LiveSync service for ${device.deviceInfo.identifier}. Will stop LiveSync operation for this device.`);
for (const projectDir in this.$liveSyncProcessDataService.getAllPersistedData()) {
try {
const deviceDescriptors = this.getDeviceDescriptors({ projectDir });
if (_.find(deviceDescriptors, d => d.identifier === device.deviceInfo.identifier)) {
await this.stop({ projectDir, deviceIdentifiers: [device.deviceInfo.identifier] });
}
} catch (err) {
this.$logger.warn(`Unable to stop LiveSync operation for ${device.deviceInfo.identifier}.`, err);
}
}
});
}
private async syncInitialDataOnDevices(projectData: IProjectData, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
this.rebuiltInformation = {};
const deviceAction = async (device: Mobile.IDevice) => {
const deviceDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
const platformData = this.$platformsDataService.getPlatformData(device.deviceInfo.platform, projectData);
const prepareData = this.$prepareDataService.getPrepareData(liveSyncInfo.projectDir, device.deviceInfo.platform,
{
...liveSyncInfo,
...deviceDescriptor.buildData,
nativePrepare: { skipNativePrepare: !!deviceDescriptor.skipNativePrepare },
watch: !liveSyncInfo.skipWatcher,
});
const prepareResultData = await this.$prepareController.prepare(prepareData);
const buildData = { ...deviceDescriptor.buildData, buildForDevice: !device.isEmulator };
try {
let packageFilePath: string = null;
// Case where we have three devices attached, a change that requires build is found,
// we'll rebuild the app only for the first device, but we should install new package on all three devices.
if (this.rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || this.rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator)) {
packageFilePath = this.rebuiltInformation[platformData.platformNameLowerCase].packageFilePath;
await this.$deviceInstallAppService.installOnDevice(device, buildData, packageFilePath);
} else {
const shouldBuild = prepareResultData.hasNativeChanges || await this.$buildController.shouldBuild(buildData);
if (shouldBuild) {
packageFilePath = await deviceDescriptor.buildAction();
this.rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath };
} else {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.LiveSync,
device,
projectDir: projectData.projectDir
});
}
await this.$deviceInstallAppService.installOnDeviceIfNeeded(device, buildData, packageFilePath);
}
const platformLiveSyncService = this.$liveSyncServiceResolver.resolveLiveSyncService(platformData.platformNameLowerCase);
const { force, useHotModuleReload, skipWatcher } = liveSyncInfo;
const liveSyncResultInfo = await platformLiveSyncService.fullSync({ force, useHotModuleReload, projectData, device, watch: !skipWatcher, liveSyncDeviceData: deviceDescriptor });
await this.refreshApplication(projectData, liveSyncResultInfo, null, deviceDescriptor);
this.emitCore(RunOnDeviceEvents.runOnDeviceExecuted, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
syncedFiles: liveSyncResultInfo.modifiedFilesData.map(m => m.getLocalPath()),
isFullSync: liveSyncResultInfo.isFullSync
});
this.$logger.info(`Successfully synced application ${liveSyncResultInfo.deviceAppData.appIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}.`);
this.emitCore(RunOnDeviceEvents.runOnDeviceStarted, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()]
});
} catch (err) {
this.$logger.warn(`Unable to apply changes on device: ${device.deviceInfo.identifier}. Error is: ${err.message}.`);
this.emitCore(RunOnDeviceEvents.runOnDeviceError, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
error: err,
});
}
};
await this.addActionToChain(projectData.projectDir, () => this.$devicesService.execute(deviceAction, (device: Mobile.IDevice) => _.some(deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier)));
}
private async syncChangedDataOnDevices(data: IFilesChangeEventData, projectData: IProjectData, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
this.rebuiltInformation = {};
const deviceAction = async (device: Mobile.IDevice) => {
const deviceDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
const platformData = this.$platformsDataService.getPlatformData(data.platform, projectData);
const prepareData = this.$prepareDataService.getPrepareData(liveSyncInfo.projectDir, device.deviceInfo.platform,
{
...liveSyncInfo,
...deviceDescriptor.buildData,
nativePrepare: { skipNativePrepare: !!deviceDescriptor.skipNativePrepare },
watch: !liveSyncInfo.skipWatcher,
});
try {
if (data.hasNativeChanges) {
const rebuiltInfo = this.rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || this.rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator);
if (!rebuiltInfo) {
await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
await deviceDescriptor.buildAction();
this.rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath: null };
}
await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, this.rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
}
const isInHMRMode = liveSyncInfo.useHotModuleReload && data.hmrData && data.hmrData.hash;
if (isInHMRMode) {
this.$hmrStatusService.watchHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
}
const platformLiveSyncService = this.$liveSyncServiceResolver.resolveLiveSyncService(device.deviceInfo.platform);
const watchInfo = {
liveSyncDeviceData: deviceDescriptor,
projectData,
filesToRemove: <any>[],
filesToSync: data.files,
isReinstalled: false,
hmrData: data.hmrData,
useHotModuleReload: liveSyncInfo.useHotModuleReload,
force: liveSyncInfo.force,
connectTimeout: 1000
};
let liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
this.emitCore(RunOnDeviceEvents.runOnDeviceExecuted, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
syncedFiles: liveSyncResultInfo.modifiedFilesData.map(m => m.getLocalPath()),
isFullSync: liveSyncResultInfo.isFullSync
});
if (!liveSyncResultInfo.didRecover && isInHMRMode) {
const status = await this.$hmrStatusService.getHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
if (status === HmrConstants.HMR_ERROR_STATUS) {
watchInfo.filesToSync = data.hmrData.fallbackFiles;
liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
// We want to force a restart of the application.
liveSyncResultInfo.isFullSync = true;
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
this.emitCore(RunOnDeviceEvents.runOnDeviceExecuted, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
syncedFiles: liveSyncResultInfo.modifiedFilesData.map(m => m.getLocalPath()),
isFullSync: liveSyncResultInfo.isFullSync
});
}
}
this.$logger.info(`Successfully synced application ${liveSyncResultInfo.deviceAppData.appIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}.`);
} catch (err) {
const allErrors = (<Mobile.IDevicesOperationError>err).allErrors;
if (allErrors && _.isArray(allErrors)) {
for (const deviceError of allErrors) {
this.$logger.warn(`Unable to apply changes for device: ${deviceError.deviceIdentifier}. Error is: ${deviceError.message}.`);
this.emitCore(RunOnDeviceEvents.runOnDeviceError, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
error: err,
});
}
}
}
};
await this.addActionToChain(projectData.projectDir, () => this.$devicesService.execute(deviceAction, (device: Mobile.IDevice) => {
const liveSyncProcessInfo = this.$liveSyncProcessDataService.getPersistedData(projectData.projectDir);
return (data.platform.toLowerCase() === device.deviceInfo.platform.toLowerCase()) && liveSyncProcessInfo && _.some(liveSyncProcessInfo.deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier);
}));
}
private async addActionToChain<T>(projectDir: string, action: () => Promise<T>): Promise<T> {
const liveSyncInfo = this.$liveSyncProcessDataService.getPersistedData(projectDir);
if (liveSyncInfo) {
liveSyncInfo.actionsChain = liveSyncInfo.actionsChain.then(async () => {
if (!liveSyncInfo.isStopped) {
liveSyncInfo.currentSyncAction = action();
const res = await liveSyncInfo.currentSyncAction;
return res;
}
});
const result = await liveSyncInfo.actionsChain;
return result;
}
}
private emitCore(event: string, data: ILiveSyncEventData): void {
this.$logger.trace(`Will emit event ${event} with data`, data);
this.emit(event, data);
}
}
$injector.register("runController", RunController);