Skip to content

Commit

Permalink
Add DependencyConstraintFactories as they are used in multiple DSLs
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Mar 20, 2024
1 parent a6419aa commit 8c90197
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.sgtsilvio.gradle.oci.dsl

import org.gradle.api.Project
import org.gradle.api.artifacts.DependencyConstraint
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderConvertible

/**
* @author Silvio Giebl
*/
interface DependencyConstraintFactories {

fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint

fun constraint(project: Project): DependencyConstraint

fun constraint(dependencyProvider: Provider<out MinimalExternalModuleDependency>): Provider<DependencyConstraint>

fun constraint(dependencyProvider: ProviderConvertible<out MinimalExternalModuleDependency>) =
constraint(dependencyProvider.asProvider())
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package io.github.sgtsilvio.gradle.oci.dsl

import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencyConstraint
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.plugins.jvm.JvmTestSuite
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderConvertible
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.testing.Test

/**
* @author Silvio Giebl
*/
interface OciDependenciesExtension {
interface OciDependenciesExtension : DependencyConstraintFactories {

fun forTestSuite(testSuite: JvmTestSuite): OciDependencies

Expand All @@ -24,38 +19,18 @@ interface OciDependenciesExtension {

fun forTest(testTask: TaskProvider<Test>, action: Action<in OciDependencies>) = action.execute(forTest(testTask))

// constraint factories

fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint

fun constraint(project: Project): DependencyConstraint

fun constraint(dependencyProvider: Provider<out MinimalExternalModuleDependency>): Provider<DependencyConstraint>

fun constraint(dependencyProvider: ProviderConvertible<out MinimalExternalModuleDependency>) =
constraint(dependencyProvider.asProvider())

// dsl syntactic sugar

val JvmTestSuite.image get() = forTestSuite(this).image

fun JvmTestSuite.imageScope(scope: String) = forTestSuite(this).imageScope(scope)

// no dsl syntactic sugar for Test because is should not be used inside a lazy task configuration
}

interface OciDependencies {
interface OciDependencies : DependencyConstraintFactories {

val image: ResolvableOciImageDependencies

fun imageScope(scope: String): ResolvableOciImageDependencies

// constraint factories

fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint

fun constraint(project: Project): DependencyConstraint

fun constraint(dependencyProvider: Provider<out MinimalExternalModuleDependency>): Provider<DependencyConstraint>

fun constraint(dependencyProvider: ProviderConvertible<out MinimalExternalModuleDependency>) =
constraint(dependencyProvider.asProvider())
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.gradle.api.provider.ProviderConvertible
/**
* @author Silvio Giebl
*/
interface OciImageDependencies<T> {
interface OciImageDependencies<T> : DependencyConstraintFactories {

val configuration: Configuration
val set: DomainObjectSet<ModuleDependency>
Expand Down Expand Up @@ -60,17 +60,6 @@ interface OciImageDependencies<T> {
action: Action<in DependencyConstraint>,
)

// create constraint from a different notation

fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint

fun constraint(project: Project): DependencyConstraint

fun constraint(dependencyProvider: Provider<out MinimalExternalModuleDependency>): Provider<DependencyConstraint>

fun constraint(dependencyProvider: ProviderConvertible<out MinimalExternalModuleDependency>) =
constraint(dependencyProvider.asProvider())

// dsl syntactic sugar for adding dependency

operator fun invoke(dependency: ModuleDependency) = add(dependency)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.sgtsilvio.gradle.oci.internal.dsl

import io.github.sgtsilvio.gradle.oci.dsl.DependencyConstraintFactories
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencyConstraint
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
import org.gradle.api.provider.Provider

/**
* @author Silvio Giebl
*/
abstract class DependencyConstraintFactoriesImpl(
private val dependencyConstraintHandler: DependencyConstraintHandler,
) : DependencyConstraintFactories {

final override fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint =
dependencyConstraintHandler.create(dependencyConstraintNotation)

final override fun constraint(project: Project): DependencyConstraint = dependencyConstraintHandler.create(project)

private fun constraint(dependency: MinimalExternalModuleDependency): DependencyConstraint =
dependencyConstraintHandler.create(dependency)

final override fun constraint(
dependencyProvider: Provider<out MinimalExternalModuleDependency>,
): Provider<DependencyConstraint> = dependencyProvider.map { constraint(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import io.github.sgtsilvio.gradle.oci.dsl.OciDependenciesExtension
import io.github.sgtsilvio.gradle.oci.dsl.OciExtension
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies
import io.github.sgtsilvio.gradle.oci.internal.concatCamelCase
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencyConstraint
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.jvm.JvmTestSuite
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.testing.Test
Expand All @@ -31,8 +27,8 @@ internal abstract class OciDependenciesExtensionImpl @Inject constructor(
private val objectFactory: ObjectFactory,
private val taskContainer: TaskContainer,
private val projectLayout: ProjectLayout,
private val dependencyHandler: DependencyHandler,
) : OciDependenciesExtension {
dependencyConstraintHandler: DependencyConstraintHandler,
) : DependencyConstraintFactoriesImpl(dependencyConstraintHandler), OciDependenciesExtension {

private val testSuiteDependencies = HashMap<String, OciDependenciesImpl>()
private val testDependencies = HashMap<String, OciDependenciesImpl>()
Expand Down Expand Up @@ -64,27 +60,14 @@ internal abstract class OciDependenciesExtensionImpl @Inject constructor(
}
objectFactory.newInstance<OciDependenciesImpl>(testTaskName, registryDataTask, oci)
}

final override fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint =
dependencyHandler.constraints.create(dependencyConstraintNotation)

final override fun constraint(project: Project): DependencyConstraint =
dependencyHandler.constraints.create(project)

private fun constraint(dependency: MinimalExternalModuleDependency): DependencyConstraint =
dependencyHandler.constraints.create(dependency)

final override fun constraint(
dependencyProvider: Provider<out MinimalExternalModuleDependency>,
): Provider<DependencyConstraint> = dependencyProvider.map { constraint(it) }
}

internal abstract class OciDependenciesImpl @Inject constructor(
private val name: String,
private val registryDataTask: TaskProvider<OciRegistryDataTask>,
private val oci: OciExtension,
private val dependencyHandler: DependencyHandler,
) : OciDependencies {
dependencyConstraintHandler: DependencyConstraintHandler,
) : DependencyConstraintFactoriesImpl(dependencyConstraintHandler), OciDependencies {

private val scopes = HashMap<String, ResolvableOciImageDependencies>()

Expand All @@ -97,17 +80,4 @@ internal abstract class OciDependenciesImpl @Inject constructor(
}
imageDependencies
}

final override fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint =
dependencyHandler.constraints.create(dependencyConstraintNotation)

final override fun constraint(project: Project): DependencyConstraint =
dependencyHandler.constraints.create(project)

private fun constraint(dependency: MinimalExternalModuleDependency): DependencyConstraint =
dependencyHandler.constraints.create(dependency)

final override fun constraint(
dependencyProvider: Provider<out MinimalExternalModuleDependency>,
): Provider<DependencyConstraint> = dependencyProvider.map { constraint(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.gradle.kotlin.dsl.withType
internal abstract class OciImageDependenciesImpl<T>(
final override val configuration: Configuration,
private val dependencyHandler: DependencyHandler,
) : OciImageDependencies<T> {
) : DependencyConstraintFactoriesImpl(dependencyHandler.constraints), OciImageDependencies<T> {

final override val set get() = configuration.allDependencies.withType(ModuleDependency::class)

Expand Down Expand Up @@ -94,19 +94,4 @@ internal abstract class OciImageDependenciesImpl<T>(
dependencyConstraintProvider: Provider<out DependencyConstraint>,
action: Action<in DependencyConstraint>,
) = configuration.dependencyConstraints.addLater(dependencyConstraintProvider.map { action.execute(it); it })

// create constraint from a different notation

final override fun constraint(dependencyConstraintNotation: CharSequence): DependencyConstraint =
dependencyHandler.constraints.create(dependencyConstraintNotation)

final override fun constraint(project: Project): DependencyConstraint =
dependencyHandler.constraints.create(project)

private fun constraint(dependency: MinimalExternalModuleDependency): DependencyConstraint =
dependencyHandler.constraints.create(dependency)

final override fun constraint(
dependencyProvider: Provider<out MinimalExternalModuleDependency>,
): Provider<DependencyConstraint> = dependencyProvider.map { constraint(it) }
}

0 comments on commit 8c90197

Please sign in to comment.