Skip to content

Commit

Permalink
Reduce bytecode size: add DependencyFactories.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 8, 2024
1 parent be28a91 commit e54f0b9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import io.github.sgtsilvio.gradle.oci.attributes.*
import io.github.sgtsilvio.gradle.oci.component.*
import io.github.sgtsilvio.gradle.oci.dsl.OciImageDefinition
import io.github.sgtsilvio.gradle.oci.internal.*
import io.github.sgtsilvio.gradle.oci.internal.gradle.LazyPublishArtifact
import io.github.sgtsilvio.gradle.oci.internal.gradle.addArtifacts
import io.github.sgtsilvio.gradle.oci.internal.gradle.zipAbsentAsNull
import io.github.sgtsilvio.gradle.oci.internal.gradle.*
import io.github.sgtsilvio.gradle.oci.mapping.defaultMappedImageNamespace
import io.github.sgtsilvio.gradle.oci.metadata.OciImageReference
import io.github.sgtsilvio.gradle.oci.platform.AllPlatformFilter
Expand Down Expand Up @@ -99,13 +97,7 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
}

private fun createDependency(): Provider<ProjectDependency> = capabilities.set.map { capabilities ->
val projectDependency = project.dependencies.create(project) as ProjectDependency
projectDependency.capabilities {
for (capability in capabilities) {
requireCapability("${capability.group}:${capability.name}")
}
}
projectDependency
project.createDependency().requireCapabilities(capabilities)
}

final override fun getName() = name
Expand Down Expand Up @@ -531,13 +523,7 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
}

fun createDependency(): Provider<ProjectDependency> = providerFactory.provider {
val projectDependency = project.dependencies.create(project) as ProjectDependency
projectDependency.capabilities {
for (capability in configuration.outgoing.capabilities) {
requireCapability("${capability.group}:${capability.name}")
}
}
projectDependency
project.createDependency().requireCapabilities(configuration.outgoing.capabilities)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sgtsilvio.gradle.oci.internal.dsl

import io.github.sgtsilvio.gradle.oci.dsl.OciImageDependencies
import io.github.sgtsilvio.gradle.oci.internal.gradle.createDependency
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.*
Expand Down Expand Up @@ -50,16 +51,15 @@ internal abstract class OciImageDependenciesImpl<T>(
private fun module(dependencyNotation: CharSequence) =
dependencyHandler.create(dependencyNotation) as ExternalModuleDependency

private fun project(project: Project) = dependencyHandler.create(project) as ProjectDependency

final override fun add(dependencyNotation: CharSequence) = add(module(dependencyNotation))

final override fun add(dependencyNotation: CharSequence, action: Action<in ExternalModuleDependency>) =
add(module(dependencyNotation), action)

final override fun add(project: Project) = add(project(project))
final override fun add(project: Project) = add(project.createDependency())

final override fun add(project: Project, action: Action<in ProjectDependency>) = add(project(project), action)
final override fun add(project: Project, action: Action<in ProjectDependency>) =
add(project.createDependency(), action)

// add constraint

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.github.sgtsilvio.gradle.oci.component.resolveOciImageSpecs
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies.Nameable
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies.Taggable
import io.github.sgtsilvio.gradle.oci.internal.gradle.attribute
import io.github.sgtsilvio.gradle.oci.internal.gradle.zipAbsentAsNull
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.ModuleDependency
Expand Down Expand Up @@ -88,19 +89,15 @@ internal abstract class ResolvableOciImageDependenciesImpl @Inject constructor(
final override fun addInternal(dependency: ModuleDependency): ReferenceSpecBuilder {
val referenceSpecBuilder = ReferenceSpecBuilder(objectFactory)
configuration.dependencies.addLater(referenceSpecBuilder.attribute.map { attribute ->
dependency.attributes {
attribute(OCI_IMAGE_REFERENCE_ATTRIBUTE, attribute)
}
dependency.attribute(OCI_IMAGE_REFERENCE_ATTRIBUTE, attribute)
})
return referenceSpecBuilder
}

final override fun addInternal(dependencyProvider: Provider<out ModuleDependency>): ReferenceSpecBuilder {
val referenceSpecBuilder = ReferenceSpecBuilder(objectFactory)
configuration.dependencies.addLater(dependencyProvider.zip(referenceSpecBuilder.attribute) { dependency, attribute ->
dependency.attributes {
attribute(OCI_IMAGE_REFERENCE_ATTRIBUTE, attribute)
}
dependency.attribute(OCI_IMAGE_REFERENCE_ATTRIBUTE, attribute)
})
return referenceSpecBuilder
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.sgtsilvio.gradle.oci.internal.gradle

import org.gradle.api.Project
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.attributes.Attribute
import org.gradle.api.capabilities.Capability

internal fun Project.createDependency() = project.dependencies.create(project) as ProjectDependency

internal fun <T : ModuleDependency> T.requireCapabilities(capabilities: Iterable<Capability>): T {
capabilities {
for (capability in capabilities) {
requireCapability("${capability.group}:${capability.name}")
}
}
return this
}

internal fun <T : ModuleDependency, A: Any> T.attribute(key: Attribute<A>, value: A): T {
attributes {
attribute(key, value)
}
return this
}

0 comments on commit e54f0b9

Please sign in to comment.