11///<reference path="../.d.ts"/> 
22
3+ import  path  =  require( "path" ) ; 
4+ import  helpers  =  require( "./../common/helpers" ) ; 
5+ 
36export  class  PlatformService  implements  IPlatformService  { 
47    constructor ( private  $errors : IErrors , 
58        private  $fs : IFileSystem , 
69        private  $projectService : IProjectService )  {  } 
710
8-     private  platformCapabilities : { [ key : string ] : IPlatformCapabilities }  =  { 
11+     private  platformCapabilities : {   [ key : string ] : IPlatformCapabilities   }  =  { 
912        ios : { 
1013            targetedOS : [ 'darwin' ] , 
1114            frameworkUrl : "" 
@@ -19,52 +22,57 @@ export class PlatformService implements IPlatformService {
1922        return  this . platformCapabilities [ platform ] ; 
2023    } 
2124
22-     private  isValidPlatform ( platform : string )  { 
23-         return  ! this . platformCapabilities [ platform . toLowerCase ( ) ] ; 
24-     } 
25- 
26-     public  addPlatforms ( platforms : string [ ] ) : Future < any >  { 
25+     public  addPlatforms ( platforms : string [ ] ) : IFuture < any >  { 
2726        return  ( ( )  =>  { 
28-             if ( ! platforms )  { 
27+ 			this . $projectService . ensureProject ( ) ; 
28+ 
29+             if ( ! platforms  ||  platforms . length  ===  0 )  { 
2930                this . $errors . fail ( "No platform specified. Please specify a platform to add" ) ; 
3031            } 
3132
3233            var  platformsDir  =  this . $projectService . projectData . platformsDir ; 
3334            if ( ! this . $fs . exists ( platformsDir ) . wait ( ) )  { 
34-                 this . $fs . createDirectory ( platformsDir ) ; 
35+                 this . $fs . createDirectory ( platformsDir ) . wait ( ) ; 
3536            } 
3637
3738            _ . each ( platforms ,  platform  =>  { 
38-                 this . addPlatform ( platform ) ; 
39+                 this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ; 
3940            } ) ; 
4041
4142        } ) . future < any > ( ) ( ) ; 
4243    } 
4344
44-     private  addPlatform ( platform : string )  { 
45-         platform  =  platform . split ( "@" ) [ 0 ] ; 
46-         var  platformPath  =  path . join ( this . $projectService . projectData . platformsDir ,  platform ) ; 
45+     private  addPlatform ( platform : string ) : IFuture < void >  { 
46+ 		return ( ( )  =>  { 
47+ 			platform  =  platform . split ( "@" ) [ 0 ] ; 
48+ 			var  platformPath  =  path . join ( this . $projectService . projectData . platformsDir ,  platform ) ; 
4749
48-          // TODO: Check for version compatability if the platform is in format platform@version . This should be done in PR for semanting versioning 
50+ 			 // TODO: Check for version compatability if the platform is in format platform@version . This should be done in PR for semanting versioning 
4951
50-          if ( ! this . isValidPlatform ( platform ) )  { 
51-              this . $errors . fail ( "" ) ; 
52-          } 
52+ 			 if   ( ! this . isValidPlatform ( platform ) )  { 
53+ 				 this . $errors . fail ( "Invalid platform %s. Valid platforms are %s." ,   platform ,   helpers . formatListOfNames ( _ . keys ( this . platformCapabilities ) ) ) ; 
54+ 			 } 
5355
54-          if ( ! this . isPlatformSupportedForOS ( platform ) )  { 
55-              this . $errors . fail ( "Applications for platform %s can not be built on this OS - %s" ,  platform ,  process . platform ) ; 
56-          } 
56+ 			 if   ( ! this . isPlatformSupportedForOS ( platform ) )  { 
57+ 				 this . $errors . fail ( "Applications for platform %s can not be built on this OS - %s" ,  platform ,  process . platform ) ; 
58+ 			 } 
5759
58-          if ( this . $fs . exists ( platformPath ) )  { 
59-              this . $errors . fail ( "Platform %s already added" ,  platform ) ; 
60-          } 
60+ 			 if   ( this . $fs . exists ( platformPath ) . wait ( ) )  { 
61+ 				 this . $errors . fail ( "Platform %s already added" ,  platform ) ; 
62+ 			 } 
6163
62-         // TODO: This should be downloaded from npm 
63-         // Copy platform specific files in platforms dir 
64+ 			// Copy platform specific files in platforms dir 
65+ 			this . $projectService . createPlatformSpecificProject ( platform ) . wait ( ) ; 
66+ 
67+ 		} ) . future < void > ( ) ( ) ; 
6468    } 
6569
66-     private  isPlatformSupportedForOS ( platform : string ) : boolean  { 
67-         var  platformCapabilities  =  this . getCapabilities ( platform )  ||  { } ; 
70+ 	private  isValidPlatform ( platform : string )  { 
71+ 		return  this . platformCapabilities [ platform ] ; 
72+ 	} 
73+ 
74+ 	private  isPlatformSupportedForOS ( platform : string ) : boolean  { 
75+         var  platformCapabilities  =  this . getCapabilities ( platform ) ; 
6876        var  targetedOS  =  platformCapabilities . targetedOS ; 
6977
7078        if ( ! targetedOS  ||  targetedOS . indexOf ( "*" )  >=  0  ||  targetedOS . indexOf ( process . platform )  >=  0 )  { 
@@ -73,4 +81,5 @@ export class PlatformService implements IPlatformService {
7381
7482        return  false ; 
7583    } 
76- } 
84+ } 
85+ $injector . register ( "platformService" ,  PlatformService ) ; 
0 commit comments