mirror of
https://github.com/status-im/react-native-config.git
synced 2025-02-24 04:48:16 +00:00
* Add support for specifying env files in gradle * Update different environment selection for Android
56 lines
1.4 KiB
Groovy
56 lines
1.4 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 (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 (key, val) = line.tokenize('=')
|
|
if (key && val && key.substring(0, 1) != "#") {
|
|
env.put(key, val)
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
println("**************************")
|
|
println("*** Missing .env 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\""
|
|
}
|
|
}
|
|
}
|