|
| 1 | +import { DeviceAndroidDebugBridge } from "../../common/mobile/android/device-android-debug-bridge"; |
| 2 | +import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service"; |
| 3 | +import { DeviceLiveSyncServiceBase } from "./device-livesync-service-base"; |
| 4 | +import { APP_FOLDER_NAME } from "../../constants"; |
| 5 | +import { AndroidLivesyncTool } from "./android-livesync-tool"; |
| 6 | +import * as path from "path"; |
| 7 | + |
| 8 | +export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService { |
| 9 | + private livesyncTool: IAndroidLivesyncTool; |
| 10 | + private static STATUS_UPDATE_INTERVAL = 10000; |
| 11 | + |
| 12 | + constructor( |
| 13 | + private data: IProjectData, |
| 14 | + private $injector: IInjector, |
| 15 | + protected $platformsData: IPlatformsData, |
| 16 | + protected $staticConfig: Config.IStaticConfig, |
| 17 | + private $logger: ILogger, |
| 18 | + protected device: Mobile.IAndroidDevice, |
| 19 | + private $options: ICommonOptions, |
| 20 | + private $processService: IProcessService) { |
| 21 | + super($platformsData, device); |
| 22 | + this.livesyncTool = this.$injector.resolve(AndroidLivesyncTool); |
| 23 | + } |
| 24 | + |
| 25 | + public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> { |
| 26 | + const platformData = this.$platformsData.getPlatformData(deviceAppData.platform, this.data); |
| 27 | + const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME); |
| 28 | + await this.device.applicationManager.startApplication({ appId: deviceAppData.appIdentifier, projectName: this.data.projectName }); |
| 29 | + await this.connectLivesyncTool(projectFilesPath, this.data.projectId); |
| 30 | + } |
| 31 | + |
| 32 | + public async finalizeSync(liveSyncInfo: ILiveSyncResultInfo) { |
| 33 | + await this.doSync(liveSyncInfo); |
| 34 | + } |
| 35 | + |
| 36 | + private async doSync(liveSyncInfo: ILiveSyncResultInfo, {doRefresh = false}: {doRefresh?: boolean} = {}): Promise<IAndroidLivesyncSyncOperationResult> { |
| 37 | + const operationId = this.livesyncTool.generateOperationIdentifier(); |
| 38 | + |
| 39 | + let result = {operationId, didRefresh: true }; |
| 40 | + |
| 41 | + if (liveSyncInfo.modifiedFilesData.length) { |
| 42 | + |
| 43 | + const doSyncPromise = this.livesyncTool.sendDoSyncOperation(doRefresh, null, operationId); |
| 44 | + |
| 45 | + const syncInterval : NodeJS.Timer = setInterval(() => { |
| 46 | + if (this.livesyncTool.isOperationInProgress(operationId)) { |
| 47 | + this.$logger.info("Sync operation in progress..."); |
| 48 | + } |
| 49 | + }, AndroidDeviceSocketsLiveSyncService.STATUS_UPDATE_INTERVAL); |
| 50 | + |
| 51 | + const clearSyncInterval = () => { |
| 52 | + clearInterval(syncInterval); |
| 53 | + }; |
| 54 | + |
| 55 | + this.$processService.attachToProcessExitSignals(this, clearSyncInterval); |
| 56 | + doSyncPromise.then(clearSyncInterval, clearSyncInterval); |
| 57 | + |
| 58 | + result = await doSyncPromise; |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo) { |
| 65 | + const canExecuteFastSync = !liveSyncInfo.isFullSync && this.canExecuteFastSyncForPaths(liveSyncInfo.modifiedFilesData, projectData, this.device.deviceInfo.platform); |
| 66 | + |
| 67 | + const syncOperationResult = await this.doSync(liveSyncInfo, {doRefresh: canExecuteFastSync}); |
| 68 | + |
| 69 | + this.livesyncTool.end(); |
| 70 | + |
| 71 | + if (!canExecuteFastSync || !syncOperationResult.didRefresh) { |
| 72 | + await this.device.applicationManager.restartApplication({ appId: liveSyncInfo.deviceAppData.appIdentifier, projectName: projectData.projectName }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> { |
| 77 | + await this.livesyncTool.removeFiles(_.map(localToDevicePaths, (element: any) => element.filePath)); |
| 78 | + } |
| 79 | + |
| 80 | + public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> { |
| 81 | + let transferredFiles; |
| 82 | + |
| 83 | + if (isFullSync) { |
| 84 | + transferredFiles = await this._transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath); |
| 85 | + } else { |
| 86 | + transferredFiles = await this._transferFiles(localToDevicePaths); |
| 87 | + } |
| 88 | + |
| 89 | + return transferredFiles; |
| 90 | + } |
| 91 | + |
| 92 | + private async _transferFiles(localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<Mobile.ILocalToDevicePathData[]> { |
| 93 | + await this.livesyncTool.sendFiles(localToDevicePaths.map(localToDevicePathData => localToDevicePathData.getLocalPath())); |
| 94 | + |
| 95 | + return localToDevicePaths; |
| 96 | + } |
| 97 | + |
| 98 | + private async _transferDirectory(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<Mobile.ILocalToDevicePathData[]> { |
| 99 | + let transferredLocalToDevicePaths : Mobile.ILocalToDevicePathData[]; |
| 100 | + const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier); |
| 101 | + const currentShasums: IStringDictionary = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths); |
| 102 | + const oldShasums = await deviceHashService.getShasumsFromDevice(); |
| 103 | + |
| 104 | + if (this.$options.force || !oldShasums) { |
| 105 | + await this.livesyncTool.sendDirectory(projectFilesPath); |
| 106 | + await deviceHashService.uploadHashFileToDevice(currentShasums); |
| 107 | + transferredLocalToDevicePaths = localToDevicePaths; |
| 108 | + } else { |
| 109 | + const changedShasums = deviceHashService.getChangedShasums(oldShasums, currentShasums); |
| 110 | + const changedFiles = _.keys(changedShasums); |
| 111 | + if (changedFiles.length) { |
| 112 | + await this.livesyncTool.sendFiles(changedFiles); |
| 113 | + await deviceHashService.uploadHashFileToDevice(currentShasums); |
| 114 | + transferredLocalToDevicePaths = localToDevicePaths.filter(localToDevicePathData => changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0); |
| 115 | + } else { |
| 116 | + transferredLocalToDevicePaths = []; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return transferredLocalToDevicePaths ; |
| 121 | + } |
| 122 | + |
| 123 | + private async connectLivesyncTool(projectFilesPath: string, appIdentifier: string) { |
| 124 | + await this.livesyncTool.connect({ |
| 125 | + appIdentifier, |
| 126 | + deviceIdentifier: this.device.deviceInfo.identifier, |
| 127 | + appPlatformsPath: projectFilesPath |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService { |
| 132 | + const adb = this.$injector.resolve(DeviceAndroidDebugBridge, { identifier: this.device.deviceInfo.identifier }); |
| 133 | + return this.$injector.resolve(AndroidDeviceHashService, { adb, appIdentifier }); |
| 134 | + } |
| 135 | +} |
0 commit comments