"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MavenDefaultOptions = exports.MvnPackage = exports.MvnVersion = exports.mvnVersionProjectListener = exports.mavenIncrementPatchVersionCommand = exports.MavenVersionPreparation = exports.MavenProjectVersioner = exports.MavenOptions = void 0;
const df = require("dateformat");
const logInterpreters_1 = require("../../../../api-helper/log/logInterpreters");
const child_process_1 = require("../../../../api-helper/misc/child_process");
const GoalInvocation_1 = require("../../../../api/goal/GoalInvocation");
const projectVersioner_1 = require("../../../../core/delivery/build/local/projectVersioner");
const mavenCommand_1 = require("../mavenCommand");
const MavenProgressReporter_1 = require("../MavenProgressReporter");
const pomParser_1 = require("../parse/pomParser");
const pushTests_1 = require("../pushTests");
exports.MavenOptions = [
    "-B",
    "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
];
async function newVersion(sdmGoal, p) {
    const pi = await pomParser_1.MavenProjectIdentifier(p);
    const branch = sdmGoal.branch.split("/").join(".");
    return `${pi.version}-${branch}.${df(new Date(), "yyyymmddHHMMss")}`;
}
/**
 * ProjectVersioner to be used with Maven projects
 * @param sdmGoal
 * @param p
 * @param log
 * @constructor
 */
exports.MavenProjectVersioner = async (sdmGoal, p, log) => {
    const version = await newVersion(sdmGoal, p);
    await changeMavenVersion(version, p, log);
    return version;
};
/**
 * PrepareForGoalExecution for updating the Maven version in project
 * @param p
 * @param goalInvocation
 * @constructor
 */
exports.MavenVersionPreparation = async (p, goalInvocation) => {
    const version = await newVersion(goalInvocation.goalEvent, p);
    return changeMavenVersion(version, p, goalInvocation.progressLog);
};
async function changeMavenVersion(version, p, progressLog) {
    const command = await mavenCommand_1.determineMavenCommand(p);
    const args = [
        "build-helper:parse-version",
        "versions:set",
        `-DnewVersion=${version}`,
        "versions:commit",
        ...exports.MavenOptions,
    ];
    return child_process_1.spawnLog(command, args, { cwd: p.baseDir, log: progressLog });
}
async function mavenIncrementPatchVersionCommand(p, progressLog) {
    const command = await mavenCommand_1.determineMavenCommand(p);
    const args = [
        "build-helper:parse-version",
        "versions:set",
        // tslint:disable-next-line:no-invalid-template-strings
        "${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-${parsedVersion.qualifier}",
        "versions:commit",
        ...exports.MavenOptions,
    ];
    return child_process_1.spawnLog(command, args, { cwd: p.baseDir, log: progressLog });
}
exports.mavenIncrementPatchVersionCommand = mavenIncrementPatchVersionCommand;
async function mvnVersionProjectListener(p, gi, event) {
    const command = await mavenCommand_1.determineMavenCommand(p);
    if (event === GoalInvocation_1.GoalProjectListenerEvent.before) {
        const v = await projectVersioner_1.readSdmVersion(gi.goalEvent.repo.owner, gi.goalEvent.repo.name, gi.goalEvent.repo.providerId, gi.goalEvent.sha, gi.goalEvent.branch, gi.context);
        return child_process_1.spawnLog(command, ["versions:set", `-DnewVersion=${v}`, "versions:commit", ...exports.MavenOptions], {
            cwd: p.baseDir,
            log: gi.progressLog,
        });
    }
}
exports.mvnVersionProjectListener = mvnVersionProjectListener;
exports.MvnVersion = {
    name: "mvn-version",
    listener: mvnVersionProjectListener,
    pushTest: pushTests_1.IsMaven,
};
async function mvnPackageProjectListener(p, gi, event) {
    const command = await mavenCommand_1.determineMavenCommand(p);
    if (event === GoalInvocation_1.GoalProjectListenerEvent.before) {
        return child_process_1.spawnLog(command, ["package", ...exports.MavenOptions, "-DskipTests=true", `-Dartifact.name=${p.id.repo}`], {
            cwd: p.baseDir,
            log: gi.progressLog,
        });
    }
}
exports.MvnPackage = {
    name: "mvn-package",
    listener: mvnPackageProjectListener,
    pushTest: pushTests_1.IsMaven,
};
exports.MavenDefaultOptions = {
    pushTest: pushTests_1.IsMaven,
    logInterpreter: logInterpreters_1.LogSuppressor,
    progressReporter: MavenProgressReporter_1.MavenProgressReporter,
};
//# sourceMappingURL=helpers.js.map