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 all commits
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
111 changes: 48 additions & 63 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,81 +329,64 @@ 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,
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);
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.syncAfterInstall(device, watchInfo);
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
3 changes: 2 additions & 1 deletion lib/definitions/livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ declare global {
interface ILiveSyncWatchInfo extends IProjectDataComposition, IHasUseHotModuleReloadOption, IConnectTimeoutOption {
filesToRemove: string[];
filesToSync: string[];
isReinstalled: boolean;
liveSyncDeviceData: ILiveSyncDeviceDescriptor;
hmrData: IPlatformHmrData;
force?: boolean;
Expand Down Expand Up @@ -250,6 +249,8 @@ 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>;
syncAfterInstall(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<void>;
}

interface IRestartApplicationInfo {
Expand Down
9 changes: 3 additions & 6 deletions lib/services/livesync/ios-livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,16 @@ export class IOSLiveSyncService extends PlatformLiveSyncServiceBase implements I
};
}

@performanceLog()
public liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo> {
if (liveSyncInfo.isReinstalled) {
public async syncAfterInstall(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<void> {
if (!device.isEmulator) {
// In this case we should execute fullsync because iOS Runtime requires the full content of app dir to be extracted in the root of sync dir.
return this.fullSync({
await this.fullSync({
projectData: liveSyncInfo.projectData,
device,
liveSyncDeviceData: liveSyncInfo.liveSyncDeviceData,
watch: true,
useHotModuleReload: liveSyncInfo.useHotModuleReload
});
} else {
return super.liveSyncWatchAction(device, liveSyncInfo);
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/services/livesync/platform-livesync-service-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from "path";
import * as util from "util";
import { APP_FOLDER_NAME } from "../../constants";
import { getHash } from "../../common/helpers";
import { performanceLog } from "../../common/decorators";

export abstract class PlatformLiveSyncServiceBase {
private _deviceLiveSyncServicesCache: IDictionary<INativeScriptDeviceLiveSyncService> = {};
Expand Down Expand Up @@ -32,6 +33,8 @@ export abstract class PlatformLiveSyncServiceBase {
return shouldRestart;
}

public async syncAfterInstall(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<void> { /* intentionally left blank */ }

public async restartApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
const deviceLiveSyncService = this.getDeviceLiveSyncService(liveSyncInfo.deviceAppData.device, projectData);
this.$logger.info(`Restarting application on device ${liveSyncInfo.deviceAppData.device.deviceInfo.identifier}...`);
Expand Down Expand Up @@ -72,6 +75,7 @@ export abstract class PlatformLiveSyncServiceBase {
};
}

@performanceLog()
public async liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo> {
const projectData = liveSyncInfo.projectData;
const deviceLiveSyncService = this.getDeviceLiveSyncService(device, projectData);
Expand Down Expand Up @@ -122,7 +126,7 @@ export abstract class PlatformLiveSyncServiceBase {

return {
modifiedFilesData: modifiedLocalToDevicePaths,
isFullSync: liveSyncInfo.isReinstalled,
isFullSync: false,
deviceAppData,
useHotModuleReload: liveSyncInfo.useHotModuleReload
};
Expand All @@ -141,7 +145,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