From 2083d50ccc1a7d2f390ae19ea43844bee343e89a Mon Sep 17 00:00:00 2001 From: DatLag Date: Sun, 8 Dec 2024 23:49:32 +0100 Subject: [PATCH] auto detect version catalog --- .../gradle/generator/BuildFileGenerator.kt | 5 ++- .../tasks/GenerateSekretBuildScriptTask.kt | 42 ++++++++++++++++++- .../sekret/gradle/tasks/GenerateSekretTask.kt | 26 +++++++----- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/generator/BuildFileGenerator.kt b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/generator/BuildFileGenerator.kt index 97ff711..7489c29 100644 --- a/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/generator/BuildFileGenerator.kt +++ b/sekret-gradle-plugin/src/main/java/dev/datlag/sekret/gradle/generator/BuildFileGenerator.kt @@ -13,6 +13,7 @@ object BuildFileGenerator { version: String = SekretPlugin.getVersion(), targets: Iterable, packageName: String, + versionCatalogSekretDependency: String?, outputDir: File, overwrite: Boolean = false ) { @@ -22,6 +23,7 @@ object BuildFileGenerator { .addSourceSets( version = version, commonJS = targets.any { it.isJS }, + versionCatalogSekretDependency = versionCatalogSekretDependency, sourceSets = targets.toSet() ) .endControlFlow() @@ -62,6 +64,7 @@ object BuildFileGenerator { private fun FileSpec.Builder.addSourceSets( version: String, commonJS: Boolean, + versionCatalogSekretDependency: String?, sourceSets: Set ): FileSpec.Builder { var spec = this @@ -90,7 +93,7 @@ object BuildFileGenerator { spec = spec.beginControlFlow("sourceSets") spec = spec.beginControlFlow("commonMain.dependencies") - spec = spec.addStatement("api(%S)", "dev.datlag.sekret:sekret:$version") + spec = spec.addStatement("api(%S)", versionCatalogSekretDependency ?: "dev.datlag.sekret:sekret:$version") spec = spec.endControlFlow() spec = spec.addStatement("") 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 e8ab3c5..f4ecf24 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 @@ -3,9 +3,8 @@ package dev.datlag.sekret.gradle.tasks import dev.datlag.sekret.gradle.SekretPlugin import dev.datlag.sekret.gradle.SekretPluginExtension import dev.datlag.sekret.gradle.Target -import dev.datlag.sekret.gradle.common.allTargets +import dev.datlag.sekret.gradle.common.* import dev.datlag.sekret.gradle.common.sekretExtension -import dev.datlag.sekret.gradle.common.sourceSets import dev.datlag.sekret.gradle.common.targetsMapped import dev.datlag.sekret.gradle.extension.PropertiesExtension import dev.datlag.sekret.gradle.generator.BuildFileGenerator @@ -37,6 +36,18 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { @get:Input open val targets: SetProperty = project.objects.setProperty(Target::class.java) + @get:Input + 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 versionCatalogName: Property = project.objects.property(String::class.java) + + @get:Input + open val versionCatalogLibraryAlias: Property = project.objects.property(String::class.java) + @get:OutputDirectory open val outputDirectory: DirectoryProperty = project.objects.directoryProperty() @@ -70,9 +81,21 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { 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.") + val hasVersionCatalogs = hasVersionCatalogs.getOrElse(false) ?: false + val usesVersionCatalogForPlugin = usesVersionCatalogForPlugin.getOrElse(false) ?: false + + if (hasVersionCatalogs && !usesVersionCatalogForPlugin) { + logger.log(LogLevel.WARN, "Seems like you use version catalogs but not for the sekret plugin.") + } + BuildFileGenerator.generate( targets = requiredTargets, packageName = packageName.getOrElse(PropertiesExtension.sekretPackageName), + versionCatalogSekretDependency = versionCatalogLibraryAlias.orNull?.ifBlank { null }?.let { lib -> + versionCatalogName.orNull?.ifBlank { null }?.let { catalog -> + "${catalog}.${lib}" + } + }, outputDir = ModuleGenerator.createBase(outputDir), overwrite = true ) @@ -81,9 +104,24 @@ open class GenerateSekretBuildScriptTask : DefaultTask() { fun apply(project: Project, extension: SekretPluginExtension = project.sekretExtension) { enabled.set(extension.properties.enabled) packageName.set(extension.properties.packageName) + + // Provider values are resolved lazily targets.set(project.provider { project.targetsMapped }) + hasVersionCatalogs.set(project.provider { + project.hasVersionCatalogs + }) + usesVersionCatalogForPlugin.set(project.provider { + project.hasSekretVersionCatalog + }) + versionCatalogName.set(project.provider { + project.sekretVersionCatalog?.name + }) + versionCatalogLibraryAlias.set(project.provider { + project.sekretVersionCatalog?.sekretLibraryAlias + }) + 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 e5b3d7f..64bcab8 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 @@ -41,10 +41,7 @@ open class GenerateSekretTask : DefaultTask() { open val targets: SetProperty = project.objects.setProperty(Target::class.java) @get:Input - open val hasVersionCatalogs: Property = project.objects.property(Boolean::class.java) - - @get:Input - open val usesVersionCatalogForPlugin: Property = project.objects.property(Boolean::class.java) + open val versionCatalogName: Property = project.objects.property(String::class.java) @get:Input open val versionCatalogLibraryAlias: Property = project.objects.property(String::class.java) @@ -89,13 +86,14 @@ open class GenerateSekretTask : DefaultTask() { 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}") - BuildFileGenerator.generate( targets = requiredTargets, packageName = packageName.getOrElse(PropertiesExtension.sekretPackageName), + versionCatalogSekretDependency = versionCatalogLibraryAlias.orNull?.ifBlank { null }?.let { lib -> + versionCatalogName.orNull?.ifBlank { null }?.let { catalog -> + "${catalog}.${lib}" + } + }, outputDir = sekretDir, overwrite = false ) @@ -141,12 +139,18 @@ open class GenerateSekretTask : DefaultTask() { fun apply(project: Project, extension: SekretPluginExtension = project.sekretExtension) { enabled.set(extension.properties.enabled) packageName.set(extension.properties.packageName) + + // Provider values are resolved lazily targets.set(project.provider { project.targetsMapped }) - hasVersionCatalogs.set(project.hasVersionCatalogs) - usesVersionCatalogForPlugin.set(project.hasSekretVersionCatalog) - versionCatalogLibraryAlias.set(project.sekretVersionCatalog?.sekretLibraryAlias) + versionCatalogName.set(project.provider { + project.sekretVersionCatalog?.name + }) + versionCatalogLibraryAlias.set(project.provider { + project.sekretVersionCatalog?.sekretLibraryAlias + }) + encryptionKey.set(extension.properties.encryptionKey) outputDirectory.set(project.findProject("sekret")?.projectDir ?: File(project.projectDir, "sekret")) propertiesFile.set(propertiesFile(project, extension.properties))