"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildPluginCommand = void 0;
const os_1 = require("os");
const path = require("path");
const constants = require("../../constants");
const yok_1 = require("../../common/yok");
class BuildPluginCommand {
    constructor($androidPluginBuildService, $errors, $logger, $fs, $options, $tempService) {
        this.$androidPluginBuildService = $androidPluginBuildService;
        this.$errors = $errors;
        this.$logger = $logger;
        this.$fs = $fs;
        this.$options = $options;
        this.$tempService = $tempService;
        this.allowedParameters = [];
        this.pluginProjectPath = path.resolve(this.$options.path || ".");
    }
    async execute(args) {
        const platformsAndroidPath = path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android");
        let pluginName = "";
        const pluginPackageJsonPath = path.join(this.pluginProjectPath, constants.PACKAGE_JSON_FILE_NAME);
        if (this.$fs.exists(pluginPackageJsonPath)) {
            const packageJsonContents = this.$fs.readJson(pluginPackageJsonPath);
            if (packageJsonContents && packageJsonContents["name"]) {
                pluginName = packageJsonContents["name"];
            }
        }
        const tempAndroidProject = await this.$tempService.mkdirSync("android-project");
        const options = {
            gradlePath: this.$options.gradlePath,
            gradleArgs: this.$options.gradleArgs,
            aarOutputDir: platformsAndroidPath,
            platformsAndroidDirPath: platformsAndroidPath,
            pluginName: pluginName,
            tempPluginDirPath: tempAndroidProject,
        };
        const androidPluginBuildResult = await this.$androidPluginBuildService.buildAar(options);
        if (androidPluginBuildResult) {
            this.$logger.info(`${pluginName} successfully built aar at ${platformsAndroidPath}.${os_1.EOL}Temporary Android project can be found at ${tempAndroidProject}.`);
        }
        const migratedIncludeGradle = this.$androidPluginBuildService.migrateIncludeGradle(options);
        if (migratedIncludeGradle) {
            this.$logger.info(`${pluginName} include gradle updated.`);
        }
    }
    async canExecute(args) {
        if (!this.$fs.exists(path.join(this.pluginProjectPath, constants.PLATFORMS_DIR_NAME, "android"))) {
            this.$errors.fail("No plugin found at the current directory, or the plugin does not need to have its platforms/android components built into an `.aar`.");
        }
        return true;
    }
}
exports.BuildPluginCommand = BuildPluginCommand;
yok_1.injector.registerCommand("plugin|build", BuildPluginCommand);
