Skip to content

Commit

Permalink
(refactor) Clean up unused and duplicate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Dec 5, 2024
1 parent 611db9f commit 1a30407
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
DistroProperties parentProperties = distribution.getParent().getEffectiveProperties();
if (StringUtils.isBlank(property)) {
List<String> currentExclusions = distribution.getProperties().getExclusions();
List<String> options = parentProperties.getPropertyNames().stream().filter(prop -> !currentExclusions.contains(prop)).collect(Collectors.toList());
List<String> options = parentProperties.getAllKeys().stream().filter(prop -> !currentExclusions.contains(prop)).collect(Collectors.toList());
property = wizard.promptForMissingValueWithOptions("Enter the property you want to exclude", null, null, options);
}
else {
if (!parentProperties.getPropertyNames().contains(property)) {
if (!parentProperties.getAllKeys().contains(property)) {
wizard.showWarning(WARNING_PROPERTY_NOT_IN_PARENT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {

if (StringUtils.isBlank(property)) {
property = wizard.promptForMissingValueWithOptions("Enter the property you want to remove",
null, null, new ArrayList<>(properties.getPropertyNames()));
null, null, new ArrayList<>(properties.getAllKeys()));
}

if (StringUtils.isNotBlank(property)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import java.util.Properties;
import java.util.Set;

/**
*
*/
public abstract class BaseSdkProperties {

public static final String PROPERTY_DISTRO_ARTIFACT_ID = "distro.artifactId";
Expand All @@ -37,38 +34,6 @@ public abstract class BaseSdkProperties {

protected Properties properties;

public Properties getModuleAndWarProperties(List<Artifact> warArtifacts, List<Artifact> moduleArtifacts) {
Properties properties = new Properties();
for (Artifact artifact : warArtifacts) {

stripArtifactId(artifact);

if (!artifact.getType().equals(TYPE_WAR)) {
properties.setProperty(TYPE_WAR + "." + artifact.getArtifactId() + "." + TYPE, artifact.getType());
}

if (!artifact.getGroupId().equals(Artifact.GROUP_WEB)) {
properties.setProperty(TYPE_WAR + "." + artifact.getArtifactId() + "." + GROUP_ID, artifact.getGroupId());
}

properties.setProperty(TYPE_WAR + "." + artifact.getArtifactId(), artifact.getVersion());
}

for (Artifact artifact : moduleArtifacts) {
stripArtifactId(artifact);

if (!artifact.getType().equals(TYPE_JAR)) {
properties.setProperty(TYPE_OMOD + "." + artifact.getArtifactId() + "." + TYPE, artifact.getType());
}
if (!artifact.getGroupId().equals(Artifact.GROUP_MODULE)) {
properties.setProperty(TYPE_OMOD + "." + artifact.getArtifactId() + "." + GROUP_ID, artifact.getGroupId());
}

properties.setProperty(TYPE_OMOD + "." + artifact.getArtifactId(), artifact.getVersion());
}
return properties;
}

public String getPlatformVersion(){
return getParam("war.openmrs");
}
Expand All @@ -95,8 +60,7 @@ public void setName(String name) {

public List<Artifact> getModuleArtifacts() {
List<Artifact> artifactList = new ArrayList<>();
for (Object keyObject: getAllKeys()) {
String key = keyObject.toString();
for (String key: getAllKeys()) {
String artifactType = getArtifactType(key);
if(artifactType.equals(TYPE_OMOD)) {
artifactList.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key), checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE), "omod"));
Expand All @@ -107,8 +71,7 @@ public List<Artifact> getModuleArtifacts() {

public List<Artifact> getOwaArtifacts() {
List<Artifact> artifactList = new ArrayList<>();
for (Object keyObject: getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
String artifactType = getArtifactType(key);
if(artifactType.equals(TYPE_OWA)) {
artifactList.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key), checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE)));
Expand All @@ -119,8 +82,7 @@ public List<Artifact> getOwaArtifacts() {

public Map<String, String> getSpaProperties() {
Map<String, String> spaProperties = new HashMap<>();
for (Object keyObject: getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
if (key.startsWith(TYPE_SPA + ".")) {
spaProperties.put(key.substring(TYPE_SPA.length() + 1), getParam(key));
}
Expand Down Expand Up @@ -155,8 +117,7 @@ public List<Artifact> getSpaArtifacts() {

public List<Artifact> getWarArtifacts() {
List<Artifact> artifactList = new ArrayList<>();
for (Object keyObject: getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
String artifactType = getArtifactType(key);
if(artifactType.equals(TYPE_WAR)) {
artifactList.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key), checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE)));
Expand All @@ -167,8 +128,7 @@ public List<Artifact> getWarArtifacts() {

public List<Artifact> getConfigArtifacts() {
List<Artifact> artifactList = new ArrayList<>();
for (Object keyObject : getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
String artifactType = getArtifactType(key);
if (artifactType.equals(TYPE_CONFIG)) {
artifactList.add(
Expand All @@ -181,8 +141,7 @@ public List<Artifact> getConfigArtifacts() {

public List<Artifact> getContentArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
for (Object keyObject : getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
String artifactType = getArtifactType(key);
if (artifactType.equals(TYPE_CONTENT)) {
artifacts.add(new Artifact(
Expand All @@ -196,14 +155,22 @@ public List<Artifact> getContentArtifacts() {
return artifacts;
}

protected Set<Object> getAllKeys() {
return properties.keySet();
public Set<String> getAllKeys() {
return properties.stringPropertyNames();
}

public Properties getAllProperties() {
return properties;
}

public void addProperty(String property, String value) {
properties.put(property, value);
}

public boolean contains(String propertyName) {
return properties.containsKey(propertyName);
}

protected String getArtifactType(String key) {
String[] wordsArray = key.split("\\.");
if(!(wordsArray[wordsArray.length-1].equals(TYPE) || wordsArray[wordsArray.length-1].equals(ARTIFACT_ID) || wordsArray[wordsArray.length-1].equals(GROUP_ID))){
Expand Down Expand Up @@ -386,19 +353,19 @@ private void setCustomModuleType(Artifact artifact){
properties.setProperty(TYPE_OMOD+"."+artifact.getArtifactId()+"."+TYPE, artifact.getType());
}

private void setCustomModuleGroupId(Artifact artifact){
private void setCustomModuleGroupId(Artifact artifact) {
properties.setProperty(TYPE_OMOD+"."+artifact.getArtifactId()+"."+GROUP_ID, artifact.getGroupId());
}

public void synchronize(BaseSdkProperties other){
for(Object key: getAllKeys()){
if (isBaseSdkProperty(key.toString())) {
public void synchronize(BaseSdkProperties other) {
for(String key : getAllKeys()){
if (isBaseSdkProperty(key)) {
other.properties.put(key, properties.get(key));
}
}
for(Object key: new ArrayList<>(other.getAllKeys())){
if(isBaseSdkProperty(key.toString())){
if(StringUtils.isBlank(getParam(key.toString()))){
for(String key : new ArrayList<>(other.getAllKeys())) {
if(isBaseSdkProperty(key)){
if(StringUtils.isBlank(getParam(key))) {
other.properties.remove(key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -20,20 +18,14 @@

import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromFile;

/**
*
*/
public class DistroProperties extends BaseSdkProperties {

public static final String PROPERTY_PROMPT_KEY = "property.%s.prompt";
private static final String DEAFAULT_FILE_NAME = "openmrs-distro-%s.properties";
public static final String DISTRO_FILE_NAME = "openmrs-distro.properties";
private static final String DB_SQL = "db.sql";
public static final String PROPERTY_DEFAULT_VALUE_KEY = "property.%s.default";
public static final String PROPERTY_KEY = "property.%s";

private static final Logger log = LoggerFactory.getLogger(DistroProperties.class);

public DistroProperties(String name, String platformVersion){
properties = new Properties();
setName(name);
Expand Down Expand Up @@ -76,9 +68,9 @@ public String getPropertyValue(String propertyName){

public Set<String> getPropertiesNames(){
Set<String> propertiesNames = new HashSet<>();
for(Object key: getAllKeys()){
if(key.toString().startsWith("property.")){
propertiesNames.add(extractPropertyName(key.toString()));
for(String key : getAllKeys()){
if(key.startsWith("property.")){
propertiesNames.add(extractPropertyName(key));
}
}
return propertiesNames;
Expand Down Expand Up @@ -113,8 +105,7 @@ private String extractPropertyName(String key) {
*/
public Artifact getParentDistroArtifact() throws MojoExecutionException {
Artifact artifact = null;
for (Object keyObject: getAllKeys()) {
String key = keyObject.toString();
for (String key : getAllKeys()) {
String artifactType = getArtifactType(key);
if (artifactType.equals(TYPE_DISTRO)) {
String artifactId = checkIfOverwritten(key, ARTIFACT_ID);
Expand Down Expand Up @@ -166,10 +157,6 @@ public void resolvePlaceholders(Properties projectProperties) throws MojoExecuti
PropertiesUtils.resolvePlaceholders(properties, projectProperties);
}

public Set<Object> getAllKeys() {
return properties.keySet();
}

public List<String> getExclusions() {
String exclusions = getParam("exclusions");
if(exclusions == null) {
Expand All @@ -178,10 +165,6 @@ public List<String> getExclusions() {
return Arrays.asList(exclusions.split(","));
}

public Set<String> getPropertyNames() {
return properties.stringPropertyNames();
}

public void removeProperty(String property) throws MojoExecutionException {
if (!properties.containsKey(property)) {
throw new MojoExecutionException("The property " + property + " was not found in the distro");
Expand All @@ -198,41 +181,4 @@ public void addExclusion(String exclusion) {

properties.setProperty("exclusions", exclusions + "," + exclusion);
}

public void addProperty(String property, String value) throws MojoExecutionException {
properties.put(property, value);
}

public boolean contains(String propertyName) {
return properties.containsKey(propertyName);
}

/**
* Adds a dependency with the specified version to the properties.
*
* @param dependency The name of the dependency.
* @param version The version of the dependency.
*/
public void add(String dependency, String version) {
if (StringUtils.isBlank(dependency) || StringUtils.isBlank(version)) {
log.error("Dependency name or version cannot be blank");
return;
}

if (dependency.startsWith("omod.") || dependency.startsWith("owa.") || dependency.startsWith("war")
|| dependency.startsWith("spa.frontendModule")) {
properties.setProperty(dependency, version);
log.info("Added dependency: {} with version: {}", dependency, version);
}
}

public String get(String contentDependencyKey) {
return properties.getProperty(contentDependencyKey);
}

@Override
public String checkIfOverwritten(String key, String param) {
return super.checkIfOverwritten(key, param);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,7 @@ public String getArtifactId(String filename) {

public void setValuesFromDistroProperties(DistroProperties distroProperties) {
if (distroProperties != null) {
for (Object property : distroProperties.getAllKeys()) {
String key = property.toString();
for (String key : distroProperties.getAllKeys()) {
if (distroProperties.isBaseSdkProperty(key)) {
this.properties.put(key, distroProperties.getParam(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,16 @@ protected void processContentProperties(Properties contentProperties, DistroProp
if (dependency.startsWith("omod.") || dependency.startsWith("owa.") || dependency.startsWith("war")
|| dependency.startsWith("spa.frontendModule") || dependency.startsWith("content.")) {
String versionRange = contentProperties.getProperty(dependency);
String distroVersion = distroProperties.get(dependency);
String distroVersion = distroProperties.getParam(dependency);

if (distroVersion == null) {
String latestVersion = findLatestMatchingVersion(dependency, versionRange);
if (latestVersion == null) {
throw new MojoExecutionException(
"No matching version found for dependency " + dependency + " in " + zipFileName);
}
distroProperties.add(dependency, latestVersion);

distroProperties.addProperty(dependency, latestVersion);
} else {
checkVersionInRange(dependency, versionRange, distroVersion, contentProperties.getProperty("name"));
}
Expand Down

0 comments on commit 1a30407

Please sign in to comment.