Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands: Support tasks as parameters and make overriding usable #1825

Merged
merged 2 commits into from
Apr 8, 2022
Merged
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
11 changes: 9 additions & 2 deletions main/src/mill/main/MainModule.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mill.main

import mainargs.TokensReader

import java.util.concurrent.LinkedBlockingQueue
import mill.{BuildInfo, T}
import mill.api.{Ctx, PathRef, Result, internal}
Expand Down Expand Up @@ -79,8 +81,13 @@ object MainModule {
trait MainModule extends mill.Module {

implicit def millDiscover: mill.define.Discover[_]
implicit def millScoptTasksReads[T] = new mill.main.Tasks.Scopt[T]()
implicit def millScoptEvaluatorReads[T] = new mill.main.EvaluatorScopt[T]()
// TODO: change return type to mainargs.TokensReader[Tasks[T]] when in 0.11 milestone
implicit def millScoptTasksReads[T]: Tasks.Scopt[T] = new mill.main.Tasks.Scopt[T]()
// TODO: change return type to mainargs.TokensReader[Evaluator] when in 0.11 milestone
implicit def millScoptEvaluatorReads[T]: EvaluatorScopt[T] = new mill.main.EvaluatorScopt[T]()
implicit def taskTokensReader[T](implicit
tokensReaderOfT: TokensReader[T]
): TokensReader[Task[T]] = new TaskScopt[T](tokensReaderOfT)

/**
* Show the mill version.
Expand Down
16 changes: 14 additions & 2 deletions main/src/mill/main/MainScopts.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mill.main

import mainargs.TokensReader
import mill.eval.Evaluator
import mill.define.SelectMode
import mill.define.{SelectMode, Target, Task}

case class Tasks[T](value: Seq[mill.define.NamedTask[T]])

object Tasks {

class Scopt[T]()
extends mainargs.TokensReader[Tasks[T]](
shortName = "<tasks>",
Expand All @@ -30,3 +30,15 @@ class EvaluatorScopt[T]()
allowEmpty = true,
noTokens = true
)

/**
* Transparently handle `Task[T]` like simple `T` but lift the result into a T.task.
*/
class TaskScopt[T](tokensReaderOfT: TokensReader[T])
extends mainargs.TokensReader[Task[T]](
shortName = tokensReaderOfT.shortName,
read = s => tokensReaderOfT.read(s).map(t => Target.task(t)),
alwaysRepeatable = tokensReaderOfT.alwaysRepeatable,
allowEmpty = tokensReaderOfT.allowEmpty,
noTokens = tokensReaderOfT.noTokens
)