Skip to content

Commit 0f9c0aa

Browse files
rosen-vladimirovDimitar Kerezov
authored andcommitted
Extract all common logic of livesync services to platform-livesync-service-base
1 parent 6b3662d commit 0f9c0aa

File tree

3 files changed

+152
-205
lines changed

3 files changed

+152
-205
lines changed
Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,21 @@
1-
import * as path from "path";
21
import { AndroidDeviceLiveSyncService } from "./android-device-livesync-service";
3-
import { APP_FOLDER_NAME } from "../../constants";
42
import { PlatformLiveSyncServiceBase } from "./platform-livesync-service-base";
53

64
export class AndroidLiveSyncService extends PlatformLiveSyncServiceBase implements IPlatformLiveSyncService {
7-
constructor(private $projectFilesManager: IProjectFilesManager,
8-
private $platformsData: IPlatformsData,
9-
private $logger: ILogger,
10-
private $projectFilesProvider: IProjectFilesProvider,
11-
private $fs: IFileSystem,
5+
constructor(protected $platformsData: IPlatformsData,
6+
protected $projectFilesManager: IProjectFilesManager,
127
private $injector: IInjector,
13-
$devicePathProvider: IDevicePathProvider) {
14-
super($devicePathProvider);
8+
$devicePathProvider: IDevicePathProvider,
9+
$fs: IFileSystem,
10+
$logger: ILogger,
11+
$projectFilesProvider: IProjectFilesProvider,
12+
) {
13+
super($fs, $logger, $platformsData, $projectFilesManager, $devicePathProvider, $projectFilesProvider);
1514
}
1615

17-
public async fullSync(syncInfo: IFullSyncInfo): Promise<ILiveSyncResultInfo> {
18-
const projectData = syncInfo.projectData;
19-
const device = syncInfo.device;
20-
const deviceLiveSyncService = this.$injector.resolve<AndroidDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: device });
21-
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
22-
23-
const deviceAppData = await this.getAppData(syncInfo);
24-
await deviceLiveSyncService.beforeLiveSyncAction(deviceAppData);
25-
26-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
27-
const localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, []);
28-
await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, true);
29-
30-
return {
31-
modifiedFilesData: localToDevicePaths,
32-
isFullSync: true,
33-
deviceAppData
34-
};
35-
}
36-
37-
public async liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo> {
38-
const projectData = liveSyncInfo.projectData;
39-
const syncInfo = _.merge<IFullSyncInfo>({ device, watch: true }, liveSyncInfo);
40-
const deviceAppData = await this.getAppData(syncInfo);
41-
42-
let modifiedLocalToDevicePaths: Mobile.ILocalToDevicePathData[] = [];
43-
if (liveSyncInfo.filesToSync.length) {
44-
const filesToSync = liveSyncInfo.filesToSync;
45-
const mappedFiles = _.map(filesToSync, filePath => this.$projectFilesProvider.mapFilePath(filePath, device.deviceInfo.platform, projectData));
46-
47-
// Some plugins modify platforms dir on afterPrepare (check nativescript-dev-sass) - we want to sync only existing file.
48-
const existingFiles = mappedFiles.filter(m => this.$fs.exists(m));
49-
this.$logger.trace("Will execute livesync for files: ", existingFiles);
50-
const skippedFiles = _.difference(mappedFiles, existingFiles);
51-
if (skippedFiles.length) {
52-
this.$logger.trace("The following files will not be synced as they do not exist:", skippedFiles);
53-
}
54-
55-
if (existingFiles.length) {
56-
let platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
57-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
58-
let localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData,
59-
projectFilesPath, mappedFiles, []);
60-
modifiedLocalToDevicePaths.push(...localToDevicePaths);
61-
await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, false);
62-
}
63-
}
64-
65-
if (liveSyncInfo.filesToRemove.length) {
66-
const filePaths = liveSyncInfo.filesToRemove;
67-
let platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
68-
69-
const mappedFiles = _.map(filePaths, filePath => this.$projectFilesProvider.mapFilePath(filePath, device.deviceInfo.platform, projectData));
70-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
71-
let localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, mappedFiles, []);
72-
modifiedLocalToDevicePaths.push(...localToDevicePaths);
73-
74-
const deviceLiveSyncService = this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: device });
75-
deviceLiveSyncService.removeFiles(projectData.projectId, localToDevicePaths, projectData.projectId);
76-
}
77-
78-
return {
79-
modifiedFilesData: modifiedLocalToDevicePaths,
80-
isFullSync: liveSyncInfo.isRebuilt,
81-
deviceAppData
82-
};
83-
}
84-
85-
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
86-
if (liveSyncInfo.isFullSync || liveSyncInfo.modifiedFilesData.length) {
87-
let deviceLiveSyncService = this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: liveSyncInfo.deviceAppData.device });
88-
this.$logger.info("Refreshing application...");
89-
await deviceLiveSyncService.refreshApplication(projectData, liveSyncInfo);
90-
}
91-
}
92-
93-
protected async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, canTransferDirectory: boolean): Promise<void> {
94-
if (canTransferDirectory) {
95-
await deviceAppData.device.fileSystem.transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath);
96-
} else {
97-
await deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths);
98-
}
16+
public getDeviceLiveSyncService(device: Mobile.IDevice): INativeScriptDeviceLiveSyncService {
17+
const service = this.$injector.resolve<INativeScriptDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { _device: device });
18+
return service;
9919
}
10020
}
10121
$injector.register("androidLiveSyncService", AndroidLiveSyncService);
Lines changed: 45 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,75 @@
11
import * as path from "path";
2-
import * as iosdls from "./ios-device-livesync-service";
32
import * as temp from "temp";
3+
4+
import { IOSDeviceLiveSyncService } from "./ios-device-livesync-service";
45
import { PlatformLiveSyncServiceBase } from "./platform-livesync-service-base";
5-
// import * as uuid from "uuid";
6+
import { APP_FOLDER_NAME, TNS_MODULES_FOLDER_NAME } from "../../constants";
67

78
export class IOSLiveSyncService extends PlatformLiveSyncServiceBase implements IPlatformLiveSyncService {
8-
constructor(private $devicesService: Mobile.IDevicesService,
9-
private $projectFilesManager: IProjectFilesManager,
10-
private $platformsData: IPlatformsData,
11-
private $logger: ILogger,
12-
private $projectFilesProvider: IProjectFilesProvider,
13-
private $fs: IFileSystem,
9+
constructor(protected $fs: IFileSystem,
10+
protected $platformsData: IPlatformsData,
11+
protected $projectFilesManager: IProjectFilesManager,
1412
private $injector: IInjector,
15-
$devicePathProvider: IDevicePathProvider) {
16-
super($devicePathProvider);
13+
$devicePathProvider: IDevicePathProvider,
14+
$logger: ILogger,
15+
$projectFilesProvider: IProjectFilesProvider,
16+
) {
17+
super($fs, $logger, $platformsData, $projectFilesManager, $devicePathProvider, $projectFilesProvider);
1718
}
1819

19-
/*
20-
fullSync(projectData: IProjectData, device: Mobile.IDevice): Promise<ILiveSyncResultInfo>;
21-
liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo>;
22-
refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void>;
23-
*/
24-
2520
public async fullSync(syncInfo: IFullSyncInfo): Promise<ILiveSyncResultInfo> {
26-
const projectData = syncInfo.projectData;
2721
const device = syncInfo.device;
28-
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
29-
const deviceAppData = await this.getAppData(syncInfo);
30-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app");
3122

3223
if (device.isEmulator) {
33-
const localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, []);
34-
await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, true);
35-
return {
36-
deviceAppData,
37-
isFullSync: true,
38-
modifiedFilesData: localToDevicePaths
39-
};
40-
} else {
41-
temp.track();
42-
let tempZip = temp.path({ prefix: "sync", suffix: ".zip" });
43-
let tempApp = temp.mkdirSync("app");
44-
this.$logger.trace("Creating zip file: " + tempZip);
45-
this.$fs.copyFile(path.join(path.dirname(projectFilesPath), "app/*"), tempApp);
46-
47-
if (!syncInfo.syncAllFiles) {
48-
this.$logger.info("Skipping node_modules folder! Use the syncAllFiles option to sync files from this folder.");
49-
this.$fs.deleteDirectory(path.join(tempApp, "tns_modules"));
50-
}
51-
52-
await this.$fs.zipFiles(tempZip, this.$fs.enumerateFilesInDirectorySync(tempApp), (res) => {
53-
return path.join("app", path.relative(tempApp, res));
54-
});
55-
56-
await device.fileSystem.transferFiles(deviceAppData, [{
57-
getLocalPath: () => tempZip,
58-
getDevicePath: () => deviceAppData.deviceSyncZipPath,
59-
getRelativeToProjectBasePath: () => "../sync.zip",
60-
deviceProjectRootPath: await deviceAppData.getDeviceProjectRootPath()
61-
}]);
62-
63-
return {
64-
deviceAppData,
65-
isFullSync: true,
66-
modifiedFilesData: []
67-
};
24+
return super.fullSync(syncInfo);
6825
}
69-
}
7026

71-
public async liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo> {
72-
const projectData = liveSyncInfo.projectData;
73-
const syncInfo = _.merge<IFullSyncInfo>({ device, watch: true }, liveSyncInfo);
27+
const projectData = syncInfo.projectData;
28+
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
7429
const deviceAppData = await this.getAppData(syncInfo);
75-
let modifiedLocalToDevicePaths: Mobile.ILocalToDevicePathData[] = [];
76-
77-
if (liveSyncInfo.isRebuilt) {
78-
// In this case we should execute fullsync:
79-
await this.fullSync({ projectData, device, syncAllFiles: liveSyncInfo.syncAllFiles, watch: true });
80-
} else {
81-
if (liveSyncInfo.filesToSync.length) {
82-
const filesToSync = liveSyncInfo.filesToSync;
83-
const mappedFiles = _.map(filesToSync, filePath => this.$projectFilesProvider.mapFilePath(filePath, device.deviceInfo.platform, projectData));
30+
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
8431

85-
// Some plugins modify platforms dir on afterPrepare (check nativescript-dev-sass) - we want to sync only existing file.
86-
const existingFiles = mappedFiles.filter(m => this.$fs.exists(m));
87-
this.$logger.trace("Will execute livesync for files: ", existingFiles);
88-
const skippedFiles = _.difference(mappedFiles, existingFiles);
89-
if (skippedFiles.length) {
90-
this.$logger.trace("The following files will not be synced as they do not exist:", skippedFiles);
91-
}
32+
temp.track();
33+
const tempZip = temp.path({ prefix: "sync", suffix: ".zip" });
34+
const tempApp = temp.mkdirSync("app");
35+
this.$logger.trace("Creating zip file: " + tempZip);
36+
this.$fs.copyFile(path.join(path.dirname(projectFilesPath), `${APP_FOLDER_NAME}/*`), tempApp);
9237

93-
if (existingFiles.length) {
94-
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
95-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app");
96-
let localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData,
97-
projectFilesPath, mappedFiles, []);
98-
modifiedLocalToDevicePaths.push(...localToDevicePaths);
99-
await this.transferFiles(deviceAppData, localToDevicePaths, projectFilesPath, false);
100-
}
101-
}
38+
if (!syncInfo.syncAllFiles) {
39+
this.$logger.info("Skipping node_modules folder! Use the syncAllFiles option to sync files from this folder.");
40+
this.$fs.deleteDirectory(path.join(tempApp, TNS_MODULES_FOLDER_NAME));
41+
}
10242

103-
if (liveSyncInfo.filesToRemove.length) {
104-
const filePaths = liveSyncInfo.filesToRemove;
105-
const platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform, projectData);
43+
await this.$fs.zipFiles(tempZip, this.$fs.enumerateFilesInDirectorySync(tempApp), (res) => {
44+
return path.join(APP_FOLDER_NAME, path.relative(tempApp, res));
45+
});
10646

107-
const mappedFiles = _.map(filePaths, filePath => this.$projectFilesProvider.mapFilePath(filePath, device.deviceInfo.platform, projectData));
108-
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app");
109-
let localToDevicePaths = await this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, mappedFiles, []);
110-
modifiedLocalToDevicePaths.push(...localToDevicePaths);
111-
112-
const deviceLiveSyncService = this.$injector.resolve(iosdls.IOSDeviceLiveSyncService, { _device: device });
113-
deviceLiveSyncService.removeFiles(projectData.projectId, localToDevicePaths, projectData.projectId);
114-
}
115-
}
47+
await device.fileSystem.transferFiles(deviceAppData, [{
48+
getLocalPath: () => tempZip,
49+
getDevicePath: () => deviceAppData.deviceSyncZipPath,
50+
getRelativeToProjectBasePath: () => "../sync.zip",
51+
deviceProjectRootPath: await deviceAppData.getDeviceProjectRootPath()
52+
}]);
11653

11754
return {
118-
modifiedFilesData: modifiedLocalToDevicePaths,
119-
isFullSync: liveSyncInfo.isRebuilt,
120-
deviceAppData
55+
deviceAppData,
56+
isFullSync: true,
57+
modifiedFilesData: []
12158
};
12259
}
12360

124-
public async refreshApplication(projectData: IProjectData, liveSyncInfo: ILiveSyncResultInfo): Promise<void> {
125-
let deviceLiveSyncService = this.$injector.resolve(iosdls.IOSDeviceLiveSyncService, { _device: liveSyncInfo.deviceAppData.device });
126-
this.$logger.info("Refreshing application...");
127-
await deviceLiveSyncService.refreshApplication(projectData, liveSyncInfo);
128-
}
129-
130-
protected async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, isFullSync: boolean): Promise<void> {
131-
let canTransferDirectory = isFullSync && this.$devicesService.isiOSDevice(deviceAppData.device);
132-
if (canTransferDirectory) {
133-
await deviceAppData.device.fileSystem.transferDirectory(deviceAppData, localToDevicePaths, projectFilesPath);
61+
public liveSyncWatchAction(device: Mobile.IDevice, liveSyncInfo: ILiveSyncWatchInfo): Promise<ILiveSyncResultInfo> {
62+
if (liveSyncInfo.isRebuilt) {
63+
// In this case we should execute fullsync because iOS Runtime requires the full content of app dir to be extracted in the root of sync dir.
64+
return this.fullSync({ projectData: liveSyncInfo.projectData, device, syncAllFiles: liveSyncInfo.syncAllFiles, watch: true });
13465
} else {
135-
await deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths);
66+
return super.liveSyncWatchAction(device, liveSyncInfo);
13667
}
68+
}
13769

138-
console.log("### ios TRANSFEREEDDDDDDD!!!!!!");
70+
public getDeviceLiveSyncService(device: Mobile.IDevice): INativeScriptDeviceLiveSyncService {
71+
const service = this.$injector.resolve<INativeScriptDeviceLiveSyncService>(IOSDeviceLiveSyncService, { _device: device });
72+
return service;
13973
}
14074
}
14175
$injector.register("iOSLiveSyncService", IOSLiveSyncService);

0 commit comments

Comments
 (0)