Skip to content

Commit

Permalink
Merge pull request #189 from Philippus/update-scalafmt-settings
Browse files Browse the repository at this point in the history
Update scalafmt settings and run scalafmt
  • Loading branch information
Philippus authored Sep 25, 2023
2 parents c478567 + 29ce490 commit ea723d7
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 127 deletions.
10 changes: 10 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
version = 3.7.14

align.preset = most
literals.double = Upper
literals.float = Upper
maxColumn = 120
newlines.source = keep
rewriteTokens = {
"⇒": "=>"
"→": "->"
"←": "<-"
}
runner.dialect = scala212
12 changes: 6 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name := "sbt-dotenv"
organization := "nl.gn0s1s"
description := "An sbt plugin to load environment variables from .env into the JVM System Environment for local development. Assists with 'Twelve Factor App' development principle 3 'Store config in the environment'."
startYear := Some(2014)
homepage := Some(url("https://github.com/philippus/sbt-dotenv"))
name := "sbt-dotenv"
organization := "nl.gn0s1s"
description := "An sbt plugin to load environment variables from .env into the JVM System Environment for local development. Assists with 'Twelve Factor App' development principle 3 'Store config in the environment'."
startYear := Some(2014)
homepage := Some(url("https://github.com/philippus/sbt-dotenv"))
licenses += ("MIT" -> url("http://opensource.org/licenses/MIT"))

developers := List(
Expand All @@ -21,7 +21,7 @@ developers := List(
)

enablePlugins(SbtPlugin)
sbtPlugin := true
sbtPlugin := true
pluginCrossBuild / sbtVersion := "1.3.9" // minimum version we target because of using Native.load, see https://github.com/Philippus/sbt-dotenv/issues/81

libraryDependencies ++= Seq(
Expand Down
42 changes: 18 additions & 24 deletions src/main/scala/au/com/onegeek/sbtdotenv/DirtyEnvironmentHack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
*
* Copyright (c) 2014 Matt Fellows (OneGeek)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package au.com.onegeek.sbtdotenv

Expand All @@ -28,8 +23,7 @@ import scala.util.control.NonFatal

/** Rewrite the runtime Environment, embedding entries from the .env file.
*
* Taken from:
* http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java/7201825#7201825
* Taken from: http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java/7201825#7201825
*
* Created by mfellows on 20/07/2014.
*/
Expand All @@ -42,7 +36,7 @@ object DirtyEnvironmentHack {
val theEnvironmentField =
processEnvironmentClass.getDeclaredField("theEnvironment")
theEnvironmentField.setAccessible(true)
val env = theEnvironmentField
val env = theEnvironmentField
.get(null)
.asInstanceOf[java.util.Map[String, String]] // scalastyle:off null
env.putAll(newEnv)
Expand All @@ -52,33 +46,33 @@ object DirtyEnvironmentHack {
"theCaseInsensitiveEnvironment"
)
theCaseInsensitiveEnvironmentField.setAccessible(true)
val ciEnv = theCaseInsensitiveEnvironmentField
val ciEnv = theCaseInsensitiveEnvironmentField
.get(null)
.asInstanceOf[java.util.Map[String, String]] // scalastyle:off null
ciEnv.putAll(newEnv)
}) match {
case Failure(_: NoSuchFieldException) =>
Try({
val classes = classOf[Collections].getDeclaredClasses
val env = System.getenv
val env = System.getenv
classes
.filter(_.getName == "java.util.Collections$UnmodifiableMap")
.foreach(cl => {
val field = cl.getDeclaredField("m")
field.setAccessible(true)
val map =
val map =
field.get(env).asInstanceOf[java.util.Map[String, String]]
map.clear()
map.putAll(newEnv)
})
}) match {
case Failure(NonFatal(e2)) =>
e2.printStackTrace()
case Success(_) =>
case Success(_) =>
}
case Failure(NonFatal(e1)) =>
case Failure(NonFatal(e1)) =>
e1.printStackTrace()
case Success(_) =>
case Success(_) =>
}
}
}
45 changes: 19 additions & 26 deletions src/main/scala/au/com/onegeek/sbtdotenv/SbtDotenv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
*
* Copyright (c) 2014 Matt Fellows (OneGeek)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package au.com.onegeek.sbtdotenv

Expand All @@ -28,17 +23,16 @@ import sbt._
import scala.collection.JavaConverters._
import scala.io.Source

/** sbt-dotenv - a dotenv (https://github.com/bkeepers/dotenv) implementation
* for Scala sbt.
/** sbt-dotenv - a dotenv (https://github.com/bkeepers/dotenv) implementation for Scala sbt.
*
* Reads a file
*/
object SbtDotenv extends AutoPlugin {

object autoImport {
lazy val envFileName =
lazy val envFileName =
settingKey[String]("The file name to define variables.")
lazy val envFromFile =
lazy val envFromFile =
taskKey[Map[String, String]]("Loads env configuration from file.")
def dotEnv(fileName: String) = (s: State) =>
configureEnvironment(s, fileName)
Expand All @@ -55,7 +49,7 @@ object SbtDotenv extends AutoPlugin {
// Automatically configure environment on load
override lazy val buildSettings =
Seq(
envFileName := ".env",
envFileName := ".env",
Global / onLoad := dotEnv((ThisBuild / envFileName).value)
.compose((Global / onLoad).value)
)
Expand Down Expand Up @@ -83,8 +77,8 @@ object SbtDotenv extends AutoPlugin {
state: State,
fileName: String
): Option[Map[String, String]] = {
val baseDirectory = state.configuration.baseDirectory
val filePath =
val baseDirectory = state.configuration.baseDirectory
val filePath =
if (fileName.startsWith("/")) fileName
else s"${baseDirectory}/${fileName}"
state.log.debug(s"Base directory: ${baseDirectory}")
Expand Down Expand Up @@ -117,8 +111,7 @@ object SbtDotenv extends AutoPlugin {
state
}

/** Parse provided file in .env format and return an immutable environment
* Map[String, String]
/** Parse provided file in .env format and return an immutable environment Map[String, String]
*
* @param file
* .env file to read
Expand Down Expand Up @@ -171,11 +164,11 @@ object SbtDotenv extends AutoPlugin {

private def removeQuotes(value: String): String = {
value.trim match {
case quoted if quoted.startsWith("'") && quoted.endsWith("'") =>
case quoted if quoted.startsWith("'") && quoted.endsWith("'") =>
quoted.substring(1, quoted.length - 1)
case quoted if quoted.startsWith("\"") && quoted.endsWith("\"") =>
quoted.substring(1, quoted.length - 1)
case unquoted => unquoted
case unquoted => unquoted
}
}

Expand Down
37 changes: 16 additions & 21 deletions src/main/scala/au/com/onegeek/sbtdotenv/VariableExpansion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
*
* Copyright (c) 2014 Matt Fellows (OneGeek)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package au.com.onegeek.sbtdotenv

Expand All @@ -31,8 +26,8 @@ object VariableExpansion {

/** Expand variables in the second map based on values in the first.
*
* This recognises variables of the form "$$FOO" and "$${FOO}", and supports
* multiple levels of reference and nesting (up to 3 times).
* This recognises variables of the form "$$FOO" and "$${FOO}", and supports multiple levels of reference and nesting
* (up to 3 times).
* @param defns
* Key/value map that will be used to expand any variable references
* @param vars
Expand Down Expand Up @@ -77,9 +72,9 @@ object VariableExpansion {
// $ and {} pair containing at least one character of anything but }
//
private val variableRegex: Regex = {
val start = "(?<=^|[^$])"
val start = "(?<=^|[^$])"
val braceless = "\\$([^{\\$]+?)\\b"
val braces = "\\$\\{([^\\}\\$]+)\\}"
val braces = "\\$\\{([^\\}\\$]+)\\}"
s"$start(?:$braceless|$braces)".r
}

Expand All @@ -91,8 +86,8 @@ object VariableExpansion {

private def expandVarsInValue(defns: Env, value: String): String = {
val replaceMatch: (Regex.Match => String) = m => {
val lookup = defns.get(variableName(m))
val fallback = m.matched
val lookup = defns.get(variableName(m))
val fallback = m.matched
val replacement = lookup.getOrElse(fallback)

Matcher.quoteReplacement(replacement)
Expand Down
33 changes: 14 additions & 19 deletions src/test/scala/au/com/onegeek/sbtdotenv/SbtDotenvSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
*
* Copyright (c) 2014 Matt Fellows (OneGeek)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package au.com.onegeek.sbtdotenv

Expand All @@ -32,7 +27,7 @@ class SbtDotenvSpec extends AnyWordSpec with Matchers {
"The plugin parser" should {
"do nothing if no .env file exists" in {
val file = new File("thisfilecannotexistunlessyoucreateit")
val map = SbtDotenv.parseFile(file)
val map = SbtDotenv.parseFile(file)
map should equal(None)
}

Expand All @@ -42,10 +37,10 @@ class SbtDotenvSpec extends AnyWordSpec with Matchers {
SbtDotenv.parseFile(file) should equal(
Some(
Map(
"EMPTY_VARIABLE" -> "",
"MONGO_PORT" -> "17017",
"EMPTY_VARIABLE" -> "",
"MONGO_PORT" -> "17017",
"COVERALLS_REPO_TOKEN" -> "aoeucaPDc2rvkFugUGlNaCGu3EOeoaeu63WLo5",
"MONGO_URL" -> "http://localhost:$MONGO_PORT/mongo#asdf"
"MONGO_URL" -> "http://localhost:$MONGO_PORT/mongo#asdf"
)
)
)
Expand Down
Loading

0 comments on commit ea723d7

Please sign in to comment.