react-native-config/android/dotenv.gradle

60 lines
1.6 KiB
Groovy

import java.util.regex.Matcher
import java.util.regex.Pattern
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests()[0].args
Matcher matcher = Pattern.compile("([A-Z]\\w+)").matcher( tskReqStr )
if( matcher.find() ) {
return matcher.group(1).toLowerCase()
} else {
return "";
}
}
def readDotEnv = {
def envFile = ".env"
if (project.hasProperty("defaultEnvFile")) {
envFile = project.defaultEnvFile
}
if (System.env['ENVFILE']) {
envFile = System.env['ENVFILE'];
} else if (project.hasProperty("envConfigFiles")) {
def possibleFile = project.envConfigFiles.get(getCurrentFlavor())
if (possibleFile) {
envFile = possibleFile;
}
}
def env = [:]
println("Reading env from: $envFile")
try {
new File("$project.rootDir/../$envFile").eachLine { line ->
def matcher = (line =~ /^\s*([\w\d\.\-_]+)\s*=\s*(.*)?\s*$/)
if (matcher.getCount() == 1 && matcher[0].size() == 3){
env.put(matcher[0][1], matcher[0][2])
}
}
} catch (FileNotFoundException ex) {
println("**************************")
println("*** Missing $envFile file ****")
println("**************************")
}
project.ext.set("env", env)
}
readDotEnv()
android {
defaultConfig {
project.env.each { k, v ->
def escaped = v.replaceAll("%","\\\\u0025")
buildConfigField "String", k, "\"$v\""
resValue "string", k, "\"$escaped\""
}
}
}