Skip to content

Commit

Permalink
auto detect version catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Dec 8, 2024
1 parent b07bfb6 commit 2083d50
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object BuildFileGenerator {
version: String = SekretPlugin.getVersion(),
targets: Iterable<Target>,
packageName: String,
versionCatalogSekretDependency: String?,
outputDir: File,
overwrite: Boolean = false
) {
Expand All @@ -22,6 +23,7 @@ object BuildFileGenerator {
.addSourceSets(
version = version,
commonJS = targets.any { it.isJS },
versionCatalogSekretDependency = versionCatalogSekretDependency,
sourceSets = targets.toSet()
)
.endControlFlow()
Expand Down Expand Up @@ -62,6 +64,7 @@ object BuildFileGenerator {
private fun FileSpec.Builder.addSourceSets(
version: String,
commonJS: Boolean,
versionCatalogSekretDependency: String?,
sourceSets: Set<Target>
): FileSpec.Builder {
var spec = this
Expand Down Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +36,18 @@ open class GenerateSekretBuildScriptTask : DefaultTask() {
@get:Input
open val targets: SetProperty<Target> = project.objects.setProperty(Target::class.java)

@get:Input
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 versionCatalogName: Property<String> = project.objects.property(String::class.java)

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

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

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

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

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

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

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

0 comments on commit 2083d50

Please sign in to comment.