11import { DeviceAndroidDebugBridge } from "../../common/mobile/android/device-android-debug-bridge" ;
22import { AndroidDeviceHashService } from "../../common/mobile/android/android-device-hash-service" ;
33import { DeviceLiveSyncServiceBase } from "./device-livesync-service-base" ;
4- import { cache } from "../../common/decorators" ;
4+ import { APP_FOLDER_NAME } from "../../constants" ;
5+ import * as path from "path" ;
6+
57const LivesyncTool = require ( "nativescript-android-livesync-lib" ) ;
68
79export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService , INativeScriptDeviceLiveSyncService {
@@ -13,32 +15,61 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
1315 private $injector : IInjector ,
1416 protected $platformsData : IPlatformsData ,
1517 protected $staticConfig : Config . IStaticConfig ,
16- protected device : Mobile . IAndroidDevice ) {
18+ protected device : Mobile . IAndroidDevice ,
19+ private $options : ICommonOptions ) {
1720 super ( $platformsData , device ) ;
1821 this . livesyncTool = new LivesyncTool ( ) ;
1922 }
2023
2124 public async beforeLiveSyncAction ( deviceAppData : Mobile . IDeviceAppData ) : Promise < void > {
25+ const platformData = this . $platformsData . getPlatformData ( deviceAppData . platform , this . data ) ;
26+ const projectFilesPath = path . join ( platformData . appDestinationDirectoryPath , APP_FOLDER_NAME ) ;
27+ await this . connectLivesyncTool ( projectFilesPath , this . data . projectId ) ;
2228 await this . device . applicationManager . startApplication ( { appId : deviceAppData . appIdentifier , projectName : this . data . projectName } ) ;
2329 }
2430
2531 public async refreshApplication ( projectData : IProjectData , liveSyncInfo : ILiveSyncResultInfo ) : Promise < void > {
26- //await this.connectLivesyncTool("", projectData.projectId);
2732 await this . livesyncTool . sendDoSyncOperation ( )
2833 }
2934
3035 public async removeFiles ( deviceAppData : Mobile . IDeviceAppData , localToDevicePaths : Mobile . ILocalToDevicePathData [ ] , projectFilesPath : string ) : Promise < void > {
31- await this . connectLivesyncTool ( projectFilesPath , deviceAppData . appIdentifier ) ;
3236 await this . livesyncTool . removeFilesArray ( _ . map ( localToDevicePaths , ( element : any ) => { return element . filePath } ) ) ;
3337 }
3438
3539 public async transferFiles ( deviceAppData : Mobile . IDeviceAppData , localToDevicePaths : Mobile . ILocalToDevicePathData [ ] , projectFilesPath : string , isFullSync : boolean ) : Promise < Mobile . ILocalToDevicePathData [ ] > {
36- await this . connectLivesyncTool ( projectFilesPath , deviceAppData . appIdentifier ) ;
40+ let transferredFiles ;
41+
42+ if ( isFullSync ) {
43+ transferredFiles = await this . _transferDirectory ( deviceAppData , localToDevicePaths , projectFilesPath ) ;
44+ } else {
45+ transferredFiles = this . _transferFiles ( deviceAppData , localToDevicePaths , projectFilesPath ) ;
46+ }
47+
48+ return transferredFiles ;
49+ }
50+
51+ private async _transferFiles ( deviceAppData : Mobile . IDeviceAppData , localToDevicePaths : Mobile . ILocalToDevicePathData [ ] , projectFilesPath : string ) : Promise < Mobile . ILocalToDevicePathData [ ] > {
3752 await this . livesyncTool . sendFilesArray ( localToDevicePaths . map ( localToDevicePathData => localToDevicePathData . getLocalPath ( ) ) ) ;
3853
3954 return localToDevicePaths ;
4055 }
4156
57+ private async _transferDirectory ( deviceAppData : Mobile . IDeviceAppData , localToDevicePaths : Mobile . ILocalToDevicePathData [ ] , projectFilesPath : string ) : Promise < Mobile . ILocalToDevicePathData [ ] > {
58+ const deviceHashService = this . getDeviceHashService ( deviceAppData . appIdentifier ) ;
59+ const currentShasums : IStringDictionary = await deviceHashService . generateHashesFromLocalToDevicePaths ( localToDevicePaths ) ;
60+ const oldShasums = await deviceHashService . getShasumsFromDevice ( ) ;
61+
62+ if ( this . $options . force || ! oldShasums ) {
63+ this . livesyncTool . sendDirectory ( projectFilesPath ) ;
64+
65+ return localToDevicePaths ;
66+ } else {
67+ const changedShasums = deviceHashService . getChnagedShasums ( oldShasums , currentShasums ) ;
68+ await this . livesyncTool . sendFilesArray ( _ . map ( changedShasums , ( hash : string , pathToFile : string ) => pathToFile ) ) ;
69+ await deviceHashService . uploadHashFileToDevice ( currentShasums ) ;
70+ }
71+ }
72+
4273 private async connectLivesyncTool ( projectFilesPath : string , appIdentifier : string ) {
4374 const adbPath = await this . $staticConfig . getAdbFilePath ( ) ;
4475 await this . livesyncTool . connect ( {
@@ -50,8 +81,6 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
5081 } ) ;
5182 }
5283
53-
54- @cache ( )
5584 public getDeviceHashService ( appIdentifier : string ) : Mobile . IAndroidDeviceHashService {
5685 const adb = this . $injector . resolve ( DeviceAndroidDebugBridge , { identifier : this . device . deviceInfo . identifier } ) ;
5786 return this . $injector . resolve ( AndroidDeviceHashService , { adb, appIdentifier } ) ;
0 commit comments