|
| 1 | +/* |
| 2 | + This module can inject build properties from a JSON file. Each property in the |
| 3 | + JSON file will be mapped to a build property using the key of that property. |
| 4 | + Property keys ending with _comment will be skipped. |
| 5 | +
|
| 6 | + If a secretFile property exists and points to a valid JSON file that file will |
| 7 | + be automatically loaded. You can manually load a file using the loadProperties |
| 8 | + method. |
| 9 | +*/ |
| 10 | +import groovy.json.JsonSlurper |
| 11 | + |
| 12 | +// Auto detects a secret file and injects it. |
| 13 | +if (project.rootProject.hasProperty("secretFile")) { |
| 14 | + project.logger.lifecycle("Automatically loading properties from the secretFile") |
| 15 | + final def secretsFile = project.rootProject.file(project.rootProject.getProperty("secretFile")) |
| 16 | + |
| 17 | + if (secretsFile.exists() && secretsFile.name.endsWith(".json")) { |
| 18 | + loadProperties(secretsFile) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Loads properties using a specified json file. |
| 23 | +def loadProperties(propertyFile) { |
| 24 | + if (propertyFile.exists()) { |
| 25 | + propertyFile.withReader { |
| 26 | + Map propMap = new JsonSlurper().parse it |
| 27 | + |
| 28 | + for (entry in propMap) { |
| 29 | + |
| 30 | + // Filter entries that use _comment in the key. |
| 31 | + if (!entry.key.endsWith("_comment")) { |
| 32 | + |
| 33 | + project.ext.set(entry.key, entry.value) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + project.logger.lifecycle("Successfully loaded " + propMap.size() + " properties") |
| 38 | + propMap.clear() |
| 39 | + } |
| 40 | + } else { |
| 41 | + project.logger.warn("The property file " + propertyFile.getName() + " could not be loaded. It does not exist.") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Allows other scripts to use these methods. |
| 46 | +ext { |
| 47 | + loadProperties = this.&loadProperties |
| 48 | +} |
0 commit comments