Add support for specifying custom env files in gradle (#52)
* Add support for specifying env files in gradle * Update different environment selection for Android
This commit is contained in:
parent
b0f7eb10e8
commit
c6f12527fb
14
README.md
14
README.md
|
@ -82,7 +82,19 @@ By default react-native-config will read from `.env`, but you can change it when
|
||||||
|
|
||||||
#### Android
|
#### Android
|
||||||
|
|
||||||
To pick which file to use in Android, just set `ENVFILE` before building/running your app. For instance:
|
To pick which file to use in Android, set a variable in your `build.config` before the `apply from:`:
|
||||||
|
|
||||||
|
```
|
||||||
|
project.ext.envConfigFiles = [
|
||||||
|
debug: ".env.development",
|
||||||
|
release: ".env.production",
|
||||||
|
anyCustomBuildTypeName: ".env",
|
||||||
|
]
|
||||||
|
|
||||||
|
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, you can set `ENVFILE` before building/running your app. For instance:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ ENVFILE=.env.staging react-native run-android
|
$ ENVFILE=.env.staging react-native run-android
|
||||||
|
|
|
@ -1,6 +1,31 @@
|
||||||
|
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 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 = [:]
|
def env = [:]
|
||||||
def envFile = System.env['ENVFILE'] ?: ".env"
|
|
||||||
println("Reading env from: $envFile")
|
println("Reading env from: $envFile")
|
||||||
try {
|
try {
|
||||||
new File("$project.rootDir/../$envFile").eachLine { line ->
|
new File("$project.rootDir/../$envFile").eachLine { line ->
|
||||||
|
|
Loading…
Reference in New Issue