Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace conventionCompat with fileCollection #1105

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pluginManagement {
}

plugins {
id("com.gradle.develocity") version "3.18.2"
id("com.gradle.develocity") version "3.19"
}

develocity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jengelman.gradle.plugins.shadow.internal

import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.JavaPlugin
Expand All @@ -26,11 +27,25 @@ internal inline fun <reified T : Any> ObjectFactory.property(defaultValue: T? =
/**
* 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 {
return fileCollection().apply {
@Suppress("UnstableApiUsage")
if (GradleVersion.current() >= GradleVersion.version("8.8")) {
convention(path())
} else {
setFrom(path())
}
}
}

/**
* TODO: this could be removed after bumping the min Gradle requirement to 8.11 or above.
*/
internal fun ProjectDependency.dependencyProjectCompat(project: Project): Project {
return if (GradleVersion.current() >= GradleVersion.version("8.11")) {
project.project(path)
} else {
setFrom(paths)
this
@Suppress("DEPRECATION")
dependencyProject
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class UnusedTracker private constructor(
apiDependencies.forEach { dep ->
when (dep) {
is ProjectDependency -> {
apiJars.addAll(getApiJarsFromProject(dep.dependencyProject))
apiJars.addAll(getApiJarsFromProject(dep.dependencyProjectCompat(project)))
addJar(runtimeConfiguration, dep, apiJars)
}
is SelfResolvingDependency -> {
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
Expand Up @@ -8,9 +8,8 @@ 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.relocation.CacheableRelocator
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
Expand All @@ -20,6 +19,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 @@ -53,6 +53,7 @@ public abstract class ShadowJar :
Jar(),
ShadowSpec {
private val dependencyFilterForMinimize = MinimizeDependencyFilter(project)
private inline val sourceSets get() = project.extensions.getByType(SourceSetContainer::class.java)

init {
// shadow filters out files later. This was the default behavior in Gradle < 6.x
Expand All @@ -68,32 +69,36 @@ 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) {
sourceSets.map { sourceSet -> sourceSet.output.classesDirs.filter(File::isDirectory) }
} else {
emptySet()
}
}
}
Expand Down Expand Up @@ -127,8 +132,9 @@ public abstract class ShadowJar :
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 +152,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
Loading