Skip to content

Commit af3648d

Browse files
Merge pull request #1941 from NativeScript/KristinaKoeva/LiveSync2
Livesync service refactoring
2 parents 8490606 + 703e386 commit af3648d

16 files changed

+486
-373
lines changed

lib/bootstrap.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ $injector.require("androidProjectService", "./services/android-project-service")
1212
$injector.require("iOSProjectService", "./services/ios-project-service");
1313

1414
$injector.require("cocoapodsService", "./services/cocoapods-service");
15-
$injector.require("liveSyncServiceBase", "./services/livesync/livesync-service-base");
1615

1716
$injector.require("projectTemplatesService", "./services/project-templates-service");
1817
$injector.require("projectNameService", "./services/project-name-service");
@@ -105,8 +104,10 @@ $injector.requireCommand("platform|clean", "./commands/platform-clean");
105104

106105
$injector.requireCommand("livesync", "./commands/livesync");
107106
$injector.require("usbLiveSyncService", "./services/livesync/livesync-service"); // The name is used in https://github.com/NativeScript/nativescript-dev-typescript
108-
$injector.require("iosLiveSyncServiceLocator", "./services/livesync/ios-livesync-service");
109-
$injector.require("androidLiveSyncServiceLocator", "./services/livesync/android-livesync-service");
107+
$injector.require("iosPlatformLiveSyncServiceLocator", "./services/livesync/ios-platform-livesync-service");
108+
$injector.require("iosLiveSyncServiceLocator", "./services/livesync/ios-device-livesync-service");
109+
$injector.require("androidPlatformLiveSyncServiceLocator", "./services/livesync/android-platform-livesync-service");
110+
$injector.require("androidLiveSyncServiceLocator", "./services/livesync/android-device-livesync-service");
110111

111112
$injector.require("sysInfo", "./sys-info");
112113

lib/commands/debug.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,26 @@
66
private $childProcess: IChildProcess,
77
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
88
private $config: IConfiguration,
9+
private $usbLiveSyncService: ILiveSyncService,
910
protected $options: IOptions) { }
1011

1112
execute(args: string[]): IFuture<void> {
1213
if (!this.$options.rebuild && !this.$options.start) {
1314
this.$config.debugLivesync = true;
14-
let usbLiveSyncService: ILiveSyncService = this.$injector.resolve("usbLiveSyncService");
15-
let liveSyncServiceBase: any = this.$injector.resolve("liveSyncServiceBase");
16-
let liveSyncProvider: ILiveSyncProvider = this.$injector.resolve("liveSyncProvider");
15+
let applicationReloadAction = (deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): IFuture<void> => {
16+
return (() => {
17+
let projectData: IProjectData = this.$injector.resolve("projectData");
1718

18-
liveSyncServiceBase.on("sync", (device: Mobile.IDevice, data: ILiveSyncData) => {
19-
let platformLiveSyncService: IPlatformLiveSyncService = this.$injector.resolve(liveSyncProvider.platformSpecificLiveSyncServices[data.platform.toLowerCase()], { _device: device });
20-
let projectData: IProjectData = this.$injector.resolve("projectData");
21-
let appId = device.isEmulator ? projectData.projectName : data.appIdentifier;
22-
if (data.platform === this.$devicePlatformsConstants.iOS) {
23-
platformLiveSyncService.debugService.debugStop().wait();
24-
}
25-
device.applicationManager.stopApplication(appId).wait();
26-
platformLiveSyncService.debugService.debug().wait();
27-
});
19+
this.debugService.debugStop().wait();
2820

29-
liveSyncServiceBase.on("syncAfterInstall", (device: Mobile.IDevice, data: ILiveSyncData) => {
30-
let platformLiveSyncService: IPlatformLiveSyncService = this.$injector.resolve(liveSyncProvider.platformSpecificLiveSyncServices[data.platform.toLowerCase()], { _device: device });
31-
platformLiveSyncService.debugService.debug().wait();
32-
});
21+
let applicationId = deviceAppData.device.isEmulator ? projectData.projectName : deviceAppData.appIdentifier;
22+
deviceAppData.device.applicationManager.stopApplication(applicationId).wait();
3323

34-
return usbLiveSyncService.liveSync(this.$devicesService.platform);
24+
this.debugService.debug().wait();
25+
}).future<void>()();
26+
};
27+
28+
return this.$usbLiveSyncService.liveSync(this.$devicesService.platform, applicationReloadAction);
3529
}
3630
return this.debugService.debug();
3731
}
@@ -66,8 +60,9 @@ export class DebugIOSCommand extends DebugPlatformCommand {
6660
$childProcess: IChildProcess,
6761
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
6862
$config: IConfiguration,
63+
$usbLiveSyncService: ILiveSyncService,
6964
$options: IOptions) {
70-
super($iOSDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $options);
65+
super($iOSDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options);
7166
}
7267
}
7368
$injector.registerCommand("debug|ios", DebugIOSCommand);
@@ -78,10 +73,11 @@ export class DebugAndroidCommand extends DebugPlatformCommand {
7873
$injector: IInjector,
7974
$logger: ILogger,
8075
$childProcess: IChildProcess,
81-
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
76+
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
8277
$config: IConfiguration,
78+
$usbLiveSyncService: ILiveSyncService,
8379
$options: IOptions) {
84-
super($androidDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $options);
80+
super($androidDebugService, $devicesService, $injector, $logger, $childProcess, $devicePlatformsConstants, $config, $usbLiveSyncService, $options);
8581
}
8682
}
8783
$injector.registerCommand("debug|android", DebugAndroidCommand);

lib/declarations.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ interface IOpener {
5757
}
5858

5959
interface ILiveSyncService {
60-
liveSync(platform: string): IFuture<void>;
60+
liveSync(platform: string, applicationReloadAction?: (deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]) => IFuture<void>): IFuture<void>;
6161
forceExecuteFullSync: boolean;
6262
}
6363

64+
interface IPlatformLiveSyncService {
65+
fullSync(postAction?: (deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]) => IFuture<void>): IFuture<void>;
66+
partialSync(event: string, filePath: string, dispatcher: IFutureDispatcher, afterFileSyncAction: (deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]) => IFuture<void>): void;
67+
refreshApplication(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): IFuture<void>;
68+
}
69+
6470
interface IOptions extends ICommonOptions {
6571
all: boolean;
6672
baseConfig: string;

lib/definitions/debug.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
interface IDebugService {
22
debug(shouldBreak?: boolean): IFuture<void>;
33
debugStart(): IFuture<void>;
4-
debugStop?(): IFuture<void>
4+
debugStop(): IFuture<void>
55
platform: string;
66
}

lib/providers/livesync-provider.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,53 @@ import * as temp from "temp";
33

44
export class LiveSyncProvider implements ILiveSyncProvider {
55
constructor(private $androidLiveSyncServiceLocator: {factory: Function},
6+
private $androidPlatformLiveSyncServiceLocator: {factory: Function},
67
private $iosLiveSyncServiceLocator: {factory: Function},
8+
private $iosPlatformLiveSyncServiceLocator: {factory: Function},
79
private $platformService: IPlatformService,
810
private $platformsData: IPlatformsData,
911
private $logger: ILogger,
10-
private $childProcess: IChildProcess) { }
12+
private $childProcess: IChildProcess,
13+
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
1114

1215
private static FAST_SYNC_FILE_EXTENSIONS = [".css", ".xml" ,".html"];
1316

17+
private deviceSpecificLiveSyncServicesCache: IDictionary<any> = {};
18+
public get deviceSpecificLiveSyncServices(): IDictionary<any> {
19+
return {
20+
android: (_device: Mobile.IDevice, $injector: IInjector) => {
21+
if(!this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
22+
this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$androidLiveSyncServiceLocator.factory, {_device: _device});
23+
}
24+
25+
return this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
26+
},
27+
ios: (_device: Mobile.IDevice, $injector: IInjector) => {
28+
if(!this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
29+
this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$iosLiveSyncServiceLocator.factory, {_device: _device});
30+
}
31+
32+
return this.deviceSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
33+
}
34+
};
35+
}
36+
1437
private platformSpecificLiveSyncServicesCache: IDictionary<any> = {};
1538
public get platformSpecificLiveSyncServices(): IDictionary<any> {
1639
return {
17-
android: (_device: Mobile.IDevice, $injector: IInjector): IPlatformLiveSyncService => {
18-
if(!this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
19-
this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$androidLiveSyncServiceLocator.factory, {_device: _device});
40+
android: (_liveSyncData: ILiveSyncData, $injector: IInjector) => {
41+
if(!this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android]) {
42+
this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android] = $injector.resolve(this.$androidPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
2043
}
2144

22-
return this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
45+
return this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.Android];
2346
},
24-
ios: (_device: Mobile.IDevice, $injector: IInjector) => {
25-
if(!this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier]) {
26-
this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier] = $injector.resolve(this.$iosLiveSyncServiceLocator.factory, {_device: _device});
47+
ios: (_liveSyncData: ILiveSyncData, $injector: IInjector) => {
48+
if(!this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS]) {
49+
this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS] = $injector.resolve(this.$iosPlatformLiveSyncServiceLocator.factory, { _liveSyncData: _liveSyncData });
2750
}
2851

29-
return this.platformSpecificLiveSyncServicesCache[_device.deviceInfo.identifier];
52+
return this.platformSpecificLiveSyncServicesCache[this.$devicePlatformsConstants.iOS];
3053
}
3154
};
3255
}

lib/services/android-debug-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ class AndroidDebugService implements IDebugService {
190190
}).future<void>()();
191191
}
192192

193+
public debugStop(): IFuture<void> {
194+
return Future.fromResult();
195+
}
196+
193197
private debugStartCore(): IFuture<void> {
194198
return (() => {
195199
let packageName = this.$projectData.projectId;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {DeviceAndroidDebugBridge} from "../../common/mobile/android/device-android-debug-bridge";
22
import {AndroidDeviceHashService} from "../../common/mobile/android/android-device-hash-service";
3-
import {PlatformLiveSyncServiceBase} from "./platform-livesync-service-base";
3+
import {DeviceLiveSyncServiceBase} from "./device-livesync-service-base";
44
import Future = require("fibers/future");
55
import * as helpers from "../../common/helpers";
66
import * as path from "path";
77
import * as net from "net";
88

9-
class AndroidLiveSyncService extends PlatformLiveSyncServiceBase<Mobile.IAndroidDevice> implements IPlatformLiveSyncService {
9+
class AndroidLiveSyncService extends DeviceLiveSyncServiceBase<Mobile.IAndroidDevice> implements IDeviceLiveSyncService {
1010
private static BACKEND_PORT = 18182;
1111

1212
constructor(_device: Mobile.IDevice,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {PlatformLiveSyncServiceBase} from "./platform-livesync-service-base";
2+
3+
class AndroidPlatformLiveSyncService extends PlatformLiveSyncServiceBase {
4+
constructor(_liveSyncData: ILiveSyncData,
5+
protected $devicesService: Mobile.IDevicesService,
6+
protected $mobileHelper: Mobile.IMobileHelper,
7+
protected $logger: ILogger,
8+
protected $options: ICommonOptions,
9+
protected $deviceAppDataFactory: Mobile.IDeviceAppDataFactory,
10+
protected $fs: IFileSystem,
11+
protected $injector: IInjector,
12+
protected $projectFilesManager: IProjectFilesManager,
13+
protected $projectFilesProvider: IProjectFilesProvider,
14+
protected $liveSyncProvider: ILiveSyncProvider) {
15+
super(_liveSyncData, $devicesService, $mobileHelper, $logger, $options, $deviceAppDataFactory, $fs, $injector, $projectFilesManager, $projectFilesProvider, $liveSyncProvider);
16+
}
17+
18+
public fullSync(postAction?: (deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]) => IFuture<void>): IFuture<void> {
19+
return (() => {
20+
let appIdentifier = this.liveSyncData.appIdentifier;
21+
let platform = this.liveSyncData.platform;
22+
let projectFilesPath = this.liveSyncData.projectFilesPath;
23+
let canExecute = this.getCanExecuteAction(platform, appIdentifier);
24+
let action = (device: Mobile.IDevice): IFuture<void> => {
25+
return (() => {
26+
let deviceLiveSyncService = this.resolveDeviceSpecificLiveSyncService(platform, device);
27+
let deviceAppData = this.$deviceAppDataFactory.create(appIdentifier, this.$mobileHelper.normalizePlatformName(platform), device);
28+
29+
deviceLiveSyncService.beforeLiveSyncAction(deviceAppData).wait();;
30+
31+
let installed = this.tryInstallApplication(device, deviceAppData).wait();
32+
let localToDevicePaths = this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, this.liveSyncData.excludedProjectDirsAndFiles);
33+
let afterSyncAction: () => IFuture<void>;
34+
35+
if (installed) {
36+
deviceLiveSyncService.afterInstallApplicationAction(deviceAppData, localToDevicePaths).wait();
37+
afterSyncAction = () => device.applicationManager.tryStartApplication(deviceAppData.appIdentifier);
38+
} else {
39+
this.transferFiles(deviceAppData, localToDevicePaths, this.liveSyncData.projectFilesPath, true).wait();
40+
afterSyncAction = () => this.refreshApplication(deviceAppData, localToDevicePaths);
41+
}
42+
43+
if (postAction) {
44+
return postAction(deviceAppData, localToDevicePaths);
45+
}
46+
47+
return afterSyncAction();
48+
}).future<void>()();
49+
};
50+
this.$devicesService.execute(action, canExecute).wait();
51+
}).future<void>()();
52+
}
53+
54+
protected getCanExecuteActionCore(platform: string, appIdentifier: string): (dev: Mobile.IDevice) => boolean {
55+
return (device: Mobile.IDevice) => true;
56+
}
57+
}
58+
59+
$injector.register("androidPlatformLiveSyncServiceLocator", {factory: AndroidPlatformLiveSyncService});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export abstract class DeviceLiveSyncServiceBase<T extends Mobile.IDevice> {
2+
protected get device(): T {
3+
return <T>(this._device);
4+
}
5+
6+
constructor(private _device: Mobile.IDevice,
7+
private $liveSyncProvider: ILiveSyncProvider) { }
8+
9+
public refreshApplication(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], forceExecuteFullSync: boolean): IFuture<void> {
10+
let canExecuteFastSync = !forceExecuteFullSync && !_.some(localToDevicePaths, (localToDevicePath:any) => !this.$liveSyncProvider.canExecuteFastSync(localToDevicePath.getLocalPath(), deviceAppData.platform));
11+
12+
if (canExecuteFastSync) {
13+
return this.reloadPage(deviceAppData);
14+
}
15+
16+
return this.restartApplication(deviceAppData);
17+
}
18+
19+
protected abstract restartApplication(deviceAppData: Mobile.IDeviceAppData): IFuture<void>;
20+
protected abstract reloadPage(deviceAppData: Mobile.IDeviceAppData): IFuture<void>;
21+
}

0 commit comments

Comments
 (0)