Skip to content

Commit efba2b1

Browse files
Fatme HavaluovaFatme
Fatme Havaluova
authored andcommitted
Support for android native libraries in plugin
1 parent 012a406 commit efba2b1

11 files changed

+490
-141
lines changed

lib/definitions/libref.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ interface ILibRef {
22
idx: number;
33
path: string;
44
adjustedPath?: string;
5+
key?: string;
56
}

lib/definitions/plugins.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface IPluginsService {
77
}
88

99
interface IPluginData extends INodeModuleData {
10-
platformsData: IPluginPlatformsData;
10+
platformsData: IPluginPlatformsData;
11+
pluginPlatformsFolderPath(platform: string): string;
1112
}
1213

1314
interface INodeModuleData {

lib/definitions/project.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ interface IPlatformProjectService {
3333
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
3434
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
3535
updatePlatform(currentVersion: string, newVersion: string): IFuture<void>;
36+
preparePluginNativeCode(pluginData: IPluginData): IFuture<void>;
37+
removePluginNativeCode(pluginData: IPluginData): IFuture<void>;
38+
}
39+
40+
interface IAndroidProjectPropertiesManager {
41+
getProjectReferences(): IFuture<ILibRef[]>;
42+
addProjectReference(referencePath: string): IFuture<void>;
43+
removeProjectReference(referencePath: string): IFuture<void>;
3644
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)