|
| 1 | +import { DeviceLiveSyncServiceBase } from './device-livesync-service-base'; |
| 2 | +import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service"; |
| 3 | + |
| 4 | +export abstract class AndroidDeviceLiveSyncServiceBase extends DeviceLiveSyncServiceBase { |
| 5 | + private deviceHashServices: IDictionary<Mobile.IAndroidDeviceHashService>; |
| 6 | + |
| 7 | + constructor(protected $injector: IInjector, |
| 8 | + protected $platformsData: IPlatformsData, |
| 9 | + protected $filesHashService: IFilesHashService, |
| 10 | + protected $logger: ILogger, |
| 11 | + protected device: Mobile.IAndroidDevice) { |
| 12 | + super($platformsData, device); |
| 13 | + this.deviceHashServices = {}; |
| 14 | + } |
| 15 | + |
| 16 | + public abstract async transferFilesOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<void>; |
| 17 | + public abstract async transferDirectoryOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void>; |
| 18 | + |
| 19 | + public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService { |
| 20 | + const key = `${this.device.deviceInfo.identifier}${appIdentifier}`; |
| 21 | + if (!this.deviceHashServices[key]) { |
| 22 | + const deviceHashService = this.$injector.resolve(AndroidDeviceHashService, { adb: this.device.adb, appIdentifier }); |
| 23 | + this.deviceHashServices[key] = deviceHashService; |
| 24 | + } |
| 25 | + |
| 26 | + return this.deviceHashServices[key]; |
| 27 | + } |
| 28 | + |
| 29 | + public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, projectData: IProjectData, liveSyncDeviceInfo: ILiveSyncDeviceInfo, options: ITransferFilesOptions): Promise<Mobile.ILocalToDevicePathData[]> { |
| 30 | + const transferredFiles = await this.transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, options); |
| 31 | + await this.updateHashes(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo); |
| 32 | + return transferredFiles; |
| 33 | + } |
| 34 | + |
| 35 | + private async transferFilesCore(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, options: ITransferFilesOptions): Promise<Mobile.ILocalToDevicePathData[]> { |
| 36 | + if (options.force && options.isFullSync) { |
| 37 | + const hashFileDevicePath = this.getDeviceHashService(deviceAppData.appIdentifier).hashFileDevicePath; |
| 38 | + await this.device.fileSystem.deleteFile(hashFileDevicePath, deviceAppData.appIdentifier); |
| 39 | + this.$logger.trace("Before transfer directory on device ", localToDevicePaths); |
| 40 | + await this.transferDirectoryOnDevice(deviceAppData, localToDevicePaths, projectFilesPath); |
| 41 | + return localToDevicePaths; |
| 42 | + } |
| 43 | + |
| 44 | + const localToDevicePathsToTransfer = await this.getLocalToDevicePathsToTransfer(deviceAppData, localToDevicePaths, options); |
| 45 | + this.$logger.trace("Files to transfer: ", localToDevicePathsToTransfer); |
| 46 | + await this.transferFilesOnDevice(deviceAppData, localToDevicePathsToTransfer); |
| 47 | + return localToDevicePathsToTransfer; |
| 48 | + } |
| 49 | + |
| 50 | + private async getLocalToDevicePathsToTransfer(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], options: ITransferFilesOptions): Promise<Mobile.ILocalToDevicePathData[]> { |
| 51 | + if (options.force || !options.isFullSync) { |
| 52 | + return localToDevicePaths; |
| 53 | + } |
| 54 | + |
| 55 | + const changedLocalToDevicePaths = await this.getChangedLocalToDevicePaths(deviceAppData.appIdentifier, localToDevicePaths); |
| 56 | + return changedLocalToDevicePaths; |
| 57 | + } |
| 58 | + |
| 59 | + private async getChangedLocalToDevicePaths(appIdentifier: string, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<Mobile.ILocalToDevicePathData[]> { |
| 60 | + const deviceHashService = this.getDeviceHashService(appIdentifier); |
| 61 | + const currentHashes = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths); |
| 62 | + const oldHashes = (await deviceHashService.getShasumsFromDevice()) || {}; |
| 63 | + const changedHashes = deviceHashService.getChangedShasums(oldHashes, currentHashes); |
| 64 | + const changedFiles = _.keys(changedHashes); |
| 65 | + const changedLocalToDevicePaths = localToDevicePaths.filter(localToDevicePathData => changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0); |
| 66 | + return changedLocalToDevicePaths; |
| 67 | + } |
| 68 | + |
| 69 | + private async updateHashes(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectData: IProjectData, liveSyncDeviceInfo: ILiveSyncDeviceInfo): Promise<void> { |
| 70 | + const hashes = await this.updateHashesOnDevice(deviceAppData, localToDevicePaths, projectData, liveSyncDeviceInfo); |
| 71 | + this.updateLocalHashes(hashes, deviceAppData, projectData, liveSyncDeviceInfo); |
| 72 | + } |
| 73 | + |
| 74 | + private async updateHashesOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectData: IProjectData, liveSyncDeviceInfo: ILiveSyncDeviceInfo): Promise<IStringDictionary> { |
| 75 | + const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier); |
| 76 | + const currentHashes = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths); |
| 77 | + await deviceHashService.uploadHashFileToDevice(currentHashes); |
| 78 | + return currentHashes; |
| 79 | + } |
| 80 | + |
| 81 | + private updateLocalHashes(hashes: IStringDictionary, deviceAppData: Mobile.IDeviceAppData, projectData: IProjectData, liveSyncDeviceInfo: ILiveSyncDeviceInfo): void { |
| 82 | + const hashFilePath = liveSyncDeviceInfo.outputPath || this.$platformsData.getPlatformData(deviceAppData.platform, projectData).deviceBuildOutputPath; |
| 83 | + this.$filesHashService.saveHashes(hashes, hashFilePath); |
| 84 | + } |
| 85 | +} |
0 commit comments