Skip to content

Commit

Permalink
Simplify property initializations (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler authored Dec 16, 2024
1 parent eb61719 commit 0bd16c3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.SetProperty
import org.gradle.util.GradleVersion

/**
Expand All @@ -18,21 +21,53 @@ internal inline val Project.runtimeConfiguration: Configuration
?: configurations.getByName("runtime")
}

internal inline fun <reified T : Any> ObjectFactory.property(defaultValue: T? = null): Property<T> {
return property(T::class.java).apply {
if (defaultValue != null) convention(defaultValue)
internal inline fun <reified V : Any> ObjectFactory.property(
defaultValue: Any? = null,
): Property<V> = property(V::class.java).apply {
defaultValue ?: return@apply
if (defaultValue is Provider<*>) {
@Suppress("UNCHECKED_CAST")
convention(defaultValue as Provider<V>)
} else {
convention(defaultValue as V)
}
}

@Suppress("UNCHECKED_CAST")
internal inline fun <reified V : Any> ObjectFactory.setProperty(
defaultValue: Any? = null,
): SetProperty<V> = setProperty(V::class.java).apply {
defaultValue ?: return@apply
if (defaultValue is Provider<*>) {
convention(defaultValue as Provider<Iterable<V>>)
} else {
convention(defaultValue as Iterable<V>)
}
}

@Suppress("UNCHECKED_CAST")
internal inline fun <reified V : Any> ObjectFactory.mapProperty(
defaultValue: Any? = null,
): MapProperty<String, V> = mapProperty(String::class.java, V::class.java).apply {
defaultValue ?: return@apply
if (defaultValue is Provider<*>) {
convention(defaultValue as Provider<Map<String, V>>)
} else {
convention(defaultValue as Map<String, V>)
}
}

/**
* TODO: this could be removed after bumping the min Gradle requirement to 8.8 or above.
*/
internal fun ConfigurableFileCollection.conventionCompat(vararg paths: Any): ConfigurableFileCollection {
return if (GradleVersion.current() >= GradleVersion.version("8.8")) {
convention(paths)
internal inline fun ObjectFactory.fileCollection(
path: () -> Any,
): ConfigurableFileCollection = fileCollection().apply {
@Suppress("UnstableApiUsage")
if (GradleVersion.current() >= GradleVersion.version("8.8")) {
convention(path())
} else {
setFrom(paths)
this
setFrom(path())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ internal fun createDefaultFileTreeElement(
return DefaultFileTreeElement(file, relativePath, chmod, stat)
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> unsafeLazy(noinline initializer: () -> T): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE, initializer)
}

internal fun Properties.inputStream(
charset: Charset = Charsets.ISO_8859_1,
comments: String = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow.relocation

import com.github.jengelman.gradle.plugins.shadow.internal.setProperty
import java.util.regex.Pattern
import org.codehaus.plexus.util.SelectorUtils
import org.gradle.api.model.ObjectFactory
Expand Down Expand Up @@ -30,10 +31,10 @@ public open class SimpleRelocator @JvmOverloads constructor(
private val sourcePathExcludes = mutableSetOf<String>()

@get:Input
public val includes: SetProperty<String> = objectFactory.setProperty(String::class.java)
public val includes: SetProperty<String> = objectFactory.setProperty()

@get:Input
public val excludes: SetProperty<String> = objectFactory.setProperty(String::class.java)
public val excludes: SetProperty<String> = objectFactory.setProperty()

init {
if (rawString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter
import com.github.jengelman.gradle.plugins.shadow.internal.MinimizeDependencyFilter
import com.github.jengelman.gradle.plugins.shadow.internal.UnusedTracker
import com.github.jengelman.gradle.plugins.shadow.internal.ZipCompressor
import com.github.jengelman.gradle.plugins.shadow.internal.conventionCompat
import com.github.jengelman.gradle.plugins.shadow.internal.fileCollection
import com.github.jengelman.gradle.plugins.shadow.internal.property
import com.github.jengelman.gradle.plugins.shadow.internal.unsafeLazy
import com.github.jengelman.gradle.plugins.shadow.internal.setProperty
import com.github.jengelman.gradle.plugins.shadow.relocation.CacheableRelocator
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
Expand All @@ -20,6 +20,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionMo
import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer.Companion.create
import java.io.File
import java.util.jar.JarFile
import org.apache.tools.zip.ZipOutputStream
import org.gradle.api.Action
Expand Down Expand Up @@ -68,32 +69,37 @@ public abstract class ShadowJar :
@get:Internal
override val stats: ShadowStats = ShadowStats()

/**
* Minimize the jar by removing unused classes.
*
* Defaults to `false`.
*/
@get:Input
public open val minimizeJar: Property<Boolean> = objectFactory.property(false)

@get:Classpath
public open val toMinimize: ConfigurableFileCollection by unsafeLazy {
objectFactory.fileCollection().apply {
if (minimizeJar.get()) {
conventionCompat(dependencyFilterForMinimize.resolve(configurations.get()) - apiJars)
}
public open val toMinimize: ConfigurableFileCollection = objectFactory.fileCollection {
minimizeJar.map {
if (it) (dependencyFilterForMinimize.resolve(configurations.get()) - apiJars) else emptySet()
}
}

@get:Classpath
public open val apiJars: ConfigurableFileCollection by unsafeLazy {
objectFactory.fileCollection().apply {
if (minimizeJar.get()) {
conventionCompat(UnusedTracker.getApiJarsFromProject(project))
}
public open val apiJars: ConfigurableFileCollection = objectFactory.fileCollection {
minimizeJar.map {
if (it) UnusedTracker.getApiJarsFromProject(project) else emptySet()
}
}

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
public open val sourceSetsClassesDirs: ConfigurableFileCollection by unsafeLazy {
objectFactory.fileCollection().apply {
if (minimizeJar.get()) {
project.extensions.getByType(SourceSetContainer::class.java).forEach { sourceSet ->
from(sourceSet.output.classesDirs.filter { it.isDirectory })
}
public open val sourceSetsClassesDirs: ConfigurableFileCollection = objectFactory.fileCollection {
minimizeJar.map {
if (it) {
project.extensions.getByType(SourceSetContainer::class.java)
.map { sourceSet -> sourceSet.output.classesDirs.filter(File::isDirectory) }
} else {
emptySet()
}
}
}
Expand All @@ -113,22 +119,23 @@ public abstract class ShadowJar :
}

@get:Nested
public open val transformers: SetProperty<Transformer> = objectFactory.setProperty(Transformer::class.java)
public open val transformers: SetProperty<Transformer> = objectFactory.setProperty()

@get:Nested
public open val relocators: SetProperty<Relocator> = objectFactory.setProperty(Relocator::class.java)
public open val relocators: SetProperty<Relocator> = objectFactory.setProperty()

@get:Classpath
@get:Optional
public open val configurations: SetProperty<Configuration> = objectFactory.setProperty(Configuration::class.java)
public open val configurations: SetProperty<Configuration> = objectFactory.setProperty()

@get:Internal
public open val dependencyFilter: Property<DependencyFilter> =
objectFactory.property(DefaultDependencyFilter(project))

@get:Classpath
public open val includedDependencies: ConfigurableFileCollection = objectFactory.fileCollection()
.conventionCompat(dependencyFilter.map { it.resolve(configurations.get()) })
public open val includedDependencies: ConfigurableFileCollection = objectFactory.fileCollection {
dependencyFilter.zip(configurations) { df, cs -> df.resolve(cs) }
}

/**
* Enable relocation of packages in the jar.
Expand All @@ -146,14 +153,6 @@ public abstract class ShadowJar :
@get:Input
public open val relocationPrefix: Property<String> = objectFactory.property(ShadowBasePlugin.SHADOW)

/**
* Minimize the jar by removing unused classes.
*
* Defaults to `false`.
*/
@get:Input
public open val minimizeJar: Property<Boolean> = objectFactory.property(false)

@Internal
override fun getManifest(): InheritManifest = super.manifest as InheritManifest

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.internal.setProperty
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.jar.JarFile.MANIFEST_NAME
Expand All @@ -25,10 +26,8 @@ public open class ManifestAppenderTransformer @Inject constructor(
) : Transformer {
private var manifestContents = ByteArray(0)

@Suppress("UNCHECKED_CAST")
@get:Input
public open val attributes: SetProperty<Pair<String, Comparable<*>>> =
objectFactory.setProperty(Pair::class.java) as SetProperty<Pair<String, Comparable<*>>>
public open val attributes: SetProperty<Pair<String, Comparable<*>>> = objectFactory.setProperty()

override fun canTransformResource(element: FileTreeElement): Boolean {
return MANIFEST_NAME.equals(element.relativePath.pathString, ignoreCase = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.internal.mapProperty
import com.github.jengelman.gradle.plugins.shadow.internal.property
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext.Companion.getEntryTimestamp
import java.io.IOException
Expand Down Expand Up @@ -39,8 +40,7 @@ public open class ManifestResourceTransformer @Inject constructor(

@get:Optional
@get:Input
public open val manifestEntries: MapProperty<String, Attributes> =
objectFactory.mapProperty(String::class.java, Attributes::class.java)
public open val manifestEntries: MapProperty<String, Attributes> = objectFactory.mapProperty()

override fun canTransformResource(element: FileTreeElement): Boolean {
val path = element.relativePath.pathString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.internal.CleanProperties
import com.github.jengelman.gradle.plugins.shadow.internal.inputStream
import com.github.jengelman.gradle.plugins.shadow.internal.mapProperty
import com.github.jengelman.gradle.plugins.shadow.internal.property
import com.github.jengelman.gradle.plugins.shadow.internal.setProperty
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy
import groovy.lang.Closure
import groovy.lang.Closure.IDENTITY
Expand Down Expand Up @@ -107,12 +109,10 @@ public open class PropertiesFileTransformer @Inject constructor(
internal val propertiesEntries = mutableMapOf<String, CleanProperties>()

@get:Input
public open val paths: SetProperty<String> = objectFactory.setProperty(String::class.java)
public open val paths: SetProperty<String> = objectFactory.setProperty()

@Suppress("UNCHECKED_CAST")
@get:Input
public open val mappings: MapProperty<String, Map<String, String>> =
objectFactory.mapProperty(String::class.java, Map::class.java) as MapProperty<String, Map<String, String>>
public open val mappings: MapProperty<String, Map<String, String>> = objectFactory.mapProperty()

@get:Input
public open val mergeStrategy: Property<MergeStrategy> = objectFactory.property(MergeStrategy.First)
Expand All @@ -123,10 +123,8 @@ public open class PropertiesFileTransformer @Inject constructor(
@get:Input
public open val charsetName: Property<String> = objectFactory.property(Charsets.ISO_8859_1.name())

@Suppress("UNCHECKED_CAST")
@get:Internal
public open val keyTransformer: Property<Closure<String>> =
objectFactory.property(IDENTITY) as Property<Closure<String>>
public open val keyTransformer: Property<Closure<String>> = objectFactory.property(IDENTITY)

override fun canTransformResource(element: FileTreeElement): Boolean {
val mappings = mappings.get()
Expand Down

0 comments on commit 0bd16c3

Please sign in to comment.