-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.main.kts
239 lines (202 loc) · 8.62 KB
/
tree.main.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env kotlin
@file:Repository("https://repo1.maven.org/maven2/")
@file:DependsOn("net.mbonnin.xoxo:xoxo:0.3")
import xoxo.XmlDocument
import xoxo.XmlElement
import xoxo.toXmlDocument
import xoxo.walkElements
import java.net.URL
import kotlin.system.exitProcess
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
//region Args
val help = "--help" in args || "-h" in args
val legend = "--no-legend" !in args
val quiet = "--quiet" in args || "-q" in args
//endregion
//region Main
Cli.run {
if (help) return@run help()
if (!quiet) printName()
if (!quiet) printArgs(args)
val artifact = askForArtifact(args)
val repositories = askForRepositories(args)
val tree = Resolver(artifact, repositories)()
Printer(tree)(artifact)
}.also { exitProcess(0) }
//endregion
//region Classes
data class Artifact(
val groupId: String,
val artifactId: String,
val version: String,
val scope: String? = null
) {
val isResolvable = setOf(groupId, artifactId, version).none(String::isBlank)
&& version.firstOrNull() !in setOf('[', '(')
fun pom(repo: URL) = repo.toString().trimEnd('/')
.plus("/${groupId.replace(".", "/")}")
.plus("/$artifactId/$version/$artifactId-$version.pom")
.let(::URL)
override fun toString() = buildString {
append("$groupId:$artifactId:$version")
if (scope != null) append(" ($scope)")
}
companion object {
fun fromString(input: String?): Artifact = runCatching {
input.orEmpty().split(":").let { (groupId, artifactId, version) ->
Artifact(groupId = groupId, artifactId = artifactId, version = version)
}
}.getOrElse { throw IllegalArgumentException("Failed to parse artifact: [$input]", it) }
}
}
@OptIn(ExperimentalTime::class)
class Resolver(private val artifact: Artifact, private val repositories: List<URL>) {
private val cache = mutableMapOf<URL, Result<List<Artifact>>>()
operator fun invoke(): Map<Artifact, List<Artifact>?> {
if (!quiet) println("📡 Crawling POM files…")
val (tree, duration) = measureTimedValue { artifact.resolve(repositories) }
if (!quiet) println("⏱ Total duration: $duration\n")
return tree
}
private fun Artifact.resolve(repos: List<URL>): Map<Artifact, List<Artifact>?> = buildMap {
val queue = ArrayDeque(listOf(this@resolve))
while (queue.isNotEmpty()) {
val artifact = queue.removeFirst()
val dependencies = getOrPut(artifact) {
if (!artifact.isResolvable) emptyList()
else artifact.resolveDependencies(repos).getOrNull()
}
queue.addAll(dependencies.orEmpty().distinct().filterNot(::contains))
}
}
private fun Artifact.resolveDependencies(repositories: List<URL>): Result<List<Artifact>> =
repositories.asSequence()
.map { resolveDependencies(it) }
.firstOrNull { it.isSuccess }
?: Result.failure(Exception("Not found in any of the repositories."))
private fun Artifact.resolveDependencies(repository: URL): Result<List<Artifact>> {
if (!isResolvable) return Result.failure(Exception("Not resolvable"))
val pom = pom(repository)
return cache.getOrPut(pom) { readAndParse(pom) }
}
private fun readAndParse(pom: URL): Result<List<Artifact>> = measureTimedValue {
pom.runCatching(URL::readText)
}.let { (result, duration) ->
result
.onSuccess { if (!quiet) println("✅ $pom in $duration".let(Cli::green)) }
.onFailure { if (!quiet) println("❌ $pom ${it.javaClass.simpleName} in $duration".let(Cli::red)) }
.mapCatching(String::toXmlDocument)
.map { it.dependencies() }
}
private fun XmlDocument.dependencies() = root
.namedChildOrNull("dependencies")?.walkElements().orEmpty()
.filter { it.name == "dependency" }
.map { it.childElements.toArtifact(this) }
.toList()
private fun List<XmlElement>.toArtifact(document: XmlDocument) = Artifact(
groupId = named("groupId").resolvedTextContext(document),
artifactId = named("artifactId").resolvedTextContext(document),
version = namedOrNull("version")?.resolvedTextContext(document).orEmpty(),
scope = namedOrNull("scope")?.resolvedTextContext(document),
)
// Does not support parent pom
private fun XmlElement.resolvedTextContext(document: XmlDocument) =
textContent.replace("""\$\{(.+?)\}""".toRegex()) {
document.findProperty(it.groupValues[1]).orEmpty()
}
private fun XmlDocument.findProperty(name: String): String? = root
.namedChildOrNull("properties")
?.namedChildOrNull(name)?.textContent
}
class Printer(private val tree: Map<Artifact, List<Artifact>?>) {
operator fun invoke(root: Artifact) {
tree.printRecursive(root)
if (legend) legend()
}
private fun Map<Artifact, List<Artifact>?>.printRecursive(
artifact: Artifact,
printed: MutableSet<Artifact> = mutableSetOf(),
seen: List<Artifact> = emptyList(),
indent: String = "",
isLast: Boolean = true,
) {
print(indent + (if (seen.isEmpty()) "" else if (isLast) "└─ " else "├─ ") + artifact)
val dependencies = get(artifact)
when {
artifact in seen -> return println(" 🔁")
artifact in printed -> return println(" ↩️")
!artifact.isResolvable -> return println(" ⛔")
dependencies == null -> return println(" 💀")
else -> println()
}
printed += artifact
val childSeen by lazy { seen + artifact }
val childIndent by lazy { indent + if (seen.isEmpty()) "" else if (isLast) " " else "│ " }
fun isLast(index: Int) = index == dependencies.lastIndex
dependencies.sortedBy(Artifact::toString).forEachIndexed { idx, dep ->
printRecursive(dep, printed, childSeen, childIndent, isLast(idx))
}
}
private fun legend() = """
🔣 Legend:
- 🔁 Cyclic dependency
- ↩️ Already printed
- ⛔ Non resolvable
- 💀 Failed to resolve
""".trimIndent().let(::println)
}
//endregion
//region Cli
object Cli {
fun printName() = println("🌲 Maven Dependency Tree\n")
fun printArgs(args: Array<String>) = args.toList().takeUnless { it.isEmpty() }?.let { println("⚙️ Arguments: $it") }
private fun <T> ask(question: String, transform: (String?) -> T): T {
while (true) {
print("?".let(Cli::green) + " $question ")
kotlin.runCatching {
return transform(readLine())
}.getOrElse(::println)
}
}
fun askForArtifact(args: Array<String>): Artifact = args
.firstOrNull { !it.startsWith("-") }?.let(Artifact.Companion::fromString)
?: ask("Artifact (groupId:artifactId:version)", Artifact.Companion::fromString)
fun askForRepositories(args: Array<String>): List<URL> = args
.filter { it.startsWith("--repository=") }
.map { it.removePrefix("--repository=").removeSurrounding("\"") }
.also { if (it.any(String::isBlank)) error("Invalid repository!") }
.map(::URL)
.ifEmpty {
ask("Optional repositories (URLs)") { input ->
input?.takeUnless(String::isNullOrBlank)?.split(" ").orEmpty()
.ifEmpty(Defaults::repositories)
.map(::URL)
}
}
fun help() = println(
"""
Usage: tree.main.kts [ARGUMENTS] [OPTIONS]
Arguments:
artifact [groupId:artifactId:version] -> Name of the artifact to explore (optional)
Options:
--help, -h -> Usage info
--quiet, -q [false] -> Hide debug message
--no-legend [false] -> Hide the legend
--repository=<URL> -> Maven repository (vararg) defaults to Maven Central
""".trimIndent()
)
fun red(string: String) = "\u001B[31m$string\u001B[0m"
fun green(string: String) = "\u001B[32m$string\u001B[0m"
object Defaults {
val repositories = listOf("https://repo1.maven.org/maven2")
}
}
//endregion
//region Extensions
fun XmlElement.namedChild(name: String) = childElements.single(withName(name))
fun XmlElement.namedChildOrNull(name: String) = childElements.singleOrNull(withName(name))
fun Iterable<XmlElement>.named(name: String) = first(withName(name))
fun Iterable<XmlElement>.namedOrNull(name: String) = firstOrNull(withName(name))
fun withName(name: String): (XmlElement) -> Boolean = { it.name == name }
//endregion