-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-project-properties-manager.ts
101 lines (85 loc) · 3.23 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
import * as path from "path";
export class AndroidProjectPropertiesManager implements IAndroidProjectPropertiesManager {
private _editor: IPropertiesParserEditor = null;
private filePath: string = null;
private projectReferences: ILibRef[];
private dirty = false;
constructor(private $propertiesParser: IPropertiesParser,
private $logger: ILogger,
directoryPath: string) {
this.filePath = path.join(directoryPath, "project.properties");
}
public async getProjectReferences(): Promise<ILibRef[]> {
if (!this.projectReferences || this.dirty) {
const allProjectProperties = await this.getAllProjectProperties();
const allProjectPropertiesKeys = _.keys(allProjectProperties);
this.projectReferences = _(allProjectPropertiesKeys)
.filter(key => _.startsWith(key, "android.library.reference."))
.map(key => this.createLibraryReference(key, allProjectProperties[key]))
.value();
}
return this.projectReferences;
}
public async addProjectReference(referencePath: string): Promise<void> {
const references = await this.getProjectReferences();
const libRefExists = _.some(references, r => path.normalize(r.path) === path.normalize(referencePath));
if (!libRefExists) {
await this.addToPropertyList("android.library.reference", referencePath);
}
}
public async removeProjectReference(referencePath: string): Promise<void> {
const references = await this.getProjectReferences();
const libRefExists = _.some(references, r => path.normalize(r.path) === path.normalize(referencePath));
if (libRefExists) {
await this.removeFromPropertyList("android.library.reference", referencePath);
} else {
this.$logger.error(`Could not find ${referencePath}.`);
}
}
private async createEditor(): Promise<any> {
return this._editor || await this.$propertiesParser.createEditor(this.filePath);
}
private buildKeyName(key: string, index: number): string {
return `${key}.${index}`;
}
private async getAllProjectProperties(): Promise<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 async addToPropertyList(key: string, value: string): Promise<void> {
const editor = await this.createEditor();
let i = 1;
while (editor.get(this.buildKeyName(key, i))) {
i++;
}
editor.set(this.buildKeyName(key, i), value);
await this.$propertiesParser.saveEditor();
this.dirty = true;
}
private async removeFromPropertyList(key: string, value: string): Promise<void> {
const editor = await this.createEditor();
const 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++;
}
await this.$propertiesParser.saveEditor();
this.dirty = true;
}
}