Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allVariables property to DotEnvRoot extension #5

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/basic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/basic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
gradlePluginPortal()
}
}

Expand Down
3 changes: 3 additions & 0 deletions examples/basic/sub/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion plugin/src/main/kotlin/co/uzzu/dotenv/EnvProviders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface EnvProvider {
*/
class SystemEnvProvider : EnvProvider {
override fun getenv(name: String): String? = System.getenv(name)
override fun getenv(): Map<String, String> = System.getenv()
override fun getenv(): Map<String, String> = System.getenv().filterValues { it != null }
}
31 changes: 26 additions & 5 deletions plugin/src/main/kotlin/co/uzzu/dotenv/gradle/DotEnvProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>
private val dotenvMap: Map<String, String?>
) {
/**
* @return All environment variables which are merged with variables specified in .env files.
*/
val allVariables: Map<String, String>
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

/**
Expand All @@ -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.""")

/**
Expand All @@ -29,17 +46,21 @@ open class DotEnvRoot(
*/
fun fetch(name: String, defaultValue: String) =
envProvider.getenv()[name]
?: map[name]
?: dotenvMap[name]
?: defaultValue

/**
* @return An environment variable. If it was not set, returns specified default value
*/
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,
Expand Down
30 changes: 29 additions & 1 deletion plugin/src/test/kotlin/co/uzzu/dotenv/gradle/DotEnvRootTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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"]) }
)
}
}