diff options
Diffstat (limited to 'main/api/src/mill/api/Ctx.scala')
-rw-r--r-- | main/api/src/mill/api/Ctx.scala | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/main/api/src/mill/api/Ctx.scala b/main/api/src/mill/api/Ctx.scala index 567da003..4ccf5a7d 100644 --- a/main/api/src/mill/api/Ctx.scala +++ b/main/api/src/mill/api/Ctx.scala @@ -1,53 +1,72 @@ package mill.api - import scala.annotation.{StaticAnnotation, compileTimeOnly} import scala.language.implicitConversions -object Ctx{ - @compileTimeOnly("Target.ctx() can only be used with a T{...} block") +import os.Path + +/** + * Provides access to various resources in the context of a currently execution Target. + */ +object Ctx { + @compileTimeOnly("Target.ctx() / T.ctx() can only be used with a T{...} block") @ImplicitStub implicit def taskCtx: Ctx = ??? + /** Access to the targets destination path. */ + trait Dest { + def dest: os.Path + } object Dest { implicit def pathToCtx(path: os.Path): Dest = new Dest { def dest = path } } - trait Dest{ - def dest: os.Path - } - trait Log{ + + /** Access to the targets [[Logger]] instance. */ + trait Log { def log: Logger } - trait Home{ + object Log { + implicit def logToCtx(l: Logger): Log = new Log { def log = l } + } + + /** Access to the projects home path. */ + trait Home { def home: os.Path } - trait Env{ + + /** Access to the current system environment settings. */ + trait Env { def env: Map[String, String] } - object Log{ - implicit def logToCtx(l: Logger): Log = new Log { def log = l } - } - trait Args{ + + trait Args { def args: IndexedSeq[_] } def defaultHome = os.home / ".mill" / "ammonite" + /** + * Marker annotation. + */ class ImplicitStub extends StaticAnnotation } -class Ctx(val args: IndexedSeq[_], - dest0: () => os.Path, - val log: Logger, - val home: os.Path, - val env : Map[String, String]) + + +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 dest: Path = dest0() + def length: Int = 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}") |