diff --git a/android/dotenv.gradle b/android/dotenv.gradle index 142b45c..22aabcc 100644 --- a/android/dotenv.gradle +++ b/android/dotenv.gradle @@ -1,27 +1,4 @@ -import java.util.regex.Matcher -import java.util.regex.Pattern - -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-z]+) will capture the flavor part of the task name onward (e.g., assembleRelease --> Release) - def pattern = Pattern.compile("(?:.*:)*[a-z]+([A-Z][A-Za-z]+)") - 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 readDotEnv = { +def loadDotEnv(currentFlavor) { def envFile = ".env" if (project.hasProperty("defaultEnvFile")) { @@ -31,11 +8,10 @@ def readDotEnv = { if (System.env['ENVFILE']) { envFile = System.env['ENVFILE']; } else if (project.hasProperty("envConfigFiles")) { - def flavor = getCurrentFlavor() // use startsWith because sometimes the task is "generateDebugSources", so we want to match "debug" project.ext.envConfigFiles.any { pair -> - if (flavor.startsWith(pair.key)) { + if (currentFlavor.startsWith(pair.key)) { envFile = pair.value return true } @@ -47,7 +23,7 @@ def readDotEnv = { File f = new File("$project.rootDir/../$envFile"); if (!f.exists()) { - f = new File("$envFile"); + f = new File("$envFile"); } if (f.exists()) { @@ -66,7 +42,7 @@ def readDotEnv = { project.ext.set("env", env) } -readDotEnv() +loadDotEnv('.env') android { defaultConfig { @@ -77,3 +53,26 @@ android { } } } + +tasks.whenTaskAdded { task -> + if (project.hasProperty("envConfigFiles")) { + project.envConfigFiles.each { envConfigName, envConfigFile -> + if (task.name.toLowerCase() == "generate"+envConfigName+"buildconfig") { + task.doFirst() { + android.applicationVariants.all { variant -> + def variantConfigString = variant.getVariantData().getVariantConfiguration().getFullName() + 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\"" + } + } + } + } + } + } + } +} + diff --git a/ios/ReactNativeConfig/BuildDotenvConfig.ruby b/ios/ReactNativeConfig/BuildDotenvConfig.ruby index e240004..a447f7e 100755 --- a/ios/ReactNativeConfig/BuildDotenvConfig.ruby +++ b/ios/ReactNativeConfig/BuildDotenvConfig.ruby @@ -9,6 +9,7 @@ Encoding.default_internal = Encoding::UTF_8 if File.exists?("/tmp/envfile") custom_env = true file = File.read("/tmp/envfile").strip + defaultEnvFile = ".env" else custom_env = false file = ENV["ENVFILE"] || ".env" @@ -27,6 +28,19 @@ dotenv = begin path = file end raw = File.read(path) + + if (defaultEnvFile) + defaultEnvPath = File.join(Dir.pwd, "../../../#{defaultEnvFile}") + if !File.exists?(defaultEnvPath) + # try as absolute path + defaultEnvPath = defaultEnvFile + end + defaultRaw = File.read(defaultEnvPath) + if (defaultRaw) + raw = defaultRaw + "\n" + raw + end + end + raw.split("\n").inject({}) do |h, line| m = line.match(dotenv_pattern) next h if m.nil?