From b07bfb615c01272faa9873241dd529e3deafb99d Mon Sep 17 00:00:00 2001 From: DatLag Date: Sun, 8 Dec 2024 23:17:14 +0100 Subject: [PATCH] added target logging and prepare version catalog integration --- .../gradle/common/ExtendVersionCatalog.kt | 40 +++++++++++++++++++ .../tasks/GenerateSekretBuildScriptTask.kt | 24 ++++++----- .../sekret/gradle/tasks/GenerateSekretTask.kt | 40 ++++++++++++------- 3 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/common/ExtendVersionCatalog.kt diff --git a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/common/ExtendVersionCatalog.kt b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/common/ExtendVersionCatalog.kt new file mode 100644 index 0000000..c441424 --- /dev/null +++ b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/common/ExtendVersionCatalog.kt @@ -0,0 +1,40 @@ +package dev.datlag.sekret.gradle.common + +import org.gradle.api.Project +import org.gradle.api.artifacts.MinimalExternalModuleDependency +import org.gradle.api.artifacts.VersionCatalog +import org.gradle.api.artifacts.VersionCatalogsExtension +import org.gradle.kotlin.dsl.findByType +import org.gradle.plugin.use.PluginDependency +import kotlin.jvm.optionals.getOrNull + +internal val Project.versionCatalogs: Iterable + get() = this.extensions.findByType()?.filterNotNull().orEmpty() + +internal val Project.sekretVersionCatalog: VersionCatalog? + get() = versionCatalogs.firstOrNull { it.findPluginById("dev.datlag.sekret") != null } + +internal val Project.hasVersionCatalogs: Boolean + get() = versionCatalogs.toList().isNotEmpty() + +internal val Project.hasSekretVersionCatalog: Boolean + get() = sekretVersionCatalog != null + +internal val VersionCatalog.sekretLibraryAlias: String? + get() = findLibraryBy(group = "dev.datlag.sekret", name = "sekret")?.name?.ifBlank { null } + +internal fun VersionCatalog.findPluginById(id: String, ignoreCase: Boolean = false): PluginDependency? { + return this.pluginAliases.mapNotNull { + this.findPlugin(it).getOrNull()?.orNull + }.firstOrNull { + it.pluginId.trim().equals(id, ignoreCase = ignoreCase) + } +} + +internal fun VersionCatalog.findLibraryBy(group: String, name: String, ignoreCase: Boolean = false): MinimalExternalModuleDependency? { + return this.libraryAliases.mapNotNull { + this.findLibrary(it).getOrNull()?.orNull + }.firstOrNull { + it.module.group.trim().equals(group, ignoreCase) && it.module.name.trim().equals(name, ignoreCase) + } +} \ No newline at end of file diff --git a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretBuildScriptTask.kt b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretBuildScriptTask.kt index da0ab1c..e8ab3c5 100644 --- a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretBuildScriptTask.kt +++ b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretBuildScriptTask.kt @@ -14,6 +14,7 @@ import org.gradle.api.DefaultTask import org.gradle.api.Project import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.ProjectLayout +import org.gradle.api.logging.LogLevel import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.provider.SetProperty @@ -36,9 +37,6 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { @get:Input open val targets: SetProperty = project.objects.setProperty(Target::class.java) - @get:Input - open val sourceSets: SetProperty = project.objects.setProperty(String::class.java) - @get:OutputDirectory open val outputDirectory: DirectoryProperty = project.objects.directoryProperty() @@ -60,13 +58,20 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { return } - val allTargets = listOf( - targets.get(), - Target.fromSourceSetNames(sourceSets.get()) - ).flatten().filterNotNull() + val usedTargets = targets.get() + val requiredTargets = Target.addDependingTargets(usedTargets) + val logLevel = if (usedTargets.size <= 1) { + LogLevel.WARN + } else { + LogLevel.INFO + } + + logger.log(logLevel, "Following targets in use detected: ${usedTargets.joinToString { it.name }}.") + logger.log(logLevel, "Following targets are used/required depending on your configuration: ${requiredTargets.joinToString { it.name }}.") + logger.log(logLevel, "Please report if you encounter any missing target.") BuildFileGenerator.generate( - targets = Target.addDependingTargets(allTargets), + targets = requiredTargets, packageName = packageName.getOrElse(PropertiesExtension.sekretPackageName), outputDir = ModuleGenerator.createBase(outputDir), overwrite = true @@ -79,9 +84,6 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { targets.set(project.provider { project.targetsMapped }) - sourceSets.set(project.provider { - project.sourceSets.map { it.name } - }) outputDirectory.set(project.findProject("sekret")?.projectDir ?: File(project.projectDir, "sekret")) } diff --git a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretTask.kt b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretTask.kt index d44fbaf..e5b3d7f 100644 --- a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretTask.kt +++ b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/tasks/GenerateSekretTask.kt @@ -16,6 +16,7 @@ import org.gradle.api.Project import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty +import org.gradle.api.logging.LogLevel import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.provider.SetProperty @@ -40,7 +41,13 @@ open class GenerateSekretTask : DefaultTask() { open val targets: SetProperty = project.objects.setProperty(Target::class.java) @get:Input - open val sourceSets: SetProperty = project.objects.setProperty(String::class.java) + open val hasVersionCatalogs: Property = project.objects.property(Boolean::class.java) + + @get:Input + open val usesVersionCatalogForPlugin: Property = project.objects.property(Boolean::class.java) + + @get:Input + open val versionCatalogLibraryAlias: Property = project.objects.property(String::class.java) @get:Input open val encryptionKey: Property = project.objects.property(String::class.java) @@ -65,22 +72,27 @@ open class GenerateSekretTask : DefaultTask() { @TaskAction fun generate() { - println("Running generateSekret") if (!enabled.getOrElse(false)) { - println("Plugin not enabled, skipping") return } - val allTargets = listOf( - targets.get(), - Target.fromSourceSetNames(sourceSets.get()) - ).flatten().filterNotNull() + val sekretDir = ModuleGenerator.createBase(outputDir) + val usedTargets = targets.get() + val requiredTargets = Target.addDependingTargets(usedTargets) + val logLevel = if (usedTargets.size <= 1) { + LogLevel.WARN + } else { + LogLevel.INFO + } - println("Used targets: ${allTargets.joinToString { it.name }}") - println("\n\n") + logger.log(logLevel, "Following targets in use detected: ${usedTargets.joinToString { it.name }}.") + logger.log(logLevel, "Following targets are used/required depending on your configuration: ${requiredTargets.joinToString { it.name }}.") + logger.log(logLevel, "Please report if you encounter any missing target.") + + logger.log(LogLevel.WARN, "Has Version catalogs: ${hasVersionCatalogs.getOrElse(false)}") + logger.log(LogLevel.WARN, "Uses VersionCatalog for Plugin: ${usesVersionCatalogForPlugin.getOrElse(false)}") + logger.log(LogLevel.WARN, "Sekret VersionCatalog library alias: ${versionCatalogLibraryAlias.orNull}") - val sekretDir = ModuleGenerator.createBase(outputDir) - val requiredTargets = Target.addDependingTargets(allTargets) BuildFileGenerator.generate( targets = requiredTargets, packageName = packageName.getOrElse(PropertiesExtension.sekretPackageName), @@ -132,9 +144,9 @@ open class GenerateSekretTask : DefaultTask() { targets.set(project.provider { project.targetsMapped }) - sourceSets.set(project.provider { - project.sourceSets.map { it.name } - }) + hasVersionCatalogs.set(project.hasVersionCatalogs) + usesVersionCatalogForPlugin.set(project.hasSekretVersionCatalog) + versionCatalogLibraryAlias.set(project.sekretVersionCatalog?.sekretLibraryAlias) encryptionKey.set(extension.properties.encryptionKey) outputDirectory.set(project.findProject("sekret")?.projectDir ?: File(project.projectDir, "sekret")) propertiesFile.set(propertiesFile(project, extension.properties))