-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathdotenv.gradle
More file actions
106 lines (89 loc) · 3.37 KB
/
dotenv.gradle
File metadata and controls
106 lines (89 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.regex.Matcher
import java.util.regex.Pattern
// might need to add better flavor detection
// https://stackoverflow.com/questions/30621183/how-to-get-current-flavor-in-gradle
def getCurrentFlavor() {
Gradle gradle = getGradle()
// match optional modules followed by the task
// (?:.*:)* is a non-capturing group to skip any :foo:bar: if they exist
// *[a-z]+([A-Za-z0-9]+) will capture the flavor part of the task name onward (e.g., assembleRelease --> Release)
def pattern = Pattern.compile("(?:.*:)*[a-z]+([A-Z][A-Za-z0-9]+)")
def flavor = ""
gradle.getStartParameter().getTaskNames().any { name ->
Matcher matcher = pattern.matcher(name)
if (matcher.find()) {
flavor = matcher.group(1).toLowerCase()
return true
}
}
return flavor
}
def loadDotEnv(flavor = getCurrentFlavor()) {
def envFile = ".env"
if (System.env['ENVFILE']) {
envFile = System.env['ENVFILE']
} else if (System.getProperty('ENVFILE')) {
envFile = System.getProperty('ENVFILE')
} else if (project.hasProperty("envConfigFiles")) {
// use startsWith because sometimes the task is "generateDebugSources", so we want to match "debug"
project.ext.envConfigFiles.any { pair ->
if (flavor.startsWith(pair.key)) {
envFile = pair.value
return true
}
}
} else if (project.hasProperty("defaultEnvFile")) {
envFile = project.defaultEnvFile
}
def env = [:]
println("Reading env from: $envFile")
File f = new File("$project.rootDir/../$envFile");
if (!f.exists()) {
f = new File("$envFile");
}
if (f.exists()) {
f.eachLine { line ->
def matcher = (line =~ /^\s*(?:export\s+|)([\w\d\.\-_]+)\s*=\s*['"]?(.*?)?['"]?\s*$/)
if (matcher.getCount() == 1 && matcher[0].size() == 3) {
env.put(matcher[0][1], matcher[0][2].replace('"', '\\"'))
}
}
} else {
println("**************************")
println("*** Missing .env file ****")
println("**************************")
}
project.ext.set("env", env)
}
loadDotEnv()
android {
if (project.android.hasProperty('namespace')) {
namespace 'com.byneapp.flutter_config'
}
defaultConfig {
project.env.each { k, v ->
def escaped = v.replaceAll("%", "\\\\u0025")
buildConfigField "String", k, "\"$v\""
resValue "string", k, "$escaped"
}
}
}
tasks.whenTaskAdded { task ->
if (project.hasProperty("envConfigFiles")) {
project.envConfigFiles.each { envConfigName, envConfigFile ->
if (task.name.toLowerCase() == "generate" + envConfigName + "buildconfig") {
android.applicationVariants.all { variant ->
def variantConfigString = variant.getName()
if (envConfigName.contains(variantConfigString.toLowerCase())) {
loadDotEnv(envConfigName)
project.env.each { k, v ->
def escaped = v.replaceAll("%", "\\\\u0025")
variant.buildConfigField "String", k, "\"$v\""
variant.resValue "string", k, "$escaped"
}
}
}
}
}
}
}