-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: fixes and improvements to the extension (#2)
* 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
Showing
4 changed files
with
62 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 30 additions & 17 deletions
47
plugin/src/main/kotlin/cl/franciscosolis/gradledotenv/DotEnv.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters