@@ -4,45 +4,65 @@ import path = require("path");
4
4
import helpers = require( "./../common/helpers" ) ;
5
5
6
6
export 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 ( ( ) => {
27
27
this . $projectService . ensureProject ( ) ;
28
28
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
+ }
32
32
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
+ }
37
37
38
- _ . each ( platforms , platform => {
39
- this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ;
40
- } ) ;
38
+ _ . each ( platforms , platform => {
39
+ this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ;
40
+ } ) ;
41
41
42
- } ) . future < any > ( ) ( ) ;
43
- }
42
+ } ) . future < any > ( ) ( ) ;
43
+ }
44
44
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 > {
46
66
return ( ( ) => {
47
67
platform = platform . split ( "@" ) [ 0 ] ;
48
68
var platformPath = path . join ( this . $projectService . projectData . platformsDir , platform ) ;
@@ -65,21 +85,21 @@ export class PlatformService implements IPlatformService {
65
85
this . $projectService . createPlatformSpecificProject ( platform ) . wait ( ) ;
66
86
67
87
} ) . future < void > ( ) ( ) ;
68
- }
88
+ }
69
89
70
90
private isValidPlatform ( platform : string ) {
71
91
return this . platformCapabilities [ platform ] ;
72
92
}
73
93
74
94
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 ;
77
97
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
+ }
81
101
82
- return false ;
83
- }
102
+ return false ;
103
+ }
84
104
}
85
105
$injector . register ( "platformService" , PlatformService ) ;
0 commit comments