Skip to content

Commit

Permalink
(refactor) Use MavenEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Dec 5, 2024
1 parent 1a30407 commit 7954240
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import org.openmrs.maven.plugins.git.GitHelper;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.utility.ConfigurationInstaller;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DefaultJira;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.DockerHelper;
import org.openmrs.maven.plugins.utility.Jira;
import org.openmrs.maven.plugins.utility.MavenEnvironment;
import org.openmrs.maven.plugins.utility.ModuleInstaller;
import org.openmrs.maven.plugins.utility.NodeHelper;
import org.openmrs.maven.plugins.utility.OwaHelper;
import org.openmrs.maven.plugins.utility.SpaInstaller;
import org.openmrs.maven.plugins.utility.StatsManager;
Expand Down Expand Up @@ -112,6 +112,11 @@ public abstract class AbstractTask extends AbstractMojo {
*/
DistroHelper distroHelper;

/**
* handles content packages
*/
ContentHelper contentHelper;

/**
* handles OWAs
*/
Expand Down Expand Up @@ -162,6 +167,7 @@ public AbstractTask(AbstractTask other) {
this.moduleInstaller = other.moduleInstaller;
this.versionsHelper = other.versionsHelper;
this.distroHelper = other.distroHelper;
this.contentHelper = other.contentHelper;
this.owaHelper = other.owaHelper;
this.spaInstaller = other.spaInstaller;
this.configurationInstaller = other.configurationInstaller;
Expand Down Expand Up @@ -193,25 +199,28 @@ public void initTask() {
gitHelper = new DefaultGitHelper();
}
if (versionsHelper == null) {
versionsHelper = new VersionsHelper(artifactFactory, mavenProject, mavenSession, artifactMetadataSource);
versionsHelper = new VersionsHelper(mavenEnvironment);
}
if (moduleInstaller == null) {
moduleInstaller = new ModuleInstaller(mavenProject, mavenSession, pluginManager, versionsHelper);
moduleInstaller = new ModuleInstaller(mavenEnvironment);
}
if (distroHelper == null) {
distroHelper = new DistroHelper(mavenEnvironment);
}
if (owaHelper == null) {
owaHelper = new OwaHelper(mavenSession, mavenProject, pluginManager, wizard);
owaHelper = new OwaHelper(mavenEnvironment);
}
if (contentHelper == null) {
contentHelper = new ContentHelper(mavenEnvironment);
}
if (spaInstaller == null) {
spaInstaller = new SpaInstaller(distroHelper, new NodeHelper(mavenProject, mavenSession, pluginManager));
spaInstaller = new SpaInstaller(mavenEnvironment);
}
if (configurationInstaller == null) {
configurationInstaller = new ConfigurationInstaller(distroHelper);
configurationInstaller = new ConfigurationInstaller(mavenEnvironment);
}
if (dockerHelper == null) {
dockerHelper = new DockerHelper(mavenProject, mavenSession, pluginManager, wizard);
dockerHelper = new DockerHelper(mavenEnvironment);
}
if (StringUtils.isNotBlank(openMRSPath)) {
Server.setServersPath(openMRSPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void executeTask() throws MojoExecutionException {
}

private void addOwaSubmodule() throws MojoExecutionException {
new OwaHelper(mavenSession, mavenProject, pluginManager, wizard)
.setInstallationDir(new File(mavenProject.getBasedir(), "owa"))
.createOwaProject();
OwaHelper owaHelper = new OwaHelper(getMavenEnvironment());
owaHelper.setInstallationDir(new File(mavenProject.getBasedir(), "owa"));
owaHelper.createOwaProject();

//apply changes to config.xml and main pom.xml
wizard.showMessage("Modifying pom.xml files...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DistributionBuilder;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
Expand Down Expand Up @@ -300,7 +299,7 @@ private String buildDistro(File targetDirectory, Distribution distribution) thro
File configDir = new File(web, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configurationInstaller.installToDirectory(configDir, distroProperties);

ContentHelper.downloadAndMoveContentBackendConfig(web, distroProperties, moduleInstaller, wizard);
contentHelper.downloadAndMoveContentBackendConfig(web, distroProperties);

spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
setProjectType();

if (TYPE_OWA.equals(type)) {
new OwaHelper(mavenSession, mavenProject, pluginManager, wizard).createOwaProject();
new OwaHelper(getMavenEnvironment()).createOwaProject();
} else {
createModule();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void executeTask() throws MojoExecutionException {
FileUtils.deleteDirectory(server.getServerDirectory());

if (StringUtils.isNotBlank(server.getContainerId())) {
new DockerHelper(mavenProject, mavenSession, pluginManager, wizard).runDbContainer(
new DockerHelper(getMavenEnvironment()).runDbContainer(
server.getContainerId(),
server.getMySqlPort(),
server.getDbUser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Reset extends AbstractServerTask {
public void executeTask() throws MojoExecutionException {
Server server = getServer();
if (StringUtils.isNotBlank(server.getContainerId())){
new DockerHelper(mavenProject, mavenSession, pluginManager, wizard).runDbContainer(
new DockerHelper(getMavenEnvironment()).runDbContainer(
server.getContainerId(),
server.getMySqlPort(),
server.getDbUser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.UpgradeDifferential;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
Expand Down Expand Up @@ -161,7 +160,7 @@ public void upgradeToDistro(Server server, Distribution distribution, boolean ig
}

if (contentChanges.hasChanges()) {
ContentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties, parentTask.moduleInstaller, parentTask.wizard);
parentTask.contentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties);
// TODO: Where is the frontend config installation?
for (Artifact artifact : contentChanges.getArtifactsToRemove()) {
server.removePropertiesForArtifact(BaseSdkProperties.TYPE_CONTENT, artifact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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.ContentHelper;
import org.openmrs.maven.plugins.utility.DBConnector;
import org.openmrs.maven.plugins.utility.DistributionBuilder;
import org.openmrs.maven.plugins.utility.SDKConstants;
Expand Down Expand Up @@ -285,7 +284,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
distroHelper.parseContentProperties(distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties);

ContentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties, moduleInstaller, wizard);
contentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties);

if (spaInstaller != null) {
spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
public class ConfigurationInstaller {

private final Wizard wizard;

private final ModuleInstaller moduleInstaller;

public ConfigurationInstaller(DistroHelper distroHelper) {
this.wizard = distroHelper.wizard;
this.moduleInstaller = new ModuleInstaller(distroHelper.mavenProject, distroHelper.mavenSession, distroHelper.pluginManager, distroHelper.versionHelper);
public ConfigurationInstaller(MavenEnvironment mavenEnvironment) {
this.wizard = mavenEnvironment.getWizard();
this.moduleInstaller = new ModuleInstaller(mavenEnvironment);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.openmrs.maven.plugins.utility;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -8,11 +13,6 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;

/**
* This class downloads and moves content backend config to respective configuration folders.
*/
Expand All @@ -21,7 +21,15 @@ public class ContentHelper {
public static final String FRONTEND_CONFIG_FOLDER = Paths.get("configs", "frontend_config").toString();
public static final String BACKEND_CONFIG_FOLDER = Paths.get("configs", "backend_config").toString();

private static File unpackArtifact(Artifact contentArtifact, ModuleInstaller moduleInstaller) throws MojoExecutionException {
private final ModuleInstaller moduleInstaller;
private final Wizard wizard;

public ContentHelper(MavenEnvironment mavenEnvironment) {
this.moduleInstaller = new ModuleInstaller(mavenEnvironment);
this.wizard = mavenEnvironment.getWizard();
}

private File unpackArtifact(Artifact contentArtifact) throws MojoExecutionException {
String artifactId = contentArtifact.getArtifactId();
// create a temporary artifact folder
File sourceDir;
Expand All @@ -37,8 +45,8 @@ private static File unpackArtifact(Artifact contentArtifact, ModuleInstaller mod
return sourceDir;
}

private static void moveBackendConfig(Artifact contentArtifact, File targetDir, ModuleInstaller moduleInstaller) throws MojoExecutionException {
File sourceDir = unpackArtifact(contentArtifact, moduleInstaller);
private void moveBackendConfig(Artifact contentArtifact, File targetDir) throws MojoExecutionException {
File sourceDir = unpackArtifact(contentArtifact);
try {
File backendConfigFiles = sourceDir.toPath().resolve(BACKEND_CONFIG_FOLDER).toFile();
Path targetPath = targetDir.toPath().toAbsolutePath();
Expand All @@ -62,8 +70,8 @@ private static void moveBackendConfig(Artifact contentArtifact, File targetDir,
}
}

public static List<File> extractAndGetAllContentFrontendConfigs(Artifact contentArtifact, ModuleInstaller moduleInstaller) throws MojoExecutionException {
File sourceDir = unpackArtifact(contentArtifact, moduleInstaller);
public List<File> extractAndGetAllContentFrontendConfigs(Artifact contentArtifact) throws MojoExecutionException {
File sourceDir = unpackArtifact(contentArtifact);
List<File> configFiles = new ArrayList<>();
File frontendConfigFiles = sourceDir.toPath().resolve(FRONTEND_CONFIG_FOLDER).toFile();

Expand All @@ -83,27 +91,27 @@ public static List<File> extractAndGetAllContentFrontendConfigs(Artifact content
}

// This method now sets the static moduleInstaller
public static void downloadAndMoveContentBackendConfig(File serverDirectory, DistroProperties distroProperties, ModuleInstaller moduleInstaller, Wizard wizard) throws MojoExecutionException {
public void downloadAndMoveContentBackendConfig(File serverDirectory, DistroProperties distroProperties) throws MojoExecutionException {
if (distroProperties != null) {
File targetDir = new File(serverDirectory, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
List<Artifact> contents = distroProperties.getContentArtifacts();

if (contents != null) {
for (Artifact content : contents) {
wizard.showMessage("Downloading Content: " + content + "\n");
moveBackendConfig(content, targetDir, moduleInstaller);
moveBackendConfig(content, targetDir);
}
}
}
}

public static List<File> collectFrontendConfigs(DistroProperties distroProperties, ModuleInstaller moduleInstaller) throws MojoExecutionException {
public List<File> collectFrontendConfigs(DistroProperties distroProperties) throws MojoExecutionException {
List<File> allConfigFiles = new ArrayList<>();
if (distroProperties != null) {
List<Artifact> contents = distroProperties.getContentArtifacts();
if (contents != null) {
for (Artifact contentArtifact : contents) {
allConfigFiles.addAll(extractAndGetAllContentFrontendConfigs(contentArtifact, moduleInstaller));
allConfigFiles.addAll(extractAndGetAllContentFrontendConfigs(contentArtifact));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.apache.commons.io.FileUtils;
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.project.MavenProject;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
Expand Down Expand Up @@ -41,26 +38,10 @@ public class ModuleInstaller {

private static final String GOAL_UNPACK = "unpack";

final MavenProject mavenProject;
final MavenEnvironment mavenEnvironment;

final MavenSession mavenSession;

final BuildPluginManager pluginManager;

final VersionsHelper versionsHelper;

public ModuleInstaller(MavenProject mavenProject,
MavenSession mavenSession,
BuildPluginManager pluginManager,
VersionsHelper versionsHelper) {
this.mavenProject = mavenProject;
this.mavenSession = mavenSession;
this.pluginManager = pluginManager;
this.versionsHelper = versionsHelper;
}

public ModuleInstaller(DistroHelper distroHelper) {
this(distroHelper.mavenProject, distroHelper.mavenSession, distroHelper.pluginManager, distroHelper.versionHelper);
public ModuleInstaller(MavenEnvironment mavenEnvironment) {
this.mavenEnvironment = mavenEnvironment;
}

public void installDefaultModules(Server server) throws MojoExecutionException {
Expand Down Expand Up @@ -178,7 +159,11 @@ private void prepareModules(Artifact[] artifacts, String outputDir, String goal,
),
goal(goal),
configuration(configuration.toArray(new Element[0])),
executionEnvironment(mavenProject, mavenSession, pluginManager)
executionEnvironment(
mavenEnvironment.getMavenProject(),
mavenEnvironment.getMavenSession(),
mavenEnvironment.getPluginManager()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.openmrs.maven.plugins.model.Server;
Expand Down Expand Up @@ -31,23 +30,16 @@ public class NodeHelper {

public static Path tempDir;

private final MavenSession mavenSession;

private final MavenProject mavenProject;

private final BuildPluginManager pluginManager;

private final MavenEnvironment mavenEnvironment;

private static final Logger logger = LoggerFactory.getLogger(NodeHelper.class);

private static final String OPENMRS_SDK_NODE = "openmrs-sdk-node";

private static final String _OPENMRS_SDK_NODE_CACHE = "_openmrs_sdk_node_cache";

public NodeHelper(MavenProject mavenProject, MavenSession mavenSession, BuildPluginManager pluginManager) {
this.mavenProject = mavenProject;
this.mavenSession = mavenSession;
this.pluginManager = pluginManager;
public NodeHelper(MavenEnvironment mavenEnvironment) {
this.mavenEnvironment = mavenEnvironment;
}

private static File getNodeCache() {
Expand Down Expand Up @@ -89,6 +81,7 @@ public void runNpx(String arguments, String npmArguments) throws MojoExecutionEx
// it's a little weird to use a custom NPM cache for this; however, it seems to be necessary to get things working on Bamboo
// hack added in December 2021; it's use probably should be re-evaluated at some point
// additional hack: we do not use --cache on macs due to https://github.com/npm/cli/issues/3256
MavenProject mavenProject = mavenEnvironment.getMavenProject();
if (mavenProject != null && mavenProject.getBuild() != null && !SystemUtils.IS_OS_MAC_OSX) {
npmExec =
npmArguments + " --cache=" + tempDir.resolve("npm-cache").toAbsolutePath() + " exec -- " + arguments;
Expand All @@ -110,6 +103,7 @@ private void runFrontendMavenPlugin(String goal, List<MojoExecutor.Element> conf
throws MojoExecutionException {
// the proxy already works for installing node and npm; however, it does not work when running npm or npx
if (goal != null && !goal.equalsIgnoreCase("install-node-and-npm")) {
MavenSession mavenSession = mavenEnvironment.getMavenSession();
NodeProxyHelper.ProxyContext proxyContext = NodeProxyHelper.setupProxyContext(mavenSession);
proxyContext.applyProxyContext(configuration);
}
Expand All @@ -122,7 +116,11 @@ private void runFrontendMavenPlugin(String goal, List<MojoExecutor.Element> conf
),
goal(goal),
configuration(configuration.toArray(new MojoExecutor.Element[0])),
executionEnvironment(mavenProject, mavenSession, pluginManager)
executionEnvironment(
mavenEnvironment.getMavenProject(),
mavenEnvironment.getMavenSession(),
mavenEnvironment.getPluginManager()
)
);
}
}
Loading

0 comments on commit 7954240

Please sign in to comment.