Created
September 25, 2015 00:26
-
-
Save fchiron/42d7c204002d4eaf979f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
lazy val dotEnvFile = taskKey[String]("Defines the file to load environment variables from") | |
dotEnvFile := ".env" | |
// The task returns the loaded dotEnv file and a map of the loaded variables, in case it would need to be manipulated somewhere else | |
lazy val loadEnv = taskKey[(File, Map[String, String])]("""Loads the environment variables from file defined by task `dotEnvFile` (they must follow the format "export X=Y")""") | |
loadEnv := { | |
val logger = ConsoleLogger() | |
val sourceFileName = dotEnvFile.value | |
val sourceFile = file(sourceFileName) | |
val export = """^export\s+([^=]+)="?(.*?)"?$""".r | |
logger.info(s"""Loading environment variables from file "$sourceFileName" ...""") | |
val propertiesMap = scala.io.Source.fromFile(sourceFile).getLines() map { | |
_.trim | |
} flatMap { | |
case export(key, value) => | |
logger.info(s"""Loaded variable "$key"="$value"""") | |
Some((key, value)) | |
case _ => None | |
} toMap | |
propertiesMap.foreach((java.lang.System.setProperty _).tupled) | |
logger.info("Environment variables loaded.") | |
(sourceFile, propertiesMap) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment