Skip to content

fix: Build plugins when path to project has space #3562

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
May 8, 2018
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
22 changes: 22 additions & 0 deletions lib/definitions/android-plugin-migrator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,25 @@ interface IAndroidPluginBuildService {
buildAar(options: IBuildOptions): Promise<boolean>;
migrateIncludeGradle(options: IBuildOptions): boolean;
}

/**
* Describes data required for building plugin for Android.
* The data can be consumed in the buildAndroidPlugin hook.
*/
interface IBuildAndroidPluginData {
/**
* Directory where the plugin will be build.
* Usually this is the `<project dir>/platforms/tempPlugin/<plugin name>` dir.
*/
pluginDir: string;

/**
* The name of the plugin.
*/
pluginName: string;

/**
* Information about tools that will be used to build the plugin, for example compile SDK version, build tools version, etc.
*/
androidToolsInfo: IAndroidToolsInfoData;
}
55 changes: 31 additions & 24 deletions lib/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import * as path from "path";
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR } from "../constants";
import { getShortPluginName } from "../common/helpers";
import { getShortPluginName, hook } from "../common/helpers";
import { Builder, parseString } from "xml2js";
import { ILogger } from "log4js";

export class AndroidPluginBuildService implements IAndroidPluginBuildService {

constructor(private $fs: IFileSystem,
/**
* Required for hooks execution to work.
*/
private get $hooksService(): IHooksService {
return this.$injector.resolve("hooksService");
}

constructor(private $injector: IInjector,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $androidToolsInfo: IAndroidToolsInfo,
Expand Down Expand Up @@ -240,30 +248,9 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
}

// finally build the plugin
const gradlew = this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew";
const localArgs = [
gradlew,
"-p",
newPluginDir,
"assembleRelease"
];

this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true });

const androidToolsInfo = this.$androidToolsInfo.getToolsInfo();
const compileSdk = androidToolsInfo.compileSdkVersion;
const buildToolsVersion = androidToolsInfo.buildToolsVersion;
const supportVersion = androidToolsInfo.supportRepositoryVersion;

localArgs.push(`-PcompileSdk=android-${compileSdk}`);
localArgs.push(`-PbuildToolsVersion=${buildToolsVersion}`);
localArgs.push(`-PsupportVersion=${supportVersion}`);

try {
await this.$childProcess.exec(localArgs.join(" "), { cwd: newPluginDir });
} catch (err) {
throw new Error(`Failed to build plugin ${options.pluginName} : \n${err}`);
}
await this.buildPlugin( { pluginDir: newPluginDir, pluginName: options.pluginName, androidToolsInfo });

const finalAarName = `${shortPluginName}-release.aar`;
const pathToBuiltAar = path.join(newPluginDir, "build", "outputs", "aar", finalAarName);
Expand Down Expand Up @@ -318,6 +305,26 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
return false;
}

@hook("buildAndroidPlugin")
private async buildPlugin(pluginBuildSettings: IBuildAndroidPluginData): Promise<void> {
const gradlew = this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew";

const localArgs = [
"-p",
pluginBuildSettings.pluginDir,
"assembleRelease",
`-PcompileSdk=android-${pluginBuildSettings.androidToolsInfo.compileSdkVersion}`,
`-PbuildToolsVersion=${pluginBuildSettings.androidToolsInfo.buildToolsVersion}`,
`-PsupportVersion=${pluginBuildSettings.androidToolsInfo.supportRepositoryVersion}`
];

try {
await this.$childProcess.spawnFromEvent(gradlew, localArgs, "close", { cwd: pluginBuildSettings.pluginDir });
} catch (err) {
throw new Error(`Failed to build plugin ${pluginBuildSettings.pluginName} : \n${err}`);
}
}

private validateOptions(options: IBuildOptions): void {
if (!options) {
throw new Error("Android plugin cannot be built without passing an 'options' object.");
Expand Down
21 changes: 13 additions & 8 deletions test/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ temp.track();

describe('androiPluginBuildService', () => {

let execCalled = false;
let spawnFromEventCalled = false;
const createTestInjector = (): IInjector => {
const testInjector = new Yok();

testInjector.register("fs", FsLib.FileSystem);
testInjector.register("childProcess", {
exec: () => {
execCalled = true;
spawnFromEvent: async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise<ISpawnResult> => {
spawnFromEventCalled = command.indexOf("gradlew") !== -1;
return null;
}
});
testInjector.register("hostInfo", HostInfo);
Expand All @@ -36,6 +37,10 @@ describe('androiPluginBuildService', () => {
testInjector.register("options", {});
testInjector.register("config", {});
testInjector.register("staticConfig", {});
testInjector.register("hooksService", {
executeBeforeHooks: async (commandName: string, hookArguments?: IDictionary<any>): Promise<void> => undefined,
executeAfterHooks: async (commandName: string, hookArguments?: IDictionary<any>): Promise<void> => undefined
});

return testInjector;
};
Expand Down Expand Up @@ -107,7 +112,7 @@ dependencies {
});

beforeEach(() => {
execCalled = false;
spawnFromEventCalled = false;
});

describe('builds aar', () => {
Expand All @@ -127,7 +132,7 @@ dependencies {
/* intentionally left blank */
}

assert.isTrue(execCalled);
assert.isTrue(spawnFromEventCalled);
});

it('if android manifest is missing', async () => {
Expand All @@ -145,7 +150,7 @@ dependencies {
/* intentionally left blank */
}

assert.isTrue(execCalled);
assert.isTrue(spawnFromEventCalled);
});

it('if there is only an android manifest file', async () => {
Expand All @@ -163,7 +168,7 @@ dependencies {
/* intentionally left blank */
}

assert.isTrue(execCalled);
assert.isTrue(spawnFromEventCalled);
});
});

Expand All @@ -183,7 +188,7 @@ dependencies {
/* intentionally left blank */
}

assert.isFalse(execCalled);
assert.isFalse(spawnFromEventCalled);
});
});

Expand Down