diff --git a/README.md b/README.md
index 35a14dd..670e4e0 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,11 @@
# Gradle Dot Env Plugin
+![Java Version](https://img.shields.io/badge/Java-11%2B-blue?logo=java)
![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Im-Fran/GradleDotEnv/test.yml?logo=github&label=Workflow+Status)
![GitHub](https://img.shields.io/github/license/Im-Fran/GradleDotEnv?label=License)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/Im-Fran/GradleDotEnv?label=Release)
A Gradle plugin to load environment variables from a .env file.
+> **WARNING:** This plugin is still in development, so it may not work as expected, make sure to report any issues you find.
## Installation
```kotlin
@@ -20,6 +22,12 @@ plugins {
}
```
+Make sure to change the version by the current version:
+![GitHub release (latest by date)](https://img.shields.io/github/v/release/Im-Fran/GradleDotEnv?label=Release)
+
+You can also follow the [gradle plugin page instructions](https://plugins.gradle.org/plugin/cl.franciscosolis.gradledotenv#kotlin-usage).
+
+
## Usage
The plugin adds a map called `env` to the project's `extra` property.
This map contains all the environment variables loaded from the .env file and the system environment variables. The system environment variables have priority over the .env file variables.
diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts
index f69df54..76c6a3e 100644
--- a/plugin/build.gradle.kts
+++ b/plugin/build.gradle.kts
@@ -1,7 +1,6 @@
plugins {
`java-gradle-plugin`
alias(libs.plugins.jvm)
-
id("com.gradle.plugin-publish") version "1.2.1"
}
@@ -27,7 +26,7 @@ gradlePlugin {
implementationClass = "cl.franciscosolis.gradledotenv.GradleDotEnvPlugin"
displayName = "GradleDotEnv"
description = "Gradle plugin to load environment variables from .env files"
- version = "1.0.1"
+ version = project.version
tags = listOf("env", "dotenv", "envfile", "environment", "variables")
}
}
@@ -45,10 +44,22 @@ val functionalTest by tasks.registering(Test::class) {
gradlePlugin.testSourceSets.add(functionalTestSourceSet)
-tasks.named("check") {
- dependsOn(functionalTest)
+tasks {
+ named("check") {
+ dependsOn(functionalTest)
+ }
+
+ named("test") {
+ useJUnitPlatform()
+ }
}
-tasks.named("test") {
- useJUnitPlatform()
+configure {
+ sourceCompatibility = JavaVersion.VERSION_11
+ targetCompatibility = JavaVersion.VERSION_11
+
+ sourceSets {
+ getByName("main").java.srcDirs("src/main/kotlin")
+ getByName("test").java.srcDirs("src/test/kotlin")
+ }
}
diff --git a/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt b/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt
index 62ba69b..602e8b1 100644
--- a/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt
+++ b/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt
@@ -1,32 +1,45 @@
package cl.franciscosolis.gradledotenv
+import org.gradle.api.Project
import java.io.File
import java.nio.charset.Charset
-class DotEnv {
+class DotEnv(project: Project) {
private val variables = mutableMapOf()
init {
- val environment = System.getenv("ENV") ?: System.getProperty("ENV")
- val file = File(".env.${environment?.lowercase() ?: ""}").let {
- if (it.exists()) {
- it
- } else {
- File(".env")
- }
+ val envFile = File(project.rootDir, determineSystemEnvFile())
+ if(envFile.exists()) {
+ loadEnvFile(envFile)
}
- if (file.exists()) {
- file.readLines(Charset.defaultCharset())
- .filter { it.isNotBlank() && !it.startsWith(';') && !it.startsWith('#') }.associateTo(variables) {
- val (key, value) = it.split("=")
- key to value
+ variables.putAll(System.getenv())
+ }
+
+ private fun loadEnvFile(file: File) {
+ file.readLines(Charset.defaultCharset())
+ .filter { it.isNotBlank() && !it.startsWith(';') && !it.startsWith('#') }
+ .forEach { line ->
+ val keyValue = line.split("=", limit = 2)
+ if (keyValue.size == 2) {
+ val (key, value) = keyValue
+ variables[key.trim()] = removeQuotes(value.trim())
+ } else if (keyValue.size == 1) {
+ variables[keyValue[0].trim()] = ""
+ }
}
- }
+ }
- variables.putAll(System.getenv())
+ private fun removeQuotes(value: String): String = if (value.startsWith('"') && value.endsWith('"')) {
+ value.substring(1, value.length - 1)
+ } else {
+ value
}
- fun environments(): Map = variables.toMap()
-}
\ No newline at end of file
+ private fun determineSystemEnvFile(): String = (System.getenv("ENV") ?: System.getProperty("ENV"))?.let {
+ ".env.${it.lowercase()}"
+ } ?: ".env"
+
+ fun get(key: String): String? = variables[key]
+}
diff --git a/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/GradleDotEnvPlugin.kt b/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/GradleDotEnvPlugin.kt
index 0068ed5..87ecc38 100644
--- a/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/GradleDotEnvPlugin.kt
+++ b/plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/GradleDotEnvPlugin.kt
@@ -3,13 +3,14 @@ package cl.franciscosolis.gradledotenv
import org.gradle.api.Project
import org.gradle.api.Plugin
-class GradleDotEnvPlugin: Plugin {
-
+class GradleDotEnvPlugin : Plugin {
override fun apply(project: Project) {
- // Read the all env variables from the '.env' file at the root of the project and from the system environment variables.
- val env = DotEnv()
+ val env = DotEnv(project)
- // Add the 'env' extension to the project.
- project.extensions.add("env", env.environments())
+ project.extensions.create("env", DotEnvPluginExtension::class.java, env)
}
}
+
+open class DotEnvPluginExtension(private val env: DotEnv) {
+ operator fun get(key: String): String? = env.get(key)
+}