Skip to content

Commit

Permalink
Reviewed and updated some target types (#2494)
Browse files Browse the repository at this point in the history
Reviewed return types of major modules.

1. Replaced `Input` and `Sources` with their respective target types,
e.g. `T[Seq[PathRef]]`.
2. Added missing return types for public targets
3. Changed some extension points in `GenIdeaModule` from `Command` to
`Task` and updated `IdeaConfigFile`. This is a binary breaking change,
but better matches their purpose

Pull request: #2494
  • Loading branch information
lefou authored May 18, 2023
1 parent 9191b3b commit 7411b9a
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ trait ScalaPBModule extends ScalaModule {

def scalaPBProtocPath: T[Option[String]] = T { None }

def scalaPBSources: Sources = T.sources {
def scalaPBSources: T[Seq[PathRef]] = T.sources {
millSourcePath / "protobuf"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object TutorialTests extends TestSuite {

object TutorialWithSpecificSources extends TutorialBase {
object core extends TutorialModule {
override def scalaPBSources: Sources = T.sources {
override def scalaPBSources: T[Seq[PathRef]] = T.sources {
millSourcePath / "protobuf" / "tutorial" / "Tutorial.proto"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ trait ScoverageModule extends ScalaModule { outer: ScalaModule =>
* The persistent data dir used to store scoverage coverage data.
* Use to store coverage data at compile-time and by the various report targets.
*/
def data: Persistent[PathRef] = T.persistent {
def data: T[PathRef] = T.persistent {
// via the persistent target, we ensure, the dest dir doesn't get cleared
PathRef(T.dest)
}
Expand All @@ -195,14 +195,14 @@ trait ScoverageModule extends ScalaModule { outer: ScalaModule =>
override def allSources: Target[Seq[PathRef]] = T { outer.allSources() }
override def moduleDeps: Seq[JavaModule] = outer.moduleDeps
override def compileModuleDeps: Seq[JavaModule] = outer.compileModuleDeps
override def sources: Sources = T.sources { outer.sources() }
override def resources: Sources = T.sources { outer.resources() }
override def sources: T[Seq[PathRef]] = T.sources { outer.sources() }
override def resources: T[Seq[PathRef]] = T.sources { outer.resources() }
override def scalaVersion = T { outer.scalaVersion() }
override def repositoriesTask: Task[Seq[Repository]] = T.task { outer.repositoriesTask() }
override def compileIvyDeps: Target[Loose.Agg[Dep]] = T { outer.compileIvyDeps() }
override def ivyDeps: Target[Loose.Agg[Dep]] =
override def compileIvyDeps: Target[Agg[Dep]] = T { outer.compileIvyDeps() }
override def ivyDeps: Target[Agg[Dep]] =
T { outer.ivyDeps() ++ outer.scoverageRuntimeDeps() }
override def unmanagedClasspath: Target[Loose.Agg[PathRef]] = T { outer.unmanagedClasspath() }
override def unmanagedClasspath: Target[Agg[PathRef]] = T { outer.unmanagedClasspath() }

/** Add the scoverage scalac plugin. */
override def scalacPluginIvyDeps: Target[Loose.Agg[Dep]] =
Expand Down
2 changes: 1 addition & 1 deletion contrib/twirllib/src/mill/twirllib/TwirlModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait TwirlModule extends mill.Module { twirlModule =>
*/
def twirlScalaVersion: T[String]

def twirlSources: Sources = T.sources {
def twirlSources: T[Seq[PathRef]] = T.sources {
millSourcePath / "views"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import mill._, scalalib._
trait VersionFileModule extends Module {

/** The file containing the current version. */
def versionFile: Source = T.source(millSourcePath / "version")
def versionFile: T[PathRef] = T.source(millSourcePath / "version")

/** The current version. */
def currentVersion: T[Version] = T { Version.of(os.read(versionFile().path).trim) }

/** The release version. */
def releaseVersion = T { currentVersion().asRelease }
def releaseVersion: T[Version] = T { currentVersion().asRelease }

/** The next snapshot version. */
def nextVersion(bump: String) = T.command { currentVersion().asSnapshot.bump(bump) }
def nextVersion(bump: String): Task[Version] = T.task { currentVersion().asSnapshot.bump(bump) }

/** Writes the release version to file. */
def setReleaseVersion = T {
def setReleaseVersion(): Command[Unit] = T.command {
setVersionTask(releaseVersion)()
}

/** Writes the next snapshot version to file. */
def setNextVersion(bump: String) = T.command {
def setNextVersion(bump: String): Command[Unit] = T.command {
setVersionTask(nextVersion(bump))()
}

/** Writes the given version to file. */
def setVersion(version: Version) = T.command {
setVersionTask(T.task { version })()
def setVersion(version: Task[Version]): Command[Unit] = T.command {
setVersionTask(version)()
}

protected def setVersionTask(version: T[Version]) = T.task {
protected def setVersionTask(version: Task[Version]) = T.task {
T.log.info(generateCommitMessage(version()))
writeVersionToFile(versionFile(), version())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mill.contrib.versionfile

import mill.T
import mill.api.Result
import mill.util.{TestEvaluator, TestUtil}
import utest.{TestSuite, Tests, assert, assertMatch, intercept, test}
Expand Down Expand Up @@ -114,7 +115,7 @@ object VersionFileModuleTests extends TestSuite {

test("setReleaseVersion") - workspaceTest(versions: _*) { eval =>
val expected = eval(TestModule.versionFile.releaseVersion)
val write = eval(TestModule.versionFile.setReleaseVersion)
val write = eval(TestModule.versionFile.setReleaseVersion())
val actual = eval(TestModule.versionFile.currentVersion)
assert(expected.value == actual.value)
}
Expand All @@ -129,7 +130,7 @@ object VersionFileModuleTests extends TestSuite {

test("setVersion") - workspaceTest(versions: _*) { eval =>
val expected = Version.Release(1, 2, 4)
val write = eval(TestModule.versionFile.setVersion(expected))
val write = eval(TestModule.versionFile.setVersion(T.task(expected)))
val actual = eval(TestModule.versionFile.currentVersion)
assert(actual.value == Right(expected))
}
Expand Down
12 changes: 6 additions & 6 deletions integration/feature/gen-idea/repo/extended/build.sc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import $ivy.`org.scalameta::munit:0.7.29`

import mill._
import mill.api.PathRef
import mill.scalalib
import mill.define.Command
import mill.scalalib.GenIdeaModule._
import mill.scalalib.TestModule

Expand All @@ -18,8 +18,8 @@ trait HelloWorldModule extends scalalib.ScalaModule {
override def scalaVersion = "3.0.2"
}

def ideaJavaModuleFacets(ideaConfigVersion: Int): Command[Seq[JavaFacet]] =
T.command {
def ideaJavaModuleFacets(ideaConfigVersion: Int): Task[Seq[JavaFacet]] =
T.task {
ideaConfigVersion match {
case 4 =>
Seq(
Expand Down Expand Up @@ -47,7 +47,7 @@ trait HelloWorldModule extends scalalib.ScalaModule {

override def ideaConfigFiles(
ideaConfigVersion: Int
): Command[Seq[IdeaConfigFile]] = T.command {
): Task[Seq[IdeaConfigFile]] = T.task {
ideaConfigVersion match {
case 4 =>
Seq(
Expand All @@ -59,7 +59,7 @@ trait HelloWorldModule extends scalalib.ScalaModule {
),
// components in project file
IdeaConfigFile(
name = "compiler.xml",
os.sub / "compiler.xml",
component = "AjcSettings",
config = Seq(
Element(
Expand All @@ -69,7 +69,7 @@ trait HelloWorldModule extends scalalib.ScalaModule {
)
),
IdeaConfigFile(
name = "compiler.xml",
os.sub / "compiler.xml",
component = "CompilerConfiguration",
config = Seq(
Element(
Expand Down
10 changes: 0 additions & 10 deletions main/resolve/src/mill/resolve/ParseArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import mill.util.EitherOps

import scala.annotation.tailrec

sealed trait SelectMode
object SelectMode {

/** All args are treated as targets or commands. If a `--` is detected, subsequent args are parameters to all commands. */
object Multi extends SelectMode

/** Like a combination of [[Single]] and [[Multi]], behaving like [[Single]] but using a special separator (`++`) to start parsing another target/command. */
object Separated extends SelectMode
}

object ParseArgs {

type TargetsWithParams = (Seq[(Option[Segments], Segments)], Seq[String])
Expand Down
12 changes: 12 additions & 0 deletions main/resolve/src/mill/resolve/SelectMode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mill.resolve

sealed trait SelectMode

object SelectMode {

/** All args are treated as targets or commands. If a `--` is detected, subsequent args are parameters to all commands. */
object Multi extends SelectMode

/** Like a combination of [[Single]] and [[Multi]], behaving like [[Single]] but using a special separator (`++`) to start parsing another target/command. */
object Separated extends SelectMode
}
3 changes: 1 addition & 2 deletions runner/src/mill/runner/MillBuildBootstrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import mill.api.{PathRef, Val, internal}
import mill.eval.Evaluator
import mill.main.{RootModule, RunScript}
import mill.resolve.SelectMode
import mill.main.TokenReaders._
import mill.define.{Discover, Segments}

import java.net.URLClassLoader
Expand Down Expand Up @@ -292,7 +291,7 @@ object MillBuildBootstrap {
// Copy the current location of the enclosing classes to `mill-launcher.jar`
// if it has the wrong file extension, because the Zinc incremental compiler
// doesn't recognize classpath entries without the proper file extension
val millLauncherOpt =
val millLauncherOpt: Option[os.Path] =
if (
os.isFile(selfClassLocation) &&
!Set("zip", "jar", "class").contains(selfClassLocation.ext)
Expand Down
11 changes: 5 additions & 6 deletions runner/src/mill/runner/MillBuildRootModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import coursier.Repository
import mill._
import mill.api.{Loose, PathRef, Result, internal}
import mill.define.{Caller, Discover, Target, Task}
import mill.scalalib.{BoundDep, Dep, DepSyntax, Lib, ScalaModule}
import mill.util.CoursierSupport
import mill.util.Util.millProjectModule
import mill.scalalib.{BoundDep, DepSyntax, Lib, ScalaModule}
import mill.scalalib.api.Versions
import os.{Path, rel}
import pprint.Util.literalize
Expand Down Expand Up @@ -55,7 +55,7 @@ class MillBuildRootModule()(implicit
}
}

override def scalaVersion = "2.13.10"
override def scalaVersion: T[String] = "2.13.10"

def scriptSources = T.sources {
MillBuildRootModule
Expand Down Expand Up @@ -152,7 +152,7 @@ class MillBuildRootModule()(implicit
enclosingClasspath() ++ lineNumberPluginClasspath()
}

override def scalacPluginIvyDeps = Agg(
override def scalacPluginIvyDeps: T[Agg[Dep]] = Agg(
ivy"com.lihaoyi:::scalac-mill-moduledefs-plugin:${Versions.millModuledefsVersion}"
)

Expand Down Expand Up @@ -222,7 +222,7 @@ object MillBuildRootModule {
enclosingClasspath: Seq[os.Path],
millTopLevelProjectRoot: os.Path,
cliImports: Seq[String]
) = {
): Unit = {
for (scriptSource <- scriptSources) {
val relative = scriptSource.path.relativeTo(base)
val dest = targetDest / FileImportGraph.fileImportToSegments(base, scriptSource.path, false)
Expand Down Expand Up @@ -253,8 +253,7 @@ object MillBuildRootModule {
millTopLevelProjectRoot: os.Path,
originalFilePath: os.Path,
cliImports: Seq[String]
) = {

): String = {
val superClass =
if (pkg.size <= 1 && name == "build") "_root_.mill.main.RootModule"
else {
Expand Down
10 changes: 7 additions & 3 deletions scalalib/src/mill/scalalib/Dependency.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package mill.scalalib

import mill.T
import mill.define.{Discover, ExternalModule}
import mill.define.{Command, Discover, ExternalModule}
import mill.eval.Evaluator
import mill.main.EvaluatorTokenReader
import mill.scalalib.dependency.DependencyUpdatesImpl
import mill.scalalib.dependency.updates.ModuleDependenciesUpdates

object Dependency extends ExternalModule {

/** Calculate possible dependency updates. */
def updates(ev: Evaluator, allowPreRelease: Boolean = false) =
def updates(
ev: Evaluator,
allowPreRelease: Boolean = false
): Command[Seq[ModuleDependenciesUpdates]] =
T.command {
DependencyUpdatesImpl(
ev,
Expand All @@ -21,7 +25,7 @@ object Dependency extends ExternalModule {
}

/** Show possible dependency updates. */
def showUpdates(ev: Evaluator, allowPreRelease: Boolean = false) = T.command {
def showUpdates(ev: Evaluator, allowPreRelease: Boolean = false): Command[Unit] = T.command {
DependencyUpdatesImpl.showAllUpdates(updates(ev, allowPreRelease)())
}

Expand Down
10 changes: 5 additions & 5 deletions scalalib/src/mill/scalalib/GenIdeaImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ case class GenIdeaImpl(
os.sub / wf._1 -> ideaConfigElementTemplate(wf._2)
}

type FileComponent = (SubPath, String)
type FileComponent = (SubPath, Option[String])

/** Ensure, the additional configs don't collide. */
def collisionFreeExtraConfigs(
Expand All @@ -262,7 +262,7 @@ case class GenIdeaImpl(
)
}
val msg =
s"Config collision in file `${conf.name}` and component `${conf.component}`: ${details(
s"Config collision in file `${conf.subPath}` and component `${conf.component}`: ${details(
conf.config
)} vs. ${details(existing)}"
ctx.map(_.log.error(msg))
Expand All @@ -274,7 +274,7 @@ case class GenIdeaImpl(
val fileComponentContributions: Seq[(SubPath, Elem)] =
collisionFreeExtraConfigs(configFileContributions).toSeq.map {
case (file, configs) =>
val map: Map[String, Seq[GenIdeaModule.Element]] =
val map: Map[Option[String], Seq[GenIdeaModule.Element]] =
configs
.groupBy(_.component)
.view
Expand Down Expand Up @@ -626,12 +626,12 @@ case class GenIdeaImpl(
}

def ideaConfigFileTemplate(
components: Map[String, Seq[GenIdeaModule.Element]]
components: Map[Option[String], Seq[GenIdeaModule.Element]]
): Elem = {
<project version={"" + ideaConfigVersion}>
{
components.toSeq.map { case (name, config) =>
<component name={name}>{config.map(ideaConfigElementTemplate)}</component>
<component name={name.getOrElse("")}>{config.map(ideaConfigElementTemplate)}</component>
}
}
</project>
Expand Down
Loading

0 comments on commit 7411b9a

Please sign in to comment.