diff options
Diffstat (limited to 'core/src/mill')
19 files changed, 51 insertions, 1147 deletions
diff --git a/core/src/mill/Main.scala b/core/src/mill/Main.scala deleted file mode 100644 index 3025994c..00000000 --- a/core/src/mill/Main.scala +++ /dev/null @@ -1,83 +0,0 @@ -package mill - -import ammonite.main.Cli.{formatBlock, genericSignature, replSignature} -import ammonite.ops._ -import ammonite.util.Util - -object Main { - case class Config(home: ammonite.ops.Path = pwd/'out/'ammonite, - colored: Option[Boolean] = None, - help: Boolean = false, - repl: Boolean = false, - watch: Boolean = false) - - def main(args: Array[String]): Unit = { - - import ammonite.main.Cli - - var show = false - val showCliArg = Cli.Arg[Cli.Config, Unit]( - "show", - None, - "Display the json-formatted value of the given target, if any", - (x, _) => { - show = true - x - } - ) - val removed = Set("predef-code", "home", "no-home-predef") - val millArgSignature = (Cli.genericSignature :+ showCliArg).filter(a => !removed(a.name)) - Cli.groupArgs( - args.toList, - millArgSignature, - Cli.Config(remoteLogging = false) - ) match{ - case Left(msg) => - System.err.println(msg) - System.exit(1) - case Right((cliConfig, _)) if cliConfig.help => - val leftMargin = millArgSignature.map(ammonite.main.Cli.showArg(_).length).max + 2 - System.out.println( - s"""Mill Build Tool - |usage: mill [mill-options] [target [target-options]] - | - |${formatBlock(millArgSignature, leftMargin).mkString(Util.newLine)}""".stripMargin - ) - System.exit(0) - case Right((cliConfig, leftoverArgs)) => - - val repl = leftoverArgs.isEmpty - val config = - if(!repl) cliConfig - else cliConfig.copy( - predefCode = - """import $file.build, build._ - |implicit val replApplyHandler = mill.main.ReplApplyHandler( - | interp.colors(), - | repl.pprinter(), - | build.millSelf.get, - | build.millDiscover - |) - |repl.pprinter() = replApplyHandler.pprinter - |import replApplyHandler.generatedEval._ - | - """.stripMargin, - welcomeBanner = None - ) - - val runner = new mill.main.MainRunner( - config, show, - System.out, System.err, System.in - ) - if (repl){ - runner.printInfo("Loading...") - runner.runRepl() - } else { - val result = runner.runScript(pwd / "build.sc", leftoverArgs) - System.exit(if(result) 0 else 1) - } - } - } -} - - diff --git a/core/src/mill/define/BaseModule.scala b/core/src/mill/define/BaseModule.scala index 5253e691..fccb19ae 100644 --- a/core/src/mill/define/BaseModule.scala +++ b/core/src/mill/define/BaseModule.scala @@ -1,8 +1,6 @@ package mill.define -import mill.main.Router.Overrides import ammonite.ops.Path -import mill.main.ParseArgs object BaseModule{ case class Implicit(value: BaseModule) @@ -11,7 +9,8 @@ object BaseModule{ abstract class BaseModule(millSourcePath0: Path, external0: Boolean = false) (implicit millModuleEnclosing0: sourcecode.Enclosing, millModuleLine0: sourcecode.Line, - millName0: sourcecode.Name) + millName0: sourcecode.Name, + millFile0: sourcecode.File) extends Module()( mill.define.Ctx.make( implicitly, @@ -19,8 +18,9 @@ abstract class BaseModule(millSourcePath0: Path, external0: Boolean = false) implicitly, BasePath(millSourcePath0), Segments(), - Overrides(0), - Ctx.External(external0) + mill.util.Router.Overrides(0), + Ctx.External(external0), + millFile0 ) ){ // A BaseModule should provide an empty Segments list to it's children, since diff --git a/core/src/mill/define/Ctx.scala b/core/src/mill/define/Ctx.scala index 11e9e1f5..6075b804 100644 --- a/core/src/mill/define/Ctx.scala +++ b/core/src/mill/define/Ctx.scala @@ -1,6 +1,6 @@ package mill.define -import mill.main.Router.Overrides + import ammonite.ops.{Path, RelPath} import scala.annotation.implicitNotFound @@ -57,7 +57,8 @@ case class Ctx(enclosing: String, millSourcePath: Path, segments: Segments, overrides: Int, - external: Boolean){ + external: Boolean, + fileName: String){ } object Ctx{ @@ -67,8 +68,9 @@ object Ctx{ millName0: sourcecode.Name, millModuleBasePath0: BasePath, segments0: Segments, - overrides0: mill.main.Router.Overrides, - external0: External): Ctx = { + overrides0: mill.util.Router.Overrides, + external0: External, + fileName: sourcecode.File): Ctx = { Ctx( millModuleEnclosing0.value, millModuleLine0.value, @@ -76,7 +78,8 @@ object Ctx{ millModuleBasePath0.value, segments0, overrides0.value, - external0.value + external0.value, + fileName.value ) } }
\ No newline at end of file diff --git a/core/src/mill/define/Discover.scala b/core/src/mill/define/Discover.scala index fd5bd449..1b6b002a 100644 --- a/core/src/mill/define/Discover.scala +++ b/core/src/mill/define/Discover.scala @@ -1,6 +1,7 @@ package mill.define +import mill.util.Router.EntryPoint + import language.experimental.macros -import mill.main.Router.{EntryPoint, Overrides} import sourcecode.Compat.Context import scala.collection.mutable @@ -41,7 +42,7 @@ object Discover { } rec(weakTypeOf[T]) - val router = new mill.main.Router(c) + val router = new mill.util.Router(c) val mapping = for{ discoveredModuleType <- seen val curCls = discoveredModuleType.asInstanceOf[router.c.Type] @@ -57,7 +58,7 @@ object Discover { val (overrides, routes) = overridesRoutes.unzip val lhs = q"classOf[${discoveredModuleType.typeSymbol.asClass}]" val clsType = discoveredModuleType.typeSymbol.asClass - val rhs = q"scala.Seq[(Int, mill.main.Router.EntryPoint[_])](..$overridesRoutes)" + val rhs = q"scala.Seq[(Int, mill.util.Router.EntryPoint[_])](..$overridesRoutes)" q"$lhs -> $rhs" } diff --git a/core/src/mill/define/Module.scala b/core/src/mill/define/Module.scala index a53ed345..bfc15191 100644 --- a/core/src/mill/define/Module.scala +++ b/core/src/mill/define/Module.scala @@ -2,12 +2,10 @@ package mill.define import java.lang.reflect.Modifier -import mill.main.Router.{EntryPoint, Overrides} import ammonite.ops.Path import scala.language.experimental.macros import scala.reflect.ClassTag -import scala.reflect.macros.blackbox /** * `Module` is a class meant to be extended by `trait`s *only*, in order to * propagate the implicit parameters forward to the final concrete diff --git a/core/src/mill/eval/Evaluator.scala b/core/src/mill/eval/Evaluator.scala index 347ad321..70fab152 100644 --- a/core/src/mill/eval/Evaluator.scala +++ b/core/src/mill/eval/Evaluator.scala @@ -2,7 +2,7 @@ package mill.eval import java.net.URLClassLoader -import mill.main.Router.EntryPoint +import mill.util.Router.EntryPoint import ammonite.ops._ import ammonite.runtime.SpecialClassLoader import mill.define.{Ctx => _, _} @@ -330,6 +330,12 @@ class Evaluator[T](val outPath: Path, object Evaluator{ + // This needs to be a ThreadLocal because we need to pass it into the body of + // the TargetScopt#read call, which does not accept additional parameters. + // Until we migrate our CLI parsing off of Scopt (so we can pass the BaseModule + // in directly) we are forced to pass it in via a ThreadLocal + val currentEvaluator = new ThreadLocal[mill.eval.Evaluator[_]] + case class Paths(out: Path, dest: Path, meta: Path, diff --git a/core/src/mill/main/MagicScopt.scala b/core/src/mill/main/MagicScopt.scala deleted file mode 100644 index acba57cb..00000000 --- a/core/src/mill/main/MagicScopt.scala +++ /dev/null @@ -1,49 +0,0 @@ -package mill.main -import mill.define.ExternalModule -import mill.main.ParseArgs - -object MagicScopt{ - // This needs to be a ThreadLocal because we need to pass it into the body of - // the TargetScopt#read call, which does not accept additional parameters. - // Until we migrate our CLI parsing off of Scopt (so we can pass the BaseModule - // in directly) we are forced to pass it in via a ThreadLocal - val currentEvaluator = new ThreadLocal[mill.eval.Evaluator[_]] - - case class Tasks[T](items: Seq[mill.define.NamedTask[T]]) -} -class EvaluatorScopt[T]() - extends scopt.Read[mill.eval.Evaluator[T]]{ - def arity = 0 - def reads = s => try{ - MagicScopt.currentEvaluator.get.asInstanceOf[mill.eval.Evaluator[T]] - } -} -class TargetScopt[T]() - extends scopt.Read[MagicScopt.Tasks[T]]{ - def arity = 0 - def reads = s => { - val rootModule = MagicScopt.currentEvaluator.get.rootModule - val d = rootModule.millDiscover - val (expanded, leftover) = ParseArgs(Seq(s)).fold(e => throw new Exception(e), identity) - val resolved = expanded.map{ - case (Some(scoping), segments) => - val moduleCls = rootModule.getClass.getClassLoader.loadClass(scoping.render + "$") - val externalRootModule = moduleCls.getField("MODULE$").get(moduleCls).asInstanceOf[ExternalModule] - val crossSelectors = segments.value.map { - case mill.define.Segment.Cross(x) => x.toList.map(_.toString) - case _ => Nil - } - mill.main.Resolve.resolve(segments.value.toList, externalRootModule, d, leftover, crossSelectors.toList, Nil) - case (None, segments) => - val crossSelectors = segments.value.map { - case mill.define.Segment.Cross(x) => x.toList.map(_.toString) - case _ => Nil - } - mill.main.Resolve.resolve(segments.value.toList, rootModule, d, leftover, crossSelectors.toList, Nil) - } - mill.util.EitherOps.sequence(resolved) match{ - case Left(s) => throw new Exception(s) - case Right(ts) => MagicScopt.Tasks(ts.flatten).asInstanceOf[MagicScopt.Tasks[T]] - } - } -}
\ No newline at end of file diff --git a/core/src/mill/main/MainModule.scala b/core/src/mill/main/MainModule.scala deleted file mode 100644 index fd46fb77..00000000 --- a/core/src/mill/main/MainModule.scala +++ /dev/null @@ -1,27 +0,0 @@ -package mill.main - -trait MainModule extends mill.Module{ - implicit def millDiscover: mill.define.Discover[_] - implicit def millScoptTargetReads[T] = new mill.main.TargetScopt[T]() - implicit def millScoptEvaluatorReads[T] = new mill.main.EvaluatorScopt[T]() - def resolve(targets: mill.main.MagicScopt.Tasks[Any]*) = mill.T.command{ - targets.flatMap(_.items).foreach(println) - } - def all(evaluator: mill.eval.Evaluator[Any], - targets: mill.main.MagicScopt.Tasks[Any]*) = mill.T.command{ - val (watched, res) = mill.main.RunScript.evaluate( - evaluator, - mill.util.Strict.Agg.from(targets.flatMap(_.items)) - ) - } - def show(evaluator: mill.eval.Evaluator[Any], - targets: mill.main.MagicScopt.Tasks[Any]*) = mill.T.command{ - val (watched, res) = mill.main.RunScript.evaluate( - evaluator, - mill.util.Strict.Agg.from(targets.flatMap(_.items)) - ) - for(json <- res.right.get.flatMap(_._2)){ - println(json) - } - } -} diff --git a/core/src/mill/main/MainRunner.scala b/core/src/mill/main/MainRunner.scala deleted file mode 100644 index 9004de39..00000000 --- a/core/src/mill/main/MainRunner.scala +++ /dev/null @@ -1,119 +0,0 @@ -package mill.main -import java.io.{InputStream, OutputStream, PrintStream} - -import ammonite.Main -import ammonite.interp.{Interpreter, Preprocessor} -import ammonite.ops.Path -import ammonite.util._ -import mill.define.Discover -import mill.eval.{Evaluator, PathRef} -import mill.util.PrintLogger -import upickle.Js - -/** - * Customized version of [[ammonite.MainRunner]], allowing us to run Mill - * `build.sc` scripts with mill-specific tweaks such as a custom - * `scriptCodeWrapper` or with a persistent evaluator between runs. - */ -class MainRunner(config: ammonite.main.Cli.Config, - show: Boolean, - outprintStream: PrintStream, - errPrintStream: PrintStream, - stdIn: InputStream) - extends ammonite.MainRunner( - config, outprintStream, errPrintStream, - stdIn, outprintStream, errPrintStream - ){ - - var lastEvaluator: Option[(Seq[(Path, Long)], Evaluator[Any])] = None - - override def runScript(scriptPath: Path, scriptArgs: List[String]) = - watchLoop( - isRepl = false, - printing = true, - mainCfg => { - val (result, interpWatched) = RunScript.runScript( - mainCfg.wd, - scriptPath, - mainCfg.instantiateInterpreter(), - scriptArgs, - lastEvaluator, - new PrintLogger( - colors != ammonite.util.Colors.BlackWhite, - colors, - if (show) errPrintStream else outprintStream, - errPrintStream, - errPrintStream - ) - ) - - result match{ - case Res.Success(data) => - val (eval, evaluationWatches, res) = data - - lastEvaluator = Some((interpWatched, eval)) - - (Res(res), interpWatched ++ evaluationWatches) - case _ => (result, interpWatched) - } - } - ) - - override def handleWatchRes[T](res: Res[T], printing: Boolean) = { - res match{ - case Res.Success(value) => -// if (show){ -// for(json <- value.asInstanceOf[Seq[Js.Value]]){ -// outprintStream.println(json) -// } -// } - - true - - case _ => super.handleWatchRes(res, printing) - } - - } - override def initMain(isRepl: Boolean) = { - super.initMain(isRepl).copy( - scriptCodeWrapper = CustomCodeWrapper, - // Ammonite does not properly forward the wd from CliConfig to Main, so - // force forward it outselves - wd = config.wd - ) - } - object CustomCodeWrapper extends Preprocessor.CodeWrapper { - def top(pkgName: Seq[Name], imports: Imports, indexedWrapperName: Name) = { - val wrapName = indexedWrapperName.backticked - val literalPath = pprint.Util.literalize(config.wd.toString) - s""" - |package ${pkgName.head.encoded} - |package ${Util.encodeScalaSourcePath(pkgName.tail)} - |$imports - |import mill._ - |object $wrapName - |extends mill.define.BaseModule(ammonite.ops.Path($literalPath)) - |with $wrapName{ - | // Stub to make sure Ammonite has something to call after it evaluates a script, - | // even if it does nothing... - | def $$main() = Iterator[String]() - | - | implicit def millDiscover: mill.define.Discover[this.type] = mill.define.Discover[this.type] - | // Need to wrap the returned Module in Some(...) to make sure it - | // doesn't get picked up during reflective child-module discovery - | val millSelf = Some(this) - |} - | - |sealed trait $wrapName extends mill.main.MainModule{ - |""".stripMargin - } - - - def bottom(printCode: String, indexedWrapperName: Name, extraCode: String) = { - // We need to disable the `$main` method definition inside the wrapper class, - // because otherwise it might get picked up by Ammonite and run as a static - // class method, which blows up since it's defined as an instance method - "\n}" - } - } -} diff --git a/core/src/mill/main/ReplApplyHandler.scala b/core/src/mill/main/ReplApplyHandler.scala deleted file mode 100644 index 0849f2c8..00000000 --- a/core/src/mill/main/ReplApplyHandler.scala +++ /dev/null @@ -1,125 +0,0 @@ -package mill.main - - -import mill.define.Applicative.ApplyHandler -import mill.define.Segment.Label -import mill.define._ -import mill.eval.{Evaluator, Result} -import mill.util.Strict.Agg - -import scala.collection.mutable -object ReplApplyHandler{ - def apply[T](colors: ammonite.util.Colors, - pprinter0: pprint.PPrinter, - rootModule: mill.define.BaseModule, - discover: Discover[_]) = { - new ReplApplyHandler( - pprinter0, - new Evaluator( - ammonite.ops.pwd / 'out, - ammonite.ops.pwd / 'out, - rootModule, - discover, - new mill.util.PrintLogger( - colors != ammonite.util.Colors.BlackWhite, - colors, - System.out, - System.err, - System.err - ) - ) - ) - } -} -class ReplApplyHandler(pprinter0: pprint.PPrinter, - evaluator: Evaluator[_]) extends ApplyHandler[Task] { - // Evaluate classLoaderSig only once in the REPL to avoid busting caches - // as the user enters more REPL commands and changes the classpath - val classLoaderSig = Evaluator.classLoaderSig - override def apply[V](t: Task[V]) = { - val res = evaluator.evaluate(Agg(t)) - res.values match{ - case Seq(head: V) => head - case Nil => - val msg = new mutable.StringBuilder() - msg.append(res.failing.keyCount + " targets failed\n") - for((k, vs) <- res.failing.items){ - msg.append(k match{ - case Left(t) => "Anonymous Task\n" - case Right(k) => k.segments.render + "\n" - }) - - for(v <- vs){ - v match{ - case Result.Failure(m, _) => msg.append(m + "\n") - case Result.Exception(t, outerStack) => - msg.append( - t.toString + - t.getStackTrace.dropRight(outerStack.value.length).map("\n " + _).mkString + - "\n" - ) - - } - } - } - throw new Exception(msg.toString) - } - } - - val generatedEval = new EvalGenerated(evaluator) - - val millHandlers: PartialFunction[Any, pprint.Tree] = { - case c: Cross[_] => - pprint.Tree.Lazy( ctx => - Iterator(c.millOuterCtx.enclosing , ":", c.millOuterCtx.lineNum.toString, ctx.applyPrefixColor("\nChildren:").toString) ++ - c.items.iterator.map(x => - "\n (" + x._1.map(pprint.PPrinter.BlackWhite.apply(_)).mkString(", ") + ")" - ) - ) - case m: mill.Module if evaluator.rootModule.millInternal.modules.contains(m) => - pprint.Tree.Lazy( ctx => - Iterator(m.millInternal.millModuleEnclosing, ":", m.millInternal.millModuleLine.toString) ++ - (if (m.millInternal.reflect[mill.Module].isEmpty) Nil - else - ctx.applyPrefixColor("\nChildren:").toString +: - m.millInternal.reflect[mill.Module].map("\n ." + _.millOuterCtx.segment.pathSegments.mkString("."))) ++ - (evaluator.discover.value.get(m.getClass) match{ - case None => Nil - case Some(commands) => - ctx.applyPrefixColor("\nCommands:").toString +: commands.map{c => - "\n ." + c._2.name + "(" + - c._2.argSignatures.map(s => s.name + ": " + s.typeString).mkString(", ") + - ")()" - } - }) ++ - (if (m.millInternal.reflect[Target[_]].isEmpty) Nil - else { - Seq(ctx.applyPrefixColor("\nTargets:").toString) ++ - m.millInternal.reflect[Target[_]].sortBy(_.label).map(t => - "\n ." + t.label + "()" - ) - }) - - ) - case t: mill.define.Target[_] if evaluator.rootModule.millInternal.targets.contains(t) => - val seen = mutable.Set.empty[Task[_]] - def rec(t: Task[_]): Seq[Segments] = { - if (seen(t)) Nil // do nothing - else t match { - case t: Target[_] if evaluator.rootModule.millInternal.targets.contains(t) => - Seq(t.ctx.segments) - case _ => - seen.add(t) - t.inputs.flatMap(rec) - } - } - pprint.Tree.Lazy(ctx => - Iterator(t.ctx.enclosing, ":", t.ctx.lineNum.toString, "\n", ctx.applyPrefixColor("Inputs:").toString) ++ - t.inputs.iterator.flatMap(rec).map("\n " + _.render) - ) - - } - val pprinter = pprinter0.copy( - additionalHandlers = millHandlers orElse pprinter0.additionalHandlers - ) -} diff --git a/core/src/mill/main/Resolve.scala b/core/src/mill/main/Resolve.scala deleted file mode 100644 index 1932c241..00000000 --- a/core/src/mill/main/Resolve.scala +++ /dev/null @@ -1,143 +0,0 @@ -package mill.main - -import mill.define._ -import mill.define.TaskModule -import mill.main.Router.EntryPoint -import ammonite.util.{Res} - -object Resolve { - def resolve[T, V](remainingSelector: List[Segment], - obj: mill.Module, - discover: Discover[_], - rest: Seq[String], - remainingCrossSelectors: List[List[String]], - revSelectorsSoFar: List[Segment]): Either[String, Seq[NamedTask[Any]]] = { - - remainingSelector match{ - case Segment.Cross(_) :: Nil => Left("Selector cannot start with a [cross] segment") - case Segment.Label(last) :: Nil => - val target = - obj - .millInternal - .reflect[Target[_]] - .find(_.label == last) - .map(Right(_)) - - def shimArgsig[T](a: mill.main.Router.ArgSig[T, _]) = { - ammonite.main.Router.ArgSig[T]( - a.name, - a.typeString, - a.doc, - a.default - ) - } - def invokeCommand(target: mill.Module, name: String) = for{ - (cls, entryPoints) <- discover.value - if cls.isAssignableFrom(target.getClass) - ep <- entryPoints - if ep._2.name == name - } yield mill.main.Scripts.runMainMethod( - target, - ep._2.asInstanceOf[EntryPoint[mill.Module]], - ammonite.main.Scripts.groupArgs(rest.toList) - ) match{ - case Res.Success(v: Command[_]) => Right(v) - case Res.Failure(msg) => Left(msg) - case Res.Exception(ex, msg) => - val sw = new java.io.StringWriter() - ex.printStackTrace(new java.io.PrintWriter(sw)) - val prefix = if (msg.nonEmpty) msg + "\n" else msg - Left(prefix + sw.toString) - - } - - val runDefault = for{ - child <- obj.millInternal.reflectNestedObjects[mill.Module] - if child.millOuterCtx.segment == Segment.Label(last) - res <- child match{ - case taskMod: TaskModule => Some(invokeCommand(child, taskMod.defaultCommandName()).headOption) - case _ => None - } - } yield res - - val command = invokeCommand(obj, last).headOption - - command orElse target orElse runDefault.flatten.headOption match{ - case None => Left("Cannot resolve task " + - Segments((Segment.Label(last) :: revSelectorsSoFar).reverse:_*).render - ) - // Contents of `either` *must* be a `Task`, because we only select - // methods returning `Task` in the discovery process - case Some(either) => either.right.map(Seq(_)) - } - - - case head :: tail => - val newRevSelectorsSoFar = head :: revSelectorsSoFar - head match{ - case Segment.Label(singleLabel) => - if (singleLabel == "__") { - - val matching = - obj.millInternal.modules - .map(resolve(tail, _, discover, rest, remainingCrossSelectors, newRevSelectorsSoFar)) - .collect{case Right(vs) => vs}.flatten - - if (matching.nonEmpty)Right(matching) - else Left("Cannot resolve module " + Segments(newRevSelectorsSoFar.reverse:_*).render) - } else if (singleLabel == "_") { - - val matching = - obj.millModuleDirectChildren - .map(resolve(tail, _, discover, rest, remainingCrossSelectors, newRevSelectorsSoFar)) - .collect{case Right(vs) => vs}.flatten - - if (matching.nonEmpty)Right(matching) - else Left("Cannot resolve module " + Segments(newRevSelectorsSoFar.reverse:_*).render) - }else{ - - obj.millInternal.reflectNestedObjects[mill.Module].find{ - _.millOuterCtx.segment == Segment.Label(singleLabel) - } match{ - case Some(child: mill.Module) => resolve(tail, child, discover, rest, remainingCrossSelectors, newRevSelectorsSoFar) - case None => Left("Cannot resolve module " + Segments(newRevSelectorsSoFar.reverse:_*).render) - } - } - - case Segment.Cross(cross) => - obj match{ - case c: Cross[_] => - if(cross == Seq("__")){ - val matching = - for ((k, v) <- c.items) - yield resolve(tail, v.asInstanceOf[mill.Module], discover, rest, remainingCrossSelectors, newRevSelectorsSoFar) - - val results = matching.collect{case Right(res) => res}.flatten - - if (results.isEmpty) Left("Cannot resolve cross " + Segments(newRevSelectorsSoFar.reverse:_*).render) - else Right(results) - } else if (cross.contains("_")){ - val matching = for { - (k, v) <- c.items - if k.length == cross.length - if k.zip(cross).forall { case (l, r) => l == r || r == "_" } - } yield resolve(tail, v.asInstanceOf[mill.Module], discover, rest, remainingCrossSelectors, newRevSelectorsSoFar) - - val results = matching.collect{case Right(res) => res}.flatten - - if (results.isEmpty) Left("Cannot resolve cross " + Segments(newRevSelectorsSoFar.reverse:_*).render) - else Right(results) - }else{ - c.itemMap.get(cross.toList) match{ - case Some(m: mill.Module) => resolve(tail, m, discover, rest, remainingCrossSelectors, newRevSelectorsSoFar) - case None => Left("Cannot resolve cross " + Segments(newRevSelectorsSoFar.reverse:_*).render) - } - } - case _ => Left("Cannot resolve cross " + Segments(newRevSelectorsSoFar.reverse:_*).render) - } - } - - case Nil => Left("Selector cannot be empty") - } - } -} diff --git a/core/src/mill/main/RunScript.scala b/core/src/mill/main/RunScript.scala deleted file mode 100644 index 17d520e7..00000000 --- a/core/src/mill/main/RunScript.scala +++ /dev/null @@ -1,231 +0,0 @@ -package mill.main - -import java.nio.file.NoSuchFileException - -import ammonite.interp.Interpreter -import ammonite.ops.{Path, read} -import ammonite.runtime.SpecialClassLoader -import ammonite.util.Util.CodeSource -import ammonite.util.{Name, Res, Util} -import mill.{PathRef, define} -import mill.define.{Discover, ExternalModule, Segment, Task} -import mill.eval.{Evaluator, Result} -import mill.util.{EitherOps, Logger} -import mill.util.Strict.Agg -import upickle.Js - -/** - * Custom version of ammonite.main.Scripts, letting us run the build.sc script - * directly without going through Ammonite's main-method/argument-parsing - * subsystem - */ -object RunScript{ - def runScript(wd: Path, - path: Path, - instantiateInterpreter: => Either[(Res.Failing, Seq[(Path, Long)]), ammonite.interp.Interpreter], - scriptArgs: Seq[String], - lastEvaluator: Option[(Seq[(Path, Long)], Evaluator[Any])], - log: Logger) - : (Res[(Evaluator[Any], Seq[(Path, Long)], Either[String, Seq[Js.Value]])], Seq[(Path, Long)]) = { - - val (evalRes, interpWatched) = lastEvaluator match{ - case Some((prevInterpWatchedSig, prevEvaluator)) - if watchedSigUnchanged(prevInterpWatchedSig) => - - (Res.Success(prevEvaluator), prevInterpWatchedSig) - - case _ => - instantiateInterpreter match{ - case Left((res, watched)) => (res, watched) - case Right(interp) => - interp.watch(path) - val eval = - for((mapping, discover) <- evaluateMapping(wd, path, interp)) - yield new Evaluator[Any]( - wd / 'out, wd / 'out, mapping, discover, log, - mapping.getClass.getClassLoader.asInstanceOf[SpecialClassLoader].classpathSignature - ) - - (eval, interp.watchedFiles) - } - } - - val evaluated = for{ - evaluator <- evalRes - (evalWatches, res) <- Res(evaluateTarget(evaluator, scriptArgs)) - } yield { - val alreadyStale = evalWatches.exists(p => p.sig != new PathRef(p.path, p.quick).sig) - // If the file changed between the creation of the original - // `PathRef` and the current moment, use random junk .sig values - // to force an immediate re-run. Otherwise calculate the - // pathSignatures the same way Ammonite would and hand over the - // values, so Ammonite can watch them and only re-run if they - // subsequently change - val evaluationWatches = - if (alreadyStale) evalWatches.map(_.path -> util.Random.nextLong()) - else evalWatches.map(p => p.path -> Interpreter.pathSignature(p.path)) - - (evaluator, evaluationWatches, res.map(_.flatMap(_._2))) - } - (evaluated, interpWatched) - } - - def watchedSigUnchanged(sig: Seq[(Path, Long)]) = { - sig.forall{case (p, l) => Interpreter.pathSignature(p) == l} - } - - def evaluateMapping(wd: Path, - path: Path, - interp: ammonite.interp.Interpreter): Res[(mill.define.BaseModule, Discover[Any])] = { - - val (pkg, wrapper) = Util.pathToPackageWrapper(Seq(), path relativeTo wd) - - for { - scriptTxt <- - try Res.Success(Util.normalizeNewlines(read(path))) - catch { case e: NoSuchFileException => Res.Failure("Script file not found: " + path) } - - processed <- interp.processModule( - scriptTxt, - CodeSource(wrapper, pkg, Seq(Name("ammonite"), Name("$file")), Some(path)), - autoImport = true, - extraCode = "", - hardcoded = true - ) - - buildClsName <- processed.blockInfo.lastOption match { - case Some(meta) => Res.Success(meta.id.wrapperPath) - case None => Res.Skip - } - - buildCls = interp - .evalClassloader - .loadClass(buildClsName) - - module <- try { - Util.withContextClassloader(interp.evalClassloader) { - Res.Success( - buildCls.getMethod("millSelf") - .invoke(null) - .asInstanceOf[Some[mill.define.BaseModule]] - .get - ) - } - } catch { - case e: Throwable => Res.Exception(e, "") - } - discover <- try { - Util.withContextClassloader(interp.evalClassloader) { - Res.Success( - buildCls.getMethod("millDiscover") - .invoke(module) - .asInstanceOf[Discover[Any]] - ) - } - } catch { - case e: Throwable => Res.Exception(e, "") - } -// _ <- Res(consistencyCheck(mapping)) - } yield (module, discover) - } - - def evaluateTarget[T](evaluator: Evaluator[T], scriptArgs: Seq[String]) = { - for { - parsed <- ParseArgs(scriptArgs) - (selectors, args) = parsed - targets <- { - val selected = selectors.map { case (scopedSel, sel) => - val (rootModule, discover) = scopedSel match{ - case None => (evaluator.rootModule, evaluator.discover) - case Some(scoping) => - val moduleCls = - evaluator.rootModule.getClass.getClassLoader.loadClass(scoping.render + "$") - - val rootModule = moduleCls.getField("MODULE$").get(moduleCls).asInstanceOf[ExternalModule] - (rootModule, rootModule.millDiscover) - } - val crossSelectors = sel.value.map { - case Segment.Cross(x) => x.toList.map(_.toString) - case _ => Nil - } - - try { - // We inject the `evaluator.rootModule` into the TargetScopt, rather - // than the `rootModule`, because even if you are running an external - // module we still want you to be able to resolve targets from your - // main build. Resolving targets from external builds as CLI arguments - // is not currently supported - mill.main.MagicScopt.currentEvaluator.set(evaluator) - mill.main.Resolve.resolve( - sel.value.toList, rootModule, - discover, - args, crossSelectors.toList, Nil - ) - } finally{ - mill.main.MagicScopt.currentEvaluator.set(null) - } - } - EitherOps.sequence(selected) - } - (watched, res) = evaluate( - evaluator, - Agg.from(targets.flatten.distinct) - ) - } yield (watched, res) - } - - def evaluate(evaluator: Evaluator[_], - targets: Agg[Task[Any]]): (Seq[PathRef], Either[String, Seq[(Any, Option[upickle.Js.Value])]]) = { - val evaluated = evaluator.evaluate(targets) - val watched = evaluated.results - .iterator - .collect { - case (t: define.Sources, Result.Success(p: Seq[PathRef])) => p - } - .flatten - .toSeq - - val errorStr = - (for((k, fs) <- evaluated.failing.items()) yield { - val ks = k match{ - case Left(t) => t.toString - case Right(t) => t.segments.render - } - val fss = fs.map{ - case Result.Exception(t, outerStack) => - t.toString + - t.getStackTrace.dropRight(outerStack.value.length).map("\n " + _).mkString - case Result.Failure(t, _) => t - } - s"$ks ${fss.mkString(", ")}" - }).mkString("\n") - - evaluated.failing.keyCount match { - case 0 => - val json = for(t <- targets.toSeq) yield { - t match { - case t: mill.define.NamedTask[_] => - val jsonFile = Evaluator - .resolveDestPaths(evaluator.outPath, t.ctx.segments) - .meta - val metadata = upickle.json.read(jsonFile.toIO) - Some(metadata(1)) - - case _ => None - } - } - - watched -> Right(evaluated.values.zip(json)) - case n => watched -> Left(s"$n targets failed\n$errorStr") - } - } - -// def consistencyCheck[T](mapping: Discovered.Mapping[T]): Either[String, Unit] = { -// val consistencyErrors = Discovered.consistencyCheck(mapping) -// if (consistencyErrors.nonEmpty) { -// Left(s"Failed Discovered.consistencyCheck: ${consistencyErrors.map(_.render)}") -// } else { -// Right(()) -// } -// } -} diff --git a/core/src/mill/modules/Jvm.scala b/core/src/mill/modules/Jvm.scala deleted file mode 100644 index 297dcf1f..00000000 --- a/core/src/mill/modules/Jvm.scala +++ /dev/null @@ -1,257 +0,0 @@ -package mill.modules - -import java.io.FileOutputStream -import java.lang.reflect.Modifier -import java.net.URLClassLoader -import java.nio.file.attribute.PosixFilePermission -import java.util.jar.{JarEntry, JarFile, JarOutputStream} - -import ammonite.ops._ -import mill.define.Task -import mill.eval.PathRef -import mill.util.{Ctx, Loose} -import mill.util.Ctx.Log -import mill.util.Loose.Agg -import upickle.default.{Reader, Writer} - -import scala.annotation.tailrec -import scala.collection.mutable -import scala.reflect.ClassTag - - -object Jvm { - - def interactiveSubprocess(mainClass: String, - classPath: Agg[Path], - jvmArgs: Seq[String] = Seq.empty, - envArgs: Map[String, String] = Map.empty, - mainArgs: Seq[String] = Seq.empty, - workingDir: Path = null): Unit = { - import ammonite.ops.ImplicitWd._ - val commandArgs = - Vector("java") ++ - jvmArgs ++ - Vector("-cp", classPath.mkString(":"), mainClass) ++ - mainArgs - - %.copy(envArgs = envArgs)(commandArgs)(workingDir) - } - - def runLocal(mainClass: String, - classPath: Agg[Path], - mainArgs: Seq[String] = Seq.empty) - (implicit ctx: Ctx): Unit = { - inprocess(classPath, classLoaderOverrideSbtTesting = false, cl => { - getMainMethod(mainClass, cl).invoke(null, mainArgs.toArray) - }) - } - - private def getMainMethod(mainClassName: String, cl: ClassLoader) = { - val mainClass = cl.loadClass(mainClassName) - val method = mainClass.getMethod("main", classOf[Array[String]]) - // jvm allows the actual main class to be non-public and to run a method in the non-public class, - // we need to make it accessible - method.setAccessible(true) - val modifiers = method.getModifiers - if (!Modifier.isPublic(modifiers)) - throw new NoSuchMethodException(mainClassName + ".main is not public") - if (!Modifier.isStatic(modifiers)) - throw new NoSuchMethodException(mainClassName + ".main is not static") - method - } - - - - def inprocess[T](classPath: Agg[Path], - classLoaderOverrideSbtTesting: Boolean, - body: ClassLoader => T): T = { - val cl = if (classLoaderOverrideSbtTesting) { - val outerClassLoader = getClass.getClassLoader - new URLClassLoader(classPath.map(_.toIO.toURI.toURL).toArray, null){ - override def findClass(name: String) = { - if (name.startsWith("sbt.testing.")){ - outerClassLoader.loadClass(name) - }else{ - super.findClass(name) - } - } - } - } else { - new URLClassLoader(classPath.map(_.toIO.toURI.toURL).toArray, null) - } - val oldCl = Thread.currentThread().getContextClassLoader - Thread.currentThread().setContextClassLoader(cl) - try { - body(cl) - }finally{ - Thread.currentThread().setContextClassLoader(oldCl) - cl.close() - } - } - - def subprocess(mainClass: String, - classPath: Agg[Path], - jvmArgs: Seq[String] = Seq.empty, - envArgs: Map[String, String] = Map.empty, - mainArgs: Seq[String] = Seq.empty, - workingDir: Path = null) - (implicit ctx: Ctx) = { - - val commandArgs = - Vector("java") ++ - jvmArgs ++ - Vector("-cp", classPath.mkString(":"), mainClass) ++ - mainArgs - - val workingDir1 = Option(workingDir).getOrElse(ctx.dest) - mkdir(workingDir1) - val builder = - new java.lang.ProcessBuilder() - .directory(workingDir1.toIO) - .command(commandArgs:_*) - .redirectOutput(ProcessBuilder.Redirect.PIPE) - .redirectError(ProcessBuilder.Redirect.PIPE) - - for((k, v) <- envArgs) builder.environment().put(k, v) - val proc = builder.start() - val stdout = proc.getInputStream - val stderr = proc.getErrorStream - val sources = Seq( - (stdout, Left(_: Bytes), ctx.log.outputStream), - (stderr, Right(_: Bytes),ctx.log.errorStream ) - ) - val chunks = mutable.Buffer.empty[Either[Bytes, Bytes]] - while( - // Process.isAlive doesn't exist on JDK 7 =/ - util.Try(proc.exitValue).isFailure || - stdout.available() > 0 || - stderr.available() > 0 - ){ - var readSomething = false - for ((subStream, wrapper, parentStream) <- sources){ - while (subStream.available() > 0){ - readSomething = true - val array = new Array[Byte](subStream.available()) - val actuallyRead = subStream.read(array) - chunks.append(wrapper(new ammonite.ops.Bytes(array))) - parentStream.write(array, 0, actuallyRead) - } - } - // if we did not read anything sleep briefly to avoid spinning - if(!readSomething) - Thread.sleep(2) - } - - if (proc.exitValue() != 0) throw new InteractiveShelloutException() - else ammonite.ops.CommandResult(proc.exitValue(), chunks) - } - - private def createManifest(mainClass: Option[String]) = { - val m = new java.util.jar.Manifest() - m.getMainAttributes.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0") - m.getMainAttributes.putValue( "Created-By", "Scala mill" ) - mainClass.foreach( - m.getMainAttributes.put(java.util.jar.Attributes.Name.MAIN_CLASS, _) - ) - m - } - - def createJar(inputPaths: Agg[Path], mainClass: Option[String] = None) - (implicit ctx: Ctx.Dest): PathRef = { - val outputPath = ctx.dest / "out.jar" - rm(outputPath) - - val seen = mutable.Set.empty[RelPath] - seen.add("META-INF" / "MANIFEST.MF") - val jar = new JarOutputStream( - new FileOutputStream(outputPath.toIO), - createManifest(mainClass) - ) - - try{ - assert(inputPaths.forall(exists(_))) - for{ - p <- inputPaths - (file, mapping) <- - if (p.isFile) Iterator(p -> empty/p.last) - else ls.rec(p).filter(_.isFile).map(sub => sub -> sub.relativeTo(p)) - if !seen(mapping) - } { - seen.add(mapping) - val entry = new JarEntry(mapping.toString) - entry.setTime(file.mtime.toMillis) - jar.putNextEntry(entry) - jar.write(read.bytes(file)) - jar.closeEntry() - } - } finally { - jar.close() - } - - PathRef(outputPath) - } - - def createAssembly(inputPaths: Agg[Path], - mainClass: Option[String] = None, - prependShellScript: String = "") - (implicit ctx: Ctx.Dest): PathRef = { - val outputPath = ctx.dest / "out.jar" - rm(outputPath) - - if(inputPaths.nonEmpty) { - - val output = new FileOutputStream(outputPath.toIO) - - // Prepend shell script and make it executable - if (prependShellScript.nonEmpty) { - output.write((prependShellScript + "\n").getBytes) - val perms = java.nio.file.Files.getPosixFilePermissions(outputPath.toNIO) - perms.add(PosixFilePermission.GROUP_EXECUTE) - perms.add(PosixFilePermission.OWNER_EXECUTE) - perms.add(PosixFilePermission.OTHERS_EXECUTE) - java.nio.file.Files.setPosixFilePermissions(outputPath.toNIO, perms) - } - - val jar = new JarOutputStream( - output, - createManifest(mainClass) - ) - - val seen = mutable.Set("META-INF/MANIFEST.MF") - try{ - - - for{ - p <- inputPaths - if exists(p) - (file, mapping) <- - if (p.isFile) { - val jf = new JarFile(p.toIO) - import collection.JavaConverters._ - for(entry <- jf.entries().asScala if !entry.isDirectory) yield { - read.bytes(jf.getInputStream(entry)) -> entry.getName - } - } - else { - ls.rec(p).iterator - .filter(_.isFile) - .map(sub => read.bytes(sub) -> sub.relativeTo(p).toString) - } - if !seen(mapping) - } { - seen.add(mapping) - val entry = new JarEntry(mapping.toString) - jar.putNextEntry(entry) - jar.write(file) - jar.closeEntry() - } - } finally { - jar.close() - output.close() - } - - } - PathRef(outputPath) - } - -} diff --git a/core/src/mill/modules/Util.scala b/core/src/mill/modules/Util.scala deleted file mode 100644 index cef11859..00000000 --- a/core/src/mill/modules/Util.scala +++ /dev/null @@ -1,65 +0,0 @@ -package mill.modules - - -import ammonite.ops.{Path, RelPath, empty, mkdir, read} -import mill.eval.PathRef -import mill.util.Ctx - -object Util { - def download(url: String, dest: RelPath = "download")(implicit ctx: Ctx.Dest) = { - val out = ctx.dest / dest - - val website = new java.net.URI(url).toURL - val rbc = java.nio.channels.Channels.newChannel(website.openStream) - try{ - val fos = new java.io.FileOutputStream(out.toIO) - try{ - fos.getChannel.transferFrom(rbc, 0, java.lang.Long.MAX_VALUE) - PathRef(out) - } finally{ - fos.close() - } - } finally{ - rbc.close() - } - } - - def downloadUnpackZip(url: String, dest: RelPath = "unpacked") - (implicit ctx: Ctx.Dest) = { - - val tmpName = if (dest == empty / "tmp.zip") "tmp2.zip" else "tmp.zip" - val downloaded = download(url, tmpName) - unpackZip(downloaded.path, dest) - } - - def unpackZip(src: Path, dest: RelPath = "unpacked") - (implicit ctx: Ctx.Dest) = { - - val byteStream = read.getInputStream(src) - val zipStream = new java.util.zip.ZipInputStream(byteStream) - while({ - zipStream.getNextEntry match{ - case null => false - case entry => - if (!entry.isDirectory) { - val entryDest = ctx.dest / dest / RelPath(entry.getName) - mkdir(entryDest / ammonite.ops.up) - val fileOut = new java.io.FileOutputStream(entryDest.toString) - val buffer = new Array[Byte](4096) - while ( { - zipStream.read(buffer) match { - case -1 => false - case n => - fileOut.write(buffer, 0, n) - true - } - }) () - fileOut.close() - } - zipStream.closeEntry() - true - } - })() - PathRef(ctx.dest / dest) - } -} diff --git a/core/src/mill/package.scala b/core/src/mill/package.scala deleted file mode 100644 index 93916c8b..00000000 --- a/core/src/mill/package.scala +++ /dev/null @@ -1,12 +0,0 @@ -import mill.util.JsonFormatters - -package object mill extends JsonFormatters{ - val T = define.Target - type T[T] = define.Target[T] - val PathRef = mill.eval.PathRef - type PathRef = mill.eval.PathRef - type Module = define.Module - type Cross[T] = define.Cross[T] - type Agg[T] = util.Loose.Agg[T] - val Agg = util.Loose.Agg -} diff --git a/core/src/mill/main/ParseArgs.scala b/core/src/mill/util/ParseArgs.scala index bafcd907..315edabc 100644 --- a/core/src/mill/main/ParseArgs.scala +++ b/core/src/mill/util/ParseArgs.scala @@ -1,6 +1,5 @@ -package mill.main +package mill.util -import mill.util.EitherOps import fastparse.all._ import mill.define.{Segment, Segments} diff --git a/core/src/mill/main/Router.scala b/core/src/mill/util/Router.scala index 935ffc72..f628730b 100644 --- a/core/src/mill/main/Router.scala +++ b/core/src/mill/util/Router.scala @@ -1,12 +1,12 @@ -package mill.main - +package mill.util import ammonite.main.Compat +import language.experimental.macros import scala.annotation.StaticAnnotation import scala.collection.mutable -import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context + /** * More or less a minimal version of Autowire's Server that lets you generate * a set of "routes" from the methods defined in an object, and call them @@ -24,7 +24,7 @@ object Router{ implicit def generate: Overrides = macro impl def impl(c: Context): c.Tree = { import c.universe._ - q"new _root_.mill.main.Router.Overrides(${c.internal.enclosingOwner.overrides.length})" + q"new _root_.mill.util.Router.Overrides(${c.internal.enclosingOwner.overrides.length})" } } @@ -262,8 +262,8 @@ object Router{ } def makeReadCall(dict: Map[String, String], - default: => Option[Any], - arg: ArgSig[_, _]) = { + default: => Option[Any], + arg: ArgSig[_, _]) = { read(dict, default, arg, arg.reads.reads(_)) } def makeReadVarargsCall(arg: ArgSig[_, _], values: Seq[String]) = { @@ -271,6 +271,7 @@ object Router{ } } + class Router [C <: Context](val c: C) { import c.universe._ def getValsOrMeths(curCls: Type): Iterable[MethodSymbol] = { @@ -365,7 +366,7 @@ class Router [C <: Context](val c: C) { } val argSig = q""" - mill.main.Router.ArgSig[$curCls, $docUnwrappedType]( + mill.util.Router.ArgSig[$curCls, $docUnwrappedType]( ${arg.name.toString}, ${docUnwrappedType.toString + (if(vararg) "*" else "")}, $docTree, @@ -375,12 +376,12 @@ class Router [C <: Context](val c: C) { val reader = if(vararg) q""" - mill.main.Router.makeReadVarargsCall( + mill.util.Router.makeReadVarargsCall( $argSig, $extrasSymbol ) """ else q""" - mill.main.Router.makeReadCall( + mill.util.Router.makeReadCall( $argListSymbol, $default, $argSig @@ -403,7 +404,7 @@ class Router [C <: Context](val c: C) { val res = q""" - mill.main.Router.EntryPoint[$curCls]( + mill.util.Router.EntryPoint[$curCls]( ${meth.name.toString}, scala.Seq(..$argSigs), ${methodDoc match{ @@ -412,12 +413,12 @@ class Router [C <: Context](val c: C) { }}, ${varargs.contains(true)}, ($baseArgSym: $curCls, $argListSymbol: Map[String, String], $extrasSymbol: Seq[String]) => - mill.main.Router.validate(Seq(..$readArgs)) match{ - case mill.main.Router.Result.Success(List(..$argNames)) => - mill.main.Router.Result.Success( + mill.util.Router.validate(Seq(..$readArgs)) match{ + case mill.util.Router.Result.Success(List(..$argNames)) => + mill.util.Router.Result.Success( $baseArgSym.${meth.name.toTermName}(..$argNameCasts) ) - case x: mill.main.Router.Result.Error => x + case x: mill.util.Router.Result.Error => x }, ammonite.main.Router.Overrides() ) @@ -439,4 +440,3 @@ class Router [C <: Context](val c: C) { } } } - diff --git a/core/src/mill/main/Scripts.scala b/core/src/mill/util/Scripts.scala index 334c610f..7dde8252 100644 --- a/core/src/mill/main/Scripts.scala +++ b/core/src/mill/util/Scripts.scala @@ -1,14 +1,14 @@ -package mill.main -import java.nio.file.NoSuchFileException +package mill.util +import java.nio.file.NoSuchFileException -import mill.main.Router.{ArgSig, EntryPoint} import ammonite.ops._ import ammonite.runtime.Evaluator.AmmoniteExit import ammonite.util.Name.backtickWrap import ammonite.util.Util.CodeSource import ammonite.util.{Name, Res, Util} import fastparse.utils.Utils._ +import mill.util.Router.{ArgSig, EntryPoint} /** * Logic around using Ammonite as a script-runner; invoking scripts via the diff --git a/core/src/mill/util/Watched.scala b/core/src/mill/util/Watched.scala new file mode 100644 index 00000000..f1ef4fee --- /dev/null +++ b/core/src/mill/util/Watched.scala @@ -0,0 +1,8 @@ +package mill.util + +import mill.eval.PathRef + +case class Watched[T](value: T, watched: Seq[PathRef]) +object Watched{ + implicit def readWrite[T: upickle.default.ReadWriter] = upickle.default.macroRW[Watched[T]] +} |