-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-device-livesync-sockets-service.ts
136 lines (118 loc) · 6.55 KB
/
android-device-livesync-sockets-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { AndroidDeviceLiveSyncServiceBase } from "./android-device-livesync-service-base";
import { APP_FOLDER_NAME } from "../../constants";
import { LiveSyncPaths } from "../../common/constants";
import { AndroidLivesyncTool } from "./android-livesync-tool";
import * as path from "path";
import * as temp from "temp";
import * as semver from "semver";
export class AndroidDeviceSocketsLiveSyncService extends AndroidDeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
private livesyncTool: IAndroidLivesyncTool;
private static STATUS_UPDATE_INTERVAL = 10000;
private static MINIMAL_VERSION_LONG_LIVING_CONNECTION = "0.2.0";
constructor(
private data: IProjectData,
$injector: IInjector,
protected $platformsData: IPlatformsData,
protected $staticConfig: Config.IStaticConfig,
$logger: ILogger,
protected device: Mobile.IAndroidDevice,
private $options: IOptions,
private $processService: IProcessService,
private $fs: IFileSystem,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
$filesHashService: IFilesHashService) {
super($injector, $platformsData, $filesHashService, $logger, device);
this.livesyncTool = this.$injector.resolve(AndroidLivesyncTool);
}
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
if (!this.livesyncTool.hasConnection()) {
try {
const pathToLiveSyncFile = temp.path({ prefix: "livesync" });
this.$fs.writeFile(pathToLiveSyncFile, "");
await this.device.fileSystem.putFile(pathToLiveSyncFile, this.getPathToLiveSyncFileOnDevice(deviceAppData.appIdentifier), deviceAppData.appIdentifier);
await this.device.applicationManager.startApplication({ appId: deviceAppData.appIdentifier, projectName: this.data.projectName, justLaunch: true });
await this.connectLivesyncTool(this.data.projectIdentifiers.android, deviceAppData.connectTimeout);
} catch (err) {
await this.device.fileSystem.deleteFile(this.getPathToLiveSyncFileOnDevice(deviceAppData.appIdentifier), deviceAppData.appIdentifier);
throw err;
}
}
}
private getPathToLiveSyncFileOnDevice(appIdentifier: string): string {
return `${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${appIdentifier}-livesync-in-progress`;
}
public async finalizeSync(liveSyncInfo: ILiveSyncResultInfo, projectData: IProjectData): Promise<IAndroidLivesyncSyncOperationResult> {
try {
const result = await this.doSync(liveSyncInfo, projectData);
if (!semver.gte(this.livesyncTool.protocolVersion, AndroidDeviceSocketsLiveSyncService.MINIMAL_VERSION_LONG_LIVING_CONNECTION)) {
this.livesyncTool.end();
}
return result;
} catch (e) {
this.livesyncTool.end();
throw e;
}
}
private async doSync(liveSyncInfo: ILiveSyncResultInfo, projectData: IProjectData): Promise<IAndroidLivesyncSyncOperationResult> {
const operationId = this.livesyncTool.generateOperationIdentifier();
let result = { operationId, didRefresh: true };
if (liveSyncInfo.modifiedFilesData.length) {
const canExecuteFastSync = !liveSyncInfo.isFullSync && this.canExecuteFastSyncForPaths(liveSyncInfo, liveSyncInfo.modifiedFilesData, projectData, this.device.deviceInfo.platform);
const doSyncPromise = this.livesyncTool.sendDoSyncOperation({ doRefresh: canExecuteFastSync, operationId });
const syncInterval: NodeJS.Timer = setInterval(() => {
if (this.livesyncTool.isOperationInProgress(operationId)) {
this.$logger.info("Sync operation in progress...");
}
}, AndroidDeviceSocketsLiveSyncService.STATUS_UPDATE_INTERVAL);
const actionOnEnd = async () => {
clearInterval(syncInterval);
await this.device.fileSystem.deleteFile(this.getPathToLiveSyncFileOnDevice(liveSyncInfo.deviceAppData.appIdentifier), liveSyncInfo.deviceAppData.appIdentifier);
};
this.$processService.attachToProcessExitSignals(this, actionOnEnd);
// We need to clear resources when the action fails
// But we also need the real result of the action.
await doSyncPromise.then(actionOnEnd.bind(this), actionOnEnd.bind(this));
result = await doSyncPromise;
} else {
await this.device.fileSystem.deleteFile(this.getPathToLiveSyncFileOnDevice(liveSyncInfo.deviceAppData.appIdentifier), liveSyncInfo.deviceAppData.appIdentifier);
}
return result;
}
public async refreshApplication(projectData: IProjectData, liveSyncInfo: IAndroidLiveSyncResultInfo) {
const canExecuteFastSync = !liveSyncInfo.isFullSync && this.canExecuteFastSyncForPaths(liveSyncInfo, liveSyncInfo.modifiedFilesData, projectData, this.device.deviceInfo.platform);
if (!canExecuteFastSync || !liveSyncInfo.didRefresh) {
await this.device.applicationManager.restartApplication({ appId: liveSyncInfo.deviceAppData.appIdentifier, projectName: projectData.projectName });
if (!this.$options.justlaunch && this.livesyncTool.protocolVersion && semver.gte(this.livesyncTool.protocolVersion, AndroidDeviceSocketsLiveSyncService.MINIMAL_VERSION_LONG_LIVING_CONNECTION)) {
try {
await this.connectLivesyncTool(liveSyncInfo.deviceAppData.appIdentifier);
} catch (e) {
this.$logger.trace("Failed to connect after app restart.");
}
}
}
}
public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
await this.livesyncTool.removeFiles(_.map(localToDevicePaths, (element: any) => element.filePath));
const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
await deviceHashService.removeHashes(localToDevicePaths);
}
public async transferFilesOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<void> {
const files = _.map(localToDevicePaths, localToDevicePath => localToDevicePath.getLocalPath());
await this.livesyncTool.sendFiles(files);
}
public async transferDirectoryOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
await this.livesyncTool.sendDirectory(projectFilesPath);
}
private async connectLivesyncTool(appIdentifier: string, connectTimeout?: number) {
const platformData = this.$platformsData.getPlatformData(this.$devicePlatformsConstants.Android, this.data);
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
if (!this.livesyncTool.hasConnection()) {
await this.livesyncTool.connect({
appIdentifier,
deviceIdentifier: this.device.deviceInfo.identifier,
appPlatformsPath: projectFilesPath,
connectTimeout
});
}
}
}