-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.gradle
241 lines (217 loc) · 8.07 KB
/
tasks.gradle
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import org.apache.tools.ant.taskdefs.condition.Os
ext.composeDir = "$buildDir/compose"
ext.composeFilesPattern = '**/compose/*.yml'
def propertiesFile = 'gradle.properties'
ext.isBBDDEnabled = project.hasProperty("docker_com.etendoerp.docker_db") ? project.property("docker_com.etendoerp.docker_db").toBoolean() : false
ext.isTomcatEnabled = project.hasProperty("docker_com.etendoerp.tomcat") ? project.property("docker_com.etendoerp.tomcat").toBoolean() : false
ext.envFile = { ->
def envFile = file("$composeDir/.env")
if (!envFile.parentFile.exists()) {
envFile.parentFile.mkdirs()
}
if (!file(propertiesFile).exists()) {
throw new GradleException("gradle.properties file not found")
}
def properties = new Properties()
file(propertiesFile).withInputStream { input ->
properties.load(input)
}
return [properties: properties, envFile: envFile]
}
/**
* Retrieves legacy properties from a configuration file located in the project's config directory.
* The configuration file is "Openbravo.properties".
* @return A Properties object containing the properties from the configuration file.
* If the file does not exist, an empty Properties object is returned.
*/
def getLegacyProperties = { ->
def configFile = file("$projectDir/config/").listFiles().find { (it.name == "Openbravo.properties") }
def properties = new Properties()
project.logger.info("Reading properties from: $configFile")
if (!configFile.exists()) {
project.logger.info("File not found: $configFile")
return properties
}
configFile.withInputStream { input ->
properties.load(input)
}
return properties
}
/**
* Converts a given key to a standardized format.
* The method performs the following transformations:
* 1. Inserts an underscore between lowercase and uppercase letters.
* 2. Converts the entire string to uppercase.
* 3. Replaces periods with underscores.
*
* @param key the input string to be standardized
* @return the standardized key as a string
*/
def standarizeKey(key) {
return key.replaceAll(/([a-z])([A-Z])/, '$1_$2').toUpperCase().replace('.', '_')
}
/**
* Task: generateEnvFile
*
* Description:
* Converts gradle.properties configurations into a .env file located in build/compose/.env.
*
* Group:
* Docker
*
* Details:
* - Reads properties from gradle.properties and legacy properties.
* - Writes these properties to a .env file, excluding the 'volumes.path' property.
* - Standardizes property keys before writing.
* - Ensures that legacy properties do not overwrite existing properties from gradle.properties.
* - Sets the VOLUMES_PATH in the .env file, defaulting to "$buildDir/../volumes" if not specified.
* - Sets the DOCKER_BBDD_PROFILE based on the isBBDDEnabled flag.
* - Creates the directory specified by VOLUMES_PATH if it does not exist.
*
* Outputs:
* - Generates a .env file at the specified location.
*/
task generateEnvFile {
description = 'Converts gradle.properties configurations into a .env file in build/compose/.env'
group = 'Docker'
doLast {
def volumesPathKey = 'volumes.path'
def props = envFile()
def env = props.envFile
def properties = props.properties
def legacyProperties = getLegacyProperties()
def keys = []
env.withWriter { writer ->
project.logger.debug("Generating .env file with properties: ")
properties.each { key, value ->
if (key == volumesPathKey) {
return
}
def envKey = standarizeKey(key.toString())
keys.add(envKey)
project.logger.debug("Writing property: $envKey=$value")
writer.write("${envKey}=${value}\n")
}
project.logger.debug("Generating .env file with legacy properties:")
legacyProperties.each { key, value ->
if (key == volumesPathKey) {
return
}
def envKey = standarizeKey(key.toString())
if (keys.contains(envKey)) {
project.logger.debug("Skipping legacy property: $envKey=$value due gradle.properties already contains it.")
}else{
project.logger.debug("Writing legacy property: $envKey=$value")
writer.write("${envKey}=${value}\n")
}
}
def volumesPath = properties.getProperty(volumesPathKey, "$buildDir/../volumes")
writer.write("VOLUMES_PATH=${volumesPath}\n")
if (isBBDDEnabled) {
writer.write("DOCKER_BBDD_PROFILE=default\n")
} else {
writer.write("DOCKER_BBDD_PROFILE=disabled\n")
}
new File(volumesPath).mkdirs()
}
println "Generated .env file at: $env"
}
}
task copyComposeFiles {
description = 'Finds all Docker Compose YAML files and copies them to build/compose'
group = 'Docker'
dependsOn 'generateEnvFile'
doLast {
def destDir = file(composeDir)
if (!destDir.exists()) {
destDir.mkdirs()
} else {
destDir.eachFile { file ->
if (file.name.endsWith(".yml")) {
file.delete()
}
}
}
destDir.mkdirs()
def dirs = ['modules', "${buildDir}/etendo/modules"]
dirs.each { dir ->
{
fileTree(dir: dir, include: composeFilesPattern).each { file ->
def module = file.name.replaceFirst(/.yml/, '')
if (file.name == "docker-compose.yml" ||
(project.hasProperty("docker_" + module) &&
project.property("docker_" + module) == "true")) {
println "Using Docker Compose file: $file"
copy {
from file
into destDir
}
} else {
println "Skipping Docker Compose file: $file"
}
}
}
}
destDir.eachFileMatch(~/.*\.yml/) { file ->
file.text = file.text.replace('{CONFIG_URL}', isTomcatEnabled ? "tomcat" : "host.docker.internal")
file.text = file.text.replace('{CONTEXT_NAME}', project.property("context.name").toString())
}
}
}
ext.executeDockerComposeCommand = { String action ->
def composeFiles = fileTree(dir: composeDir, include: '*.yml').files
if (composeFiles.size() <= 1) {
throw new GradleException("Currently, there are no Docker-enabled resources available.")
}
def command = ['docker', 'compose', "--profile", "default"]
composeFiles.each { file ->
command << '-f' << file.absolutePath
}
action.split(" ").each { arg ->
command << arg
}
println "Running: $command"
def os_cmd = []
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
os_cmd = ['cmd', '/c'] + command
} else {
def lastCommandFile = file("$composeDir/.last-command")
lastCommandFile.text = command.join(' ')
os_cmd = ['sh', "$composeDir/.last-command"]
}
exec {
commandLine os_cmd
}
}
task "resources.up" {
description = 'Runs docker compose to bring up the Docker services'
group = 'Docker'
dependsOn copyComposeFiles
doLast {
executeDockerComposeCommand('up -d')
}
}
task "resources.down" {
description = 'Runs docker compose to bring down the Docker services'
group = 'Docker'
dependsOn copyComposeFiles
doLast {
executeDockerComposeCommand('down')
}
}
task "resources.stop" {
description = 'Runs docker compose to stop the Docker services'
group = 'Docker'
dependsOn copyComposeFiles
doLast {
executeDockerComposeCommand('stop')
}
}
afterEvaluate {
tasks.named("smartbuild").configure { task ->
task.dependsOn("copyComposeFiles")
}
tasks.named("compile.complete").configure { task ->
task.dependsOn("copyComposeFiles")
}
}