com.autonomousapps.DependencyAnalysisPlugin.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
@file:Suppress("UnstableApiUsage")
package com.autonomousapps
import com.autonomousapps.Flags.compatibility
import com.autonomousapps.internal.android.AgpVersion
import com.autonomousapps.subplugin.ProjectPlugin
import com.autonomousapps.subplugin.RootPlugin
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
internal const val TASK_GROUP_DEP = "dependency-analysis"
internal const val TASK_GROUP_DEP_INTERNAL = "dependency-analysis-internal"
@Suppress("unused")
class DependencyAnalysisPlugin : Plugin {
override fun apply(project: Project): Unit = project.run {
applyForRoot()
checkPluginWasAppliedToRoot()
applyForProject()
}
/** If this is the root project, apply configuration necessary for the root. */
private fun Project.applyForRoot() {
if (this == rootProject) {
checkAgpVersion()
RootPlugin(this).apply()
}
}
/** Warn Android users if they're using an untested version of AGP. */
private fun Project.checkAgpVersion() {
val current = try {
AgpVersion.current()
} catch (_: Throwable) {
logger.info("AGP not on classpath; assuming non-Android project")
return
}
logger.debug("AgpVersion = $current")
val compatibility = compatibility()
if (compatibility != Flags.Compatibility.NONE && !current.isSupported() && this == rootProject) {
val message = "The Dependency Analysis plugin is only known to work with versions of AGP between " +
"${AgpVersion.AGP_MIN.version} and ${AgpVersion.AGP_MAX.version}. You are using ${current.version}. " +
"Proceed at your own risk."
@Suppress("KotlinConstantConditions")
when (compatibility) {
Flags.Compatibility.DEBUG -> logger.debug(message)
Flags.Compatibility.WARN -> logger.warn(message)
Flags.Compatibility.ERROR -> logger.error(message)
Flags.Compatibility.NONE -> error("Not possible")
}
}
}
/** Plugin _must_ be applied to the root for it to work. */
private fun Project.checkPluginWasAppliedToRoot() {
// "test" is the name of the dummy project that Kotlin DSL applies a plugin to when generating
// script accessors
if (getExtensionOrNull() == null && rootProject.name != "test") {
throw GradleException("You must apply the plugin to the root project. Current project is $path")
}
}
/** The following configuration is used by all projects, including the root. */
private fun Project.applyForProject() {
ProjectPlugin(this).apply()
}
}