|
| 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 | + this.pluginProjectPath = path.resolve(this.$options.path || "."); |
| 15 | + } |
| 16 | + |
| 17 | + public async execute(args: string[]): Promise<void> { |
| 18 | + const platformsAndroidPath = path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android"); |
| 19 | + let pluginName = ""; |
| 20 | + |
| 21 | + const pluginPackageJsonPath = path.join(this.pluginProjectPath, constants.PACKAGE_JSON_FILE_NAME); |
| 22 | + |
| 23 | + if (this.$fs.exists(pluginPackageJsonPath)) { |
| 24 | + const packageJsonContents = this.$fs.readJson(pluginPackageJsonPath); |
| 25 | + |
| 26 | + if (packageJsonContents && packageJsonContents["name"]) { |
| 27 | + pluginName = packageJsonContents["name"]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const tempAndroidProject = path.join(platformsAndroidPath, "android-project"); |
| 32 | + |
| 33 | + const options: IBuildOptions = { |
| 34 | + aarOutputDir: platformsAndroidPath, |
| 35 | + platformsAndroidDirPath: platformsAndroidPath, |
| 36 | + pluginName: pluginName, |
| 37 | + tempPluginDirPath: tempAndroidProject |
| 38 | + }; |
| 39 | + |
| 40 | + const androidPluginBuildResult = await this.$androidPluginBuildService.buildAar(options); |
| 41 | + |
| 42 | + if (androidPluginBuildResult) { |
| 43 | + this.$logger.info(`${pluginName} successfully built aar at ${platformsAndroidPath}.${EOL}Temporary Android project can be found at ${tempAndroidProject}.`); |
| 44 | + } |
| 45 | + |
| 46 | + const migratedIncludeGradle = this.$androidPluginBuildService.migrateIncludeGradle(options); |
| 47 | + |
| 48 | + if (migratedIncludeGradle) { |
| 49 | + this.$logger.info(`${pluginName} include gradle updated.`); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public async canExecute(args: string[]): Promise<boolean> { |
| 54 | + if (!this.$fs.exists(path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android"))) { |
| 55 | + 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`."); |
| 56 | + } |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +$injector.registerCommand("plugin|build", BuildPluginCommand); |
0 commit comments