com.javiersc.hubdle.settings.HubdleSettingsPlugin.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hubdle-gradle-plugin Show documentation
Show all versions of hubdle-gradle-plugin Show documentation
Single entry point to configurate any kind of project
package com.javiersc.hubdle.settings
import com.gradle.develocity.agent.gradle.DevelocityConfiguration
import com.javiersc.gradle.properties.extensions.getStringProperty
import com.javiersc.gradle.properties.extensions.provider
import com.javiersc.hubdle.project.extensions._internal.hubdleCatalog
import com.javiersc.hubdle.project.extensions._internal.library
import com.javiersc.hubdle.project.extensions.config.HubdleConfigExtension.ProjectConfig
import com.javiersc.hubdle.project.extensions.shared.PluginId
import com.javiersc.hubdle.project.extensions.shared.PluginId.JetbrainsKotlinxKover
import com.javiersc.hubdle.settings.extensions.extractedBuildProjects
import com.javiersc.hubdle.settings.extensions.extractedProjects
import com.javiersc.hubdle.settings.tasks.GenerateHubdleCatalogTask
import java.io.File
import javax.inject.Inject
import jetbrainsComposeDev
import jetbrainsKotlinBootstrap
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.ModuleIdentifier
import org.gradle.api.initialization.Settings
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.project
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.the
import org.gradle.language.base.plugins.LifecycleBasePlugin.ASSEMBLE_TASK_NAME
public open class HubdleSettingsPlugin @Inject constructor(private val objects: ObjectFactory) :
Plugin {
override fun apply(target: Settings) {
target.enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
target.extensions.create("hubdleSettings")
target.configureRootProjectName()
target.configureRepositories()
target.configureGradleDevelocity()
target.configureKoverMergeReports()
target.gradle.settingsEvaluated { settings ->
target.createHubdleVersionCatalog()
target.configureHubdleCatalogTask()
target.useHubdleOnAllProjects()
settings.configureAutoInclude()
settings.configureAutoIncludeVersionCatalogs(objects)
}
}
private val Settings.hubdleExtension: HubdleSettingsExtension
get() = the()
private fun Settings.configureRootProjectName() {
val rootProjectName: Provider = provider {
getStringProperty(ProjectConfig.rootProjectName).orNull ?: ""
}
hubdleExtension.rootProjectName.set(rootProjectName)
rootProject.name = hubdleExtension.rootProjectName.get()
}
private fun Settings.createHubdleVersionCatalog() {
if (hubdleSettings.catalog.isEnabled.orNull == false) return
dependencyResolutionManagement.versionCatalogs.register("hubdle") { builder ->
val hubdleCatalogVersion: String = hubdleSettings.catalog.version.get()
builder.from("com.javiersc.hubdle:hubdle-version-catalog:$hubdleCatalogVersion")
val catalog = hubdleSettings.catalog
for ((alias, version) in catalog.replaceableVersions.get().entries) {
builder.version(alias, version)
}
for ((alias, version) in catalog.replaceableStrictVersions.get().entries) {
builder.version(alias) { it.strictly(version) }
}
}
}
private fun Settings.configureKoverMergeReports() {
gradle.lifecycle.beforeProject { project ->
val rootProject: Project = project.rootProject
rootProject.pluginManager.withPlugin(JetbrainsKotlinxKover.id) {
project.pluginManager.withPlugin(JetbrainsKotlinxKover.id) {
rootProject.dependencies { "kover"(project(project.path)) }
}
}
}
}
}
@DslMarker public annotation class HubdleSettingsDslMarker
internal val Settings.hubdleSettings: HubdleSettingsExtension
get() = checkNotNull(extensions.findByType())
private fun Settings.useHubdleOnAllProjects() {
val useOnAllProjects: Boolean = hubdleSettings.useOnAllProjects.get()
settings.gradle.lifecycle.beforeProject { project: Project ->
if (useOnAllProjects) {
project.pluginManager.apply(PluginId.JavierscHubdle.id)
}
}
}
private fun Settings.configureRepositories() {
dependencyResolutionManagement { management ->
management.repositories { repository ->
repository.google { google ->
google.mavenContent { content ->
content.includeGroupByRegex("androidx.*")
content.includeGroupByRegex("com\\.android.*")
content.includeGroupByRegex("com\\.google.*")
}
}
repository.mavenCentral()
repository.gradlePluginPortal()
repository.jetbrainsKotlinBootstrap()
repository.jetbrainsComposeDev()
}
}
}
private fun Settings.configureHubdleCatalogTask() {
gradle.rootProject { project ->
project.pluginManager.apply(BasePlugin::class)
val generateHubdleCatalogTask: TaskProvider =
project.tasks.register("generateHubdleCatalog")
val hubdleAliasToLibraries: Provider