1
+ ///<reference path="../.d.ts"/>
2
+
3
+ import path = require( "path" ) ;
4
+ import util = require( "util" ) ;
5
+ import helpers = require( "./../common/helpers" ) ;
6
+
7
+ export class PlatformService implements IPlatformService {
8
+ private platformCapabilities : { [ key : string ] : IPlatformCapabilities } = {
9
+ ios : {
10
+ targetedOS : [ 'darwin' ]
11
+ } ,
12
+ android : {
13
+ }
14
+ } ;
15
+
16
+ private platformNames = [ ] ;
17
+
18
+ constructor ( private $errors : IErrors ,
19
+ private $fs : IFileSystem ,
20
+ private $projectService : IProjectService ) {
21
+ this . platformNames = Object . keys ( this . platformCapabilities ) ;
22
+ }
23
+
24
+ public getCapabilities ( platform : string ) : IPlatformCapabilities {
25
+ return this . platformCapabilities [ platform ] ;
26
+ }
27
+
28
+ public addPlatforms ( platforms : string [ ] ) : IFuture < void > {
29
+ return ( ( ) => {
30
+ if ( ! platforms || platforms . length === 0 ) {
31
+ this . $errors . fail ( "No platform specified. Please specify a platform to add" ) ;
32
+ }
33
+
34
+ this . $projectService . ensureProject ( ) ;
35
+
36
+ var platformsDir = this . $projectService . projectData . platformsDir ;
37
+ this . $fs . ensureDirectoryExists ( platformsDir ) . wait ( ) ;
38
+
39
+ _ . each ( platforms , platform => {
40
+ this . addPlatform ( platform . toLowerCase ( ) ) . wait ( ) ;
41
+ } ) ;
42
+
43
+ } ) . future < void > ( ) ( ) ;
44
+ }
45
+
46
+ private addPlatform ( platform : string ) : IFuture < void > {
47
+ return ( ( ) => {
48
+ platform = platform . split ( "@" ) [ 0 ] ;
49
+
50
+ this . validatePlatform ( platform ) ;
51
+
52
+ var platformPath = path . join ( this . $projectService . projectData . platformsDir , platform ) ;
53
+
54
+ // TODO: Check for version compatability if the platform is in format platform@version . This should be done in PR for semanting versioning
55
+
56
+ if ( this . $fs . exists ( platformPath ) . wait ( ) ) {
57
+ this . $errors . fail ( "Platform %s already added" , platform ) ;
58
+ }
59
+
60
+ // Copy platform specific files in platforms dir
61
+ this . $projectService . createPlatformSpecificProject ( platform ) . wait ( ) ;
62
+
63
+ } ) . future < void > ( ) ( ) ;
64
+ }
65
+
66
+ public getInstalledPlatforms ( ) : IFuture < string [ ] > {
67
+ return ( ( ) => {
68
+ if ( ! this . $fs . exists ( this . $projectService . projectData . platformsDir ) . wait ( ) ) {
69
+ return [ ] ;
70
+ }
71
+
72
+ var subDirs = this . $fs . readDirectory ( this . $projectService . projectData . platformsDir ) . wait ( ) ;
73
+ return _ . filter ( subDirs , p => { return this . platformNames . indexOf ( p ) > - 1 ; } ) ;
74
+ } ) . future < string [ ] > ( ) ( ) ;
75
+ }
76
+
77
+ public getAvailablePlatforms ( ) : IFuture < string [ ] > {
78
+ return ( ( ) => {
79
+ var installedPlatforms = this . getInstalledPlatforms ( ) . wait ( ) ;
80
+ return _ . filter ( this . platformNames , p => {
81
+ return installedPlatforms . indexOf ( p ) < 0 && this . isPlatformSupportedForOS ( p ) ; // Only those not already installed
82
+ } ) ;
83
+ } ) . future < string [ ] > ( ) ( ) ;
84
+ }
85
+
86
+ public runPlatform ( platform : string ) : IFuture < void > {
87
+ return ( ( ) => {
88
+
89
+ } ) . future < void > ( ) ( ) ;
90
+ }
91
+
92
+ public preparePlatform ( platform : string ) : IFuture < void > {
93
+ return ( ( ) => {
94
+ platform = platform . toLowerCase ( ) ;
95
+ this . validatePlatform ( platform ) ;
96
+ var normalizedPlatformName = this . normalizePlatformName ( platform ) ;
97
+
98
+ this . $projectService . prepareProject ( normalizedPlatformName , this . platformNames ) . wait ( ) ;
99
+ } ) . future < void > ( ) ( ) ;
100
+ }
101
+
102
+ public buildPlatform ( platform : string ) : IFuture < void > {
103
+ return ( ( ) => {
104
+ platform = platform . toLocaleLowerCase ( ) ;
105
+ this . validatePlatform ( platform ) ;
106
+
107
+ this . $projectService . buildProject ( platform ) . wait ( ) ;
108
+ } ) . future < void > ( ) ( ) ;
109
+ }
110
+
111
+ private validatePlatform ( platform : string ) : void {
112
+ if ( ! this . isValidPlatform ( platform ) ) {
113
+ this . $errors . fail ( "Invalid platform %s. Valid platforms are %s." , platform , helpers . formatListOfNames ( this . platformNames ) ) ;
114
+ }
115
+
116
+ if ( ! this . isPlatformSupportedForOS ( platform ) ) {
117
+ this . $errors . fail ( "Applications for platform %s can not be built on this OS - %s" , platform , process . platform ) ;
118
+ }
119
+ }
120
+
121
+ private isValidPlatform ( platform : string ) {
122
+ return this . platformCapabilities [ platform ] ;
123
+ }
124
+
125
+ private isPlatformSupportedForOS ( platform : string ) : boolean {
126
+ var platformCapabilities = this . getCapabilities ( platform ) ;
127
+ var targetedOS = platformCapabilities . targetedOS ;
128
+
129
+ if ( ! targetedOS || targetedOS . indexOf ( "*" ) >= 0 || targetedOS . indexOf ( process . platform ) >= 0 ) {
130
+ return true ;
131
+ }
132
+
133
+ return false ;
134
+ }
135
+
136
+ private normalizePlatformName ( platform : string ) : string {
137
+ switch ( platform ) {
138
+ case "android" :
139
+ return "Android" ;
140
+ case "ios" :
141
+ return "iOS" ;
142
+ }
143
+
144
+ return undefined ;
145
+ }
146
+ }
147
+ $injector . register ( "platformService" , PlatformService ) ;
0 commit comments