summaryrefslogtreecommitdiff
path: root/main/core/src/mill/util/Ctx.scala
blob: bbc243b76d1c01f50251f4bc76a72800ab867c39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package mill.util

import mill.define.Applicative.ImplicitStub

import scala.annotation.compileTimeOnly
import scala.language.implicitConversions

object Ctx{
  @compileTimeOnly("Target.ctx() can only be used with a T{...} block")
  @ImplicitStub
  implicit def taskCtx: Ctx = ???

  object Dest {
    implicit def pathToCtx(path: os.Path): Dest = new Dest { def dest = path }
  }
  trait Dest{
    def dest: os.Path
  }
  trait Log{
    def log: Logger
  }
  trait Home{
    def home: os.Path
  }
  trait Env{
    def env: Map[String, String]
  }
  object Log{
    implicit def logToCtx(l: Logger): Log = new Log { def log = l }
  }
  trait Args{
    def args: IndexedSeq[_]
  }

  def defaultHome = ammonite.ops.home / ".mill" / "ammonite"

}
class Ctx(val args: IndexedSeq[_],
          dest0: () => os.Path,
          val log: Logger,
          val home: os.Path,
          val env : Map[String, String])
  extends Ctx.Dest
  with Ctx.Log
  with Ctx.Args
  with Ctx.Home
  with Ctx.Env {

  def dest = dest0()
  def length = args.length
  def apply[T](index: Int): T = {
    if (index >= 0 && index < args.length) args(index).asInstanceOf[T]
    else throw new IndexOutOfBoundsException(s"Index $index outside of range 0 - ${args.length}")
  }
}