|
| 1 | +package org.openmrs.maven.plugins; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.apache.maven.plugin.MojoExecutionException; |
| 5 | +import org.apache.maven.plugin.MojoFailureException; |
| 6 | +import org.apache.maven.plugins.annotations.Mojo; |
| 7 | +import org.apache.maven.plugins.annotations.Parameter; |
| 8 | +import org.openmrs.maven.plugins.model.Artifact; |
| 9 | +import org.openmrs.maven.plugins.model.DistroProperties; |
| 10 | + |
| 11 | +import org.openmrs.maven.plugins.utility.NPMHelper; |
| 12 | +import org.openmrs.maven.plugins.utility.SDKConstants; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +@Mojo(name = "add-dependency", requiresProject = false) |
| 21 | +public class AddDependency extends AbstractTask { |
| 22 | + |
| 23 | + /** |
| 24 | + * Path to the openmrs-distro.properties file to modify |
| 25 | + */ |
| 26 | + @Parameter(property = "distro") |
| 27 | + private String distro; |
| 28 | + |
| 29 | + /** |
| 30 | + * Type of the dependency to add |
| 31 | + */ |
| 32 | + @Parameter(property = "type") |
| 33 | + private String type; |
| 34 | + |
| 35 | + /** |
| 36 | + * Maven group id of the dependency |
| 37 | + */ |
| 38 | + @Parameter(property = "groupId") |
| 39 | + private String groupId; |
| 40 | + |
| 41 | + /** |
| 42 | + * Maven artifact id of the dependency |
| 43 | + */ |
| 44 | + @Parameter(property = "artifactId") |
| 45 | + private String artifactId; |
| 46 | + |
| 47 | + /** |
| 48 | + * Version of the dependency |
| 49 | + */ |
| 50 | + @Parameter(property = "version") |
| 51 | + private String version; |
| 52 | + |
| 53 | + /** |
| 54 | + * Name of the frontend module |
| 55 | + */ |
| 56 | + @Parameter(property = "moduleName") |
| 57 | + private String moduleName; |
| 58 | + |
| 59 | + |
| 60 | + private final String DEPENDENCY_TYPE_PROMPT = "Enter the type of dependency you need to add"; |
| 61 | + |
| 62 | + |
| 63 | + @Override |
| 64 | + public void executeTask() throws MojoExecutionException, MojoFailureException { |
| 65 | + if (distro == null) { |
| 66 | + File userDir = new File(System.getProperty("user.dir")); |
| 67 | + File distroFile = new File(userDir, DistroProperties.DISTRO_FILE_NAME); |
| 68 | + if (distroFile.exists()) { |
| 69 | + distro = distroFile.getAbsolutePath(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if(StringUtils.isBlank(type)) { |
| 74 | + List<String> dependencyTypes = new ArrayList<>(Arrays.asList("OMOD", "SPA", "OWA", "WAR")); |
| 75 | + type = wizard.promptForMissingValueWithOptions(DEPENDENCY_TYPE_PROMPT, null, null, dependencyTypes); |
| 76 | + } |
| 77 | + Properties properties = new Properties(); |
| 78 | + |
| 79 | + if(StringUtils.isNotBlank(distro)) { |
| 80 | + properties = distroHelper.resolveDistroPropertiesForStringSpecifier(distro, versionsHelper).getProperties(); |
| 81 | + } |
| 82 | + |
| 83 | + switch (type) { |
| 84 | + case "OMOD": |
| 85 | + groupId = wizard.promptForValueIfMissingWithDefault(null, groupId, "groupId", Artifact.GROUP_MODULE); |
| 86 | + artifactId = wizard.promptForValueIfMissing(artifactId, "artifactId"); |
| 87 | + if (StringUtils.isBlank(version)) { |
| 88 | + Artifact artifact = new Artifact(artifactId, "1.0", groupId); |
| 89 | + List<String> versions = versionsHelper.getSuggestedVersions(artifact, 5); |
| 90 | + version = wizard.promptForMissingValueWithOptions("Enter the version", null, null, versions, |
| 91 | + "Please specify the module version", null); |
| 92 | + } |
| 93 | + properties.setProperty("omod." + artifactId, version); |
| 94 | + break; |
| 95 | + case "SPA": |
| 96 | + moduleName = wizard.promptForValueIfMissing(moduleName, "frontend module name"); |
| 97 | + if (StringUtils.isBlank(version)) { |
| 98 | + List<String> versions = new NPMHelper().getPackageVersions(moduleName, 6); |
| 99 | + version = wizard.promptForMissingValueWithOptions("Enter the module version", null, null, versions, |
| 100 | + "Please specify the SPA version", null); |
| 101 | + } |
| 102 | + properties.setProperty("spa.frontendModules.@openmrs/" + moduleName, version); |
| 103 | + break; |
| 104 | + case "OWA": |
| 105 | + artifactId = wizard.promptForValueIfMissing(artifactId, "OWA name"); |
| 106 | + Artifact artifact = new Artifact(artifactId, "1.0", Artifact.GROUP_OWA); |
| 107 | + if(StringUtils.isBlank(version)) { |
| 108 | + List<String> suggestedVersions = versionsHelper.getSuggestedVersions(artifact, 6); |
| 109 | + version = wizard |
| 110 | + .promptForMissingValueWithOptions("Which version would you like to deploy?%s", null, "", suggestedVersions, |
| 111 | + "Please specify OWA version", null); |
| 112 | + } |
| 113 | + properties.setProperty("owa." + artifactId, version); |
| 114 | + break; |
| 115 | + case "WAR": |
| 116 | + Artifact platformArtifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID, |
| 117 | + SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO); |
| 118 | + if(StringUtils.isBlank(version)) { |
| 119 | + version = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(platformArtifact, 5)); |
| 120 | + } |
| 121 | + properties.setProperty("war.openmrs", version); |
| 122 | + } |
| 123 | + |
| 124 | + DistroProperties distroProperties = new DistroProperties(properties); |
| 125 | + |
| 126 | + if (StringUtils.isBlank(distro)) { |
| 127 | + wizard.showMessage("No distro.properpties file is provided. Generating a new openmrs-distro.properties file."); |
| 128 | + distro = new File(System.getProperty("user.dir"), DistroProperties.DISTRO_FILE_NAME).getAbsolutePath(); |
| 129 | + } |
| 130 | + |
| 131 | + distroProperties.saveTo(new File(distro).getParentFile()); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments