Skip to content

Commit fe7bbe3

Browse files
committed
chore: fix PR comments
1 parent 45503ed commit fe7bbe3

10 files changed

+106
-130
lines changed

lib/common/definitions/mobile.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ declare module Mobile {
118118
interface IAndroidDevice extends IDevice {
119119
adb: Mobile.IDeviceAndroidDebugBridge;
120120
init(): Promise<void>;
121+
fileSystem: Mobile.IAndroidDeviceFileSystem;
122+
}
123+
124+
interface IAndroidDeviceFileSystem extends IDeviceFileSystem {
125+
getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService;
121126
}
122127

123128
interface IiOSSimulator extends IDevice { }

lib/common/mobile/android/android-device-file-system.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ export class AndroidDeviceFileSystem implements Mobile.IDeviceFileSystem {
147147
await deviceHashService.uploadHashFileToDevice(hashes);
148148
}
149149

150-
private getTempDir(): string {
151-
temp.track();
152-
return temp.mkdirSync("application-");
153-
}
154-
155-
private getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
150+
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
156151
if (!this._deviceHashServices[appIdentifier]) {
157152
this._deviceHashServices[appIdentifier] = this.$injector.resolve(AndroidDeviceHashService, { adb: this.adb, appIdentifier });
158153
}
159154

160155
return this._deviceHashServices[appIdentifier];
161156
}
157+
158+
private getTempDir(): string {
159+
temp.track();
160+
return temp.mkdirSync("application-");
161+
}
162162
}

lib/common/mobile/android/android-device.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface IAdbDeviceStatusInfo {
1919
export class AndroidDevice implements Mobile.IAndroidDevice {
2020
public adb: Mobile.IDeviceAndroidDebugBridge;
2121
public applicationManager: Mobile.IDeviceApplicationManager;
22-
public fileSystem: Mobile.IDeviceFileSystem;
22+
public fileSystem: Mobile.IAndroidDeviceFileSystem;
2323
public deviceInfo: Mobile.IDeviceInfo;
2424

2525
// http://stackoverflow.com/questions/31178195/what-does-adb-device-status-mean

lib/common/test/unit-tests/mobile/android-device-file-system.ts

+54-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { LiveSyncPaths } from "../../../constants";
1212
const myTestAppIdentifier = "org.nativescript.myApp";
1313
let isAdbPushExecuted = false;
1414
let isAdbPushAppDirCalled = false;
15-
let androidDeviceFileSystem: Mobile.IDeviceFileSystem;
15+
let androidDeviceFileSystem: Mobile.IAndroidDeviceFileSystem;
1616

1717
class AndroidDebugBridgeMock {
1818
public executeCommand(args: string[]) {
@@ -138,10 +138,14 @@ function setup(options?: {
138138
return {
139139
localToDevicePaths,
140140
deviceAppData,
141-
projectRoot
141+
projectRoot,
142+
injector
142143
};
143144
}
144145

146+
let resolveParams: any[] = [];
147+
const appIdentifier = "testAppIdentifier";
148+
145149
describe("AndroidDeviceFileSystem", () => {
146150
describe("transferDirectory", () => {
147151
it("pushes the whole directory when hash file doesn't exist on device", async () => {
@@ -186,4 +190,52 @@ describe("AndroidDeviceFileSystem", () => {
186190
assert.isFalse(isAdbPushAppDirCalled);
187191
});
188192
});
193+
194+
describe("getDeviceHashService", () => {
195+
beforeEach(() => {
196+
resolveParams = [];
197+
const { injector } = setup();
198+
injector.resolve = (service: any, args: string[]) => resolveParams.push({ service, args });
199+
});
200+
it("should resolve AndroidDeviceHashService when the key is not stored in dictionary", () => {
201+
androidDeviceFileSystem.getDeviceHashService(appIdentifier);
202+
assert.equal(resolveParams.length, 1);
203+
assert.isFunction(resolveParams[0].service);
204+
assert.isDefined(resolveParams[0].args.adb);
205+
assert.equal(resolveParams[0].args.appIdentifier, appIdentifier);
206+
});
207+
it("should return already stored value when the method is called for second time with the same deviceIdentifier and appIdentifier", () => {
208+
androidDeviceFileSystem.getDeviceHashService(appIdentifier);
209+
assert.equal(resolveParams.length, 1);
210+
assert.isFunction(resolveParams[0].service);
211+
assert.isDefined(resolveParams[0].args.adb);
212+
assert.equal(resolveParams[0].args.appIdentifier, appIdentifier);
213+
214+
androidDeviceFileSystem.getDeviceHashService(appIdentifier);
215+
assert.equal(resolveParams.length, 1);
216+
assert.isFunction(resolveParams[0].service);
217+
assert.isDefined(resolveParams[0].args.adb);
218+
assert.equal(resolveParams[0].args.appIdentifier, appIdentifier);
219+
});
220+
it("should return AndroidDeviceHashService when the method is called for second time with different appIdentifier and same deviceIdentifier", () => {
221+
androidDeviceFileSystem.getDeviceHashService(appIdentifier);
222+
assert.equal(resolveParams.length, 1);
223+
assert.isFunction(resolveParams[0].service);
224+
assert.isDefined(resolveParams[0].args.adb);
225+
assert.equal(resolveParams[0].args.appIdentifier, appIdentifier);
226+
227+
androidDeviceFileSystem.getDeviceHashService(appIdentifier);
228+
assert.equal(resolveParams.length, 1);
229+
assert.isFunction(resolveParams[0].service);
230+
assert.isDefined(resolveParams[0].args.adb);
231+
assert.equal(resolveParams[0].args.appIdentifier, appIdentifier);
232+
233+
const newAppIdentifier = "myNewAppIdentifier";
234+
androidDeviceFileSystem.getDeviceHashService(newAppIdentifier);
235+
assert.equal(resolveParams.length, 2);
236+
assert.isFunction(resolveParams[1].service);
237+
assert.isDefined(resolveParams[1].args.adb);
238+
assert.equal(resolveParams[1].args.appIdentifier, newAppIdentifier);
239+
});
240+
});
189241
});

lib/definitions/livesync.d.ts

-7
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,6 @@ interface INativeScriptDeviceLiveSyncService extends IDeviceLiveSyncServiceBase
414414
}
415415

416416
interface IAndroidNativeScriptDeviceLiveSyncService extends INativeScriptDeviceLiveSyncService {
417-
/**
418-
* Retrieves the android device's hash service.
419-
* @param {string} appIdentifier Application identifier.
420-
* @return {Promise<Mobile.IAndroidDeviceHashService>} The hash service
421-
*/
422-
getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService;
423-
424417
/**
425418
* Guarantees all remove/update operations have finished
426419
* @param {ILiveSyncResultInfo} liveSyncInfo Describes the LiveSync operation - for which project directory is the operation and other settings.

lib/device-path-provider.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { fromWindowsRelativePathToUnix } from "./common/helpers";
22
import { APP_FOLDER_NAME } from "./constants";
33
import { LiveSyncPaths } from "./common/constants";
4-
import { AndroidDeviceLiveSyncService } from "./services/livesync/android-device-livesync-service";
54
import * as path from "path";
65

76
export class DevicePathProvider implements IDevicePathProvider {
87
constructor(private $mobileHelper: Mobile.IMobileHelper,
9-
private $injector: IInjector,
108
private $iOSSimResolver: Mobile.IiOSSimResolver,
119
private $errors: IErrors) {
1210
}
@@ -26,8 +24,7 @@ export class DevicePathProvider implements IDevicePathProvider {
2624
} else if (this.$mobileHelper.isAndroidPlatform(device.deviceInfo.platform)) {
2725
projectRoot = `${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${options.appIdentifier}`;
2826
if (!options.getDirname) {
29-
const deviceLiveSyncService = this.$injector.resolve<AndroidDeviceLiveSyncService>(AndroidDeviceLiveSyncService, { device });
30-
const hashService = deviceLiveSyncService.getDeviceHashService(options.appIdentifier);
27+
const hashService = (<Mobile.IAndroidDevice>device).fileSystem.getDeviceHashService(options.appIdentifier);
3128
const hashFile = options.syncAllFiles ? null : await hashService.doesShasumFileExistsOnDevice();
3229
const syncFolderName = options.watch || hashFile ? LiveSyncPaths.SYNC_DIR_NAME : LiveSyncPaths.FULLSYNC_DIR_NAME;
3330
projectRoot = path.join(projectRoot, syncFolderName);
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
import { DeviceLiveSyncServiceBase } from './device-livesync-service-base';
2-
import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service";
32

43
export abstract class AndroidDeviceLiveSyncServiceBase extends DeviceLiveSyncServiceBase {
5-
private deviceHashServices: IDictionary<Mobile.IAndroidDeviceHashService>;
6-
74
constructor(protected $injector: IInjector,
85
protected $platformsData: IPlatformsData,
96
protected $filesHashService: IFilesHashService,
107
protected $logger: ILogger,
118
protected device: Mobile.IAndroidDevice) {
129
super($platformsData, device);
13-
this.deviceHashServices = {};
1410
}
1511

1612
public abstract async transferFilesOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<void>;
1713
public abstract async transferDirectoryOnDevice(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void>;
1814

19-
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
20-
const key = `${this.device.deviceInfo.identifier}${appIdentifier}`;
21-
if (!this.deviceHashServices[key]) {
22-
const deviceHashService = this.$injector.resolve(AndroidDeviceHashService, { adb: this.device.adb, appIdentifier });
23-
this.deviceHashServices[key] = deviceHashService;
24-
}
25-
26-
return this.deviceHashServices[key];
27-
}
28-
2915
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, projectData: IProjectData, liveSyncDeviceInfo: ILiveSyncDeviceInfo, options: ITransferFilesOptions): Promise<Mobile.ILocalToDevicePathData[]> {
30-
const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier);
16+
const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
3117
const currentHashes = await deviceHashService.generateHashesFromLocalToDevicePaths(localToDevicePaths);
3218
const transferredFiles = await this.transferFilesCore(deviceAppData, localToDevicePaths, projectFilesPath, currentHashes, options);
33-
await this.updateHashesOnDevice(deviceAppData, currentHashes);
19+
await this.device.fileSystem.updateHashesOnDevice(currentHashes, deviceAppData.appIdentifier);
3420
return transferredFiles;
3521
}
3622

3723
private async transferFilesCore(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string, currentHashes: IStringDictionary, options: ITransferFilesOptions): Promise<Mobile.ILocalToDevicePathData[]> {
3824
if (options.force && options.isFullSync) {
39-
const hashFileDevicePath = this.getDeviceHashService(deviceAppData.appIdentifier).hashFileDevicePath;
25+
const hashFileDevicePath = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier).hashFileDevicePath;
4026
await this.device.fileSystem.deleteFile(hashFileDevicePath, deviceAppData.appIdentifier);
4127
this.$logger.trace("Before transfer directory on device ", localToDevicePaths);
4228
await this.transferDirectoryOnDevice(deviceAppData, localToDevicePaths, projectFilesPath);
@@ -59,17 +45,11 @@ export abstract class AndroidDeviceLiveSyncServiceBase extends DeviceLiveSyncSer
5945
}
6046

6147
private async getChangedLocalToDevicePaths(appIdentifier: string, localToDevicePaths: Mobile.ILocalToDevicePathData[], currentHashes: IStringDictionary): Promise<Mobile.ILocalToDevicePathData[]> {
62-
const deviceHashService = this.getDeviceHashService(appIdentifier);
48+
const deviceHashService = this.device.fileSystem.getDeviceHashService(appIdentifier);
6349
const oldHashes = (await deviceHashService.getShasumsFromDevice()) || {};
6450
const changedHashes = deviceHashService.getChangedShasums(oldHashes, currentHashes);
6551
const changedFiles = _.keys(changedHashes);
6652
const changedLocalToDevicePaths = localToDevicePaths.filter(localToDevicePathData => changedFiles.indexOf(localToDevicePathData.getLocalPath()) >= 0);
6753
return changedLocalToDevicePaths;
6854
}
69-
70-
private async updateHashesOnDevice(deviceAppData: Mobile.IDeviceAppData, currentHashes: IStringDictionary): Promise<IStringDictionary> {
71-
const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier);
72-
await deviceHashService.uploadHashFileToDevice(currentHashes);
73-
return currentHashes;
74-
}
7555
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export class AndroidDeviceLiveSyncService extends AndroidDeviceLiveSyncServiceBa
119119
await this.device.adb.executeShellCommand(["mkdir", "-p", path.dirname(deviceFilePath), " && ", "touch", deviceFilePath]);
120120
}
121121

122-
await this.getDeviceHashService(deviceAppData.appIdentifier).removeHashes(localToDevicePaths);
122+
const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
123+
await deviceHashService.removeHashes(localToDevicePaths);
123124
}
124125

125126
private async awaitRuntimeReloadSuccessMessage(): Promise<boolean> {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class AndroidDeviceSocketsLiveSyncService extends AndroidDeviceLiveSyncSe
101101

102102
public async removeFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<void> {
103103
await this.livesyncTool.removeFiles(_.map(localToDevicePaths, (element: any) => element.filePath));
104-
const deviceHashService = this.getDeviceHashService(deviceAppData.appIdentifier);
104+
const deviceHashService = this.device.fileSystem.getDeviceHashService(deviceAppData.appIdentifier);
105105
await deviceHashService.removeHashes(localToDevicePaths);
106106
}
107107

0 commit comments

Comments
 (0)