Skip to content

Commit

Permalink
patch: fixes and improvements to the extension (#2)
Browse files Browse the repository at this point in the history
* Now we're using DotEnvPluginExtension as custom class to provide better completion support.
* Support from java 11 and higher.
* Now we also support empty and quoted values
  • Loading branch information
Im-Fran authored Dec 22, 2023
1 parent c212dce commit c3d9bcb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +22,12 @@ plugins {
}
```

Make sure to change the version by the current version:<br/>
![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.
Expand Down
23 changes: 17 additions & 6 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
`java-gradle-plugin`
alias(libs.plugins.jvm)

id("com.gradle.plugin-publish") version "1.2.1"
}

Expand All @@ -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")
}
}
Expand All @@ -45,10 +44,22 @@ val functionalTest by tasks.registering(Test::class) {

gradlePlugin.testSourceSets.add(functionalTestSourceSet)

tasks.named<Task>("check") {
dependsOn(functionalTest)
tasks {
named<Task>("check") {
dependsOn(functionalTest)
}

named<Test>("test") {
useJUnitPlatform()
}
}

tasks.named<Test>("test") {
useJUnitPlatform()
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

sourceSets {
getByName("main").java.srcDirs("src/main/kotlin")
getByName("test").java.srcDirs("src/test/kotlin")
}
}
47 changes: 30 additions & 17 deletions plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt
Original file line number Diff line number Diff line change
@@ -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<String, String>()

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<String, String> = variables.toMap()
}
private fun determineSystemEnvFile(): String = (System.getenv("ENV") ?: System.getProperty("ENV"))?.let {
".env.${it.lowercase()}"
} ?: ".env"

fun get(key: String): String? = variables[key]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cl.franciscosolis.gradledotenv
import org.gradle.api.Project
import org.gradle.api.Plugin

class GradleDotEnvPlugin: Plugin<Project> {

class GradleDotEnvPlugin : Plugin<Project> {
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)
}

0 comments on commit c3d9bcb

Please sign in to comment.