"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GradleCommandService = void 0;
const yok_1 = require("../../common/yok");
class GradleCommandService {
    constructor($childProcess, $errors, $hostInfo, $logger) {
        this.$childProcess = $childProcess;
        this.$errors = $errors;
        this.$hostInfo = $hostInfo;
        this.$logger = $logger;
    }
    executeCommand(gradleArgs, options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const { message, cwd, stdio, spawnOptions } = options;
            this.$logger.info(message);
            const childProcessOptions = {
                cwd,
                stdio: stdio || "inherit",
                shell: this.$hostInfo.isWindows,
            };
            const gradleExecutable = (_a = options.gradlePath) !== null && _a !== void 0 ? _a : (this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew");
            const result = yield this.executeCommandSafe(gradleExecutable, gradleArgs, childProcessOptions, spawnOptions);
            return result;
        });
    }
    executeCommandSafe(gradleExecutable, gradleArgs, childProcessOptions, spawnOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const result = yield this.$childProcess.spawnFromEvent(gradleExecutable, gradleArgs, "close", childProcessOptions, spawnOptions);
                return result;
            }
            catch (err) {
                this.$errors.fail(err.message);
            }
        });
    }
}
exports.GradleCommandService = GradleCommandService;
yok_1.injector.register("gradleCommandService", GradleCommandService);
//# sourceMappingURL=gradle-command-service.js.map