Skip to content

Commit

Permalink
Split OciCopySpec DSL WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Feb 16, 2025
1 parent 4ef3843 commit dd162f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
67 changes: 39 additions & 28 deletions src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciCopySpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.gradle.api.Action
import org.gradle.api.provider.Property
import org.gradle.api.tasks.util.PatternFilterable

@DslMarker
annotation class OciCopySpecDsl

/**
* Set of specifications for copying files inspired by [org.gradle.api.file.CopySpec].
*
Expand All @@ -15,7 +18,7 @@ import org.gradle.api.tasks.util.PatternFilterable
*
* @author Silvio Giebl
*/
interface OciCopySpec {
interface OciCopySpec : BaseOciCopySpec {

/**
* Add source files to the current copy spec.
Expand All @@ -36,15 +39,7 @@ interface OciCopySpec {
* @param action action invoked with the created child copy spec
* @return the current copy spec
*/
fun from(source: Any, action: Action<in OciCopySpec>): OciCopySpec

/**
* Set the destination path of the current copy spec.
*
* @param destinationPath must not start with `/`, must not end with `/`
* @return the current copy spec
*/
fun into(destinationPath: String): OciCopySpec
fun from(source: Any, action: Action<in FilesOciCopySpec>): OciCopySpec

/**
* Set the destination path of a new child copy spec.
Expand All @@ -60,17 +55,43 @@ interface OciCopySpec {
fun into(destinationPath: String, action: Action<in OciCopySpec>): OciCopySpec

/**
* Inclusion and exclusion filters for source files added via [from].
* Adds the given copy spec as a child to the current copy spec.
* This allows reusing copy specs in multiple places.
*
* @param other the copy spec to add as a child to the current copy spec
* @return the current copy spec
* @throws IllegalArgumentException if adding the given copy spec as a child to the current copy spec would result in a cycle
* (the current copy spec is the same as or a transitive child of the given copy spec)
*/
fun with(other: OciCopySpec): OciCopySpec
}

interface FilesOciCopySpec : BaseOciCopySpec {

/**
* Set the destination path of the current copy spec.
*
* @param destinationPath must not start with `/`, must not end with `/`
* @return the current copy spec
*/
fun into(destinationPath: String): FilesOciCopySpec
}

@OciCopySpecDsl
interface BaseOciCopySpec {

/**
* Inclusion and exclusion filters for source files.
*/
val filter: PatternFilterable

/**
* Configure inclusion and exclusion filters for source files added via [from].
* Configure inclusion and exclusion filters for source files.
*
* @param action configuration action that can add or modify inclusion and exclusion filters
* @return the current copy spec
*/
fun filter(action: Action<in PatternFilterable>): OciCopySpec
fun filter(action: Action<in PatternFilterable>): BaseOciCopySpec

/**
* Add a file renaming rule to the current copy spec.
Expand Down Expand Up @@ -100,7 +121,7 @@ interface OciCopySpec {
* the result must not be empty and must not contain `/`
* @return the current copy spec
*/
fun rename(parentPathPattern: String, fileNameRegex: String, replacement: String): OciCopySpec
fun rename(parentPathPattern: String, fileNameRegex: String, replacement: String): BaseOciCopySpec

/**
* Add a directory movement rule to the current copy spec.
Expand Down Expand Up @@ -132,7 +153,7 @@ interface OciCopySpec {
* the result may contain multiple `/` (adding directories) but not at the start or end
* @return the current copy spec
*/
fun move(parentPathPattern: String, directoryNameRegex: String, replacement: String): OciCopySpec
fun move(parentPathPattern: String, directoryNameRegex: String, replacement: String): BaseOciCopySpec

/**
* Default (UNIX) permissions for files created at the destination.
Expand Down Expand Up @@ -161,7 +182,7 @@ interface OciCopySpec {
* @param permissions UNIX permissions, from `0000` to `0777`
* @return the current copy spec
*/
fun permissions(pathPattern: String, permissions: Int): OciCopySpec
fun permissions(pathPattern: String, permissions: Int): BaseOciCopySpec

/**
* Default user id ownership for files and directories created at the destination.
Expand All @@ -182,7 +203,7 @@ interface OciCopySpec {
* @param userId user id number
* @return the current copy spec
*/
fun userId(pathPattern: String, userId: Long): OciCopySpec
fun userId(pathPattern: String, userId: Long): BaseOciCopySpec

/**
* Default group id ownership for files and directories created at the destination.
Expand All @@ -203,16 +224,6 @@ interface OciCopySpec {
* @param groupId group id number
* @return the current copy spec
*/
fun groupId(pathPattern: String, groupId: Long): OciCopySpec
fun groupId(pathPattern: String, groupId: Long): BaseOciCopySpec

/**
* Adds the given copy spec as a child to the current copy spec.
* This allows reusing copy specs in multiple places.
*
* @param other the copy spec to add as a child to the current copy spec
* @return the current copy spec
* @throws IllegalArgumentException if adding the given copy spec as a child to the current copy spec would result in a cycle
* (the current copy spec is the same as or a transitive child of the given copy spec)
*/
fun with(other: OciCopySpec): OciCopySpec
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sgtsilvio.gradle.oci.internal.copyspec

import io.github.sgtsilvio.gradle.oci.FilesOciCopySpec
import io.github.sgtsilvio.gradle.oci.OciCopySpec
import org.gradle.api.Action
import org.gradle.api.model.ObjectFactory
Expand All @@ -16,7 +17,7 @@ import javax.inject.Inject
/**
* @author Silvio Giebl
*/
abstract class OciCopySpecImpl @Inject constructor(private val objectFactory: ObjectFactory) : OciCopySpec {
abstract class OciCopySpecImpl @Inject constructor(private val objectFactory: ObjectFactory) : OciCopySpec, FilesOciCopySpec {

val sources = objectFactory.fileCollection()
val destinationPath: Property<String> = objectFactory.property<String>().convention("")
Expand All @@ -37,7 +38,7 @@ abstract class OciCopySpecImpl @Inject constructor(private val objectFactory: Ob
return this
}

final override fun from(source: Any, action: Action<in OciCopySpec>): OciCopySpecImpl {
final override fun from(source: Any, action: Action<in FilesOciCopySpec>): OciCopySpecImpl {
addChild({ from(source) }, action)
return this
}
Expand All @@ -61,12 +62,11 @@ abstract class OciCopySpecImpl @Inject constructor(private val objectFactory: Ob
return this
}

private inline fun addChild(init: OciCopySpecImpl.() -> Unit, userAction: Action<in OciCopySpec>): OciCopySpecImpl {
private inline fun addChild(init: OciCopySpecImpl.() -> Unit, userAction: Action<in OciCopySpecImpl>) {
val child = objectFactory.newInstance<OciCopySpecImpl>()
child.init() // invoke init before adding the child as it performs validations
children += child
userAction.execute(child)
return child
}

final override fun filter(action: Action<in PatternFilterable>): OciCopySpecImpl {
Expand Down

0 comments on commit dd162f2

Please sign in to comment.