Skip to content

Commit

Permalink
Use Task instead of T when constructing targets and other tasks (#…
Browse files Browse the repository at this point in the history
…3565)

Essentially a minimized binary-compatible version of
#3356, renaming all the factory
methods for various tasks to follow more standard naming conventions:

* `T {...}` ->  `Task {...}`
* `T.command {...}` ->  `Task.Command {...}`
* `T.input {...}` ->  `Task.Input {...}`
* `T.source {...}` ->  `Task.Source {...}`
* `T.sources {...}` ->  `Task.Sources {...}`
* `T.persistent {...}` ->  `Task.Persistent {...}`
* `T.task {...}` ->  `Task.Anon {...}`

The type `T[_]` remains an alias for `Target[_]`, and `Task{ ... }`
returns a `T[_]`, to maintain binary compatibility. Not quite ideal but
can probably be hand-waved away until Mill 0.13.0 when we are allowed to
break binary compatibility.

All the `T.*` operations have been duplicated to `Task.*` by sharing
them via a `trait TargetBase`, except the factory methods which were
copied over and upper-cased while the old version deprecated. I have
updated all the code and examples to use `Task` instead of `T` where
relevant. The only exceptions are the `implicit def apply`s which needed
to be manually copied without the `implicit` (otherwise the multiple
implicits cause ambiguity).

This gets us most of the user-facing benefits of
#3356 without the bin-compat
breakage: users no longer see an odd `T { ... }` syntax in the docs and
in their build files, and now see `Task { ... }` which should be much
more familiar. Although it does not allow us to do the type-hierarchy
cleanups that the other PR provides, it's still worth doing so we can
get it in in 0.12.0

The old `T { ... }` and `T.*` syntaxes should continue to work, and are
exercised via the bootstrap tests as they continue to be used in Mill's
own build. This PR should be source compatible to avoid migration pains,
and given the prevalence of `T` everywhere we probably should just
support it forever
  • Loading branch information
lihaoyi authored Sep 18, 2024
1 parent 7200a3f commit 34876b6
Show file tree
Hide file tree
Showing 203 changed files with 1,649 additions and 1,583 deletions.
8 changes: 4 additions & 4 deletions bsp/src/mill/bsp/BSP.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mill.bsp

import mill.api.{Ctx, PathRef}
import mill.{Agg, T}
import mill.{Agg, T, Task}
import mill.define.{Command, Discover, ExternalModule}
import mill.main.BuildInfo
import mill.eval.Evaluator
Expand All @@ -12,7 +12,7 @@ object BSP extends ExternalModule with CoursierModule {

lazy val millDiscover: Discover = Discover[this.type]

private def bspWorkerLibs: T[Agg[PathRef]] = T {
private def bspWorkerLibs: T[Agg[PathRef]] = Task {
millProjectModule("mill-bsp-worker", repositoriesTask())
}

Expand All @@ -30,7 +30,7 @@ object BSP extends ExternalModule with CoursierModule {
* reason, the message and stacktrace of the exception will be
* printed to stdout.
*/
def install(jobs: Int = 1): Command[(PathRef, ujson.Value)] = T.command {
def install(jobs: Int = 1): Command[(PathRef, ujson.Value)] = Task.Command {
// we create a file containing the additional jars to load
val libUrls = bspWorkerLibs().map(_.path.toNIO.toUri.toURL).iterator.toSeq
val cpFile =
Expand All @@ -50,7 +50,7 @@ object BSP extends ExternalModule with CoursierModule {
* @return The server result, indicating if mill should re-run this command or just exit.
*/
def startSession(allBootstrapEvaluators: Evaluator.AllBootstrapEvaluators)
: Command[BspServerResult] = T.command {
: Command[BspServerResult] = Task.Command {
T.log.errorStream.println("BSP/startSession: Starting BSP session")
val res = BspContext.bspServerHandle.runSession(allBootstrapEvaluators.value)
T.log.errorStream.println(s"BSP/startSession: Finished BSP session, result: ${res}")
Expand Down
19 changes: 9 additions & 10 deletions bsp/worker/src/mill/bsp/worker/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mill.bsp.worker
import ch.epfl.scala.bsp4j
import ch.epfl.scala.bsp4j._
import com.google.gson.JsonObject
import mill.T
import mill.api.{DummyTestReporter, Result, Strict}
import mill.bsp.BspServerResult
import mill.bsp.worker.Utils.{makeBuildTarget, outputPaths, sanitizeUri}
Expand Down Expand Up @@ -230,13 +229,13 @@ private class MillBuildServer(
targetIds = _ => sourcesParams.getTargets.asScala.toSeq,
tasks = {
case module: MillBuildRootModule =>
T.task {
Task.Anon {
module.scriptSources().map(p => sourceItem(p.path, false)) ++
module.sources().map(p => sourceItem(p.path, false)) ++
module.generatedSources().map(p => sourceItem(p.path, true))
}
case module: JavaModule =>
T.task {
Task.Anon {
module.sources().map(p => sourceItem(p.path, false)) ++
module.generatedSources().map(p => sourceItem(p.path, true))
}
Expand All @@ -256,7 +255,7 @@ private class MillBuildServer(
completable(s"buildtargetInverseSources ${p}") { state =>
val tasksEvaluators = state.bspModulesById.iterator.collect {
case (id, (m: JavaModule, ev)) =>
T.task {
Task.Anon {
val src = m.allSourceFiles()
val found = src.map(sanitizeUri).contains(
p.getTextDocument.getUri
Expand Down Expand Up @@ -295,7 +294,7 @@ private class MillBuildServer(
targetIds = _ => p.getTargets.asScala.toSeq,
tasks = {
case m: JavaModule =>
T.task {
Task.Anon {
(
m.defaultResolver().resolveDeps(
m.transitiveCompileIvyDeps() ++ m.transitiveIvyDeps(),
Expand Down Expand Up @@ -334,7 +333,7 @@ private class MillBuildServer(
hint = "buildTargetDependencyModules",
targetIds = _ => params.getTargets.asScala.toSeq,
tasks = { case m: JavaModule =>
T.task { (m.transitiveCompileIvyDeps(), m.transitiveIvyDeps(), m.unmanagedClasspath()) }
Task.Anon { (m.transitiveCompileIvyDeps(), m.transitiveIvyDeps(), m.unmanagedClasspath()) }
}
) {
case (
Expand Down Expand Up @@ -364,8 +363,8 @@ private class MillBuildServer(
s"buildTargetResources ${p}",
targetIds = _ => p.getTargets.asScala.toSeq,
tasks = {
case m: JavaModule => T.task { m.resources() }
case _ => T.task { Nil }
case m: JavaModule => Task.Anon { m.resources() }
case _ => Task.Anon { Nil }
}
) {
case (ev, state, id, m, resources) =>
Expand All @@ -387,7 +386,7 @@ private class MillBuildServer(
case (m: SemanticDbJavaModule, ev) if clientWantsSemanticDb =>
(m.compiledClassesAndSemanticDbFiles, ev)
case (m: JavaModule, ev) => (m.compile, ev)
case (m, ev) => T.task {
case (m, ev) => Task.Anon {
Result.Failure(
s"Don't know how to compile non-Java target ${m.bspBuildTarget.displayName}"
)
Expand Down Expand Up @@ -446,7 +445,7 @@ private class MillBuildServer(
}.get

val args = params.getArguments.getOrElse(Seq.empty[String])
val runTask = module.run(T.task(Args(args)))
val runTask = module.run(Task.Anon(Args(args)))
val runResult = ev.evaluate(
Strict.Agg(runTask),
Utils.getBspLoggedReporterPool(runParams.getOriginId, state.bspIdByModule, client),
Expand Down
4 changes: 2 additions & 2 deletions bsp/worker/src/mill/bsp/worker/MillJavaBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ch.epfl.scala.bsp4j.{
JavacOptionsParams,
JavacOptionsResult
}
import mill.T
import mill.Task
import mill.bsp.worker.Utils.sanitizeUri
import mill.scalalib.{JavaModule, SemanticDbJavaModule}

Expand All @@ -26,7 +26,7 @@ private trait MillJavaBuildServer extends JavaBuildServer { this: MillBuildServe
sem.bspCompiledClassesAndSemanticDbFiles
case _ => m.bspCompileClassesPath
}
T.task {
Task.Anon {
(
classesPathTask(),
m.javacOptions() ++ m.mandatoryJavacOptions(),
Expand Down
4 changes: 2 additions & 2 deletions bsp/worker/src/mill/bsp/worker/MillJvmBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ch.epfl.scala.bsp4j.{
JvmTestEnvironmentParams,
JvmTestEnvironmentResult
}
import mill.T
import mill.Task
import mill.bsp.worker.Utils.sanitizeUri
import mill.scalalib.api.CompilationResult
import mill.scalalib.{JavaModule, TestModule}
Expand Down Expand Up @@ -55,7 +55,7 @@ private trait MillJvmBuildServer extends JvmBuildServer { this: MillBuildServer
case m: TestModule => m.getTestEnvironmentVars()
case _ => m.compile
}
T.task {
Task.Anon {
(
m.runClasspath(),
m.forkArgs(),
Expand Down
14 changes: 7 additions & 7 deletions bsp/worker/src/mill/bsp/worker/MillScalaBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ch.epfl.scala.bsp4j.{
ScalacOptionsParams,
ScalacOptionsResult
}
import mill.{Agg, T}
import mill.{Agg, Task}
import mill.bsp.worker.Utils.sanitizeUri
import mill.util.Jvm
import mill.scalalib.{JavaModule, ScalaModule, TestModule, UnresolvedPath}
Expand All @@ -35,13 +35,13 @@ private trait MillScalaBuildServer extends ScalaBuildServer { this: MillBuildSer
case m: JavaModule =>
val scalacOptionsTask = m match {
case m: ScalaModule => m.allScalacOptions
case _ => T.task { Seq.empty[String] }
case _ => Task.Anon { Seq.empty[String] }
}

val compileClasspathTask =
if (enableJvmCompileClasspathProvider) {
// We have a dedicated request for it
T.task { Agg.empty[UnresolvedPath] }
Task.Anon { Agg.empty[UnresolvedPath] }
} else {
m.bspCompileClasspath
}
Expand All @@ -53,7 +53,7 @@ private trait MillScalaBuildServer extends ScalaBuildServer { this: MillBuildSer
m.bspCompileClassesPath
}

T.task {
Task.Anon {
(scalacOptionsTask(), compileClasspathTask(), classesPathTask())
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ private trait MillScalaBuildServer extends ScalaBuildServer { this: MillBuildSer
hint = "buildTarget/scalaMainClasses",
targetIds = _ => p.getTargets.asScala.toSeq,
tasks = { case m: JavaModule =>
T.task((m.zincWorker().worker(), m.compile(), m.forkArgs(), m.forkEnv()))
Task.Anon((m.zincWorker().worker(), m.compile(), m.forkArgs(), m.forkEnv()))
}
) {
case (ev, state, id, m: JavaModule, (worker, compile, forkArgs, forkEnv)) =>
Expand All @@ -112,9 +112,9 @@ private trait MillScalaBuildServer extends ScalaBuildServer { this: MillBuildSer
targetIds = _ => p.getTargets.asScala.toSeq,
tasks = {
case m: TestModule =>
T.task(Some((m.runClasspath(), m.testFramework(), m.testClasspath())))
Task.Anon(Some((m.runClasspath(), m.testFramework(), m.testClasspath())))
case _ =>
T.task(None)
Task.Anon(None)
}
) {
case (ev, state, id, m: TestModule, Some((classpath, testFramework, testClasspath))) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait ArtifactoryPublishModule extends PublishModule {
artifactorySnapshotUri: String = artifactorySnapshotUri,
readTimeout: Int = 60000,
connectTimeout: Int = 5000
): define.Command[Unit] = T.command {
): define.Command[Unit] = Task.Command {
val PublishModule.PublishData(artifactInfo, artifacts) = publishArtifacts()
new ArtifactoryPublisher(
artifactoryUri,
Expand Down Expand Up @@ -59,7 +59,7 @@ object ArtifactoryPublishModule extends ExternalModule {
publishArtifacts: mill.main.Tasks[PublishModule.PublishData],
readTimeout: Int = 60000,
connectTimeout: Int = 5000
) = T.command {
) = Task.Command {

val artifacts = T.sequence(publishArtifacts.value)().map {
case data @ PublishModule.PublishData(_, _) => data.withConcretePath
Expand All @@ -76,7 +76,7 @@ object ArtifactoryPublishModule extends ExternalModule {
)
}

private def checkArtifactoryCreds(credentials: String): Task[String] = T.task {
private def checkArtifactoryCreds(credentials: String): Task[String] = Task.Anon {
if (credentials.isEmpty) {
(for {
username <- T.env.get("ARTIFACTORY_USERNAME")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ trait BintrayPublishModule extends PublishModule {

def bintrayRepo: String

def bintrayPackage = T { artifactId() }
def bintrayPackage: T[String] = Task { artifactId() }

def bintrayPublishArtifacts: T[BintrayPublishData] = T {
def bintrayPublishArtifacts: T[BintrayPublishData] = Task {
val PublishModule.PublishData(artifactInfo, artifacts) = publishArtifacts()
BintrayPublishData(artifactInfo, artifacts, bintrayPackage())
}
Expand All @@ -36,7 +36,7 @@ trait BintrayPublishModule extends PublishModule {
release: Boolean = true,
readTimeout: Int = 60000,
connectTimeout: Int = 5000
): define.Command[Unit] = T.command {
): define.Command[Unit] = Task.Command {
new BintrayPublisher(
bintrayOwner,
bintrayRepo,
Expand Down Expand Up @@ -69,7 +69,7 @@ object BintrayPublishModule extends ExternalModule {
publishArtifacts: mill.main.Tasks[BintrayPublishData],
readTimeout: Int = 60000,
connectTimeout: Int = 5000
) = T.command {
) = Task.Command {
new BintrayPublisher(
bintrayOwner,
bintrayRepo,
Expand All @@ -83,7 +83,7 @@ object BintrayPublishModule extends ExternalModule {
)
}

private def checkBintrayCreds(credentials: String): Task[String] = T.task {
private def checkBintrayCreds(credentials: String): Task[String] = Task.Anon {
if (credentials.isEmpty) {
(for {
username <- T.env.get("BINTRAY_USERNAME")
Expand Down
Loading

0 comments on commit 34876b6

Please # to comment.