Skip to content

Commit 96c13dd

Browse files
authored
Renamed mvnDepsTree to showMvnDepsTree (#5161)
Fix #4939 Also make the tree as string available via a protected task `mvnDepsTree`. Pull request: #5161
1 parent 789e684 commit 96c13dd

File tree

3 files changed

+75
-71
lines changed

3 files changed

+75
-71
lines changed

integration/feature/inspect/src/InspectTests.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ object InspectTests extends UtestIntegrationTestSuite {
8585
run
8686
)
8787

88-
assert(eval(("inspect", "core.mvnDepsTree")).isSuccess)
88+
assert(eval(("inspect", "core.showMvnDepsTree")).isSuccess)
8989

9090
val mvnDepsTree = out("inspect").json.str
9191

9292
assertGlobMatches(
93-
"""core.mvnDepsTree(JavaModule.scala:...)
93+
"""core.showMvnDepsTree(JavaModule.scala:...)
9494
| Command to print the transitive dependency tree to STDOUT.
9595
|
9696
| --inverse Invert the tree representation, so that the root is on the bottom val
@@ -131,8 +131,8 @@ object InspectTests extends UtestIntegrationTestSuite {
131131

132132
// Make sure both kebab-case and camelCase flags work, even though the
133133
// docs from `inspect` only show the kebab-case version
134-
assert(eval(("core.mvnDepsTree", "--withCompile", "--withRuntime")).isSuccess)
135-
assert(eval(("core.mvnDepsTree", "--with-compile", "--with-runtime")).isSuccess)
134+
assert(eval(("core.showMvnDepsTree", "--withCompile", "--withRuntime")).isSuccess)
135+
assert(eval(("core.showMvnDepsTree", "--with-compile", "--with-runtime")).isSuccess)
136136

137137
val basic = eval(("inspect", "basic"))
138138
assert(basic.isSuccess)

libs/scalalib/src/mill/scalalib/JavaModule.scala

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,11 +1126,11 @@ trait JavaModule
11261126
* @param whatDependsOn possible list of modules to target in the tree in order to see
11271127
* where a dependency stems from.
11281128
*/
1129-
protected def printDepsTree(
1129+
private def renderDepsTree(
11301130
inverse: Boolean,
11311131
additionalDeps: Task[Seq[BoundDep]],
11321132
whatDependsOn: List[JavaOrScalaModule]
1133-
): Task[Unit] =
1133+
): Task[String] =
11341134
Task.Anon {
11351135
val dependencies =
11361136
(additionalDeps() ++ Seq(BoundDep(coursierDependency, force = false))).iterator.to(Seq)
@@ -1148,7 +1148,7 @@ trait JavaModule
11481148
case List() =>
11491149
val mandatoryModules =
11501150
mandatoryMvnDeps().map(bindDependency()).iterator.map(_.dep.module).toSet
1151-
val (mandatory, main) = resolution.dependenciesOf(coursierDependency)
1151+
val (mandatory, main) = resolution.dependenciesOf0(coursierDependency).toTry.get
11521152
.partition(dep => mandatoryModules.contains(dep.module))
11531153
additionalDeps().iterator.toSeq.map(_.dep) ++ main ++ mandatory
11541154
case _ =>
@@ -1180,71 +1180,76 @@ trait JavaModule
11801180
.replace(":0+mill-internal ", " ")
11811181
.replace(":0+mill-internal" + System.lineSeparator(), System.lineSeparator())
11821182

1183-
println(processedTree)
1184-
1185-
()
1183+
processedTree
11861184
}
11871185

11881186
/**
11891187
* Command to print the transitive dependency tree to STDOUT.
11901188
*/
1191-
def mvnDepsTree(args: MvnDepsTreeArgs = MvnDepsTreeArgs()): Command[Unit] = {
1189+
def showMvnDepsTree(args: MvnDepsTreeArgs = MvnDepsTreeArgs()): Command[String] = {
1190+
val treeTask = mvnDepsTree(args)
1191+
Task.Command(exclusive = true) {
1192+
val rendered = treeTask()
1193+
Task.log.streams.out.println(rendered)
1194+
rendered
1195+
}
1196+
}
11921197

1193-
val (invalidModules, validModules) =
1194-
args.whatDependsOn.map(ModuleParser.javaOrScalaModule(_)).partitionMap(identity)
1198+
protected def mvnDepsTree(args: MvnDepsTreeArgs): Task[String] = {
1199+
val (invalidModules, modules) =
1200+
args.whatDependsOn.partitionMap(ModuleParser.javaOrScalaModule)
11951201

1196-
if (invalidModules.isEmpty) {
1202+
if (invalidModules.nonEmpty) {
1203+
Task.Anon {
1204+
val msg = invalidModules.mkString("\n")
1205+
Task.fail(msg)
1206+
}
1207+
} else {
11971208
(args.withCompile, args.withRuntime) match {
11981209
case (Flag(true), Flag(true)) =>
1199-
Task.Command {
1200-
printDepsTree(
1201-
args.inverse.value,
1202-
Task.Anon {
1203-
Seq(
1204-
coursierDependency.withConfiguration(cs.Configuration.provided),
1205-
coursierDependency.withConfiguration(cs.Configuration.runtime)
1206-
).map(BoundDep(_, force = false))
1207-
},
1208-
validModules
1209-
)()
1210-
}
1210+
renderDepsTree(
1211+
args.inverse.value,
1212+
Task.Anon {
1213+
Seq(
1214+
coursierDependency.withConfiguration(cs.Configuration.provided),
1215+
coursierDependency.withConfiguration(cs.Configuration.runtime)
1216+
).map(BoundDep(_, force = false))
1217+
},
1218+
modules
1219+
)
12111220
case (Flag(true), Flag(false)) =>
1212-
Task.Command {
1213-
printDepsTree(
1214-
args.inverse.value,
1215-
Task.Anon {
1216-
Seq(BoundDep(
1217-
coursierDependency.withConfiguration(cs.Configuration.provided),
1218-
force = false
1219-
))
1220-
},
1221-
validModules
1222-
)()
1223-
}
1221+
renderDepsTree(
1222+
args.inverse.value,
1223+
Task.Anon {
1224+
Seq(BoundDep(
1225+
coursierDependency.withConfiguration(cs.Configuration.provided),
1226+
force = false
1227+
))
1228+
},
1229+
modules
1230+
)
12241231
case (Flag(false), Flag(true)) =>
1225-
Task.Command {
1226-
printDepsTree(
1227-
args.inverse.value,
1228-
Task.Anon {
1229-
Seq(BoundDep(
1230-
coursierDependency.withConfiguration(cs.Configuration.runtime),
1231-
force = false
1232-
))
1233-
},
1234-
validModules
1235-
)()
1236-
}
1232+
renderDepsTree(
1233+
args.inverse.value,
1234+
Task.Anon {
1235+
Seq(BoundDep(
1236+
coursierDependency.withConfiguration(cs.Configuration.runtime),
1237+
force = false
1238+
))
1239+
},
1240+
modules
1241+
)
12371242
case _ =>
1238-
Task.Command {
1239-
printDepsTree(args.inverse.value, Task.Anon { Seq.empty[BoundDep] }, validModules)()
1240-
}
1241-
}
1242-
} else {
1243-
Task.Command {
1244-
val msg = invalidModules.mkString("\n")
1245-
Task.fail(msg)
1243+
renderDepsTree(
1244+
args.inverse.value,
1245+
Task.Anon {
1246+
Seq.empty[BoundDep]
1247+
},
1248+
modules
1249+
)
12461250
}
12471251
}
1252+
12481253
}
12491254

12501255
/**

libs/scalalib/test/src/mill/scalalib/CrossVersionTests.scala

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mill.scalalib
22

33
import mill.define.Discover
4-
import mill.define.ExecutionPaths
54
import mill.util.TokenReaders.*
65
import mill.testkit.UnitTester
76
import mill.testkit.TestRootModule
@@ -136,23 +135,15 @@ object CrossVersionTests extends TestSuite {
136135
expectedDeps: Seq[String],
137136
expectedLibs: Seq[String],
138137
expectedMvnDepsTree: Option[String] = None
139-
)(implicit
138+
)(using
140139
testPath: TestPath
141140
) = {
142141
init().scoped { eval =>
143-
eval.apply(mod.mvnDepsTree(MvnDepsTreeArgs()))
142+
val Right(result) = eval.apply(mod.showMvnDepsTree(MvnDepsTreeArgs())): @unchecked
144143

145144
expectedMvnDepsTree.foreach { tree =>
146-
if (!scala.util.Properties.isWin) {
147-
// Escape-sequence formatting isn't working under bare Windows
148-
val expectedDepsTree = tree
149-
val depsTree =
150-
os.read(ExecutionPaths.resolve(
151-
eval.outPath,
152-
mod.mvnDepsTree(MvnDepsTreeArgs())
153-
).log)
154-
assert(depsTree == expectedDepsTree)
155-
}
145+
val diffed = diff(result.value.trim, tree.trim)
146+
assert(diffed == Nil)
156147
}
157148

158149
val Right(libs) = eval.apply(mod.compileClasspath): @unchecked
@@ -162,6 +153,14 @@ object CrossVersionTests extends TestSuite {
162153
}
163154
}
164155

156+
def diff(actual: String, expected: String): List[String] = {
157+
actual.lazyZip(expected).collect {
158+
case (x, y) if x != y => s"'$x' != '$y'"
159+
}.toList ++
160+
actual.drop(expected.length).map(x => s"'$x' is unexpected") ++
161+
expected.drop(actual.length).map(y => s"'$y' is expected but missing")
162+
}
163+
165164
def tests: Tests = Tests {
166165

167166
test("StandaloneScala213") {

0 commit comments

Comments
 (0)