Skip to content

Commit fb8af69

Browse files
FatmeKristianDD
authored andcommitted
1 parent 4e3477e commit fb8af69

File tree

7 files changed

+529
-27
lines changed

7 files changed

+529
-27
lines changed

lib/definitions/livesync-global.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as stream from "stream";
2+
3+
declare global {
4+
interface IDuplexSocket extends stream.Duplex {
5+
uid?: string;
6+
}
7+
}
8+

lib/definitions/livesync.d.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,70 @@ interface IAndroidNativeScriptDeviceLiveSyncService {
377377
getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService;
378378
}
379379

380+
interface ILivesyncTool {
381+
/**
382+
* Creates new socket connection.
383+
* @param configuration - The configuration to the socket connection.
384+
* @returns {Promise<void>}
385+
*/
386+
connect(configuration: ILivesyncToolConfiguration): Promise<void>;
387+
/**
388+
* Sends a file through the socket.
389+
* @param filePath - The full path to the file.
390+
* @returns {Promise<void>}
391+
*/
392+
sendFile(filePath: string): Promise<void>;
393+
/**
394+
* Sends files through the socket.
395+
* @param filePaths - Array of files that will be send by the socket.
396+
* @returns {Promise<void>}
397+
*/
398+
sendFiles(filePaths: string[]): Promise<void>;
399+
/**
400+
* Sends all files from directory by the socket.
401+
* @param directoryPath - The path to the directory which files will be send by the socket.
402+
* @returns {Promise<void>}
403+
*/
404+
sendDirectory(directoryPath: string): Promise<void>;
405+
/**
406+
* Removes file
407+
* @param filePath - The full path to the file.
408+
* @returns {Promise<boolean>}
409+
*/
410+
removeFile(filePath: string): Promise<boolean>;
411+
/**
412+
* Removes files
413+
* @param filePaths - Array of files that will be removed.
414+
* @returns {Promise<boolean[]>}
415+
*/
416+
removeFiles(filePaths: string[]): Promise<boolean[]>;
417+
/**
418+
* Sends doSyncOperation that will be handeled by the runtime.
419+
* @param operationId - The identifier of the operation
420+
* @param timeout - The timeout in miliseconds
421+
* @returns {Promise<void>}
422+
*/
423+
sendDoSyncOperation(operationId: string, timeout?: number): Promise<void>;
424+
/**
425+
* Generates new operation identifier.
426+
*/
427+
generateOperationIdentifier(): string;
428+
/**
429+
* Checks if the current operation is in progress.
430+
* @param operationId - The identifier of the operation.
431+
*/
432+
isOperationInProgress(operationId: string): boolean;
433+
end(): void;
434+
}
435+
436+
interface ILivesyncToolConfiguration {
437+
appIdentifier: string;
438+
deviceIdentifier: string;
439+
appPlatformsPath: string; // path to platforms/android/app/src/main/assets/app/
440+
localHostAddress?: string;
441+
errorHandler?: any;
442+
}
443+
380444
interface IDeviceProjectRootOptions {
381445
appIdentifier: string;
382446
getDirname?: boolean;

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import { DeviceAndroidDebugBridge } from "../../common/mobile/android/device-and
22
import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service";
33
import { DeviceLiveSyncServiceBase } from "./device-livesync-service-base";
44
import { APP_FOLDER_NAME } from "../../constants";
5+
import { LivesyncTool } from "./livesync-library";
56
import * as path from "path";
67

7-
const LivesyncTool = require("nativescript-android-livesync-lib");
8-
98
export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
10-
private port: number;
11-
private livesyncTool: any;
9+
private livesyncTool: ILivesyncTool;
1210

1311
constructor(
1412
private data: IProjectData,
@@ -19,7 +17,7 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
1917
protected device: Mobile.IAndroidDevice,
2018
private $options: ICommonOptions) {
2119
super($platformsData, device);
22-
this.livesyncTool = new LivesyncTool();
20+
this.livesyncTool = this.$injector.resolve(LivesyncTool);
2321
}
2422

2523
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
@@ -33,24 +31,24 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
3331
const canExecuteFastSync = !liveSyncInfo.isFullSync && !_.some(liveSyncInfo.modifiedFilesData,
3432
(localToDevicePath: Mobile.ILocalToDevicePathData) => !this.canExecuteFastSync(localToDevicePath.getLocalPath(), projectData, this.device.deviceInfo.platform));
3533

36-
if(liveSyncInfo.modifiedFilesData.length) {
37-
const operationUid = this.livesyncTool.generateOperationUid();
38-
const doSyncPromise = this.livesyncTool.sendDoSyncOperation(operationUid);
34+
if (liveSyncInfo.modifiedFilesData.length) {
35+
const operationIdentifier = this.livesyncTool.generateOperationIdentifier();
36+
const doSyncPromise = this.livesyncTool.sendDoSyncOperation(operationIdentifier);
3937

4038
//TODO clear interval on exit sygnals/stopLivesync
4139
const syncInterval : NodeJS.Timer = setInterval(() => {
42-
if(this.livesyncTool.isOperationInProgress(operationUid)){
40+
if (this.livesyncTool.isOperationInProgress(operationIdentifier)) {
4341
this.$logger.info("Sync operation in progress...");
4442
}
4543
}, 10000);
4644

4745
const clearSyncInterval = () => {
4846
clearInterval(syncInterval);
49-
}
47+
};
5048
doSyncPromise.then(clearSyncInterval, clearSyncInterval);
5149
await doSyncPromise;
5250

53-
if(!canExecuteFastSync){
51+
if (!canExecuteFastSync) {
5452
await this.device.applicationManager.restartApplication({ appId: liveSyncInfo.deviceAppData.appIdentifier, projectName: projectData.projectName });
5553
}
5654
}
@@ -59,7 +57,7 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
5957
}
6058

6159
public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
62-
await this.livesyncTool.removeFilesArray(_.map(localToDevicePaths, (element: any) => { return element.filePath }));
60+
await this.livesyncTool.removeFiles(_.map(localToDevicePaths, (element: any) => { return element.filePath; }));
6361
}
6462

6563
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<Mobile.ILocalToDevicePathData[]> {
@@ -75,7 +73,7 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
7573
}
7674

7775
private async _transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<Mobile.ILocalToDevicePathData[]> {
78-
await this.livesyncTool.sendFilesArray(localToDevicePaths.map(localToDevicePathData => localToDevicePathData.getLocalPath()));
76+
await this.livesyncTool.sendFiles(localToDevicePaths.map(localToDevicePathData => localToDevicePathData.getLocalPath()));
7977

8078
return localToDevicePaths;
8179
}
@@ -86,18 +84,18 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
8684
const currentShasums: IStringDictionary = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
8785
const oldShasums = await deviceHashService.getShasumsFromDevice();
8886

89-
if(this.$options.force || !oldShasums) {
87+
if (this.$options.force || !oldShasums) {
9088
await this.livesyncTool.sendDirectory(projectFilesPath);
9189
await deviceHashService.uploadHashFileToDevice(currentShasums);
9290
transferedFiles = localToDevicePaths;
9391
} else {
9492
const changedShasums = deviceHashService.getChnagedShasums(oldShasums, currentShasums);
9593
const changedFiles = _.map(changedShasums, (hash: string, pathToFile: string) => pathToFile);
96-
if(changedFiles.length){
97-
await this.livesyncTool.sendFilesArray(changedFiles);
94+
if (changedFiles.length) {
95+
await this.livesyncTool.sendFiles(changedFiles);
9896
await deviceHashService.uploadHashFileToDevice(currentShasums);
9997
transferedFiles = localToDevicePaths.filter(localToDevicePathData => {
100-
if(changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0){
98+
if (changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0) {
10199
return true;
102100
}
103101
});
@@ -110,13 +108,10 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
110108
}
111109

112110
private async connectLivesyncTool(projectFilesPath: string, appIdentifier: string) {
113-
const adbPath = await this.$staticConfig.getAdbFilePath();
114111
await this.livesyncTool.connect({
115-
fullApplicationName: appIdentifier,
116-
port: this.port,
112+
appIdentifier,
117113
deviceIdentifier: this.device.deviceInfo.identifier,
118-
baseDir: projectFilesPath,
119-
adbPath: adbPath
114+
appPlatformsPath: projectFilesPath
120115
});
121116
}
122117

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export class AndroidLiveSyncService extends PlatformLiveSyncServiceBase implemen
1515
}
1616

1717
protected _getDeviceLiveSyncService(device: Mobile.IDevice, data: IProjectDir, frameworkVersion: string): INativeScriptDeviceLiveSyncService {
18-
if(semver.gte(frameworkVersion, "4.2.0")){
18+
if (semver.gte(frameworkVersion, "4.2.0")) {
1919
return this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceSocketsLiveSyncService, { device, data });
2020
}
21-
21+
2222
return this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { device, data });
2323
}
2424

0 commit comments

Comments
 (0)