@@ -5,6 +5,7 @@ import shell = require("shelljs");
55import util = require( "util" ) ;
66import constants = require( "./../constants" ) ;
77import helpers = require( "./../common/helpers" ) ;
8+ import options = require( "./../options" ) ;
89
910class PlatformsData implements IPlatformsData {
1011 private platformsData : { [ index : string ] : any } = { } ;
@@ -34,7 +35,8 @@ export class PlatformService implements IPlatformService {
3435 private $logger : ILogger ,
3536 private $npm : INodePackageManager ,
3637 private $projectData : IProjectData ,
37- private $platformsData : IPlatformsData ) { }
38+ private $platformsData : IPlatformsData ,
39+ private $devicesServices : Mobile . IDevicesServices ) { }
3840
3941 public addPlatforms ( platforms : string [ ] ) : IFuture < void > {
4042 return ( ( ) => {
@@ -138,17 +140,9 @@ export class PlatformService implements IPlatformService {
138140 var platformProjectService = platformData . platformProjectService ;
139141
140142 var appFilesLocation = platformProjectService . prepareProject ( platformData ) . wait ( ) ;
141- var files = helpers . enumerateFilesInDirectorySync ( appFilesLocation ) ;
142143
143- _ . each ( files , fileName => {
144- var platformInfo = PlatformService . parsePlatformSpecificFileName ( path . basename ( fileName ) , this . $platformsData . platformsNames ) ;
145- var shouldExcludeFile = platformInfo && platformInfo . platform !== platform ;
146- if ( shouldExcludeFile ) {
147- this . $fs . deleteFile ( fileName ) . wait ( ) ;
148- } else if ( platformInfo && platformInfo . onDeviceName ) {
149- this . $fs . rename ( fileName , path . join ( path . dirname ( fileName ) , platformInfo . onDeviceName ) ) . wait ( ) ;
150- }
151- } ) ;
144+ this . processPlatformSpecificFiles ( platform , helpers . enumerateFilesInDirectorySync ( path . join ( appFilesLocation , constants . APP_FOLDER_NAME ) ) ) . wait ( ) ;
145+ this . processPlatformSpecificFiles ( platform , helpers . enumerateFilesInDirectorySync ( path . join ( appFilesLocation , constants . TNS_MODULES_FOLDER_NAME ) ) ) . wait ( ) ;
152146
153147 } ) . future < void > ( ) ( ) ;
154148 }
@@ -170,7 +164,14 @@ export class PlatformService implements IPlatformService {
170164 platform = platform . toLowerCase ( ) ;
171165
172166 this . preparePlatform ( platform ) . wait ( ) ;
167+
168+ // We need to set device option here
169+ var cachedDeviceOption = options . device ;
170+ options . device = true ;
173171 this . buildPlatform ( platform ) . wait ( ) ;
172+ options . device = cachedDeviceOption ;
173+
174+ this . deploy ( platform ) . wait ( ) ;
174175 } ) . future < void > ( ) ( ) ;
175176 }
176177
@@ -190,6 +191,44 @@ export class PlatformService implements IPlatformService {
190191 } ) . future < void > ( ) ( ) ;
191192 }
192193
194+ public deploy ( platform : string ) : IFuture < void > {
195+ return ( ( ) => {
196+ platform = platform . toLowerCase ( ) ;
197+
198+ this . validatePlatformInstalled ( platform ) ;
199+
200+ var platformData = this . $platformsData . getPlatformData ( platform ) ;
201+
202+ // Get latest package that is produced from build
203+ var candidates = this . $fs . readDirectory ( platformData . buildOutputPath ) . wait ( ) ;
204+ var packages = _ . filter ( candidates , candidate => {
205+ return _ . contains ( platformData . validPackageNames , candidate ) ;
206+ } ) . map ( currentPackage => {
207+ currentPackage = path . join ( platformData . buildOutputPath , currentPackage ) ;
208+
209+ return {
210+ pkg : currentPackage ,
211+ time : this . $fs . getFsStats ( currentPackage ) . wait ( ) . mtime
212+ } ;
213+ } ) ;
214+
215+ packages = _ . sortBy ( packages , pkg => pkg . time ) . reverse ( ) ; // We need to reverse because sortBy always sorts in ascending order
216+
217+ if ( packages . length === 0 ) {
218+ var packageExtName = path . extname ( platformData . validPackageNames [ 0 ] ) ;
219+ this . $errors . fail ( "No %s found in %s directory" , packageExtName , platformData . buildOutputPath )
220+ }
221+
222+ var packageFile = packages [ 0 ] . pkg ;
223+ this . $logger . out ( "Using " , packageFile ) ;
224+
225+ this . $devicesServices . initialize ( platform , options . device ) . wait ( ) ;
226+ var action = ( device : Mobile . IDevice ) : IFuture < void > => { return device . deploy ( packageFile , this . $projectData . projectId ) ; } ;
227+ this . $devicesServices . execute ( action ) . wait ( ) ;
228+
229+ } ) . future < void > ( ) ( ) ;
230+ }
231+
193232 private validatePlatform ( platform : string ) : void {
194233 if ( ! platform ) {
195234 this . $errors . fail ( "No platform specified." )
@@ -245,5 +284,20 @@ export class PlatformService implements IPlatformService {
245284 }
246285 return undefined ;
247286 }
287+
288+ private processPlatformSpecificFiles ( platform : string , files : string [ ] ) : IFuture < void > {
289+ // Renames the files that have `platform` as substring and removes the files from other platform
290+ return ( ( ) => {
291+ _ . each ( files , fileName => {
292+ var platformInfo = PlatformService . parsePlatformSpecificFileName ( path . basename ( fileName ) , this . $platformsData . platformsNames ) ;
293+ var shouldExcludeFile = platformInfo && platformInfo . platform !== platform ;
294+ if ( shouldExcludeFile ) {
295+ this . $fs . deleteFile ( fileName ) . wait ( ) ;
296+ } else if ( platformInfo && platformInfo . onDeviceName ) {
297+ this . $fs . rename ( fileName , path . join ( path . dirname ( fileName ) , platformInfo . onDeviceName ) ) . wait ( ) ;
298+ }
299+ } ) ;
300+ } ) . future < void > ( ) ( ) ;
301+ }
248302}
249303$injector . register ( "platformService" , PlatformService ) ;
0 commit comments