Skip to content

Commit 27babb8

Browse files
committed
Generate hashes and init connection on beforeLiveSyncAction
1 parent cf67b15 commit 27babb8

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

lib/services/livesync/android-device-livesync-sockets-service.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { DeviceAndroidDebugBridge } from "../../common/mobile/android/device-android-debug-bridge";
22
import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service";
33
import { DeviceLiveSyncServiceBase } from "./device-livesync-service-base";
4-
import { cache } from "../../common/decorators";
4+
import { APP_FOLDER_NAME } from "../../constants";
5+
import * as path from "path";
6+
57
const LivesyncTool = require("nativescript-android-livesync-lib");
68

79
export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
@@ -13,32 +15,61 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
1315
private $injector: IInjector,
1416
protected $platformsData: IPlatformsData,
1517
protected $staticConfig: Config.IStaticConfig,
16-
protected device: Mobile.IAndroidDevice) {
18+
protected device: Mobile.IAndroidDevice,
19+
private $options: ICommonOptions) {
1720
super($platformsData, device);
1821
this.livesyncTool = new LivesyncTool();
1922
}
2023

2124
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
25+
const platformData = this.$platformsData.getPlatformData(deviceAppData.platform, this.data);
26+
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
27+
await this.connectLivesyncTool(projectFilesPath, this.data.projectId);
2228
await this.device.applicationManager.startApplication({ appId: deviceAppData.appIdentifier, projectName: this.data.projectName });
2329
}
2430

2531
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
26-
//await this.connectLivesyncTool("", projectData.projectId);
2732
await this.livesyncTool.sendDoSyncOperation()
2833
}
2934

3035
public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
31-
await this.connectLivesyncTool(projectFilesPath, deviceAppData.appIdentifier);
3236
await this.livesyncTool.removeFilesArray(_.map(localToDevicePaths, (element: any) => { return element.filePath }));
3337
}
3438

3539
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> {
36-
await this.connectLivesyncTool(projectFilesPath, deviceAppData.appIdentifier);
40+
let transferredFiles;
41+
42+
if (isFullSync) {
43+
transferredFiles = await this._transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath);
44+
} else {
45+
transferredFiles = this._transferFiles(deviceAppData, localToDevicePaths, projectFilesPath);
46+
}
47+
48+
return transferredFiles;
49+
}
50+
51+
private async _transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<Mobile.ILocalToDevicePathData[]> {
3752
await this.livesyncTool.sendFilesArray(localToDevicePaths.map(localToDevicePathData => localToDevicePathData.getLocalPath()));
3853

3954
return localToDevicePaths;
4055
}
4156

57+
private async _transferDirectory(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<Mobile.ILocalToDevicePathData[]> {
58+
const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier);
59+
const currentShasums: IStringDictionary = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
60+
const oldShasums = await deviceHashService.getShasumsFromDevice();
61+
62+
if(this.$options.force || !oldShasums) {
63+
this.livesyncTool.sendDirectory(projectFilesPath);
64+
65+
return localToDevicePaths;
66+
} else {
67+
const changedShasums = deviceHashService.getChnagedShasums(oldShasums, currentShasums);
68+
await this.livesyncTool.sendFilesArray(_.map(changedShasums, (hash: string, pathToFile: string) => pathToFile));
69+
await deviceHashService.uploadHashFileToDevice(currentShasums);
70+
}
71+
}
72+
4273
private async connectLivesyncTool(projectFilesPath: string, appIdentifier: string) {
4374
const adbPath = await this.$staticConfig.getAdbFilePath();
4475
await this.livesyncTool.connect({
@@ -50,8 +81,6 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
5081
});
5182
}
5283

53-
54-
@cache()
5584
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
5685
const adb = this.$injector.resolve(DeviceAndroidDebugBridge, { identifier: this.device.deviceInfo.identifier });
5786
return this.$injector.resolve(AndroidDeviceHashService, { adb, appIdentifier });

lib/services/livesync/platform-livesync-service-base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from "path";
22
import * as util from "util";
33
import { APP_FOLDER_NAME } from "../../constants";
4+
import { getHash } from "../common/helpers";
45

56
export abstract class PlatformLiveSyncServiceBase {
67
private _deviceLiveSyncServicesCache: IDictionary<INativeScriptDeviceLiveSyncService> = {};
@@ -13,8 +14,9 @@ export abstract class PlatformLiveSyncServiceBase {
1314
private $projectFilesProvider: IProjectFilesProvider) { }
1415

1516
public getDeviceLiveSyncService(device: Mobile.IDevice, projectData: IProjectData): INativeScriptDeviceLiveSyncService {
16-
const key = device.deviceInfo.identifier + projectData.projectId;
17-
const frameworkVersion = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData).platformProjectService.getFrameworkVersion(projectData);
17+
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
18+
const frameworkVersion = platformData.platformProjectService.getFrameworkVersion(projectData);
19+
const key = getHash(`${device.deviceInfo.identifier}${projectData.projectId}${projectData.projectDir}${frameworkVersion}`);
1820
if (!this._deviceLiveSyncServicesCache[key]) {
1921
this._deviceLiveSyncServicesCache[key] = this._getDeviceLiveSyncService(device, projectData, frameworkVersion);
2022
}

0 commit comments

Comments
 (0)