"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extraEnv = void 0;
exports.gradleWrapperFileName = gradleWrapperFileName;
exports.prepareGradleCommand = prepareGradleCommand;
exports.getJavaConstraint = getJavaConstraint;
exports.extractGradleVersion = extractGradleVersion;
const tslib_1 = require("tslib");
const node_os_1 = tslib_1.__importDefault(require("node:os"));
const global_1 = require("../../../config/global");
const logger_1 = require("../../../logger");
const fs_1 = require("../../../util/fs");
const regex_1 = require("../../../util/regex");
const gradle_1 = tslib_1.__importDefault(require("../../versioning/gradle"));
exports.extraEnv = {
    GRADLE_OPTS: '-Dorg.gradle.parallel=true -Dorg.gradle.configureondemand=true -Dorg.gradle.daemon=false -Dorg.gradle.caching=false',
};
function gradleWrapperFileName() {
    if (node_os_1.default.platform() === 'win32' &&
        global_1.GlobalConfig.get('binarySource') !== 'docker') {
        return 'gradlew.bat';
    }
    return './gradlew';
}
async function prepareGradleCommand(gradlewFile) {
    const gradlewStat = await (0, fs_1.statLocalFile)(gradlewFile);
    if (gradlewStat?.isFile() === true) {
        // if the file is not executable by others
        if (node_os_1.default.platform() !== 'win32' && (gradlewStat.mode & 0o1) === 0) {
            logger_1.logger.debug('Gradle wrapper is missing the executable bit');
            // add the execution permission to the owner, group and others
            await (0, fs_1.chmodLocalFile)(gradlewFile, gradlewStat.mode | 0o111);
        }
        return gradleWrapperFileName();
    }
    return null;
}
/**
 * Find compatible java version for gradle.
 * see https://docs.gradle.org/current/userguide/compatibility.html
 * @param gradleVersion current gradle version
 * @returns A Java semver range
 */
function getJavaConstraint(gradleVersion) {
    const major = gradleVersion ? gradle_1.default.getMajor(gradleVersion) : null;
    const minor = gradleVersion ? gradle_1.default.getMinor(gradleVersion) : null;
    if (major && (major > 7 || (major >= 7 && minor && minor >= 3))) {
        return '^17.0.0';
    }
    if (major && major >= 7) {
        return '^16.0.0';
    }
    // first public gradle version was 2.0
    if (major && major > 0 && major < 5) {
        return '^8.0.0';
    }
    return '^11.0.0';
}
// https://regex101.com/r/IcOs7P/1
const DISTRIBUTION_URL_REGEX = (0, regex_1.regEx)('^(?:distributionUrl\\s*=\\s*)(?<url>\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip)\\s*$');
function extractGradleVersion(fileContent) {
    const lines = fileContent?.split(regex_1.newlineRegex) ?? [];
    for (const line of lines) {
        const distributionUrlMatch = DISTRIBUTION_URL_REGEX.exec(line);
        if (distributionUrlMatch?.groups) {
            return {
                url: distributionUrlMatch.groups.url,
                version: distributionUrlMatch.groups.version,
            };
        }
    }
    logger_1.logger.debug('Gradle wrapper version and url could not be extracted from properties - skipping update');
    return null;
}
//# sourceMappingURL=utils.js.map