summaryrefslogtreecommitdiff
path: root/core/src/main/scala
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2018-01-06 23:25:29 -0800
committerLi Haoyi <haoyi.sg@gmail.com>2018-01-07 00:21:37 -0800
commit7cd18fd936b95410274be031a8231e5a3d9866a4 (patch)
treec3d9a1f4a110dc5f25824eb9f118ea8ebfcd2b37 /core/src/main/scala
parenta6febe05a5650274110301a09bb97e4770ea0400 (diff)
downloadmill-7cd18fd936b95410274be031a8231e5a3d9866a4.tar.gz
mill-7cd18fd936b95410274be031a8231e5a3d9866a4.tar.bz2
mill-7cd18fd936b95410274be031a8231e5a3d9866a4.zip
First pass at implicitly propagating a `def basePath: Path` up the `Module` hierarchy, which each module receives and extends.
One constraint is that now must define your abstract modules as `trait`s rather than `class`es, or otherwise add an implicit `ctx: ModuleCtx` parameter to your class definition. So far this lets us remove some explicit `basePath` definitions in `build.sc`. Proper handling of `basePath` in `CrossModule`s is future work
Diffstat (limited to 'core/src/main/scala')
-rw-r--r--core/src/main/scala/mill/define/Cross.scala10
-rw-r--r--core/src/main/scala/mill/define/Task.scala35
-rw-r--r--core/src/main/scala/mill/main/MainRunner.scala14
-rw-r--r--core/src/main/scala/mill/package.scala3
4 files changed, 48 insertions, 14 deletions
diff --git a/core/src/main/scala/mill/define/Cross.scala b/core/src/main/scala/mill/define/Cross.scala
index 90a383a6..26eef933 100644
--- a/core/src/main/scala/mill/define/Cross.scala
+++ b/core/src/main/scala/mill/define/Cross.scala
@@ -28,4 +28,12 @@ object Cross{
class CrossModule[T, V](constructor: T => V, cases: T*)
(implicit e: sourcecode.Enclosing, l: sourcecode.Line)
-extends Cross[V](cases.toList.map(x => (List(x), constructor(x)))) \ No newline at end of file
+extends Cross[V](cases.toList.map(x => (List(x), constructor(x))))
+
+class CrossModule2[T1, T2, V](constructor: (T1, T2) => V, cases: (T1, T2)*)
+ (implicit e: sourcecode.Enclosing, l: sourcecode.Line)
+extends Cross[V](cases.toList.map(x => (List(x._2, x._1), constructor(x._1, x._2))))
+
+class CrossModule3[T1, T2, T3, V](constructor: (T1, T2, T3) => V, cases: (T1, T2, T3)*)
+ (implicit e: sourcecode.Enclosing, l: sourcecode.Line)
+extends Cross[V](cases.toList.map(x => (List(x._3, x._2, x._1), constructor(x._1, x._2, x._3)))) \ No newline at end of file
diff --git a/core/src/main/scala/mill/define/Task.scala b/core/src/main/scala/mill/define/Task.scala
index 6ee9c606..0704c7c8 100644
--- a/core/src/main/scala/mill/define/Task.scala
+++ b/core/src/main/scala/mill/define/Task.scala
@@ -1,6 +1,7 @@
package mill.define
import ammonite.main.Router.Overrides
+import ammonite.ops.Path
import mill.define.Applicative.Applyable
import mill.eval.{PathRef, Result}
import mill.util.Ctx
@@ -361,10 +362,9 @@ class Input[T](t: Task[T],
def evaluate(args: Ctx) = args[T](0)
override def sideHash = util.Random.nextInt()
}
-
+case class BasePath(value: Path)
object Task {
-
trait TaskModule extends Module {
def defaultCommandName(): String
}
@@ -375,14 +375,37 @@ object Task {
* instantiation site so they can capture the enclosing/line information of
* the concrete instance.
*/
- class Module(implicit millModuleEnclosing0: sourcecode.Enclosing,
- millModuleLine0: sourcecode.Line) extends mill.moduledefs.Cacher{
+ class Module(implicit ctx: Module.Ctx) extends mill.moduledefs.Cacher{
// Ensure we do not propagate the implicit parameters as implicits within
// the body of any inheriting class/trait/objects, as it would screw up any
// one else trying to use sourcecode.{Enclosing,Line} to capture debug info
- val millModuleEnclosing = millModuleEnclosing0
- val millModuleLine = millModuleLine0
+ val millModuleEnclosing = ctx.millModuleEnclosing0
+ val millModuleLine = ctx.millModuleLine0
+ def basePath: Path = ctx.millModuleBasePath0.value / ctx.millName0.value
+ implicit def millModuleBasePath: BasePath = BasePath(basePath)
+ }
+ object Module{
+ case class Ctx(millModuleEnclosing0: sourcecode.Enclosing,
+ millModuleLine0: sourcecode.Line,
+ millName0: sourcecode.Name,
+ millModuleBasePath0: BasePath)
+ object Ctx{
+ implicit def make(implicit millModuleEnclosing0: sourcecode.Enclosing,
+ millModuleLine0: sourcecode.Line,
+ millName0: sourcecode.Name,
+ millModuleBasePath0: BasePath): Ctx = Ctx(
+ millModuleEnclosing0,
+ millModuleLine0,
+ millName0,
+ millModuleBasePath0
+ )
+ }
}
+ class BaseModule(basePath: Path)
+ (implicit millModuleEnclosing0: sourcecode.Enclosing,
+ millModuleLine0: sourcecode.Line,
+ millName0: sourcecode.Name)
+ extends Module()(Module.Ctx.make(implicitly, implicitly, implicitly, BasePath(basePath)))
class Task0[T](t: T) extends Task[T]{
lazy val t0 = t
diff --git a/core/src/main/scala/mill/main/MainRunner.scala b/core/src/main/scala/mill/main/MainRunner.scala
index 76e2d069..322795ea 100644
--- a/core/src/main/scala/mill/main/MainRunner.scala
+++ b/core/src/main/scala/mill/main/MainRunner.scala
@@ -75,15 +75,12 @@ class MainRunner(config: ammonite.main.Cli.Config,
}
override def initMain(isRepl: Boolean) = {
super.initMain(isRepl).copy(
- scriptCodeWrapper = mill.main.MainRunner.CustomCodeWrapper,
+ scriptCodeWrapper = CustomCodeWrapper,
// Ammonite does not properly forward the wd from CliConfig to Main, so
// force forward it outselves
wd = config.wd
)
}
-}
-
-object MainRunner{
object CustomCodeWrapper extends Preprocessor.CodeWrapper {
def top(pkgName: Seq[Name], imports: Imports, indexedWrapperName: Name) = {
val wrapName = indexedWrapperName.backticked
@@ -93,14 +90,17 @@ object MainRunner{
|$imports
|import mill._
|
- |object $wrapName extends $wrapName{
+ |object $wrapName extends mill.define.Task.BaseModule(ammonite.ops.Path(${pprint.Util.literalize(config.wd.toString)})) with $wrapName{
+ | // Make sure we don't include the `build` wrapper-object's name in
+ | // the `basePath`s of our build
+ | override def basePath = super.basePath / ammonite.ops.up
| // Stub to make sure Ammonite has something to call after it evaluates a script,
| // even if it does nothing...
| def $$main() = Iterator[String]()
| lazy val mapping = mill.discover.Discovered.make[$wrapName].mapping(this)
|}
|
- |sealed abstract class $wrapName extends mill.Module{
+ |sealed trait $wrapName extends mill.Module{
|""".stripMargin
}
@@ -112,4 +112,4 @@ object MainRunner{
"\n}"
}
}
-} \ No newline at end of file
+}
diff --git a/core/src/main/scala/mill/package.scala b/core/src/main/scala/mill/package.scala
index b7332f9e..f833b82c 100644
--- a/core/src/main/scala/mill/package.scala
+++ b/core/src/main/scala/mill/package.scala
@@ -6,5 +6,8 @@ package object mill extends JsonFormatters{
val PathRef = mill.eval.PathRef
type PathRef = mill.eval.PathRef
type Module = define.Task.Module
+ val Module = define.Task.Module
type CrossModule[T, V] = define.CrossModule[T, V]
+ type CrossModule2[T1, T2, V] = define.CrossModule2[T1, T2, V]
+ type CrossModule3[T1, T2, T3, V] = define.CrossModule3[T1, T2, T3, V]
}