-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbuild-openmrs-frontend.groovy
More file actions
114 lines (95 loc) · 5.51 KB
/
Copy pathbuild-openmrs-frontend.groovy
File metadata and controls
114 lines (95 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.nio.file.Paths
log.info("Checking for frontend customizations in ${Paths.get("${project.build.directory}", "openmrs_frontend", "spa-assemble-config.json").toString()}")
def refAppConfigFile = Paths.get("${project.build.directory}", "openmrs_frontend", "reference-application-spa-assemble-config.json").toFile()
def slurper = new groovy.json.JsonSlurper()
def refAppConfig = slurper.parse(refAppConfigFile)
def openmrsVersion = refAppConfig["coreVersion"] ?: "next"
def outputDirectory = "${project.groupId}" == "com.ozonehis" && "${project.artifactId}" == "ozone-distro" ?
"${project.build.directory}/${project.artifactId}-${project.version}/binaries/openmrs/frontend" :
"${project.build.directory}/${project.artifactId}-${project.version}/distro/binaries/openmrs/frontend"
def outputDirectoryFile = new File(outputDirectory)
def assembleCommand = "npx --legacy-peer-deps openmrs@${openmrsVersion} assemble --manifest --mode config --target ${outputDirectory} --config ${refAppConfigFile.getAbsolutePath()}"
log.info("Project: ${project.groupId}:${project.artifactId}")
// by default, we build the frontend as part of Ozone and only rebuild if there are local customizations
def shouldBuildFrontend = "${project.groupId}" == "com.ozonehis" && "${project.artifactId}" == "ozone-distro"
if (!shouldBuildFrontend) {
frontendCustomizationsFile = Paths.get("${project.build.directory}", "openmrs_frontend", "spa-assemble-config.json").toFile()
if (frontendCustomizationsFile.exists()) {
log.info("Found frontend customizations. Rebuilding frontend...")
assembleCommand += " --config ${frontendCustomizationsFile.getAbsolutePath()}"
shouldBuildFrontend = true
// Update the OpenMRS version to the one specified in the customizations file if it exists.
def newOpenmrsCoreVersion = slurper.parse(frontendCustomizationsFile)["coreVersion"] ?: openmrsVersion
// Update the OpenMRS version in the assemble command if it is different from the current version.
if (newOpenmrsCoreVersion != openmrsVersion) {
assembleCommand = assembleCommand.replace(openmrsVersion, newOpenmrsCoreVersion)
openmrsVersion = newOpenmrsCoreVersion
}
log.info("Using OpenMRS Frontend Core Version: ${openmrsVersion}")
}
}
if (shouldBuildFrontend) {
log.info("Cleaning ${outputDirectory}...")
if (outputDirectoryFile.exists()) {
outputDirectoryFile.eachFile(it -> it.delete())
outputDirectoryFile.eachDir(it -> { if (it.getName() != "ozone") { it.deleteDir() } })
}
def cleanupBuildToolArtifacts = {
["package.json", "package-lock.json"].each { fileName ->
def buildArtifactFile = new File(outputDirectory, fileName)
if (buildArtifactFile.exists() && !buildArtifactFile.delete()) {
log.warn("Failed to remove temporary frontend build artifact ${buildArtifactFile.getAbsolutePath()}")
}
}
def nodeModulesDirectory = new File(outputDirectory, "node_modules")
if (nodeModulesDirectory.exists() && !nodeModulesDirectory.deleteDir()) {
log.warn("Failed to remove temporary frontend dependency directory ${nodeModulesDirectory.getAbsolutePath()}")
}
}
log.info("Running assemble command...")
log.info(assembleCommand)
def assembleProcess = assembleCommand.execute()
assembleProcess.consumeProcessOutput(System.out, System.err)
assembleProcess.waitFor()
if (assembleProcess.exitValue() != 0) {
throw new RuntimeException("'openmrs assemble' step failed. See previous messages for details.")
}
log.info("Running build command...")
cleanupBuildToolArtifacts()
try {
def packageJsonFile = new File(outputDirectory, "package.json")
log.info("Writing package.json with openmrs dependency and overrides to ${packageJsonFile.getAbsolutePath()}...")
packageJsonFile.text = """\
{
"name": "openmrs-frontend-build",
"version": "1.0.0",
"dependencies": {
"openmrs": "${openmrsVersion}"
},
"overrides": {
"react-aria-components": "1.8.0"
}
}
"""
// Install openmrs and all dependencies locally so webpack can resolve overridden packages correctly.
log.info("Installing openmrs@${openmrsVersion} and dependencies with overrides in ${outputDirectory}...")
def installProcess = "npm install --legacy-peer-deps".execute(null, new File(outputDirectory))
installProcess.consumeProcessOutput(System.out, System.err)
installProcess.waitFor()
if (installProcess.exitValue() != 0) {
throw new RuntimeException("Failed to install openmrs and dependencies. See previous messages for details.")
}
log.info("Using OpenMRS Frontend Core Version for build: ${openmrsVersion}")
// Run the build using the locally installed openmrs binary so it picks up the local node_modules with overrides.
def buildProcess = "${outputDirectory}/node_modules/.bin/openmrs build --config-url \$SPA_CONFIG_URLS --default-locale \$SPA_DEFAULT_LOCALE --target ${outputDirectory}".execute()
buildProcess.consumeProcessOutput(System.out, System.err)
buildProcess.waitFor()
if (buildProcess.exitValue() != 0) {
throw new RuntimeException("'openmrs build' step failed. See previous messages for details.")
}
} finally {
cleanupBuildToolArtifacts()
}
} else {
log.info("No need to re-build the frontend detected")
}