Skip to content

Support for android native libraries in plugin #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/definitions/libref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ interface ILibRef {
idx: number;
path: string;
adjustedPath?: string;
key?: string;
}
3 changes: 2 additions & 1 deletion lib/definitions/plugins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ interface IPluginsService {
}

interface IPluginData extends INodeModuleData {
platformsData: IPluginPlatformsData;
platformsData: IPluginPlatformsData;
pluginPlatformsFolderPath(platform: string): string;
}

interface INodeModuleData {
Expand Down
8 changes: 8 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ interface IPlatformProjectService {
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
updatePlatform(currentVersion: string, newVersion: string): IFuture<void>;
preparePluginNativeCode(pluginData: IPluginData): IFuture<void>;
removePluginNativeCode(pluginData: IPluginData): IFuture<void>;
}

interface IAndroidProjectPropertiesManager {
getProjectReferences(): IFuture<ILibRef[]>;
addProjectReference(referencePath: string): IFuture<void>;
removeProjectReference(referencePath: string): IFuture<void>;
}
118 changes: 118 additions & 0 deletions lib/services/android-project-properties-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}.`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding else with logger.error("Could not find.....") as it will be easier for debugging

}).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>()();
}
}
Loading