com.autonomousapps.internal.advice.AdvicePrinter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-analysis-gradle-plugin Show documentation
Show all versions of dependency-analysis-gradle-plugin Show documentation
Analyzes dependency usage in Android and JVM projects
package com.autonomousapps.internal.advice
import com.autonomousapps.advice.Dependency
import com.autonomousapps.internal.ConsoleReport
import org.gradle.api.GradleException
/**
* Only concerned with human-readable advice meant to be printed to the console.
*/
internal class AdvicePrinter(
private val consoleReport: ConsoleReport,
private val dependencyRenamingMap: Map? = null
) {
/**
* Returns "add-advice" (or null if none) for printing to console.
*/
fun getAddAdvice(): String? {
val undeclaredApiDeps = consoleReport.addToApiAdvice
val undeclaredImplDeps = consoleReport.addToImplAdvice
if (undeclaredApiDeps.isEmpty() && undeclaredImplDeps.isEmpty()) {
return null
}
val apiAdvice = undeclaredApiDeps.joinToString(prefix = "- ", separator = "\n- ") {
"${it.toConfiguration}(${printableIdentifier(it.dependency)})"
}
val implAdvice = undeclaredImplDeps.joinToString(prefix = "- ", separator = "\n- ") {
"${it.toConfiguration}(${printableIdentifier(it.dependency)})"
}
return if (undeclaredApiDeps.isNotEmpty() && undeclaredImplDeps.isNotEmpty()) {
"$apiAdvice\n$implAdvice"
} else if (undeclaredApiDeps.isNotEmpty()) {
apiAdvice
} else if (undeclaredImplDeps.isNotEmpty()) {
implAdvice
} else {
// One or the other list must be non-empty
throw GradleException("Impossible")
}
}
/**
* Returns "remove-advice" (or null if none) for printing to console.
*/
fun getRemoveAdvice(): String? {
val unusedDependencies = consoleReport.removeAdvice
if (unusedDependencies.isEmpty()) {
return null
}
return unusedDependencies.joinToString(prefix = "- ", separator = "\n- ") {
"${it.fromConfiguration}(${printableIdentifier(it.dependency)})"
}
}
/**
* Returns "change-advice" (or null if none) for printing to console.
*/
fun getChangeAdvice(): String? {
val changeToApi = consoleReport.changeToApiAdvice
val changeToImpl = consoleReport.changeToImplAdvice
if (changeToApi.isEmpty() && changeToImpl.isEmpty()) {
return null
}
val apiAdvice = changeToApi.joinToString(prefix = "- ", separator = "\n- ") {
"${it.toConfiguration}(${printableIdentifier(it.dependency)}) (was ${it.fromConfiguration})"
}
val implAdvice = changeToImpl.joinToString(prefix = "- ", separator = "\n- ") {
"${it.toConfiguration}(${printableIdentifier(it.dependency)}) (was ${it.fromConfiguration})"
}
return if (changeToApi.isNotEmpty() && changeToImpl.isNotEmpty()) {
"$apiAdvice\n$implAdvice"
} else if (changeToApi.isNotEmpty()) {
apiAdvice
} else if (changeToImpl.isNotEmpty()) {
implAdvice
} else {
// One or the other list must be non-empty
throw GradleException("Impossible")
}
}
/**
* Returns "compileOnly-advice" (or null if none) for printing to console.
*/
fun getCompileOnlyAdvice(): String? {
val compileOnlyDependencies = consoleReport.compileOnlyDependencies
if (compileOnlyDependencies.isEmpty()) {
return null
}
return compileOnlyDependencies.joinToString(prefix = "- ", separator = "\n- ") {
// TODO be variant-aware
"compileOnly(${printableIdentifier(it.dependency)}) (was ${it.fromConfiguration})"
}
}
fun getRemoveProcAdvice(): String? {
val unusedProcs = consoleReport.unusedProcsAdvice
if (unusedProcs.isEmpty()) {
return null
}
return unusedProcs.joinToString(prefix = "- ", separator = "\n- ") {
"${it.fromConfiguration}(${printableIdentifier(it.dependency)})"
}
}
private fun printableIdentifier(dependency: Dependency): String =
if (dependency.identifier.startsWith(":")) {
"project(\"${dependency.identifier}\")"
} else {
val dependencyId = "${dependency.identifier}:${dependency.resolvedVersion}"
dependencyRenamingMap?.getOrDefault(dependencyId, null) ?: "\"$dependencyId\""
}
}