<commit_before>package org.openmrs.maven.plugins;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.openmrs.maven.plugins.bintray.BintrayId;
import org.openmrs.maven.plugins.bintray.BintrayPackage;
import org.openmrs.maven.plugins.bintray.OpenmrsBintray;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.Project;
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
 * @goal deploy
 * @requiresProject false
 */
public class Deploy extends AbstractTask {

    private static final String DEFAULT_OK_MESSAGE = "Module '%s' installed successfully";
    private static final String DEFAULT_UPDATE_MESSAGE = "Module '%s' was updated to version '%s'";
    private static final String TEMPLATE_UPDATE = "Module is installed already. Do you want to upgrade it to version '%s'?";
    private static final String TEMPLATE_DOWNGRADE = "Installed version '%s' of module higher than target '%s'";
    private static final String TEMPLATE_CURRENT_VERSION = "The server currently has the OpenMRS %s in version %s installed.";

    private static final String DEPLOY_MODULE_OPTION = "Module";
    private static final String DEPLOY_OWA_OPTION = "Open Web App";
    private static final String DEPLOY_DISTRO_OPTION = "Distribution";
    private static final String DEPLOY_PLATFORM_OPTION = "Platform";

    /**
     * @parameter expression="${serverId}"
     */
    private String serverId;

    /**
     * @parameter expression="${artifactId}"
     */
    private String artifactId;

    /**
     * @parameter expression="${groupId}"
     */
    private String groupId;

    /**
     * @parameter expression="${version}"
     */
    private String version;
    /**
     * @parameter expression="${distro}"
     */
    private String distro;

    /**
     * @parameter expression="${platform}"
     */
    private String platform;

    /**
     * @parameter expression="${owa}"
     */
    private String owa;

    public Deploy() {}

    public Deploy(AbstractTask other) {super(other);}

    public Deploy(MavenProject project, MavenSession session, BuildPluginManager manager) {
        super.mavenProject = project;
        super.mavenSession = session;
        super.pluginManager = manager;
    }

    public void executeTask() throws MojoExecutionException, MojoFailureException {
        if (serverId == null) {
            File currentProperties = wizard.getCurrentServerPath();
            if (currentProperties != null) serverId = currentProperties.getName();
        }
        serverId = wizard.promptForExistingServerIdIfMissing(serverId);
        Server server = Server.loadServer(serverId);
        /**
         * workflow:
         * -if user specified both distro and platform, ignore them and enter interactive mode
         * -if user specified distro or platform, always upgrade distro/platform
         * -if user don't specify any:
         * -- if in module project dir/ core dir, install it
         * -- if in directory containing distro-properties file, use it to upgrade
         * -- if those conditions are not met or user doesn't agree, start interactive mode
         */
        ServerUpgrader serverUpgrader = new ServerUpgrader(this);

        if((platform == null && distro == null && owa == null)){
            Artifact artifact = checkCurrentDirectoryForOpenmrsWebappUpdate(server);
            DistroProperties distroProperties = checkCurrentDirectoryForDistroProperties(server);
            if(artifact != null){
                deployOpenmrsWar(server, artifact);
            } else if(distroProperties!=null){
                serverUpgrader.upgradeToDistro(server, distroProperties);
            } else if(checkCurrentDirForModuleProject()) {
                deployModule(groupId, artifactId, version, server);
            } else {
                runInteractiveMode(server, serverUpgrader);
            }
        } else if(distro != null) {
            DistroProperties distroProperties = distroHelper.retrieveDistroProperties(distro);
            serverUpgrader.upgradeToDistro(server, distroProperties);
        } else if(platform != null) {
            serverUpgrader.upgradePlatform(server, platform);
        } else if(owa != null){
            BintrayId id = OpenmrsBintray.parseOwa(owa);
            deployOwa(server, id.getName(), id.getVersion());
        } else throw new MojoExecutionException("Invalid installation option");
    }

    private void runInteractiveMode(Server server, ServerUpgrader upgrader) throws MojoExecutionException, MojoFailureException {
        DistroProperties distroProperties;
        List<String> options = new ArrayList<>(Arrays.asList(
                DEPLOY_MODULE_OPTION,
                DEPLOY_OWA_OPTION,
                DEPLOY_DISTRO_OPTION,
                DEPLOY_PLATFORM_OPTION
                ));<commit_msg>SDK-86 followup: fix deploying module with flags<commit_after>package org.openmrs.maven.plugins;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.openmrs.maven.plugins.bintray.BintrayId;
import org.openmrs.maven.plugins.bintray.BintrayPackage;
import org.openmrs.maven.plugins.bintray.OpenmrsBintray;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.Project;
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
 * @goal deploy
 * @requiresProject false
 */
public class Deploy extends AbstractTask {

    private static final String DEFAULT_OK_MESSAGE = "Module '%s' installed successfully";
    private static final String DEFAULT_UPDATE_MESSAGE = "Module '%s' was updated to version '%s'";
    private static final String TEMPLATE_UPDATE = "Module is installed already. Do you want to upgrade it to version '%s'?";
    private static final String TEMPLATE_DOWNGRADE = "Installed version '%s' of module higher than target '%s'";
    private static final String TEMPLATE_CURRENT_VERSION = "The server currently has the OpenMRS %s in version %s installed.";

    private static final String DEPLOY_MODULE_OPTION = "Module";
    private static final String DEPLOY_OWA_OPTION = "Open Web App";
    private static final String DEPLOY_DISTRO_OPTION = "Distribution";
    private static final String DEPLOY_PLATFORM_OPTION = "Platform";

    /**
     * @parameter expression="${serverId}"
     */
    private String serverId;

    /**
     * @parameter expression="${artifactId}"
     */
    private String artifactId;

    /**
     * @parameter expression="${groupId}"
     */
    private String groupId;

    /**
     * @parameter expression="${version}"
     */
    private String version;
    /**
     * @parameter expression="${distro}"
     */
    private String distro;

    /**
     * @parameter expression="${platform}"
     */
    private String platform;

    /**
     * @parameter expression="${owa}"
     */
    private String owa;

    public Deploy() {}

    public Deploy(AbstractTask other) {super(other);}

    public Deploy(MavenProject project, MavenSession session, BuildPluginManager manager) {
        super.mavenProject = project;
        super.mavenSession = session;
        super.pluginManager = manager;
    }

    public void executeTask() throws MojoExecutionException, MojoFailureException {
        if (serverId == null) {
            File currentProperties = wizard.getCurrentServerPath();
            if (currentProperties != null) serverId = currentProperties.getName();
        }
        serverId = wizard.promptForExistingServerIdIfMissing(serverId);
        Server server = Server.loadServer(serverId);
        /**
         * workflow:
         * -if user specified both distro and platform, ignore them and enter interactive mode
         * -if user specified distro or platform, always upgrade distro/platform
         * -if user don't specify any:
         * -- if in module project dir/ core dir, install it
         * -- if in directory containing distro-properties file, use it to upgrade
         * -- if those conditions are not met or user doesn't agree, start interactive mode
         */
        ServerUpgrader serverUpgrader = new ServerUpgrader(this);

        if((platform == null && distro == null && owa == null) && artifactId == null){
            Artifact artifact = checkCurrentDirectoryForOpenmrsWebappUpdate(server);
            DistroProperties distroProperties = checkCurrentDirectoryForDistroProperties(server);
            if(artifact != null){
                deployOpenmrsWar(server, artifact);
            } else if(distroProperties!=null){
                serverUpgrader.upgradeToDistro(server, distroProperties);
            } else if(checkCurrentDirForModuleProject()) {
                deployModule(groupId, artifactId, version, server);
            } else {
                runInteractiveMode(server, serverUpgrader);
            }
        } else if(distro != null) {
            DistroProperties distroProperties = distroHelper.retrieveDistroProperties(distro);
            serverUpgrader.upgradeToDistro(server, distroProperties);
        } else if(platform != null) {
            serverUpgrader.upgradePlatform(server, platform);
        } else if(owa != null){
            BintrayId id = OpenmrsBintray.parseOwa(owa);
            deployOwa(server, id.getName(), id.getVersion());
        } else if(artifactId != null){
            deployModule(serverId, groupId, artifactId, version);
        } else throw new MojoExecutionException("Invalid installation option");
    }

    private void runInteractiveMode(Server server, ServerUpgrader upgrader) throws MojoExecutionException, MojoFailureException {
        DistroProperties distroProperties;
        List<String> options = new ArrayList<>(Arrays.asList(
                DEPLOY_MODULE_OPTION,
                DEPLOY_OWA_OPTION,
                DEPLOY_DISTRO_OPTION,
                DEPLOY_PLATFORM_OPTION
                ));
{'fraction_non_alphanumeric': 0.0951435277125835, 'fraction_numerical': 0.00018053800324968405, 'mean_word_length': 3.144781144781145, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7589401', 'n_tokens_mistral': 3070, 'n_tokens_neox': 2926, 'n_words': 745}