Skip to content

Commit cf67b15

Browse files
committed
Initial integration of sockets tool
1 parent a397430 commit cf67b15

8 files changed

+103
-28
lines changed

lib/definitions/livesync.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ interface INativeScriptDeviceLiveSyncService extends IDeviceLiveSyncServiceBase
364364
* @param {Mobile.ILocalToDevicePathData[]} localToDevicePaths Object containing a mapping of file paths from the system to the device.
365365
* @return {Promise<void>}
366366
*/
367-
removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<void>;
367+
removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath?: string): Promise<void>;
368+
transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]>;
368369
}
369370

370371
interface IAndroidNativeScriptDeviceLiveSyncService {

lib/device-path-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class DevicePathProvider implements IDevicePathProvider {
2525
} else if (this.$mobileHelper.isAndroidPlatform(device.deviceInfo.platform)) {
2626
projectRoot = `/data/local/tmp/${options.appIdentifier}`;
2727
if (!options.getDirname) {
28-
const deviceLiveSyncService = this.$injector.resolve<AndroidDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: device });
28+
const deviceLiveSyncService = this.$injector.resolve<AndroidDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { device });
2929
const hashService = deviceLiveSyncService.getDeviceHashService(options.appIdentifier);
3030
const hashFile = options.syncAllFiles ? null : await hashService.doesShasumFileExistsOnDevice();
3131
const syncFolderName = options.watch || hashFile ? LiveSyncPaths.SYNC_DIR_NAME : LiveSyncPaths.FULLSYNC_DIR_NAME;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import * as path from "path";
88
import * as net from "net";
99

1010
export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
11-
private device: Mobile.IAndroidDevice;
1211
private port: number;
1312

14-
constructor(_device: Mobile.IDevice,
13+
constructor(
1514
private $mobileHelper: Mobile.IMobileHelper,
1615
private $devicePathProvider: IDevicePathProvider,
1716
private $injector: IInjector,
1817
private $androidProcessService: Mobile.IAndroidProcessService,
19-
protected $platformsData: IPlatformsData) {
20-
super($platformsData);
21-
this.device = <Mobile.IAndroidDevice>(_device);
18+
protected $platformsData: IPlatformsData,
19+
protected device: Mobile.IAndroidDevice) {
20+
super($platformsData, device);
2221
}
2322

2423
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 { cache } from "../../common/decorators";
5+
const LivesyncTool = require("nativescript-android-livesync-lib");
6+
7+
export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
8+
private port: number;
9+
private livesyncTool: any;
10+
11+
constructor(
12+
private data: IProjectData,
13+
private $injector: IInjector,
14+
protected $platformsData: IPlatformsData,
15+
protected $staticConfig: Config.IStaticConfig,
16+
protected device: Mobile.IAndroidDevice) {
17+
super($platformsData, device);
18+
this.livesyncTool = new LivesyncTool();
19+
}
20+
21+
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
22+
await this.device.applicationManager.startApplication({ appId: deviceAppData.appIdentifier, projectName: this.data.projectName });
23+
}
24+
25+
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
26+
//await this.connectLivesyncTool("", projectData.projectId);
27+
await this.livesyncTool.sendDoSyncOperation()
28+
}
29+
30+
public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
31+
await this.connectLivesyncTool(projectFilesPath, deviceAppData.appIdentifier);
32+
await this.livesyncTool.removeFilesArray(_.map(localToDevicePaths, (element: any) => { return element.filePath }));
33+
}
34+
35+
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> {
36+
await this.connectLivesyncTool(projectFilesPath, deviceAppData.appIdentifier);
37+
await this.livesyncTool.sendFilesArray(localToDevicePaths.map(localToDevicePathData => localToDevicePathData.getLocalPath()));
38+
39+
return localToDevicePaths;
40+
}
41+
42+
private async connectLivesyncTool(projectFilesPath: string, appIdentifier: string) {
43+
const adbPath = await this.$staticConfig.getAdbFilePath();
44+
await this.livesyncTool.connect({
45+
fullApplicationName: appIdentifier,
46+
port: this.port,
47+
deviceIdentifier: this.device.deviceInfo.identifier,
48+
baseDir: projectFilesPath,
49+
adbPath: adbPath
50+
});
51+
}
52+
53+
54+
@cache()
55+
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
56+
const adb = this.$injector.resolve(DeviceAndroidDebugBridge, { identifier: this.device.deviceInfo.identifier });
57+
return this.$injector.resolve(AndroidDeviceHashService, { adb, appIdentifier });
58+
}
59+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { AndroidDeviceLiveSyncService } from "./android-device-livesync-service";
2+
import { AndroidDeviceSocketsLiveSyncService } from "./android-device-livesync-sockets-service";
23
import { PlatformLiveSyncServiceBase } from "./platform-livesync-service-base";
4+
import * as semver from "semver";
35

46
export class AndroidLiveSyncService extends PlatformLiveSyncServiceBase implements IPlatformLiveSyncService {
57
constructor(protected $platformsData: IPlatformsData,
@@ -12,9 +14,12 @@ export class AndroidLiveSyncService extends PlatformLiveSyncServiceBase implemen
1214
super($fs, $logger, $platformsData, $projectFilesManager, $devicePathProvider, $projectFilesProvider);
1315
}
1416

15-
protected _getDeviceLiveSyncService(device: Mobile.IDevice, data: IProjectDir): INativeScriptDeviceLiveSyncService {
16-
const service = this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: device, data });
17-
return service;
17+
protected _getDeviceLiveSyncService(device: Mobile.IDevice, data: IProjectDir, frameworkVersion: string): INativeScriptDeviceLiveSyncService {
18+
if(semver.gte(frameworkVersion, "4.2.0")){
19+
return this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceSocketsLiveSyncService, { device, data });
20+
}
21+
22+
return this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { device, data });
1823
}
1924

2025
public async prepareForLiveSync(device: Mobile.IDevice, data: IProjectDir): Promise<void> { /* */ }

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import * as path from "path";
44
export abstract class DeviceLiveSyncServiceBase {
55
private static FAST_SYNC_FILE_EXTENSIONS = [".css", ".xml", ".html"];
66

7-
constructor(protected $platformsData: IPlatformsData) { }
7+
constructor(
8+
protected $platformsData: IPlatformsData,
9+
protected device: Mobile.IDevice
10+
) { }
811

912
public canExecuteFastSync(filePath: string, projectData: IProjectData, platform: string): boolean {
1013
const fastSyncFileExtensions = this.getFastLiveSyncFileExtensions(platform, projectData);
@@ -18,4 +21,15 @@ export abstract class DeviceLiveSyncServiceBase {
1821
return fastSyncFileExtensions;
1922
}
2023

24+
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> {
25+
let transferredFiles: Mobile.ILocalToDevicePathData[] = [];
26+
27+
if (isFullSync) {
28+
transferredFiles = await this.device.fileSystem.transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath);
29+
} else {
30+
transferredFiles = await this.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths);
31+
}
32+
33+
return transferredFiles;
34+
}
2135
}

lib/services/livesync/ios-device-livesync-service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ let currentPageReloadId = 0;
77

88
export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implements INativeScriptDeviceLiveSyncService {
99
private socket: net.Socket;
10-
private device: Mobile.IiOSDevice;
1110

12-
constructor(_device: Mobile.IiOSDevice,
13-
data: IProjectDir,
11+
constructor(
1412
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
1513
private $iOSNotification: IiOSNotification,
1614
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
1715
private $iOSDebuggerPortService: IIOSDebuggerPortService,
1816
private $logger: ILogger,
1917
private $fs: IFileSystem,
2018
private $processService: IProcessService,
21-
protected $platformsData: IPlatformsData) {
22-
super($platformsData);
23-
this.device = _device;
19+
protected $platformsData: IPlatformsData,
20+
protected device: Mobile.IiOSDevice) {
21+
super($platformsData, device);
2422
}
2523

2624
private async setupSocketIfNeeded(projectData: IProjectData): Promise<boolean> {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export abstract class PlatformLiveSyncServiceBase {
1414

1515
public getDeviceLiveSyncService(device: Mobile.IDevice, projectData: IProjectData): INativeScriptDeviceLiveSyncService {
1616
const key = device.deviceInfo.identifier + projectData.projectId;
17+
const frameworkVersion = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData).platformProjectService.getFrameworkVersion(projectData);
1718
if (!this._deviceLiveSyncServicesCache[key]) {
18-
this._deviceLiveSyncServicesCache[key] = this._getDeviceLiveSyncService(device, projectData);
19+
this._deviceLiveSyncServicesCache[key] = this._getDeviceLiveSyncService(device, projectData, frameworkVersion);
1920
}
2021

2122
return this._deviceLiveSyncServicesCache[key];
2223
}
2324

24-
protected abstract _getDeviceLiveSyncService(device: Mobile.IDevice, data: IProjectDir): INativeScriptDeviceLiveSyncService;
25+
protected abstract _getDeviceLiveSyncService(device: Mobile.IDevice, data: IProjectData, frameworkVersion: string): INativeScriptDeviceLiveSyncService;
2526

2627
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
2728
if (liveSyncInfo.isFullSync || liveSyncInfo.modifiedFilesData.length) {
@@ -44,7 +45,7 @@ export abstract class PlatformLiveSyncServiceBase {
4445

4546
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
4647
const localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, []);
47-
const modifiedFilesData = await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, true);
48+
const modifiedFilesData = await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, true, projectData);
4849

4950
return {
5051
modifiedFilesData,
@@ -77,7 +78,7 @@ export abstract class PlatformLiveSyncServiceBase {
7778
const localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData,
7879
projectFilesPath, existingFiles, []);
7980
modifiedLocalToDevicePaths.push(...localToDevicePaths);
80-
modifiedLocalToDevicePaths = await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, false);
81+
modifiedLocalToDevicePaths = await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, false, projectData);
8182
}
8283
}
8384

@@ -94,7 +95,7 @@ export abstract class PlatformLiveSyncServiceBase {
9495
modifiedLocalToDevicePaths.push(...localToDevicePaths);
9596

9697
const deviceLiveSyncService = this.getDeviceLiveSyncService(device, projectData);
97-
await deviceLiveSyncService.removeFiles(deviceAppData, localToDevicePaths);
98+
await deviceLiveSyncService.removeFiles(deviceAppData, localToDevicePaths, projectFilesPath);
9899
}
99100

100101
return {
@@ -104,13 +105,11 @@ export abstract class PlatformLiveSyncServiceBase {
104105
};
105106
}
106107

107-
protected async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> {
108+
protected async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean, projectData: IProjectData): Promise<Mobile.ILocalToDevicePathData[]> {
108109
let transferredFiles: Mobile.ILocalToDevicePathData[] = [];
109-
if (isFullSync) {
110-
transferredFiles = await deviceAppData.device.fileSystem.transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath);
111-
} else {
112-
transferredFiles = await deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths);
113-
}
110+
const deviceLiveSyncService = this.getDeviceLiveSyncService(deviceAppData.device, projectData);
111+
112+
transferredFiles = await deviceLiveSyncService.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, isFullSync);
114113

115114
this.logFilesSyncInformation(transferredFiles, "Successfully transferred %s.", this.$logger.info);
116115

0 commit comments

Comments
 (0)