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