Skip to content

Commit

Permalink
added target logging and prepare version catalog integration
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Dec 8, 2024
1 parent 023d198 commit b07bfb6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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<VersionCatalog>
get() = this.extensions.findByType<VersionCatalogsExtension>()?.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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,9 +37,6 @@ open class GenerateSekretBuildScriptTask : DefaultTask() {
@get:Input
open val targets: SetProperty<Target> = project.objects.setProperty(Target::class.java)

@get:Input
open val sourceSets: SetProperty<String> = project.objects.setProperty(String::class.java)

@get:OutputDirectory
open val outputDirectory: DirectoryProperty = project.objects.directoryProperty()

Expand All @@ -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
Expand All @@ -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"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +41,13 @@ open class GenerateSekretTask : DefaultTask() {
open val targets: SetProperty<Target> = project.objects.setProperty(Target::class.java)

@get:Input
open val sourceSets: SetProperty<String> = project.objects.setProperty(String::class.java)
open val hasVersionCatalogs: Property<Boolean> = project.objects.property(Boolean::class.java)

@get:Input
open val usesVersionCatalogForPlugin: Property<Boolean> = project.objects.property(Boolean::class.java)

@get:Input
open val versionCatalogLibraryAlias: Property<String> = project.objects.property(String::class.java)

@get:Input
open val encryptionKey: Property<String> = project.objects.property(String::class.java)
Expand All @@ -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),
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b07bfb6

Please sign in to comment.