Skip to content

Commit dc3638b

Browse files
author
Tony Robalik
committed
Improved misused-dependencies report.
1 parent 8c26eb2 commit dc3638b

File tree

1 file changed

+68
-29
lines changed

1 file changed

+68
-29
lines changed

src/main/kotlin/com/autonomousapps/DependencyMisuseTask.kt

+68-29
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,42 @@
22

33
package com.autonomousapps
44

5-
import com.autonomousapps.internal.*
6-
import kotlinx.html.*
5+
import com.autonomousapps.internal.Component
6+
import com.autonomousapps.internal.TransitiveDependency
7+
import com.autonomousapps.internal.UnusedDirectDependency
8+
import com.autonomousapps.internal.asString
9+
import com.autonomousapps.internal.fromJsonList
10+
import com.autonomousapps.internal.toJson
11+
import com.autonomousapps.internal.writeToFile
12+
import kotlinx.html.body
713
import kotlinx.html.dom.create
14+
import kotlinx.html.em
15+
import kotlinx.html.h1
16+
import kotlinx.html.head
17+
import kotlinx.html.html
18+
import kotlinx.html.li
19+
import kotlinx.html.p
20+
import kotlinx.html.strong
21+
import kotlinx.html.table
22+
import kotlinx.html.td
23+
import kotlinx.html.title
24+
import kotlinx.html.tr
25+
import kotlinx.html.ul
826
import org.gradle.api.DefaultTask
927
import org.gradle.api.artifacts.result.ResolutionResult
1028
import org.gradle.api.artifacts.result.ResolvedDependencyResult
1129
import org.gradle.api.file.FileCollection
1230
import org.gradle.api.file.RegularFileProperty
1331
import org.gradle.api.model.ObjectFactory
1432
import org.gradle.api.provider.Property
15-
import org.gradle.api.tasks.*
33+
import org.gradle.api.tasks.CacheableTask
34+
import org.gradle.api.tasks.Classpath
35+
import org.gradle.api.tasks.InputFile
36+
import org.gradle.api.tasks.Internal
37+
import org.gradle.api.tasks.OutputFile
38+
import org.gradle.api.tasks.PathSensitive
39+
import org.gradle.api.tasks.PathSensitivity
40+
import org.gradle.api.tasks.TaskAction
1641
import java.io.File
1742
import javax.inject.Inject
1843
import javax.xml.parsers.DocumentBuilderFactory
@@ -142,7 +167,8 @@ open class DependencyMisuseTask @Inject constructor(objects: ObjectFactory) : De
142167
// Reports
143168
outputUnusedDependenciesFile.writeText(unusedDepsWithTransitives.toJson())
144169
outputUsedTransitivesFile.writeText(usedTransitives.toJson())
145-
logger.quiet("""===Misused Dependencies===
170+
logger.quiet(
171+
"""===Misused Dependencies===
146172
|This report contains directly declared dependencies (in your `dependencies {}` block) which are either:
147173
| 1. Completely unused; or
148174
| 2. Unused except for transitive dependencies which _are_ used.
@@ -154,8 +180,12 @@ open class DependencyMisuseTask @Inject constructor(objects: ObjectFactory) : De
154180
|Used-transitive dependencies report: ${outputUsedTransitivesFile.path}
155181
|
156182
|Completely unused dependencies:
157-
|${if (completelyUnusedDeps.isEmpty()) "none" else completelyUnusedDeps.joinToString(separator = "\n- ", prefix = "- ")}
158-
""".trimMargin())
183+
|${if (completelyUnusedDeps.isEmpty()) "none" else completelyUnusedDeps.joinToString(
184+
separator = "\n- ",
185+
prefix = "- "
186+
)}
187+
""".trimMargin()
188+
)
159189

160190
writeHtmlReport(completelyUnusedDeps, unusedDepsWithTransitives, usedTransitives, outputHtmlFile)
161191
}
@@ -187,6 +217,9 @@ private fun writeHtmlReport(
187217
head { title("Misused Dependencies Report") }
188218
body {
189219
h1 { +"Completely unused direct dependencies" }
220+
p {
221+
em { +"You can remove these" }
222+
}
190223
table {
191224
tr {
192225
td {}
@@ -200,30 +233,10 @@ private fun writeHtmlReport(
200233
}
201234
}
202235

203-
h1 { +"Unused direct dependencies" }
204-
table {
205-
unusedDepsWithTransitives.forEachIndexed { i, unusedDep ->
206-
tr {
207-
// TODO is valign="bottom" supported?
208-
td { +"${i + 1}" }
209-
td {
210-
strong { +unusedDep.identifier }
211-
if (unusedDep.usedTransitiveDependencies.isNotEmpty()) {
212-
p {
213-
em { +"Used transitives" }
214-
ul {
215-
unusedDep.usedTransitiveDependencies.forEach {
216-
li { +it }
217-
}
218-
}
219-
}
220-
}
221-
}
222-
}
223-
}
224-
}
225-
226236
h1 { +"Used transitive dependencies" }
237+
p {
238+
em { +"You should consider declaring these as direct dependencies" }
239+
}
227240
table {
228241
tr {
229242
td {}
@@ -246,6 +259,32 @@ private fun writeHtmlReport(
246259
}
247260
}
248261
}
262+
263+
h1 { +"Unused direct dependencies" }
264+
p {
265+
em { +"You only use the transitive dependencies of these dependencies. In some cases, you can remove use of these and just declare the transitives directly. In other cases, you should continue to declare these. This report is provided for informational purposes." }
266+
}
267+
table {
268+
unusedDepsWithTransitives.forEachIndexed { i, unusedDep ->
269+
tr {
270+
// TODO is valign="bottom" supported?
271+
td { +"${i + 1}" }
272+
td {
273+
strong { +unusedDep.identifier }
274+
if (unusedDep.usedTransitiveDependencies.isNotEmpty()) {
275+
p {
276+
em { +"Used transitives" }
277+
ul {
278+
unusedDep.usedTransitiveDependencies.forEach {
279+
li { +it }
280+
}
281+
}
282+
}
283+
}
284+
}
285+
}
286+
}
287+
}
249288
}
250289
}.writeToFile(outputHtmlFile)
251290
}

0 commit comments

Comments
 (0)