diff --git a/build.gradle b/build.gradle index 26947c9d78..b93d05b9e9 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,8 @@ plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "1.1.8" // https://github.com/JetBrains/gradle-idea-ext-plugin } -apply from: './gradle/java.gradle' +apply from: "./gradle/java.gradle" +apply from: "gradle/property_loader.gradle" boolean dev = System.getenv('RELEASE') == null || System.getenv('RELEASE').equals('false') ext.buildNumber = System.getenv('BUILD_NUMBER') diff --git a/gradle/property_loader.gradle b/gradle/property_loader.gradle new file mode 100644 index 0000000000..0f28530156 --- /dev/null +++ b/gradle/property_loader.gradle @@ -0,0 +1,48 @@ +/* + This module can inject build properties from a JSON file. Each property in the + JSON file will be mapped to a build property using the key of that property. + Property keys ending with _comment will be skipped. + + If a secretFile property exists and points to a valid JSON file that file will + be automatically loaded. You can manually load a file using the loadProperties + method. +*/ +import groovy.json.JsonSlurper + +// Auto detects a secret file and injects it. +if (project.rootProject.hasProperty("secretFile")) { + project.logger.lifecycle("Automatically loading properties from the secretFile") + final def secretsFile = project.rootProject.file(project.rootProject.getProperty("secretFile")) + + if (secretsFile.exists() && secretsFile.name.endsWith(".json")) { + loadProperties(secretsFile) + } +} + +// Loads properties using a specified json file. +def loadProperties(propertyFile) { + if (propertyFile.exists()) { + propertyFile.withReader { + Map propMap = new JsonSlurper().parse it + + for (entry in propMap) { + + // Filter entries that use _comment in the key. + if (!entry.key.endsWith("_comment")) { + + project.ext.set(entry.key, entry.value) + } + } + + project.logger.lifecycle("Successfully loaded " + propMap.size() + " properties") + propMap.clear() + } + } else { + project.logger.warn("The property file " + propertyFile.getName() + " could not be loaded. It does not exist.") + } +} + +// Allows other scripts to use these methods. +ext { + loadProperties = this.&loadProperties +}