|
1 | 1 | import * as path from "path";
|
2 |
| -import * as iosdls from "./ios-device-livesync-service"; |
3 | 2 | import * as temp from "temp";
|
| 3 | + |
| 4 | +import { IOSDeviceLiveSyncService } from "./ios-device-livesync-service"; |
4 | 5 | import { PlatformLiveSyncServiceBase } from "./platform-livesync-service-base";
|
5 |
| -// import * as uuid from "uuid"; |
| 6 | +import { APP_FOLDER_NAME, TNS_MODULES_FOLDER_NAME } from "../../constants"; |
6 | 7 |
|
7 | 8 | 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, |
14 | 12 | 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); |
17 | 18 | }
|
18 | 19 |
|
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 |
| - |
25 | 20 | public async fullSync(syncInfo: IFullSyncInfo): Promise<ILiveSyncResultInfo> {
|
26 |
| - const projectData = syncInfo.projectData; |
27 | 21 | 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"); |
31 | 22 |
|
32 | 23 | 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); |
68 | 25 | }
|
69 |
| - } |
70 | 26 |
|
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); |
74 | 29 | 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); |
84 | 31 |
|
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); |
92 | 37 |
|
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 | + } |
102 | 42 |
|
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 | + }); |
106 | 46 |
|
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 | + }]); |
116 | 53 |
|
117 | 54 | return {
|
118 |
| - modifiedFilesData: modifiedLocalToDevicePaths, |
119 |
| - isFullSync: liveSyncInfo.isRebuilt, |
120 |
| - deviceAppData |
| 55 | + deviceAppData, |
| 56 | + isFullSync: true, |
| 57 | + modifiedFilesData: [] |
121 | 58 | };
|
122 | 59 | }
|
123 | 60 |
|
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 }); |
134 | 65 | } else {
|
135 |
| - await deviceAppData.device.fileSystem.transferFiles(deviceAppData, localToDevicePaths); |
| 66 | + return super.liveSyncWatchAction(device, liveSyncInfo); |
136 | 67 | }
|
| 68 | + } |
137 | 69 |
|
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; |
139 | 73 | }
|
140 | 74 | }
|
141 | 75 | $injector.register("iOSLiveSyncService", IOSLiveSyncService);
|
0 commit comments