diff --git a/examples/basic/build.gradle.kts b/examples/basic/build.gradle.kts index ff5b1b9..d131d1f 100644 --- a/examples/basic/build.gradle.kts +++ b/examples/basic/build.gradle.kts @@ -11,3 +11,6 @@ try { println("example print: ${e.message}") } println(env.QUX.value) + +// All environment variables which are merged with variables specified in .env files. +env.allVariables diff --git a/examples/basic/settings.gradle.kts b/examples/basic/settings.gradle.kts index 7ebc927..6e33b56 100644 --- a/examples/basic/settings.gradle.kts +++ b/examples/basic/settings.gradle.kts @@ -1,7 +1,7 @@ pluginManagement { repositories { - gradlePluginPortal() mavenLocal() + gradlePluginPortal() } } diff --git a/examples/basic/sub/build.gradle.kts b/examples/basic/sub/build.gradle.kts index c6ae2da..47ff8f9 100644 --- a/examples/basic/sub/build.gradle.kts +++ b/examples/basic/sub/build.gradle.kts @@ -6,3 +6,6 @@ try { println("[$name] example print: ${e.message}") } println("""[$name] ${env.QUX.value}""") + +// All environment variables which are merged with variables specified in .env files. +env.allVariables diff --git a/plugin/src/main/kotlin/co/uzzu/dotenv/EnvProviders.kt b/plugin/src/main/kotlin/co/uzzu/dotenv/EnvProviders.kt index 53335ff..0198c32 100644 --- a/plugin/src/main/kotlin/co/uzzu/dotenv/EnvProviders.kt +++ b/plugin/src/main/kotlin/co/uzzu/dotenv/EnvProviders.kt @@ -20,5 +20,5 @@ interface EnvProvider { */ class SystemEnvProvider : EnvProvider { override fun getenv(name: String): String? = System.getenv(name) - override fun getenv(): Map = System.getenv() + override fun getenv(): Map = System.getenv().filterValues { it != null } } diff --git a/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt b/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt index 864f5bb..02171ec 100644 --- a/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt +++ b/plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt @@ -2,16 +2,33 @@ package co.uzzu.dotenv.gradle import co.uzzu.dotenv.EnvProvider +/** + * This object will be added as extension which named "env" to the Project. + */ open class DotEnvRoot( private val envProvider: EnvProvider, - private val map: Map + private val dotenvMap: Map ) { + /** + * @return All environment variables which are merged with variables specified in .env files. + */ + val allVariables: Map + get() { + val results = envProvider.getenv().toMutableMap() + dotenvMap.forEach { (key, value) -> + if (value != null && results[key] == null) { + results[key] = value + } + } + return results.toMap() + } + /** * @return Indicates an environment variable with specified name is present */ fun isPresent(name: String): Boolean = envProvider.getenv()[name]?.let { true } - ?: map[name]?.let { true } + ?: dotenvMap[name]?.let { true } ?: false /** @@ -20,7 +37,7 @@ open class DotEnvRoot( */ fun fetch(name: String) = envProvider.getenv()[name] - ?: map[name] + ?: dotenvMap[name] ?: throw IllegalStateException("""Environment variable $name was not set.""") /** @@ -29,7 +46,7 @@ open class DotEnvRoot( */ fun fetch(name: String, defaultValue: String) = envProvider.getenv()[name] - ?: map[name] + ?: dotenvMap[name] ?: defaultValue /** @@ -37,9 +54,13 @@ open class DotEnvRoot( */ fun fetchOrNull(name: String): String? = envProvider.getenv()[name] - ?: map[name] + ?: dotenvMap[name] } +/** + * This object has environment variables defined on .env file. + * This object will be added as extension which named environment variable name, to the DotEnv object. + */ open class DotEnvProperty( private val envProvider: EnvProvider, private val name: String, diff --git a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt index 371dca9..7505281 100644 --- a/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt +++ b/plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt @@ -7,9 +7,9 @@ import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll class DotEnvRootTest { - @Test fun isPresentByEnv() { val envProvider = InMemoryEnvProvider(mapOf("FOO" to "env")) @@ -93,4 +93,32 @@ class DotEnvRootTest { val root = DotEnvRoot(envProvider, mapOf()) assertNull(root.fetchOrNull("FOO")) } + + @Test + fun allVariables() { + val envProvider = InMemoryEnvProvider( + mapOf( + "FOO" to "env", + "BAR" to "env" + ) + ) + val root = DotEnvRoot( + envProvider, + mapOf( + "BAR" to "dotenv", + "BAZ" to "dotenv", + "QUX" to "dotenv", + "QUUX" to null + ) + ) + val allVariables = root.allVariables + assertAll( + { assertEquals("env", allVariables["FOO"]) }, + { assertEquals("env", allVariables["BAR"]) }, + { assertEquals("dotenv", allVariables["BAZ"]) }, + { assertEquals("dotenv", allVariables["QUX"]) }, + { assertEquals(null, allVariables["QUUX"]) }, + { assertEquals(null, allVariables["CORGE"]) } + ) + } }