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+ private $logger : ILogger ,
17+ directoryPath : string ) {
18+ this . filePath = path . join ( directoryPath , "project.properties" ) ;
19+ }
20+
21+
22+
23+ public getProjectReferences ( ) : IFuture < ILibRef [ ] > {
24+ return ( ( ) => {
25+ if ( ! this . projectReferences || this . dirty ) {
26+ let allProjectProperties = this . getAllProjectProperties ( ) . wait ( ) ;
27+ let allProjectPropertiesKeys = _ . keys ( allProjectProperties ) ;
28+ this . projectReferences = _ ( allProjectPropertiesKeys )
29+ . filter ( key => _ . startsWith ( key , "android.library.reference." ) )
30+ . map ( key => this . createLibraryReference ( key , allProjectProperties [ key ] ) )
31+ . value ( ) ;
32+ }
33+
34+ return this . projectReferences ;
35+ } ) . future < ILibRef [ ] > ( ) ( ) ;
36+ }
37+
38+ public addProjectReference ( referencePath : string ) : IFuture < void > {
39+ return ( ( ) => {
40+ let references = this . getProjectReferences ( ) . wait ( ) ;
41+ let libRefExists = _ . any ( references , r => path . normalize ( r . path ) === path . normalize ( referencePath ) ) ;
42+ if ( ! libRefExists ) {
43+ this . addToPropertyList ( "android.library.reference" , referencePath ) . wait ( ) ;
44+ }
45+ } ) . future < void > ( ) ( ) ;
46+ }
47+
48+ public removeProjectReference ( referencePath : string ) : IFuture < void > {
49+ return ( ( ) => {
50+ let references = this . getProjectReferences ( ) . wait ( ) ;
51+ let libRefExists = _ . any ( references , r => path . normalize ( r . path ) === path . normalize ( referencePath ) ) ;
52+ if ( libRefExists ) {
53+ this . removeFromPropertyList ( "android.library.reference" , referencePath ) . wait ( ) ;
54+ } else {
55+ this . $logger . error ( `Could not find ${ referencePath } .` ) ;
56+ }
57+ } ) . future < void > ( ) ( ) ;
58+ }
59+
60+ private createEditor ( ) : IFuture < any > {
61+ return ( ( ) => {
62+ return this . _editor || this . $propertiesParser . createEditor ( this . filePath ) . wait ( ) ;
63+ } ) . future < any > ( ) ( ) ;
64+ }
65+
66+ private buildKeyName ( key : string , index : number ) : string {
67+ return `${ key } .${ index } ` ;
68+ }
69+
70+ private getAllProjectProperties ( ) : IFuture < IStringDictionary > {
71+ return this . $propertiesParser . read ( this . filePath ) ;
72+ }
73+
74+ private createLibraryReference ( referenceName : string , referencePath : string ) : ILibRef {
75+ return {
76+ idx : parseInt ( referenceName . split ( "android.library.reference." ) [ 1 ] ) ,
77+ key : referenceName ,
78+ path : referencePath ,
79+ adjustedPath : path . join ( path . dirname ( this . filePath ) , referencePath )
80+ }
81+ }
82+
83+ private addToPropertyList ( key : string , value : string ) : IFuture < void > {
84+ return ( ( ) => {
85+ let editor = this . createEditor ( ) . wait ( ) ;
86+ let i = 1 ;
87+ while ( editor . get ( this . buildKeyName ( key , i ) ) ) {
88+ i ++ ;
89+ }
90+
91+ editor . set ( this . buildKeyName ( key , i ) , value ) ;
92+ this . $propertiesParser . saveEditor ( ) . wait ( ) ;
93+ this . dirty = true ;
94+ } ) . future < void > ( ) ( ) ;
95+ }
96+
97+ private removeFromPropertyList ( key : string , value : string ) : IFuture < void > {
98+ return ( ( ) => {
99+ let editor = this . createEditor ( ) . wait ( ) ;
100+ let valueLowerCase = value . toLowerCase ( ) ;
101+ let i = 1 ;
102+ let currentValue : any ;
103+ while ( currentValue = editor . get ( this . buildKeyName ( key , i ) ) ) {
104+ if ( currentValue . toLowerCase ( ) === valueLowerCase ) {
105+ while ( currentValue = editor . get ( this . buildKeyName ( key , i + 1 ) ) ) {
106+ editor . set ( this . buildKeyName ( key , i ) , currentValue ) ;
107+ i ++ ;
108+ }
109+ editor . set ( this . buildKeyName ( key , i ) ) ;
110+ break ;
111+ }
112+ i ++ ;
113+ }
114+ this . $propertiesParser . saveEditor ( ) . wait ( ) ;
115+ this . dirty = true ;
116+ } ) . future < void > ( ) ( ) ;
117+ }
118+ }
0 commit comments