1
+ ///<reference path="../.d.ts"/>
2
+ "use strict" ;
3
+
4
+ import path = require( "path" ) ;
5
+
6
+ export class AndroidProjectPropertiesManager implements IAndroidProjectPropertiesManager {
7
+ private static LIBRARY_REFERENCE_KEY_PREFIX = "android.library.reference." ;
8
+
9
+ private _editor : IPropertiesParserEditor = null ;
10
+ private filePath : string = null ;
11
+ private projectReferences : ILibRef [ ] ;
12
+ private dirty = false ;
13
+
14
+ constructor ( private $propertiesParser : IPropertiesParser ,
15
+ private $fs : IFileSystem ,
16
+ directoryPath : string ) {
17
+ this . filePath = path . join ( directoryPath , "project.properties" ) ;
18
+ }
19
+
20
+ public addToPropertyList ( key : string , value : string ) : IFuture < void > {
21
+ return ( ( ) => {
22
+ let editor = this . createEditor ( ) . wait ( ) ;
23
+ let i = 1 ;
24
+ while ( editor . get ( this . buildKeyName ( key , i ) ) ) {
25
+ i ++ ;
26
+ }
27
+
28
+ editor . set ( this . buildKeyName ( key , i ) , value ) ;
29
+ this . $propertiesParser . saveEditor ( ) . wait ( ) ;
30
+ this . dirty = true ;
31
+ } ) . future < void > ( ) ( ) ;
32
+ }
33
+
34
+ public removeFromPropertyList ( key : string , value : string ) : IFuture < void > {
35
+ return ( ( ) => {
36
+ let editor = this . createEditor ( ) . wait ( ) ;
37
+ let valueLowerCase = value . toLowerCase ( ) ;
38
+ let i = 1 ;
39
+ let currentValue : any ;
40
+ while ( currentValue = editor . get ( this . buildKeyName ( key , i ) ) ) {
41
+ if ( currentValue . toLowerCase ( ) === valueLowerCase ) {
42
+ while ( currentValue = editor . get ( this . buildKeyName ( key , i + 1 ) ) ) {
43
+ editor . set ( this . buildKeyName ( key , i ) , currentValue ) ;
44
+ i ++ ;
45
+ }
46
+ editor . set ( this . buildKeyName ( key , i ) ) ;
47
+ break ;
48
+ }
49
+ i ++ ;
50
+ }
51
+ this . $propertiesParser . saveEditor ( ) . wait ( ) ;
52
+ this . dirty = true ;
53
+ } ) . future < void > ( ) ( ) ;
54
+ }
55
+
56
+ public getProjectReferences ( ) : IFuture < ILibRef [ ] > {
57
+ return ( ( ) => {
58
+ if ( ! this . projectReferences || this . dirty ) {
59
+ let allProjectProperties = this . getAllProjectProperties ( ) . wait ( ) ;
60
+ let allProjectPropertiesKeys = _ . keys ( allProjectProperties ) ;
61
+ this . projectReferences = _ ( allProjectPropertiesKeys )
62
+ . filter ( key => _ . startsWith ( key , "android.library.reference." ) )
63
+ . map ( key => this . createLibraryReference ( key , allProjectProperties [ key ] ) )
64
+ . value ( ) ;
65
+ }
66
+
67
+ return this . projectReferences ;
68
+ } ) . future < ILibRef [ ] > ( ) ( ) ;
69
+ }
70
+
71
+ public addProjectReference ( referencePath : string ) : IFuture < void > {
72
+ return ( ( ) => {
73
+ let references = this . getProjectReferences ( ) . wait ( ) ;
74
+ let libRefExists = _ . any ( references , r => path . normalize ( r . path ) === path . normalize ( referencePath ) ) ;
75
+ if ( ! libRefExists ) {
76
+ this . addToPropertyList ( "android.library.reference" , referencePath ) . wait ( ) ;
77
+ }
78
+ } ) . future < void > ( ) ( ) ;
79
+ }
80
+
81
+ public removeProjectReference ( referencePath : string ) : IFuture < void > {
82
+ return ( ( ) => {
83
+ let references = this . getProjectReferences ( ) . wait ( ) ;
84
+ let libRefExists = _ . any ( references , r => path . normalize ( r . path ) === path . normalize ( referencePath ) ) ;
85
+ if ( libRefExists ) {
86
+ this . removeFromPropertyList ( "android.library.reference" , referencePath ) . wait ( ) ;
87
+ }
88
+ } ) . future < void > ( ) ( ) ;
89
+ }
90
+
91
+ private createEditor ( ) : IFuture < any > {
92
+ return ( ( ) => {
93
+ return this . _editor || this . $propertiesParser . createEditor ( this . filePath ) . wait ( ) ;
94
+ } ) . future < any > ( ) ( ) ;
95
+ }
96
+
97
+ private buildKeyName ( key : string , index : number ) : string {
98
+ return `${ key } .${ index } ` ;
99
+ }
100
+
101
+ private getAllProjectProperties ( ) : IFuture < IStringDictionary > {
102
+ return this . $propertiesParser . read ( this . filePath ) ;
103
+ }
104
+
105
+ private createLibraryReference ( referenceName : string , referencePath : string ) : ILibRef {
106
+ return {
107
+ idx : parseInt ( referenceName . split ( "android.library.reference." ) [ 1 ] ) ,
108
+ key : referenceName ,
109
+ path : referencePath ,
110
+ adjustedPath : path . join ( path . dirname ( this . filePath ) , referencePath )
111
+ }
112
+ }
113
+ }
0 commit comments