From a7526783632dbf74df213ce9fd234ea95e91bf5a Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 06:39:59 -0500 Subject: [PATCH 1/5] SDK-343 - Setup task presents both the release and latest snapshot of the same version as options --- .../maven/plugins/utility/VersionsHelper.java | 53 +++++++------------ .../plugins/utility/VersionsHelperTest.java | 37 +++++++------ 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java index daedea1eb..85c9cfacb 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java @@ -19,7 +19,6 @@ */ public class VersionsHelper { - public static final String RELEASE_VERSION_REGEX = "[0-9.]+(-alpha)?(-beta)?"; public static final String NO_VERSION_AVAILABLE_MSG = "No version is available in remote repositories!"; /** @@ -143,8 +142,8 @@ public List getSuggestedVersions(Artifact artifact, int maxReleases){ return getSuggestedVersions(getVersions(artifact), maxReleases); } - public List getSuggestedVersions(List allVersions, int maxSize){ - if(allVersions.size() == 0){ + public List getSuggestedVersions(List allVersions, int maxSize) { + if(allVersions.isEmpty()){ return Collections.emptyList(); } sortDescending(allVersions); @@ -156,46 +155,34 @@ public List getSuggestedVersions(List allVersions, int advices.add(allVersions.get(0).toString()); ArtifactVersion previous = allVersions.get(0); //start from second element - for(ArtifactVersion version : allVersions.subList(1, allVersions.size())){ - if(isNotFeatureVersion(version)){ - if(equalMinorAndMajor(previous, version)) { - if(isSnapshot(previous) && !isSnapshot(version)){ - if(equalIncremental(previous, version)){ - advices.remove(advices.size()-1); - advices.add(version.toString()); - } else { - advices.add(version.toString()); - } - } else if(isSnapshot(version) && !equalIncremental(previous, version)){ - advices.add(version.toString()); - } - } else { + for (int i=1; i= maxSize) break; + } + if (advices.size() > maxSize) { + advices = advices.subList(0, maxSize); } return advices; } - private boolean equalIncremental(ArtifactVersion previous, ArtifactVersion version) { - return version.getIncrementalVersion() == previous.getIncrementalVersion(); - } - - private boolean isNotFeatureVersion(ArtifactVersion version) { - return version.toString().matches(RELEASE_VERSION_REGEX); + private boolean equalMinorAndMajor(ArtifactVersion left, ArtifactVersion right){ + return left.getMajorVersion() == right.getMajorVersion() && left.getMinorVersion() == right.getMinorVersion(); } private boolean isSnapshot(ArtifactVersion version){ - return version.getQualifier()!= null && version.getQualifier().contains("SNAPSHOT"); - } - private boolean equalMinorAndMajor(ArtifactVersion left, ArtifactVersion right){ - return left.getMajorVersion()==right.getMajorVersion() - && left.getMinorVersion() == right.getMinorVersion(); + return version.getQualifier() != null && version.getQualifier().contains("SNAPSHOT"); } } diff --git a/sdk-commons/src/test/java/org/openmrs/maven/plugins/utility/VersionsHelperTest.java b/sdk-commons/src/test/java/org/openmrs/maven/plugins/utility/VersionsHelperTest.java index 4ddce1601..67948fa39 100644 --- a/sdk-commons/src/test/java/org/openmrs/maven/plugins/utility/VersionsHelperTest.java +++ b/sdk-commons/src/test/java/org/openmrs/maven/plugins/utility/VersionsHelperTest.java @@ -18,63 +18,70 @@ public class VersionsHelperTest { VersionsHelper helper; @Before - public void before(){ + public void before() { helper = new VersionsHelper(null, null, null, null); } - private List createTestVersions(String... versions){ + private List createTestVersions(String... versions) { List list = new ArrayList<>(); - for(String version: versions) { + for (String version : versions) { list.add(new DefaultArtifactVersion(version)); } return list; } @Test - public void getLatestReleasedVersion_shouldNotIncludeSnapshots(){ + public void getLatestReleasedVersion_shouldNotIncludeSnapshots() { String res = helper.getLatestReleasedVersion( createTestVersions("1.8.8", "1.8.3", "1.8.10-SNAPSHOT", "1.8.9-alpha")); assertThat(res, is(equalTo("1.8.8"))); } + @Test - public void getLatestSnapshotVersion_shouldReturnLatestSnapshot(){ + public void getLatestSnapshotVersion_shouldReturnLatestSnapshot() { String res = helper.getLatestSnapshotVersion( createTestVersions("1.8.8", "1.8.3", "1.8.10-SNAPSHOT", "1.8.9-alpha")); assertThat(res, is(equalTo("1.8.10-SNAPSHOT"))); } + @Test - public void getSuggestedVersions_containSnapshots(){ + public void getSuggestedVersions_containSnapshots() { List res = helper.getSuggestedVersions( createTestVersions("1.8.8", "1.8.3", "1.8.10-SNAPSHOT", "1.8.9-alpha"), 3); assertThat(res, hasItem("1.8.10-SNAPSHOT")); } + @Test - public void getSuggestedVersions_containGivenNumberOfReleases(){ + public void getSuggestedVersions_containGivenNumberOfReleases() { List res = helper.getSuggestedVersions( createTestVersions("1.8.8", "1.8.3", "1.8.10-SNAPSHOT", "1.8.9-alpha"), 1); - assertThat(res, hasItem("1.8.9-alpha")); + assertThat(res, hasItem("1.8.10-SNAPSHOT")); assertThat(res, allOf(not(hasItem("1.8.3")), not(hasItem("1.8.9")))); } + @Test - public void getSuggestedVersions_shouldReturnSortedList(){ + public void getSuggestedVersions_shouldReturnSortedList() { List res = helper.getSuggestedVersions( - createTestVersions("1.1.2", "1.5.6", "1.5.7-SNAPSHOT" , "1.1.2-alpha", "1.1.0-SNAPSHOT", "1.3.0-SNAPSHOT", "1.3.0", "1.1.0"), 6); - assertThat(res, contains("1.5.7-SNAPSHOT", "1.5.6", "1.3.0", "1.3.0-SNAPSHOT", "1.1.2")); + createTestVersions("1.1.2", "1.5.6", "1.5.7-SNAPSHOT", "1.1.2-alpha", "1.1.0-SNAPSHOT", "1.3.0-SNAPSHOT", "1.3.0", "1.1.0"), 6); + assertThat(res, contains("1.5.7-SNAPSHOT", "1.5.6", "1.3.0", "1.1.2")); } + @Test - public void getSuggestedVersions_shouldContainOnlyLatestTwoSnapshots(){ + public void getSuggestedVersions_shouldContainOnlyLatestTwoSnapshots() { List res = helper.getSuggestedVersions( - createTestVersions("1.1.2", "1.5.6", "1.5.7-SNAPSHOT","1.4.5-SNAPSHOT", "1.1.0-SNAPSHOT", "1.3.0-SNAPSHOT"), 6); + createTestVersions("1.1.2", "1.5.6", "1.5.7-SNAPSHOT", "1.4.5-SNAPSHOT", "1.1.0-SNAPSHOT", "1.3.0-SNAPSHOT"), 6); assertThat(res, contains("1.5.7-SNAPSHOT", "1.5.6", "1.4.5-SNAPSHOT", "1.1.2")); } + @Test - public void getSuggestedVersions_shouldContainNoMoreThanRequestedQuantity(){ + public void getSuggestedVersions_shouldContainNoMoreThanRequestedQuantity() { List res = helper.getSuggestedVersions( createTestVersions("1.4.0", "1.5.0", "1.8.0"), 2); assertThat(res, contains("1.8.0", "1.5.0")); } + @Test - public void getSuggestedVersions_shouldContainOnlyOneReleaseFromMajorAndMinor(){ + public void getSuggestedVersions_shouldContainOnlyOneReleaseFromMajorAndMinor() { List res = helper.getSuggestedVersions( createTestVersions("1.5.2", "1.5.6", "1.5.7-SNAPSHOT"), 6); assertThat(res, contains("1.5.7-SNAPSHOT", "1.5.6")); From 9eeaa342752264adfd688b9fc55c1201e3a02baa Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 10:11:52 -0500 Subject: [PATCH 2/5] SDK-341 - Fix regression preventing setting up custom distribution --- .../openmrs/maven/plugins/BuildDistro.java | 12 ++---- .../openmrs/maven/plugins/GenerateDistro.java | 13 +++--- .../java/org/openmrs/maven/plugins/Setup.java | 40 +++++++++++-------- .../maven/plugins/utility/DefaultWizard.java | 4 +- .../maven/plugins/utility/SDKConstants.java | 4 ++ 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java index 8eb6a30bb..97dd2cb82 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java @@ -78,10 +78,6 @@ public class BuildDistro extends AbstractTask { private static final String DOCKER_COMPOSE_OVERRIDE_YML = "docker-compose.override.yml"; - private static final String O2_DISTRIBUTION = "2.x Distribution"; - - private static final String O3_DISTRIBUTION = "O3 Distribution"; - private static final Logger log = LoggerFactory.getLogger(BuildDistro.class); /** @@ -163,16 +159,16 @@ else if (StringUtils.isNotBlank(distro)) { Server server = new Server.ServerBuilder().build(); List options = new ArrayList<>(); - options.add(O2_DISTRIBUTION); - options.add(O3_DISTRIBUTION); + options.add(SDKConstants.REFAPP_2X_PROMPT); + options.add(SDKConstants.REFAPP_3X_PROMPT); String choice = wizard.promptForMissingValueWithOptions("You can setup following servers", null, null, options); switch (choice) { - case O2_DISTRIBUTION: + case SDKConstants.REFAPP_2X_PROMPT: wizard.promptForRefAppVersionIfMissing(server, versionsHelper, DISTRIBUTION_VERSION_PROMPT); distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); break; - case O3_DISTRIBUTION: + case SDKConstants.REFAPP_3X_PROMPT: wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java index bcb0f2b3b..4b7857f75 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java @@ -9,6 +9,7 @@ import org.openmrs.maven.plugins.model.DistroProperties; import org.openmrs.maven.plugins.model.Server; import org.openmrs.maven.plugins.utility.DistributionBuilder; +import org.openmrs.maven.plugins.utility.SDKConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,10 +32,6 @@ @Mojo(name = "generate-distro", requiresProject = false) public class GenerateDistro extends AbstractTask { - private static final String O2_DISTRIBUTION = "2.x Distribution"; - - private static final String O3_DISTRIBUTION = "O3 Distribution"; - private static final String GENERATE_DISTRO_PROMPT = "You can generate the distro.properties file for the following distributions"; private static final String GET_VERSION_PROMPT = "You can generate the distro.properties file for the following versions"; @@ -51,8 +48,8 @@ public class GenerateDistro extends AbstractTask { @Override public void executeTask() throws MojoExecutionException, MojoFailureException { List options = new ArrayList<>(); - options.add(O2_DISTRIBUTION); - options.add(O3_DISTRIBUTION); + options.add(SDKConstants.REFAPP_2X_PROMPT); + options.add(SDKConstants.REFAPP_3X_PROMPT); String choice = wizard.promptForMissingValueWithOptions(GENERATE_DISTRO_PROMPT, null, null, options); DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment()); @@ -68,12 +65,12 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { File outputFile = outputLocation != null ? new File(outputLocation) : new File(System.getProperty("user.dir")); switch (choice) { - case O2_DISTRIBUTION: + case SDKConstants.REFAPP_2X_PROMPT: wizard.promptForRefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT); distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); break; - case O3_DISTRIBUTION: + case SDKConstants.REFAPP_3X_PROMPT: wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT); distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); break; diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index d473e0d11..1e2ebc5c2 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -42,9 +42,11 @@ import static org.openmrs.maven.plugins.utility.SDKConstants.PLATFORM_ARTIFACT_ID; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE; import static org.openmrs.maven.plugins.utility.SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION; @@ -57,18 +59,14 @@ public class Setup extends AbstractServerTask { public static final String SETTING_UP_A_NEW_SERVER = "Setting up a new server..."; - public static final String SETUP_SERVERS_PROMPT = "You can setup the following servers"; + public static final String SETUP_SERVERS_PROMPT = "You can setup the following distributions"; public static final String ENABLE_DEBUGGING_DEFAULT_MESSAGE = "If you want to enable remote debugging by default when running the server, " + "\nspecify the %s here (e.g. 1044). Leave blank to disable debugging. \n(Do not do this on a production server)"; - private static final String O2_DISTRIBUTION = "2.x Distribution"; - private static final String PLATFORM = "Platform"; - private static final String O3_DISTRIBUTION = "O3 Distribution"; - private static final String CLASSPATH_SCRIPT_PREFIX = "classpath://"; private static final String NO_DEBUGGING_DEFAULT_ANSWER = "no debugging"; @@ -205,13 +203,15 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu List options = new ArrayList<>(); + String currentDirectoryOption = null; Distribution currentDirectoryDistribution = null; File distroPropertiesFile = distroHelper.getDistroPropertiesFileFromDir(); if (distroPropertiesFile != null) { try { currentDirectoryDistribution = builder.buildFromFile(distroPropertiesFile); if (currentDirectoryDistribution != null) { - options.add(currentDirectoryDistribution.getName() + " " + currentDirectoryDistribution.getVersion() + " from current directory"); + currentDirectoryOption = currentDirectoryDistribution.getName() + " " + currentDirectoryDistribution.getVersion() + " from current directory"; + options.add(currentDirectoryOption); } } catch (Exception e) { @@ -219,32 +219,40 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu } } - options.add(O3_DISTRIBUTION); - options.add(O2_DISTRIBUTION); + options.add(SDKConstants.REFAPP_2X_PROMPT); + options.add(SDKConstants.REFAPP_3X_PROMPT); options.add(PLATFORM); - String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options); + String customDistroMessage = "Please specify distribution artifact"; + String customDistroDefault = SDKConstants.REFERENCEAPPLICATION_2_4; + + String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options, customDistroMessage, customDistroDefault); + + // Deploy from current directory if chosen + if (currentDirectoryOption != null && currentDirectoryOption.equals(choice)) { + return currentDirectoryDistribution.getEffectiveProperties(); + } + + // Deploy platform if (PLATFORM.equals(choice)) { return resolveDistroPropertiesForPlatform(server, platform); } - if (O2_DISTRIBUTION.equals(choice)) { + if (REFAPP_2X_PROMPT.equals(choice)) { wizard.promptForRefAppVersionIfMissing(server, versionsHelper); Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); return distribution.getEffectiveProperties(); } - if (O3_DISTRIBUTION.equals(choice)) { + if (REFAPP_3X_PROMPT.equals(choice)) { wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); return distribution.getEffectiveProperties(); } - // If here, it is because the option to set up from current directory was chosen, and these properties were already loaded, just return them - if (currentDirectoryDistribution == null) { - throw new MojoExecutionException("No valid distribution could be found"); - } - return currentDirectoryDistribution.getEffectiveProperties(); + // If here, it is because custom distribution was chosen and the choice reflects the Maven coordinates + Distribution distribution = distroHelper.resolveDistributionForStringSpecifier(choice, versionsHelper); + return distribution.getEffectiveProperties(); } private DistroProperties resolveDistroPropertiesForPlatform(Server server, String version) throws MojoExecutionException { diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java index 6b8bbd957..7b08ec986 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java @@ -82,8 +82,6 @@ public class DefaultWizard implements Wizard { static final String NOYES = " [N/y]"; - static final String REFERENCEAPPLICATION_2_4 = "org.openmrs.distro:referenceapplication-package:2.4"; - static final String REFERENCEAPPLICATION_O3 = "org.openmrs:emr-distro-configuration:3.0.0"; static final String SDK_PROPERTIES_FILE = "SDK Properties file"; @@ -739,7 +737,7 @@ private String promptForVersion(Map optionsMap, String customMes String message = customMessage != null ? customMessage : DISTRIBUTION_VERSION_PROMPT; String version = promptForMissingValueWithOptions(message, null, "distribution artifact", Lists.newArrayList(optionsMap.keySet()), "Please specify %s", - REFERENCEAPPLICATION_2_4); + SDKConstants.REFERENCEAPPLICATION_2_4); String artifact = optionsMap.get(version); if (artifact != null) { diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java index 07497f6cf..340a57eb1 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java @@ -86,14 +86,18 @@ public class SDKConstants { public static final String SETUP_DEFAULT_VERSION = "2.3"; public static final String SETUP_DEFAULT_PLATFORM_VERSION = "1.11.5"; + public static final String REFAPP_2X_PROMPT = "Reference Application 2.x"; public static final String REFAPP_2X_GROUP_ID = "org.openmrs.distro"; public static final String REFAPP_2X_ARTIFACT_ID = "referenceapplication-package"; public static final String REFAPP_2X_TYPE = "jar"; + public static final String REFAPP_3X_PROMPT = "Reference Application 3.x"; public static final String REFAPP_3X_GROUP_ID = "org.openmrs"; public static final String REFAPP_3X_ARTIFACT_ID = "distro-emr-configuration"; public static final String REFAPP_3X_TYPE = "zip"; + public static final String REFERENCEAPPLICATION_2_4 = "org.openmrs.distro:referenceapplication-package:2.4"; + public static final List SUPPPORTED_REFAPP_VERSIONS_2_3_1_OR_LOWER = java.util.Arrays.asList("2.3.1", "2.2", "2.1"); private final static String[] SUPPORTED_MODULE_EXTENSIONS = new String[]{Artifact.TYPE_WAR, Artifact.TYPE_JAR, Artifact.TYPE_OMOD}; From 5980de9f6b58a222b16369e3c4f2c32e948a63f9 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 14:31:30 -0500 Subject: [PATCH 3/5] SDK-341 - Fix regression preventing setting up custom distribution --- .../org/openmrs/maven/plugins/DeployIT.java | 12 +- .../openmrs/maven/plugins/AddDependency.java | 6 +- .../openmrs/maven/plugins/BuildDistro.java | 20 +- .../org/openmrs/maven/plugins/Deploy.java | 37 +-- .../openmrs/maven/plugins/GenerateDistro.java | 25 +- .../java/org/openmrs/maven/plugins/Setup.java | 23 +- .../maven/plugins/utility/DefaultWizard.java | 303 +++++------------- .../maven/plugins/utility/VersionsHelper.java | 6 +- .../openmrs/maven/plugins/utility/Wizard.java | 32 +- 9 files changed, 140 insertions(+), 324 deletions(-) diff --git a/integration-tests/src/test/java/org/openmrs/maven/plugins/DeployIT.java b/integration-tests/src/test/java/org/openmrs/maven/plugins/DeployIT.java index d24953af3..2ac48786f 100644 --- a/integration-tests/src/test/java/org/openmrs/maven/plugins/DeployIT.java +++ b/integration-tests/src/test/java/org/openmrs/maven/plugins/DeployIT.java @@ -62,8 +62,7 @@ public void deploy_shouldReplaceDistroPlatform() throws Exception { public void deploy_shouldUpgradeDistroTo2_3_1() throws Exception { testServerId = setupTestServer("referenceapplication:2.2"); addAnswer(testServerId); - addAnswer("Distribution"); - addAnswer("referenceapplication:2.3.1"); + addTaskParam("distro", "referenceapplication:2.3.1"); addAnswer("y"); executeTask("deploy"); assertSuccess(); @@ -81,8 +80,7 @@ public void deploy_shouldUpgradeDistroTo2_3_1() throws Exception { public void deploy_shouldDowngradeDistroTo2_1() throws Exception { testServerId = setupTestServer("referenceapplication:2.2"); addAnswer(testServerId); - addAnswer("Distribution"); - addAnswer("referenceapplication:2.1"); + addTaskParam("distro", "referenceapplication:2.1"); addAnswer("y"); executeTask("deploy"); assertSuccess(); @@ -100,8 +98,7 @@ public void deploy_shouldDowngradeDistroTo2_1() throws Exception { public void deploy_shouldUpgradeDistroTo2_13_0() throws Exception { testServerId = setupTestServer("referenceapplication:2.3.1"); addAnswer(testServerId); - addAnswer("Distribution"); - addAnswer("referenceapplication:2.13.0"); + addTaskParam("distro", "referenceapplication:2.13.0"); addAnswer("y"); executeTask("deploy"); assertSuccess(); @@ -124,8 +121,7 @@ public void deploy_shouldUpgradeDistroTo2_13_0() throws Exception { public void deploy_shouldUpgradeDistroTo3_0_0() throws Exception { testServerId = setupTestServer("referenceapplication:2.2"); addAnswer(testServerId); - addAnswer("Distribution"); - addAnswer("referenceapplication:3.0.0"); + addTaskParam("distro", "referenceapplication:3.0.0"); addAnswer("y"); executeTask("deploy"); assertSuccess(); diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AddDependency.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AddDependency.java index f358efa84..cd9ff0916 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AddDependency.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AddDependency.java @@ -7,9 +7,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.openmrs.maven.plugins.model.Artifact; import org.openmrs.maven.plugins.model.DistroProperties; - import org.openmrs.maven.plugins.utility.NpmVersionHelper; -import org.openmrs.maven.plugins.utility.SDKConstants; import java.io.File; import java.nio.file.Paths; @@ -133,10 +131,8 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { distroProperties.addProperty("owa." + artifactId, version); break; case WAR_OPTION: - Artifact platformArtifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID, - SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO); if (StringUtils.isBlank(version)) { - version = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(platformArtifact, 5)); + version = wizard.promptForPlatformVersion(versionsHelper); } distroProperties.addProperty("war.openmrs", version); break; diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java index 97dd2cb82..5b2b08be9 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java @@ -35,12 +35,7 @@ import java.util.ArrayList; import java.util.List; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; /** * Create docker configuration for distributions. @@ -156,22 +151,21 @@ else if (StringUtils.isNotBlank(distro)) { } if (distribution == null) { - Server server = new Server.ServerBuilder().build(); List options = new ArrayList<>(); options.add(SDKConstants.REFAPP_2X_PROMPT); - options.add(SDKConstants.REFAPP_3X_PROMPT); + options.add(REFAPP_3X_PROMPT); + Artifact artifact = null; String choice = wizard.promptForMissingValueWithOptions("You can setup following servers", null, null, options); switch (choice) { case SDKConstants.REFAPP_2X_PROMPT: - wizard.promptForRefAppVersionIfMissing(server, versionsHelper, DISTRIBUTION_VERSION_PROMPT); - distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); + artifact = wizard.promptForRefApp2xArtifact(versionsHelper); break; - case SDKConstants.REFAPP_3X_PROMPT: - wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); - distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); + case REFAPP_3X_PROMPT: + artifact = wizard.promptForRefApp3xArtifact(versionsHelper); } + distribution = builder.buildFromArtifact(artifact); } if (distribution == null) { diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Deploy.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Deploy.java index 55e9fc320..51431c7b0 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Deploy.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Deploy.java @@ -156,9 +156,8 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { } } - private void runInteractiveMode(Server server, ServerUpgrader upgrader) - throws MojoExecutionException, MojoFailureException { - DistroProperties distroProperties; + private void runInteractiveMode(Server server, ServerUpgrader upgrader) throws MojoExecutionException, MojoFailureException { + List options = new ArrayList<>(Arrays.asList( DEPLOY_MODULE_OPTION, DEPLOY_OWA_OPTION, @@ -178,27 +177,29 @@ private void runInteractiveMode(Server server, ServerUpgrader upgrader) server.getName(), server.getVersion())); + + Artifact artifact; + + // If its impossible to define distro, prompt refapp distro versions if (server.getName().equals("Platform") || server.getDistroGroupId() == null || server.getDistroArtifactId() == null) { - // If its impossible to define distro, prompt refapp distro versions - distro = wizard.promptForRefAppVersion(versionsHelper); - } else { - // If its possible to define distro, prompt that distro's versions - distro = wizard.promptForDistroVersion(server.getDistroGroupId(), server.getDistroArtifactId(), - server.getVersion(), server.getName(), versionsHelper); + artifact = wizard.promptForRefApp2xArtifact(versionsHelper); } - wizard.showMessage("Deploying distribution: " + distro); - Distribution distribution = distroHelper.resolveDistributionForStringSpecifier(distro, versionsHelper); + else { + // If it is possible to define distro, prompt that distro's versions + artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId()); + artifact.setVersion(wizard.promptForArtifactVersion("Please choose a " + server.getName() + " version", artifact, null, versionsHelper)); + } + + DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment()); + Distribution distribution = builder.buildFromArtifact(artifact); + + wizard.showMessage("Deploying distribution: " + distribution.getName() + " " + distribution.getVersion()); upgrader.upgradeToDistro(server, distribution, ignorePeerDependencies, overrideReuseNodeCache); break; } case (DEPLOY_PLATFORM_OPTION): { - wizard.showMessage(String.format( - TEMPLATE_CURRENT_VERSION, - "platform", - server.getPlatformVersion())); - Artifact webapp = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID, - SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO); - platform = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(webapp, 3)); + wizard.showMessage(String.format(TEMPLATE_CURRENT_VERSION, "platform", server.getPlatformVersion())); + platform = wizard.promptForPlatformVersion(versionsHelper); deployOpenmrs(server, platform); break; } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java index 4b7857f75..5381d6546 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/GenerateDistro.java @@ -19,12 +19,7 @@ import java.util.ArrayList; import java.util.List; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; /** * Generates an openmrs-distro.properties file for a specific version of OpenMRS Distro @@ -64,25 +59,19 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { } File outputFile = outputLocation != null ? new File(outputLocation) : new File(System.getProperty("user.dir")); + Artifact artifact = null; switch (choice) { case SDKConstants.REFAPP_2X_PROMPT: - wizard.promptForRefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT); - distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); - break; - - case SDKConstants.REFAPP_3X_PROMPT: - wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT); - distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); - break; + artifact = wizard.promptForRefApp2xArtifact(versionsHelper); + break; + case REFAPP_3X_PROMPT: + artifact = wizard.promptForRefApp3xArtifact(versionsHelper); } - + distribution = builder.buildFromArtifact(artifact); if (distribution == null) { throw new MojoFailureException("Distribution could not be generated, the specified distribution could not be found"); } - distribution.getEffectiveProperties().saveTo(outputFile); logger.info(String.format("openmrs-distro.properties file created successfully at %s", outputFile.getAbsolutePath() + File.separator + DistroProperties.DISTRO_FILE_NAME)); - - } } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index 1e2ebc5c2..b3b8917bd 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -16,7 +16,6 @@ import org.openmrs.maven.plugins.utility.ContentHelper; import org.openmrs.maven.plugins.utility.DBConnector; import org.openmrs.maven.plugins.utility.DistributionBuilder; -import org.openmrs.maven.plugins.utility.DistroHelper; import org.openmrs.maven.plugins.utility.SDKConstants; import org.openmrs.maven.plugins.utility.ServerHelper; @@ -40,14 +39,8 @@ import static org.openmrs.maven.plugins.model.Artifact.GROUP_DISTRO; import static org.openmrs.maven.plugins.utility.SDKConstants.PLATFORM_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; -import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE; import static org.openmrs.maven.plugins.utility.SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION; @@ -239,14 +232,14 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu } if (REFAPP_2X_PROMPT.equals(choice)) { - wizard.promptForRefAppVersionIfMissing(server, versionsHelper); - Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE)); + Artifact artifact = wizard.promptForRefApp2xArtifact(versionsHelper); + Distribution distribution = builder.buildFromArtifact(artifact); return distribution.getEffectiveProperties(); } if (REFAPP_3X_PROMPT.equals(choice)) { - wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); - Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE)); + Artifact artifact = wizard.promptForRefApp3xArtifact(versionsHelper); + Distribution distribution = builder.buildFromArtifact(artifact); return distribution.getEffectiveProperties(); } @@ -257,9 +250,11 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu private DistroProperties resolveDistroPropertiesForPlatform(Server server, String version) throws MojoExecutionException { Artifact platformArtifact = new Artifact(PLATFORM_ARTIFACT_ID, SETUP_DEFAULT_PLATFORM_VERSION, GROUP_DISTRO); - version = wizard.promptForPlatformVersionIfMissing(version, versionsHelper.getSuggestedVersions(platformArtifact, 6)); - platformArtifact = DistroHelper.parseDistroArtifact(GROUP_DISTRO + ":" + PLATFORM_ARTIFACT_ID + ":" + version, versionsHelper); - server.setPlatformVersion(platformArtifact.getVersion()); + if (StringUtils.isBlank(version)) { + version = wizard.promptForPlatformVersion(versionsHelper); + } + platformArtifact.setVersion(version); + server.setPlatformVersion(version); try { DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment()); Distribution distribution = builder.buildFromArtifact(platformArtifact); diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java index 7b08ec986..e00350ea5 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java @@ -5,10 +5,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; import org.apache.http.client.utils.URIBuilder; -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.codehaus.plexus.component.annotations.Component; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; @@ -21,7 +18,6 @@ import org.openmrs.maven.plugins.model.Version; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; @@ -38,19 +34,24 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; -import java.util.Set; import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromFile; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE; /** * Class for attribute helper functions @@ -82,14 +83,8 @@ public class DefaultWizard implements Wizard { static final String NOYES = " [N/y]"; - static final String REFERENCEAPPLICATION_O3 = "org.openmrs:emr-distro-configuration:3.0.0"; - static final String SDK_PROPERTIES_FILE = "SDK Properties file"; - static final String REFAPP_OPTION_TMPL = "Reference Application %s"; - - static final String REFAPP_ARTIFACT_TMPL = "org.openmrs.distro:referenceapplication-package:%s"; - static final String JDK_ERROR_TMPL = "\nThe JDK %s is not compatible with OpenMRS Platform %s. " + "Please use %s to run this server.\n\nIf you are running " + "in a forked mode, correct the java.home property in %s\n"; @@ -106,12 +101,6 @@ public class DefaultWizard implements Wizard { static final String NO_DIFFERENTIAL = "\nNo distribution changes found"; - public static final String PLATFORM_VERSION_PROMPT = "You can deploy the following versions of a platform"; - - public static final String DISTRIBUTION_VERSION_PROMPT = "You can deploy the following versions of distribution"; - - public static final String O3_VERSION_PROMPT = "You can deploy the following versions of O3"; - public static final String DB_OPTION_H2 = "H2"; public static final String DB_OPTION_MYSQL = "MySQL 5.6 and above (requires pre-installed MySQL 5.6 and above)"; @@ -130,6 +119,10 @@ public class DefaultWizard implements Wizard { public static final String DBNAME_URL_VARIABLE = "@DBNAME@"; + public static final String HIGHER_VERSIONS = "Higher Versions..."; + + public static final String LOWER_VERSIONS = "Lower Versions..."; + private static final int MAX_OPTIONS_SIZE = 5; private final PrintStream writer; @@ -181,12 +174,6 @@ public void promptForNewServerIfMissing(Server server) throws MojoExecutionExcep /** * Prompt for a value if it not set, and default value is set - * - * @param message - * @param value - * @param parameterName - * @param defValue - * @return value */ @Override public String promptForValueIfMissingWithDefault(String message, String value, String parameterName, String defValue) @@ -231,6 +218,49 @@ public String promptForMissingValueWithOptions(String message, String value, Str return promptForMissingValueWithOptions(message, value, parameterName, options, null, null); } + @Override + public String promptForArtifactVersion(String message, Artifact artifact, String otherMessage, VersionsHelper versionsHelper) throws MojoExecutionException { + List allVersions = versionsHelper.getSuggestedVersions(artifact, null); + if (SDKConstants.REFAPP_2X_GROUP_ID.equals(artifact.getGroupId()) && SDKConstants.REFAPP_2X_ARTIFACT_ID.equals(artifact.getArtifactId())) { + for (String version : SDKConstants.SUPPPORTED_REFAPP_VERSIONS_2_3_1_OR_LOWER) { + if (!allVersions.contains(version)) { + allVersions.add(version); + } + } + } + return promptForArtifactVersion(message, allVersions, 0, MAX_OPTIONS_SIZE, otherMessage); + } + + protected String promptForArtifactVersion(String message, List options, int pageNum, int pageSize, String otherMessage) throws MojoExecutionException { + int fromIndex = pageNum * pageSize; + int toIndex = fromIndex + pageSize - 1; + List currentPage = new ArrayList<>(); + for (int i = fromIndex; i <= toIndex && i < options.size(); i++) { + currentPage.add(options.get(i)); + } + boolean isFirstPage = pageNum == 0; + boolean isLastPage = (pageNum + 1) * pageSize >= options.size(); + + if (!isFirstPage) { + currentPage.add(HIGHER_VERSIONS); + } + if (!isLastPage) { + currentPage.add(LOWER_VERSIONS); + } + + String otherPrompt = (isLastPage ? otherMessage : null); + String answer = promptForMissingValueWithOptions(message, null, null, currentPage, otherPrompt, null); + + if (HIGHER_VERSIONS.equals(answer)) { + return promptForArtifactVersion(message, options, pageNum - 1, pageSize, otherMessage); + } + else if (LOWER_VERSIONS.equals(answer)) { + return promptForArtifactVersion(message, options, pageNum + 1, pageSize, otherMessage); + } + + return answer; + } + @Override public String promptForMissingValueWithOptions(String message, String value, String parameterName, List options, String customMessage, String customDefault) throws MojoExecutionException { @@ -330,10 +360,6 @@ public void showWarning(String message) { /** * Prompt for a value if it not set, and default value is NOT set - * - * @param value - * @param parameterName - * @return */ @Override public String promptForValueIfMissing(String value, String parameterName) throws MojoExecutionException { @@ -384,9 +410,6 @@ public boolean checkYes(String value) { /** * Get path to server by serverId and prompt if missing - * - * @return - * @throws MojoFailureException */ @Override public String promptForExistingServerIdIfMissing(String serverId) throws MojoExecutionException { @@ -411,15 +434,32 @@ public String promptForExistingServerIdIfMissing(String serverId) throws MojoExe } @Override - public String promptForPlatformVersionIfMissing(String version, List versions) throws MojoExecutionException { - return promptForMissingValueWithOptions(PLATFORM_VERSION_PROMPT, - version, "version", versions, "Please specify platform version", null); + public String promptForPlatformVersion(VersionsHelper versionsHelper) throws MojoExecutionException { + return promptForPlatformArtifact(versionsHelper).getVersion(); + } + + @Override + public Artifact promptForPlatformArtifact(VersionsHelper versionsHelper) throws MojoExecutionException { + Artifact artifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID, SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO); + String version = promptForArtifactVersion( "Please specify platform version", artifact, "Please specify platform version", versionsHelper); + artifact.setVersion(version); + return artifact; + } + + @Override + public Artifact promptForRefApp2xArtifact(VersionsHelper versionsHelper) throws MojoExecutionException { + Artifact artifact = new Artifact(REFAPP_2X_ARTIFACT_ID, "2.3.1", REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE); + String version = promptForArtifactVersion("Please choose a " + REFAPP_2X_PROMPT + " version", artifact, null, versionsHelper); + artifact.setVersion(version); + return artifact; } @Override - public String promptForPlatformVersion(List versions) throws MojoExecutionException { - return promptForMissingValueWithOptions(PLATFORM_VERSION_PROMPT, - null, "version", versions, "Please specify platform version", null); + public Artifact promptForRefApp3xArtifact(VersionsHelper versionsHelper) throws MojoExecutionException { + Artifact artifact = new Artifact(REFAPP_3X_ARTIFACT_ID, "3.0.0", REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE); + String version = promptForArtifactVersion("Please choose a " + REFAPP_3X_PROMPT + " version", artifact, null, versionsHelper); + artifact.setVersion(version); + return artifact; } @Override @@ -640,7 +680,7 @@ private boolean isJavaHomeValid(String jdkPath) { private void savePropertiesChangesToFile(Properties properties, File file) throws MojoExecutionException { - try (OutputStream fos = new FileOutputStream(file)) { + try (OutputStream fos = Files.newOutputStream(file.toPath())) { properties.store(fos, DefaultWizard.SDK_PROPERTIES_FILE + ":"); } catch (IOException e) { @@ -649,168 +689,6 @@ private void savePropertiesChangesToFile(Properties properties, File file) } } - @Override - public void promptForRefAppVersionIfMissing(Server server, VersionsHelper versionsHelper) throws MojoExecutionException { - promptForRefAppVersionIfMissing(server, versionsHelper, null); - } - - @Override - public void promptForRefAppVersionIfMissing(Server server, VersionsHelper versionsHelper, String customMessage) - throws MojoExecutionException { - if (server.getVersion() == null) { - String choice = promptForRefAppVersion(versionsHelper); - Artifact distro = DistroHelper.parseDistroArtifact(choice, versionsHelper); - server.setVersion(distro.getVersion()); - server.setDistroArtifactId(distro.getArtifactId()); - server.setDistroGroupId(distro.getGroupId()); - } - } - - @Override - public void promptForO3RefAppVersionIfMissing(Server server, VersionsHelper versionsHelper) - throws MojoExecutionException { - promptForO3RefAppVersionIfMissing(server, versionsHelper, null); - } - - public void promptForO3RefAppVersionIfMissing(Server server, VersionsHelper versionsHelper, String customMessage) - throws MojoExecutionException { - if (server.getVersion() == null) { - String choice = promptForO3Version(versionsHelper, customMessage); - Artifact distro = DistroHelper.parseDistroArtifact(choice, versionsHelper); - server.setVersion(distro.getVersion()); - server.setDistroArtifactId(distro.getArtifactId()); - server.setDistroGroupId(distro.getGroupId()); - } - } - - public String promptForRefAppVersion(VersionsHelper versionsHelper) throws MojoExecutionException { - return promptForRefAppVersion(versionsHelper, null); - } - - public String promptForO3Version(VersionsHelper versionsHelper, String customMessage) throws MojoExecutionException { - return promptForO3RefAppVersion(versionsHelper, customMessage); - } - - public String promptForO3Version(VersionsHelper versionsHelper) throws MojoExecutionException { - return promptForO3Version(versionsHelper, null); - } - - @Override - public String promptForDistroVersion(String distroGroupId, String distroArtifactId, String distroVersion, - String distroName, VersionsHelper versionsHelper) throws MojoExecutionException { - return promptForDistroVersion(distroGroupId, distroArtifactId, distroVersion, distroName, versionsHelper, null); - } - - @Override - public String promptForDistroVersion(String distroGroupId, String distroArtifactId, String distroVersion, - String distroName, VersionsHelper versionsHelper, String customMessage) throws MojoExecutionException { - final String optionTemplate = distroName + " %s"; - final String artifacttemplate = distroGroupId + ":" + distroArtifactId + ":" + "%s"; - - Set versions = new LinkedHashSet<>(versionsHelper - .getSuggestedVersions(SDKConstants.getDistroModule(distroGroupId, distroArtifactId, distroVersion), - MAX_OPTIONS_SIZE)); - Map optionsMap = getDistroVersionsOptionsMap(versions, versionsHelper, optionTemplate, - artifacttemplate); - - return promptForVersion(optionsMap, customMessage); - } - - public String promptForRefAppVersion(VersionsHelper versionsHelper, String customMessage) - throws MojoExecutionException { - Set versions = new LinkedHashSet<>( - versionsHelper.getSuggestedVersions(SDKConstants.getReferenceModule("2.3.1"), MAX_OPTIONS_SIZE)); - versions.addAll(SDKConstants.SUPPPORTED_REFAPP_VERSIONS_2_3_1_OR_LOWER); - Map optionsMap = getDistroVersionsOptionsMap(versions, versionsHelper, REFAPP_OPTION_TMPL, - REFAPP_ARTIFACT_TMPL); - return promptForVersion(optionsMap, customMessage); - } - - public String promptForO3RefAppVersion(VersionsHelper versionsHelper, String customMessage) - throws MojoExecutionException { - Map optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL); - return promptForO3Version(optionsMap, customMessage); - } - - private String promptForVersion(Map optionsMap, String customMessage) - throws MojoExecutionException { - String message = customMessage != null ? customMessage : DISTRIBUTION_VERSION_PROMPT; - String version = promptForMissingValueWithOptions(message, - null, "distribution artifact", Lists.newArrayList(optionsMap.keySet()), "Please specify %s", - SDKConstants.REFERENCEAPPLICATION_2_4); - - String artifact = optionsMap.get(version); - if (artifact != null) { - return artifact; - } else { - return version; - } - } - - private String promptForO3Version(Map optionsMap, String customMessage) throws MojoExecutionException { - String message = customMessage != null ? customMessage : O3_VERSION_PROMPT; - String version = promptForMissingValueWithOptions(message, - null, "O3 artifact", Lists.newArrayList(optionsMap.keySet()), "Please specify %s", - REFERENCEAPPLICATION_O3); - - String artifact = optionsMap.get(version); - if (artifact != null) { - return artifact; - } else { - return version; - } - } - - private Map getDistroVersionsOptionsMap(Set versions, VersionsHelper versionsHelper, - String optionTemplate, String artifactTemplate) { - Map optionsMap = new LinkedHashMap<>(); - - List artifactVersions = new ArrayList<>(); - for (String version : versions) { - artifactVersions.add(new DefaultArtifactVersion(version)); - } - for (String version : versionsHelper.getSuggestedVersions(artifactVersions, MAX_OPTIONS_SIZE)) { - optionsMap.put(String.format(optionTemplate, version), String.format(artifactTemplate, version)); - if (optionsMap.size() == MAX_OPTIONS_SIZE) - break; - } - return optionsMap; - } - - /** - * Returns a map of options based on the versions of O3 - * - * @param versionsHelper The VersionsHelper object to retrieve the artifact versions from. - * @param optionTemplate The template for generating option keys in the map. - * @return A LinkedHashMap containing the generated options map. - */ - private Map getO3VersionsOptionsMap(VersionsHelper versionsHelper, String optionTemplate) { - Map optionsMap = new LinkedHashMap<>(); - - { - Artifact artifact = new Artifact(SDKConstants.REFAPP_3X_ARTIFACT_ID, "3.0.0", "org.openmrs", "zip"); - for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) { - optionsMap.put(String.format(optionTemplate, version.toString()), artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version); - } - } - - if (optionsMap.size() == MAX_OPTIONS_SIZE) { - return optionsMap; - } - - { - Artifact artifact = new Artifact(SDKConstants.REFAPP_DISTRO, "3.0.0", "org.openmrs.distro", "zip"); - for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) { - if (!version.toString().endsWith("-SNAPSHOT") && optionsMap.size() < MAX_OPTIONS_SIZE) { - optionsMap.put(String.format(optionTemplate, version), - artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version); - } - } - } - - return optionsMap; - } - @Override public void promptForDb(Server server, DockerHelper dockerHelper, boolean h2supported, String dbDriver, String dockerHost) throws MojoExecutionException { @@ -1051,17 +929,6 @@ public void promptForDockerizedDb(Server server, DockerHelper dockerHelper, Stri dockerHelper.runDbContainer(containerId, server.getDbUri(), username, password); } - private String getDefaultUri(String dockerHost) { - if (SystemUtils.IS_OS_LINUX) { - return SDKConstants.URI_MYSQL; - } else { - int beginIndex = dockerHost.indexOf("//"); - int endIndex = dockerHost.lastIndexOf(":"); - String dockerMachineIp = dockerHost.substring(beginIndex + 2, endIndex); - return String.format(SDKConstants.URI_MYSQL_DOCKER, dockerMachineIp); - } - } - @Override public void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionException { String defaultUser = "root"; @@ -1082,20 +949,12 @@ public void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionE /** * Get servers with recently used first - * - * @return */ @Override public List getListOfServers() throws MojoExecutionException { Path openMRS = Server.getServersPath(); Map sortedMap = new TreeMap<>(Collections.reverseOrder()); - try (DirectoryStream subDirectories = Files.newDirectoryStream(openMRS, new DirectoryStream.Filter() { - - @Override - public boolean accept(Path entry) { - return entry.toFile().isDirectory(); - } - })) { + try (DirectoryStream subDirectories = Files.newDirectoryStream(openMRS, entry -> entry.toFile().isDirectory())) { for (Path dir : subDirectories) { if (Server.hasServerConfig(dir)) { sortedMap.put(dir.toFile().lastModified(), dir.getFileName().toString()); @@ -1145,8 +1004,6 @@ public void showJdkErrorMessage(String jdk, String platform, String recommendedJ /** * Show confirmation prompt if there is any change besides updating modules with SNAPSHOT versions - * - * @return */ @Override public boolean promptForConfirmDistroUpgrade(UpgradeDifferential upgradeDifferential) throws MojoExecutionException { diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java index 85c9cfacb..7d5695e23 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/VersionsHelper.java @@ -138,11 +138,11 @@ else if (lastSnapshot == null){ * @param maxReleases upper limit of number of releases * @return list of suggested versions */ - public List getSuggestedVersions(Artifact artifact, int maxReleases){ + public List getSuggestedVersions(Artifact artifact, Integer maxReleases){ return getSuggestedVersions(getVersions(artifact), maxReleases); } - public List getSuggestedVersions(List allVersions, int maxSize) { + public List getSuggestedVersions(List allVersions, Integer maxSize) { if(allVersions.isEmpty()){ return Collections.emptyList(); } @@ -172,7 +172,7 @@ public List getSuggestedVersions(List allVersions, int } } } - if (advices.size() > maxSize) { + if (maxSize != null && advices.size() > maxSize) { advices = advices.subList(0, maxSize); } return advices; diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java index ad1934bf5..f0d575721 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java @@ -1,7 +1,7 @@ package org.openmrs.maven.plugins.utility; import org.apache.maven.plugin.MojoExecutionException; -import org.openmrs.maven.plugins.model.DistroProperties; +import org.openmrs.maven.plugins.model.Artifact; import org.openmrs.maven.plugins.model.Server; import org.openmrs.maven.plugins.model.UpgradeDifferential; @@ -9,6 +9,7 @@ import java.util.List; public interface Wizard { + boolean isInteractiveMode(); void setInteractiveMode(boolean interactiveMode); @@ -19,31 +20,19 @@ public interface Wizard { void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionException; - String promptForPlatformVersionIfMissing(String version, List versions) throws MojoExecutionException; - - String promptForPlatformVersion(List versions) throws MojoExecutionException; - - void promptForRefAppVersionIfMissing(Server server, VersionsHelper versionsHelper) throws MojoExecutionException; - - void promptForRefAppVersionIfMissing(Server server, VersionsHelper versionsHelper, String customMessage) throws MojoExecutionException; - - void promptForO3RefAppVersionIfMissing(Server server, VersionsHelper versionsHelper, String customMessage) throws MojoExecutionException; + String promptForMissingValueWithOptions(String message, String value, String parameterName, List options) throws MojoExecutionException; - void promptForO3RefAppVersionIfMissing(Server server, VersionsHelper versionsHelper) throws MojoExecutionException; + String promptForMissingValueWithOptions(String message, String value, String parameterName, List options, String customMessage, String customDefault) throws MojoExecutionException; - String promptForRefAppVersion(VersionsHelper versionsHelper) throws MojoExecutionException; + String promptForArtifactVersion(String message, Artifact artifact, String otherMessage, VersionsHelper versionsHelper) throws MojoExecutionException; - String promptForDistroVersion(String distroGroupId, String distroArtifactId, String distroVersion, String distroName, VersionsHelper versionsHelper, String customMessage) - throws MojoExecutionException; + String promptForPlatformVersion(VersionsHelper versionsHelper) throws MojoExecutionException; - String promptForDistroVersion(String distroGroupId, String distroArtifactId, String distroVersion, String distroName, VersionsHelper versionsHelper) - throws MojoExecutionException; + Artifact promptForPlatformArtifact(VersionsHelper versionsHelper) throws MojoExecutionException; - String promptForMissingValueWithOptions(String message, String value, String parameterName, List options) - throws MojoExecutionException; + Artifact promptForRefApp2xArtifact(VersionsHelper versionsHelper) throws MojoExecutionException; - String promptForMissingValueWithOptions(String message, String value, String parameterName, List options, String customMessage, String customDefault) - throws MojoExecutionException; + Artifact promptForRefApp3xArtifact(VersionsHelper versionsHelper) throws MojoExecutionException; void showMessage(String message); @@ -53,8 +42,7 @@ String promptForMissingValueWithOptions(String message, String value, String par void showWarning(String message); - String promptForValueIfMissingWithDefault(String message, String value, String parameterName, String defValue) - throws MojoExecutionException; + String promptForValueIfMissingWithDefault(String message, String value, String parameterName, String defValue) throws MojoExecutionException; String promptForValueIfMissing(String value, String parameterName) throws MojoExecutionException; From e7a34bb87564d6951cf6a449c49e1985b7cbdbdd Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 14:35:58 -0500 Subject: [PATCH 4/5] SDK-341 - Minor update --- .../src/main/java/org/openmrs/maven/plugins/BuildDistro.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java index 5b2b08be9..eca09fb03 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.List; +import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT; import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT; /** @@ -153,13 +154,13 @@ else if (StringUtils.isNotBlank(distro)) { if (distribution == null) { List options = new ArrayList<>(); - options.add(SDKConstants.REFAPP_2X_PROMPT); + options.add(REFAPP_2X_PROMPT); options.add(REFAPP_3X_PROMPT); Artifact artifact = null; String choice = wizard.promptForMissingValueWithOptions("You can setup following servers", null, null, options); switch (choice) { - case SDKConstants.REFAPP_2X_PROMPT: + case REFAPP_2X_PROMPT: artifact = wizard.promptForRefApp2xArtifact(versionsHelper); break; case REFAPP_3X_PROMPT: From ec1ea49c8151fdf4c97fe9ebdede2ec362522ef3 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 14:37:57 -0500 Subject: [PATCH 5/5] SDK-341 - Minor update --- .../src/main/java/org/openmrs/maven/plugins/Setup.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index b3b8917bd..2dbcd22d9 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -212,8 +212,8 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu } } - options.add(SDKConstants.REFAPP_2X_PROMPT); - options.add(SDKConstants.REFAPP_3X_PROMPT); + options.add(REFAPP_2X_PROMPT); + options.add(REFAPP_3X_PROMPT); options.add(PLATFORM); String customDistroMessage = "Please specify distribution artifact";