Skip to content

Commit 34323a3

Browse files
petekanevPlamen5kov
authored andcommitted
feat(command): introduce tns plugin build command which builds android aars
docs(plugin build command): add docs for the new command
1 parent 180fe32 commit 34323a3

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<% if (isJekyll) { %>---
2+
title: tns plugin build
3+
position: 8
4+
---<% } %>
5+
# tns plugin build
6+
==========
7+
8+
Usage | Synopsis
9+
------|-------
10+
General | `$ tns plugin build`
11+
12+
<% if(isConsole) { %>Attempts to build the plugin's Android-specific project files located in `platforms/android`. Strips and removes `include.gradle` flavor declarations, if any are present. <% } %>
13+
<% if(isHtml) { %>Attempts to build the plugin's Android-specific project files located in `platforms/android`. The resulting Android Library (aar), and the generated Android Gradle project used to build the library can be found at `platforms/android`. Also strips and removes `include.gradle` flavor declarations, if any are present. For more information about working with plugins, see [NativeScript Plugins](https://github.com/NativeScript/nativescript-cli/blob/master/PLUGINS.md).<% } %>
14+
15+
16+
<% if(isHtml) { %>
17+
### Prerequisites
18+
19+
* Verify that the command is being executed in the source directory of a plugin - e.g. `nativescript-barcodescanner/src`
20+
21+
### Related Commands
22+
23+
Command | Description
24+
----------|----------
25+
[plugin](plugin.html) | Lets you manage the plugins for your project.
26+
[plugin install](plugin-install.html) | Installs the specified plugin and its dependencies.
27+
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
28+
<% } %>

docs/man_pages/lib-management/plugin.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Lets you manage the plugins for your project.
1919
* `update` - Uninstalls and installs the specified plugin(s) and its dependencies.
2020
* `find` - Finds NativeScript plugins in npm.
2121
* `search` - Finds NativeScript plugins in npm.
22+
* `build` - Builds the Android parts of a NativeScript plugin.
2223

2324
<% if(isHtml) { %>
2425
### Related Commands
@@ -28,4 +29,5 @@ Command | Description
2829
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
2930
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
3031
[plugin update](plugin-update.html) | Updates the specified plugin(s) and its dependencies.
32+
[plugin build](plugin-build.html) | Builds the Android project of a NativeScript plugin, and updates the `include.gradle`.
3133
<% } %>

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ $injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
9191
$injector.requireCommand("plugin|install", "./commands/plugin/add-plugin");
9292
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
9393
$injector.requireCommand("plugin|update", "./commands/plugin/update-plugin");
94+
$injector.requireCommand("plugin|build", "./commands/plugin/build-plugin");
9495

9596
$injector.require("doctorService", "./services/doctor-service");
9697
$injector.require("xcprojService", "./services/xcproj-service");

lib/commands/plugin/build-plugin.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { EOL } from "os";
2+
import * as path from "path";
3+
import * as constants from "../../constants";
4+
export class BuildPluginCommand implements ICommand {
5+
public allowedParameters: ICommandParameter[] = [];
6+
public pluginProjectPath: string;
7+
8+
constructor(private $androidPluginBuildService: IAndroidPluginBuildService,
9+
private $errors: IErrors,
10+
private $logger: ILogger,
11+
private $fs: IFileSystem,
12+
private $options: IOptions) {
13+
14+
if (this.$options.path) {
15+
this.pluginProjectPath = path.resolve(this.$options.path);
16+
} else {
17+
this.pluginProjectPath = path.resolve(".");
18+
}
19+
}
20+
21+
public async execute(args: string[]): Promise<void> {
22+
const platformsAndroidPath = path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android");
23+
let pluginName = "";
24+
25+
const pluginPackageJsonPath = path.join(this.pluginProjectPath, constants.PACKAGE_JSON_FILE_NAME);
26+
27+
if (this.$fs.exists(pluginPackageJsonPath)) {
28+
const packageJsonContents = this.$fs.readJson(pluginPackageJsonPath);
29+
30+
if (packageJsonContents && packageJsonContents["name"]) {
31+
pluginName = packageJsonContents["name"];
32+
}
33+
}
34+
35+
const tempAndroidProject = path.join(platformsAndroidPath, "android-project");
36+
37+
const options: IBuildOptions = {
38+
aarOutputDir: platformsAndroidPath,
39+
platformsAndroidDirPath: platformsAndroidPath,
40+
pluginName: pluginName,
41+
tempPluginDirPath: tempAndroidProject
42+
};
43+
44+
const androidPluginBuildResult = await this.$androidPluginBuildService.buildAar(options);
45+
46+
if (androidPluginBuildResult) {
47+
this.$logger.info(`${pluginName} successfully built aar at ${platformsAndroidPath}.${EOL}Temporary Android project can be found at ${tempAndroidProject}.`);
48+
}
49+
50+
const migratedIncludeGradle = this.$androidPluginBuildService.migrateIncludeGradle(options);
51+
52+
if (migratedIncludeGradle) {
53+
this.$logger.info(`${pluginName} include gradle updated.`);
54+
}
55+
}
56+
57+
public async canExecute(args: string[]): Promise<boolean> {
58+
if (!this.$fs.exists(path.join(this.pluginProjectPath, "platforms", "android"))) {
59+
this.$errors.failWithoutHelp("No plugin found at the current directory, or the plugin does not need to have its platforms/android components built into an `.aar`.");
60+
}
61+
62+
return true;
63+
}
64+
}
65+
66+
$injector.registerCommand("plugin|build", BuildPluginCommand);

lib/definitions/android-plugin-migrator.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface IAndroidBuildOptions {
1111

1212
interface IAndroidPluginBuildService {
1313
buildAar(options: IBuildOptions): Promise<boolean>;
14-
migrateIncludeGradle(options: IBuildOptions): void;
14+
migrateIncludeGradle(options: IBuildOptions): boolean;
1515
}

lib/services/android-plugin-build-service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
291291
* @param {Object} options
292292
* @param {string} options.platformsAndroidDirPath - The path to the 'plugin/src/platforms/android' directory.
293293
*/
294-
public migrateIncludeGradle(options: IBuildOptions): void {
294+
public migrateIncludeGradle(options: IBuildOptions): boolean {
295295
this.validatePlatformsAndroidDirPathOption(options);
296296

297297
const includeGradleFilePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
@@ -310,10 +310,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
310310
const newIncludeGradleFileContent = includeGradleFileContent.replace(productFlavorsScope, "");
311311
this.$fs.writeFile(includeGradleFilePath, newIncludeGradleFileContent);
312312

313+
return true;
313314
} catch (e) {
314315
throw new Error(`Failed to write the updated include.gradle in - ${includeGradleFilePath}`);
315316
}
316317
}
318+
319+
return false;
317320
}
318321

319322
private validateOptions(options: IBuildOptions): void {

test/stubs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ export class AndroidPluginBuildServiceStub implements IAndroidPluginBuildService
291291
buildAar(options: IBuildOptions): Promise<boolean> {
292292
return Promise.resolve(true);
293293
}
294-
migrateIncludeGradle(options: IBuildOptions): void {
295-
294+
migrateIncludeGradle(options: IBuildOptions): boolean {
295+
return true;
296296
}
297297
}
298298

0 commit comments

Comments
 (0)