Skip to content

Commit

Permalink
SDK-371 - Ensure all frontend configuration is made available to fron…
Browse files Browse the repository at this point in the history
…tend
  • Loading branch information
mseaton committed Dec 12, 2024
1 parent 7ab917e commit 2daceaa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ public void testBuildDistroWithContentPackageVariables() throws Exception {
assertFileContains(",[0-9],", "distro", "web", "openmrs_config", "patientidentifiertypes", "patientidentifiertypes.csv");
assertFileContains("<value>height-uuid</value>", "distro", "web", "openmrs_config", "globalproperties", "gp.xml");
assertFileContains("<value>weight-uuid</value>", "distro", "web", "openmrs_config", "globalproperties", "gp.xml");
assertFilePresent("distro", "web", "openmrs_spa", "config.json");
assertFileContains("\"heightUuid\": \"height-uuid\"", "distro", "web", "openmrs_spa", "config.json");
assertFileContains("\"weightUuid\": \"weight-uuid\"", "distro", "web", "openmrs_spa", "config.json");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void installFromDistroProperties(File appDataDir, DistroProperties distro
installFromDistroProperties(appDataDir, distroProperties, false, null);
}

public void installFromDistroProperties(File appDataDir, DistroProperties distroProperties, boolean ignorePeerDependencies, Boolean overrideReuseNodeCache)
throws MojoExecutionException {
public void installFromDistroProperties(File appDataDir, DistroProperties distroProperties, boolean ignorePeerDependencies, Boolean overrideReuseNodeCache) throws MojoExecutionException {

File buildTargetDir = new File(appDataDir, SDKConstants.OPENMRS_SERVER_FRONTEND);
if (buildTargetDir.exists()) {
Expand All @@ -78,6 +77,8 @@ public void installFromDistroProperties(File appDataDir, DistroProperties distro
}
}

contentHelper.installFrontendConfig(distroProperties, buildTargetDir);

Map<String, String> spaArtifactProperties = distroProperties.getSpaArtifactProperties();
Map<String, String> spaBuildProperties = distroProperties.getSpaBuildProperties();
if (!spaArtifactProperties.isEmpty() && !spaBuildProperties.isEmpty()) {
Expand Down Expand Up @@ -139,21 +140,10 @@ public void installFromDistroProperties(File appDataDir, DistroProperties distro

String program = "openmrs@" + coreVersion;
String legacyPeerDeps = ignorePeerDependencies ? "--legacy-peer-deps" : "";
// print frontend tool version number
nodeHelper.runNpx(String.format("%s --version", program), legacyPeerDeps);

if (distroProperties.getContentPackages().isEmpty()) {
nodeHelper.runNpx(String.format("%s assemble --target %s --mode config --config %s", program, buildTargetDir,
spaConfigFile), legacyPeerDeps);
} else {
try (TempDirectory configFileDir = TempDirectory.create("content-frontend-config")) {
List<File> configFiles = contentHelper.installFrontendConfig(distroProperties, configFileDir.getFile());
String assembleCommand = assembleWithFrontendConfig(program, buildTargetDir, configFiles, spaConfigFile);
nodeHelper.runNpx(assembleCommand, legacyPeerDeps);
}
}
nodeHelper.runNpx(
String.format("%s build --target %s --build-config %s", program, buildTargetDir, spaConfigFile), legacyPeerDeps);
nodeHelper.runNpx(String.format("%s --version", program), legacyPeerDeps); // print frontend tool version number
nodeHelper.runNpx(String.format("%s assemble --target %s --mode config --config %s", program, buildTargetDir, spaConfigFile), legacyPeerDeps);
nodeHelper.runNpx(String.format("%s build --target %s --build-config %s", program, buildTargetDir, spaConfigFile), legacyPeerDeps);

Path nodeCache = NodeHelper.tempDir;
if (!reuseNodeCache) {
Expand All @@ -167,22 +157,6 @@ public void installFromDistroProperties(File appDataDir, DistroProperties distro
}
}

private String assembleWithFrontendConfig(String program, File buildTargetDir, List<File> configFiles, File spaConfigFile) {
StringBuilder command = new StringBuilder();
command.append(program)
.append(" assemble --target ")
.append(buildTargetDir)
.append(" --mode config --config ")
.append(spaConfigFile);

for (File configFile : configFiles) {
command.append(" --config-file ").append(configFile.getAbsolutePath());
}

return command.toString();
}


private Map<String, Object> convertPropertiesToJSON(Map<String, String> properties) throws MojoExecutionException {
Set<String> foundPropertySetKeys = new HashSet<>();
Map<String, Object> result = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class SpaInstallerTest {
@Mock
Wizard wizard;

@Mock
ContentHelper contentHelper;

@Mock
NodeHelper nodeHelper;

Expand All @@ -51,6 +54,7 @@ public void setup() throws Exception {
spaInstaller = new SpaInstaller();
spaInstaller.setModuleInstaller(moduleInstaller);
spaInstaller.setNodeHelper(nodeHelper);
spaInstaller.setContentHelper(contentHelper);
spaInstaller.setWizard(wizard);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,9 @@ void installBackendConfig(ContentPackage contentPackage, Map<String, String> var
backendDir = tempDirectory.getFile();
}

// Apply variable replacements to the downloaded files
// Apply variable replacements to the downloaded files and copy into the installDir
applyVariableReplacements(vars, backendDir);

// If a namespace is passed in, then install backend configurations into namespaced subdirectories for each domain
String namespace = contentPackage.getNamespace();
boolean emptyNamespace = StringUtils.isBlank(namespace) || namespace.equals(".") || namespace.equals("/") || namespace.equals("false");
if (emptyNamespace) {
log.debug("Copying " + backendDir + " to " + installDir);
FileUtils.copyDirectory(backendDir, installDir);
}
else {
File[] configFiles = backendDir.listFiles();
if (configFiles != null) {
for (File configFile : configFiles) {
if (configFile.isDirectory()) {
Path namespacedConfigDir = installDir.toPath().resolve(configFile.getName()).resolve(contentPackage.getNamespace());
Files.createDirectories(namespacedConfigDir);
log.debug("Copying " + configFile + " to " + namespacedConfigDir);
FileUtils.copyDirectory(configFile, namespacedConfigDir.toFile());
}
else {
log.debug("Copying " + configFile + " to " + installDir);
FileUtils.copyFile(configFile, new File(installDir, configFile.getName()));
}
}
}
}
copyDirectory(backendDir, installDir, contentPackage.getNamespace());
}
catch (IOException e) {
throw new MojoExecutionException("Unable to install backend configuration to " + installDir, e);
Expand All @@ -171,15 +147,13 @@ void installBackendConfig(ContentPackage contentPackage, Map<String, String> var
* This installs the frontend configuration for all content packages defined in the distribution
* Installation is done in the order in which they should be installed, with packages that are dependencies of other packages installed first
*/
public List<File> installFrontendConfig(DistroProperties distroProperties, File installDir) throws MojoExecutionException {
public void installFrontendConfig(DistroProperties distroProperties, File installDir) throws MojoExecutionException {
log.debug("Installing frontend configuration for content packages in distribution");
List<File> files = new ArrayList<>();
for (ContentPackage contentPackage : getContentPackagesInInstallationOrder(distroProperties)) {
log.debug("Installing content package " + contentPackage.getGroupIdAndArtifactId());
Map<String, String> vars = getReplacementVariables(distroProperties, contentPackage);
files.addAll(installFrontendConfig(contentPackage, vars, installDir));
installFrontendConfig(contentPackage, vars, installDir);
}
return files;
}

/**
Expand All @@ -188,10 +162,9 @@ public List<File> installFrontendConfig(DistroProperties distroProperties, File
* To account for these, this will also attempt to look for frontend_config, if the preferred directory does not exist
* Any text files that contain variable references will have these references populated based on the given vars map
*/
List<File> installFrontendConfig(ContentPackage contentPackage, Map<String, String> vars, File installDir) throws MojoExecutionException {
void installFrontendConfig(ContentPackage contentPackage, Map<String, String> vars, File installDir) throws MojoExecutionException {
log.debug("Installing frontend configuration for " + contentPackage + " to " + installDir);
Artifact artifact = contentPackage.getArtifact();
List<File> ret = new ArrayList<>();
try (TempDirectory tempDirectory = TempDirectory.create(artifact.getArtifactId() + "-content-package")) {
mavenEnvironment.getArtifactHelper().downloadArtifact(artifact, tempDirectory.getFile(), true);

Expand All @@ -201,28 +174,43 @@ List<File> installFrontendConfig(ContentPackage contentPackage, Map<String, Stri
frontendDir = tempDirectory.getPath().resolve("configs").resolve("frontend_config").toFile();
}

// Apply variable replacements to the downloaded files
// Apply variable replacements to the downloaded files and copy into the installDir
applyVariableReplacements(vars, frontendDir);

// If a frontend directory is found, copy files within it to target directory, in a subdirectory for the current content package
if (frontendDir.exists() && frontendDir.isDirectory()) {
File targetDir = new File(installDir, artifact.getArtifactId());
Files.createDirectory(targetDir.toPath());
log.debug("Copying " + frontendDir + " to " + targetDir);
FileUtils.copyDirectory(frontendDir, targetDir);
ret = Arrays.asList(Objects.requireNonNull(targetDir.listFiles()));
}
else {
log.warn("No frontend configuration found in content package");
}

return ret;
copyDirectory(frontendDir, installDir, contentPackage.getNamespace());
}
catch (IOException e) {
throw new MojoExecutionException("Unable to install frontend configuration to " + installDir, e);
}
}

/**
* Copies the contents of the sourceDir into the targetDir.
* If namespace is passed in, files are added to a namespaced subdirectory within each copied directory
*/
void copyDirectory(File sourceDir, File targetDir, String namespace) throws IOException {
boolean emptyNamespace = StringUtils.isBlank(namespace) || namespace.equals(".") || namespace.equals("/") || namespace.equals("false");
if (emptyNamespace) {
log.debug("Copying " + sourceDir + " to " + targetDir);
FileUtils.copyDirectory(sourceDir, targetDir);
}
else {
File[] configFiles = sourceDir.listFiles();
if (configFiles != null) {
for (File configFile : configFiles) {
if (configFile.isDirectory()) {
Path namespacedConfigDir = targetDir.toPath().resolve(configFile.getName()).resolve(namespace);
Files.createDirectories(namespacedConfigDir);
log.debug("Copying " + configFile + " to " + namespacedConfigDir);
FileUtils.copyDirectory(configFile, namespacedConfigDir.toFile());
}
else {
log.debug("Copying " + configFile + " to " + targetDir);
FileUtils.copyFile(configFile, new File(targetDir, configFile.getName()));
}
}
}
}
}

/**
* This returns all values that will be used to replace variable references in files contained within the given content package for the given distribution
Expand Down

0 comments on commit 2daceaa

Please sign in to comment.