Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ cs
.metals/
.bloop/
.idea*
.bsp
1 change: 0 additions & 1 deletion .mill-version

This file was deleted.

2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = 3.4.3
version = "3.8.3"
runner.dialect = scala213source3
50 changes: 29 additions & 21 deletions build.sc
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
//| mill-version: 1.0.3

// -*- mode: scala -*-
package build

import mill._, os._, scalalib._, publish._
import mill.*, os.*, scalalib.*, publish.*
import mill.util.BuildInfo.{millVersion, millBinPlatform}
import scala.util.Properties

object meta {

val crossVersions = Seq("2.13.8")

implicit val wd: Path = pwd

def nonEmpty(s: String): Option[String] = s.trim match {
case v if v.isEmpty => None
case v => Some(v)
case v => Some(v)
}

val MILL_VERSION = Properties.propOrNull("MILL_VERSION")
val versionFromEnv = Properties.propOrNone("PUBLISH_VERSION")
val gitSha = nonEmpty(proc("git", "rev-parse", "--short", "HEAD").call().out.trim)
val gitTag = nonEmpty(proc("git", "tag", "-l", "-n0", "--points-at", "HEAD").call().out.trim)
val publishVersion = (versionFromEnv orElse gitTag orElse gitSha).getOrElse("latest")
val gitSha = nonEmpty(
os.call(Seq("git", "rev-parse", "--short", "HEAD")).out.text().trim()
)
val gitTag = nonEmpty(
os.call(Seq("git", "tag", "-l", "-n0", "--points-at", "HEAD"))
.out
.text()
.trim()
)
val publishVersion =
(versionFromEnv orElse gitTag orElse gitSha).getOrElse("latest")
}

object dotenv extends Cross[Dotenv](meta.crossVersions: _*)
class Dotenv(val crossScalaVersion: String) extends CrossScalaModule with PublishModule { self =>
def publishVersion = meta.publishVersion
object dotenv extends ScalaModule with PublishModule {

override def scalaVersion = "3.7.1"

def artifactName = "mill-dotenv"
override def publishVersion = meta.publishVersion

def pomSettings = PomSettings(
description = "mill support for twelve-factor apps. load environment variables from .env",
override def artifactName = "mill-dotenv"

override def pomSettings = PomSettings(
description =
"mill support for twelve-factor apps. load environment variables from .env",
organization = "com.github.vic",
url = "https://github.com/vic/mill-dotenv",
licenses = Seq(License.`Apache-2.0`),
Expand All @@ -38,11 +48,9 @@ class Dotenv(val crossScalaVersion: String) extends CrossScalaModule with Publis
)
)

def compileIvyDeps = Agg(
ivy"com.lihaoyi::mill-scalalib:${meta.MILL_VERSION}"
)
override def mvnDeps = Seq(mvn"com.lihaoyi::mill-libs:$millVersion")

object tests extends Tests with TestModule.Utest {
def ivyDeps = Agg(ivy"com.lihaoyi::utest:0.7.11") ++ self.compileIvyDeps()
object tests extends ScalaTests, TestModule.Utest {
override def mvnDeps = Seq(mvn"com.lihaoyi::mill-testkit:$millVersion")
}
}
20 changes: 6 additions & 14 deletions ci
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,27 @@ function die {
case "$1" in

jitpack)
exec $0 mill -j 0 __.publishM2Local
$BASE_DIR/mill -j 0 __.publishM2Local
;;

mill)
exec $0 cs launch --scala 2.13.8 mill -- "${@:2}"
$BASE_DIR/mill "${@:2}"
;;

mill-i)
exec $0 cs launch --scala 2.13.8 mill-interactive -- -i "${@:2}"
$BASE_DIR/mill -i "${@:2}"
;;

scalafmt)
exec $0 cs launch --scala 2.13.8 scalafmt -- --diff "${@:2}"
$0 mill mill.scalalib.scalafmt/
;;

scalafmt-test)
$0 scalafmt --test
;;

cs)
test -x $BASE_DIR/cs || {
curl -o $BASE_DIR/cs -qL https://git.io/coursier-cli
chmod +x $BASE_DIR/cs
}
exec $BASE_DIR/cs "${@:2}"
$0 mill mill.scalalib.scalafmt/checkFormatAll
;;

example)
$0 mill-i -j 0 -D PUBLISH_VERSION=latest __.publishLocal
$0 mill-i -j 0 -DPUBLISH_VERSION=latest __.publishLocal
(cd example; ../ci mill hello.run)
(cd example; ../ci mill hello.tests)
;;
Expand Down
35 changes: 22 additions & 13 deletions dotenv/src/DotEnvModule.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package mill.dotenv

import mill._
import scalalib._

import mill.scalalib._
import os.Path
import mill.api.BuildCtx

object DotEnvModule {

def parse(pathRef: PathRef):Map[String,String] = {
def parse(pathRef: PathRef): Map[String, String] = {
parse(os.read(pathRef.path))
}

def parse(source: String): Map[String, String] = LINE_REGEX.findAllMatchIn(source)
.map(keyValue => (keyValue.group(1), unescapeCharacters(removeQuotes(keyValue.group(2)))))
def parse(source: String): Map[String, String] = LINE_REGEX
.findAllMatchIn(source)
.map(keyValue =>
(keyValue.group(1), unescapeCharacters(removeQuotes(keyValue.group(2))))
)
.toMap

private def removeQuotes(value: String): String = {
value.trim match {
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 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
}
}
Expand Down Expand Up @@ -54,16 +60,19 @@ object DotEnvModule {
(?:$|\z) # end of line
""".r


}

trait DotEnvModule extends JavaModule {
trait DotEnvModule extends mill.javalib.JavaModule {

def dotenvSources = T.sources { os.pwd / ".env" }
def dotenvSources = Task.Sources { BuildCtx.workspaceRoot / ".env" }

def dotenv = T.input {
dotenvSources().map(DotEnvModule.parse).foldLeft(Map[String,String]()) { _ ++ _ }
}
def dotenv = dotenvSources
.map(
_.map(DotEnvModule.parse)
.foldLeft(Map[String, String]()) {
_ ++ _
}
)

override def forkEnv = super.forkEnv() ++ dotenv()
}
32 changes: 22 additions & 10 deletions dotenv/tests/src/DotEnvModuleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@ import utest._
object DotEnvModuleTests extends TestSuite {
val tests = Tests {

"should remove quotes from value" - {
assert_env("HELLO", "WORLD", """
test("should remove quotes from value") - {
assert_env(
"HELLO",
"WORLD",
"""
HELLO="WORLD"
""")
"""
)
}

"should allow dot as key name" - {
assert_env("HELL.O", "WORLD", """
test("should allow dot as key name") - {
assert_env(
"HELL.O",
"WORLD",
"""
HELL.O=WORLD
""")
"""
)
}

"should allow new lines on value" - {
assert_env("HELLO", "BEAUTIFUL\nWORLD", """
test("should allow new lines on value") - {
assert_env(
"HELLO",
"BEAUTIFUL\nWORLD",
"""
HELLO="BEAUTIFUL
WORLD"
""")
"""
)
}

"should ignore commented values" - {
test("should ignore commented values") - {
val env = DotEnvModule.parse("#FOO=BAR")
assert(None == env.get("FOO"))
}
Expand Down
18 changes: 11 additions & 7 deletions example/build.sc
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// -*- mode: scala -*-
//| mvnDeps: ["com.github.vic::mill-dotenv:latest"]
//| mill-version: 1.0.3

import $ivy.`com.github.vic::mill-dotenv:latest` // Change to fixed release
// -*- mode: scala -*-
package build

import mill._, scalalib._, mill.dotenv._
import mill.api.BuildCtx

object hello extends ScalaModule with DotEnvModule {
override def scalaVersion = scala.util.Properties.versionNumberString
override def finalMainClass = T("hello.Main")
override def finalMainClass = Task("hello.Main")

// by default dotenv will read `$PWD/.env` file
// unless `def dotenvSources` is overriden.

object tests extends Tests with DotEnvModule with TestModule.Utest {
object tests extends ScalaTests with DotEnvModule with TestModule.Utest {
// load.env-test for test environment
override def dotenvSources = T.sources { os.pwd / ".env-test" }

def ivyDeps = Agg(ivy"com.lihaoyi::utest::0.7.11")
override def dotenvSources = Task.Sources {
BuildCtx.workspaceRoot / ".env-test"
}
override def mvnDeps = Seq(mvn"com.lihaoyi::utest:0.9.0")
}
}
4 changes: 2 additions & 2 deletions example/hello/tests/src/HelloTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package hello
import utest._

object HelloTests extends TestSuite {
val tests = Tests{
'world - {
val tests = Tests {
test("world") - {
assert("monde" == sys.env("WORLD"))
}
}
Expand Down
Loading