@@ -4,45 +4,65 @@ import path = require("path");
44import helpers = require( "./../common/helpers" ) ;
55
66export class PlatformService implements IPlatformService {
7- constructor ( private $errors : IErrors ,
8- private $fs : IFileSystem ,
9- private $projectService : IProjectService ) { }
10-
11- private platformCapabilities : { [ key : string ] : IPlatformCapabilities } = {
12- ios : {
13- targetedOS : [ 'darwin' ] ,
14- frameworkUrl : ""
15- } ,
16- android : {
17- frameworkUrl : ""
18- }
19- } ;
20-
21- public getCapabilities ( platform : string ) : IPlatformCapabilities {
22- return this . platformCapabilities [ platform ] ;
23- }
24-
25- public addPlatforms ( platforms : string [ ] ) : IFuture < any > {
26- return ( ( ) => {
7+ constructor ( private $errors : IErrors ,
8+ private $fs : IFileSystem ,
9+ private $projectService : IProjectService ) { }
10+
11+ private platformCapabilities : { [ key : string ] : IPlatformCapabilities } = {
12+ ios : {
13+ targetedOS : [ 'darwin' ] ,
14+ frameworkUrl : ""
15+ } ,
16+ android : {
17+ frameworkUrl : ""
18+ }
19+ } ;
20+
21+ public getCapabilities ( platform : string ) : IPlatformCapabilities {
22+ return this . platformCapabilities [ platform ] ;
23+ }
24+
25+ public addPlatforms ( platforms : string [ ] ) : IFuture < any > {
26+ return ( ( ) => {
2727 this . $projectService . ensureProject ( ) ;
2828
29- if ( ! platforms || platforms . length === 0 ) {
30- this . $errors . fail ( "No platform specified. Please specify a platform to add" ) ;
31- }
29+ if ( ! platforms || platforms . length === 0 ) {
30+ this . $errors . fail ( "No platform specified. Please specify a platform to add" ) ;
31+ }
3232
33- var platformsDir = this . $projectService . projectData . platformsDir ;
34- if ( ! this . $fs . exists ( platformsDir ) . wait ( ) ) {
35- this . $fs . createDirectory ( platformsDir ) . wait ( ) ;
36- }
33+ var platformsDir = this . $projectService . projectData . platformsDir ;
34+ if ( ! this . $fs . exists ( platformsDir ) . wait ( ) ) {
35+ this . $fs . createDirectory ( platformsDir ) . wait ( ) ;
36+ }
3737
38- _ . each ( platforms , platform => {
39- this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ;
40- } ) ;
38+ _ . each ( platforms , platform => {
39+ this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ;
40+ } ) ;
4141
42- } ) . future < any > ( ) ( ) ;
43- }
42+ } ) . future < any > ( ) ( ) ;
43+ }
4444
45- private addPlatform ( platform : string ) : IFuture < void > {
45+ public getInstalledPlatforms ( ) : IFuture < string [ ] > {
46+ return ( ( ) => {
47+ if ( ! this . $fs . exists ( this . $projectService . projectData . platformsDir ) . wait ( ) ) {
48+ return [ ] ;
49+ }
50+
51+ var subDirs = this . $fs . readDirectory ( this . $projectService . projectData . platformsDir ) . wait ( ) ;
52+ return _ . filter ( subDirs , p => { return Object . keys ( this . platformCapabilities ) . indexOf ( p ) > - 1 ; } ) ;
53+ } ) . future < string [ ] > ( ) ( ) ;
54+ }
55+
56+ public getAvailablePlatforms ( ) : IFuture < string [ ] > {
57+ return ( ( ) => {
58+ var installedPlatforms = this . getInstalledPlatforms ( ) . wait ( ) ;
59+ return _ . filter ( _ . keys ( this . platformCapabilities ) , p => {
60+ return installedPlatforms . indexOf ( p ) < 0 && this . isPlatformSupportedForOS ( p ) ; // Only those not already installed
61+ } ) ;
62+ } ) . future < string [ ] > ( ) ( ) ;
63+ }
64+
65+ private addPlatform ( platform : string ) : IFuture < void > {
4666 return ( ( ) => {
4767 platform = platform . split ( "@" ) [ 0 ] ;
4868 var platformPath = path . join ( this . $projectService . projectData . platformsDir , platform ) ;
@@ -65,21 +85,21 @@ export class PlatformService implements IPlatformService {
6585 this . $projectService . createPlatformSpecificProject ( platform ) . wait ( ) ;
6686
6787 } ) . future < void > ( ) ( ) ;
68- }
88+ }
6989
7090 private isValidPlatform ( platform : string ) {
7191 return this . platformCapabilities [ platform ] ;
7292 }
7393
7494 private isPlatformSupportedForOS ( platform : string ) : boolean {
75- var platformCapabilities = this . getCapabilities ( platform ) ;
76- var targetedOS = platformCapabilities . targetedOS ;
95+ var platformCapabilities = this . getCapabilities ( platform ) ;
96+ var targetedOS = platformCapabilities . targetedOS ;
7797
78- if ( ! targetedOS || targetedOS . indexOf ( "*" ) >= 0 || targetedOS . indexOf ( process . platform ) >= 0 ) {
79- return true ;
80- }
98+ if ( ! targetedOS || targetedOS . indexOf ( "*" ) >= 0 || targetedOS . indexOf ( process . platform ) >= 0 ) {
99+ return true ;
100+ }
81101
82- return false ;
83- }
102+ return false ;
103+ }
84104}
85105$injector . register ( "platformService" , PlatformService ) ;
0 commit comments