Skip to content

fix: fix native change during livesync #4766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 47 additions & 62 deletions lib/controllers/run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ export class RunController extends EventEmitter implements IRunController {
await this.refreshApplicationWithDebug(projectData, liveSyncResultInfo, filesChangeEventData, deviceDescriptor, settings) :
await this.refreshApplicationWithoutDebug(projectData, liveSyncResultInfo, filesChangeEventData, deviceDescriptor, settings);

const device = liveSyncResultInfo.deviceAppData.device;

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
});

return result;
}

Expand Down Expand Up @@ -282,14 +292,6 @@ export class RunController extends EventEmitter implements IRunController {

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, {
Expand Down Expand Up @@ -327,22 +329,6 @@ export class RunController extends EventEmitter implements IRunController {
});

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,
Expand All @@ -355,53 +341,52 @@ export class RunController extends EventEmitter implements IRunController {
force: liveSyncInfo.force,
connectTimeout: 1000
};
let liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
const deviceAppData = await platformLiveSyncService.getAppData(_.merge({ device, watch: true }, watchInfo));

await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
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 };
}

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
});
await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, this.rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
await platformLiveSyncService.restartApplication(projectData, { deviceAppData, modifiedFilesData: [], isFullSync: false, useHotModuleReload: liveSyncInfo.useHotModuleReload });
} else {
const isInHMRMode = liveSyncInfo.useHotModuleReload && data.hmrData && data.hmrData.hash;
if (isInHMRMode) {
this.$hmrStatusService.watchHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
}

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
});
let liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);

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);
}
}

await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
}

this.$logger.info(`Successfully synced application ${liveSyncResultInfo.deviceAppData.appIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}.`);
this.$logger.info(`Successfully synced application ${deviceAppData.appIdentifier} on device ${device.deviceInfo.identifier}.`);
} catch (err) {
const allErrors = (<Mobile.IDevicesOperationError>err).allErrors;
this.$logger.warn(`Unable to apply changes for device: ${device.deviceInfo.identifier}. Error is: ${err && err.message}.`);

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,
});

this.emitCore(RunOnDeviceEvents.runOnDeviceError, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectIdentifiers[device.deviceInfo.platform.toLowerCase()],
error: err,
});
}
}
await this.stop({ projectDir: projectData.projectDir, deviceIdentifiers: [device.deviceInfo.identifier] });
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/definitions/livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ declare global {
restartApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void>;
shouldRestart(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<boolean>;
getDeviceLiveSyncService(device: Mobile.IDevice, projectData: IProjectData): INativeScriptDeviceLiveSyncService;
getAppData(syncInfo: IFullSyncInfo): Promise<Mobile.IDeviceAppData>;
}

interface IRestartApplicationInfo {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/livesync/platform-livesync-service-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export abstract class PlatformLiveSyncServiceBase {
return transferredFiles;
}

protected async getAppData(syncInfo: IFullSyncInfo): Promise<Mobile.IDeviceAppData> {
public async getAppData(syncInfo: IFullSyncInfo): Promise<Mobile.IDeviceAppData> {
const platform = syncInfo.device.deviceInfo.platform.toLowerCase();
const appIdentifier = syncInfo.projectData.projectIdentifiers[platform];
const deviceProjectRootOptions: IDeviceProjectRootOptions = _.assign({ appIdentifier }, syncInfo);
Expand Down