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

Move tracking unused classes logic out of ShadowCopyAction #1257

Merged
merged 1 commit into from
Feb 17, 2025
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 api/shadow.api
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/tasks

public class com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction : org/gradle/api/internal/file/copy/CopyAction {
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction$Companion;
public fun <init> (Ljava/io/File;Lcom/github/jengelman/gradle/plugins/shadow/internal/ZipCompressor;Lorg/gradle/api/internal/DocumentationRegistry;Ljava/lang/String;Ljava/util/Set;Ljava/util/Set;Lorg/gradle/api/tasks/util/PatternSet;Lcom/github/jengelman/gradle/plugins/shadow/ShadowStats;ZZ)V
public fun <init> (Ljava/io/File;Lcom/github/jengelman/gradle/plugins/shadow/internal/ZipCompressor;Lorg/gradle/api/internal/DocumentationRegistry;Ljava/lang/String;Ljava/util/Set;Ljava/util/Set;Lorg/gradle/api/tasks/util/PatternSet;Lcom/github/jengelman/gradle/plugins/shadow/ShadowStats;ZLjava/util/Set;)V
public fun execute (Lorg/gradle/api/internal/file/copy/CopyActionProcessingStream;)Lorg/gradle/api/tasks/WorkResult;
}

Expand Down
4 changes: 4 additions & 0 deletions src/docs/changes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

- Mark `Transformer` as throwing `IOException`. ([#1248](https://github.com/GradleUp/shadow/pull/1248))

**Changed**

- **BREAKING CHANGE:** Move tracking unused classes logic out of `ShadowCopyAction`. ([#1257](https://github.com/GradleUp/shadow/pull/1257))


## [v9.0.0-beta8] (2025-02-08)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class RealStreamAction(
private val transformers: Set<Transformer>,
private val relocators: Set<Relocator>,
private val patternSet: PatternSet,
private val unused: Set<String>,
private val unusedClasses: Set<String>,
private val stats: ShadowStats,
private val zipFile: File,
private val preserveFileTimestamps: Boolean,
Expand Down Expand Up @@ -146,7 +146,7 @@ internal class RealStreamAction(
private fun isUnused(classPath: String): Boolean {
val classPathWithoutExtension = classPath.substringBeforeLast(".")
val className = classPathWithoutExtension.replace('/', '.')
val result = unused.contains(className)
val result = unusedClasses.contains(className)
if (result) {
logger.debug("Dropping unused class: $className")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.tasks
import com.github.jengelman.gradle.plugins.shadow.ShadowStats
import com.github.jengelman.gradle.plugins.shadow.internal.RealStreamAction
import com.github.jengelman.gradle.plugins.shadow.internal.RealStreamAction.Companion.CLASS_SUFFIX
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.createDefaultFileTreeElement
import com.github.jengelman.gradle.plugins.shadow.internal.zipEntry
Expand Down Expand Up @@ -36,7 +35,7 @@ import org.gradle.api.tasks.util.PatternSet
/**
* Modified from [org.gradle.api.internal.file.archive.ZipCopyAction.java](https://github.com/gradle/gradle/blob/b893c2b085046677cf858fb3d5ce00e68e556c3a/platforms/core-configuration/file-operations/src/main/java/org/gradle/api/internal/file/archive/ZipCopyAction.java).
*/
public open class ShadowCopyAction internal constructor(
public open class ShadowCopyAction(
private val zipFile: File,
private val compressor: ZipCompressor,
private val documentationRegistry: DocumentationRegistry,
Expand All @@ -46,52 +45,10 @@ public open class ShadowCopyAction internal constructor(
private val patternSet: PatternSet,
private val stats: ShadowStats,
private val preserveFileTimestamps: Boolean,
private val minimizeJar: Boolean,
private val unusedTracker: UnusedTracker?,
private val unusedClasses: Set<String>,
) : CopyAction {

public constructor(
zipFile: File,
compressor: ZipCompressor,
documentationRegistry: DocumentationRegistry,
encoding: String?,
transformers: Set<Transformer>,
relocators: Set<Relocator>,
patternSet: PatternSet,
stats: ShadowStats,
preserveFileTimestamps: Boolean,
minimizeJar: Boolean,
) : this(
zipFile,
compressor,
documentationRegistry,
encoding,
transformers,
relocators,
patternSet,
stats,
preserveFileTimestamps,
minimizeJar,
null,
)

override fun execute(stream: CopyActionProcessingStream): WorkResult {
val unusedClasses = if (minimizeJar && unusedTracker != null) {
stream.process(
object : BaseStreamAction() {
override fun visitFile(fileDetails: FileCopyDetails) {
// All project sources are already present, we just need to deal with JAR dependencies.
if (fileDetails.isJar) {
unusedTracker.addDependency(fileDetails.file)
}
}
},
)
unusedTracker.findUnused()
} else {
emptySet()
}

val zipOutStream = try {
compressor.createArchiveOutputStream(zipFile)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,14 @@ public abstract class ShadowJar :

override fun createCopyAction(): CopyAction {
val documentationRegistry = services.get(DocumentationRegistry::class.java)
val unusedTracker = if (minimizeJar.get()) {
UnusedTracker.forProject(apiJars, sourceSetsClassesDirs.files, toMinimize)
val unusedClasses = if (minimizeJar.get()) {
val unusedTracker = UnusedTracker.forProject(apiJars, sourceSetsClassesDirs.files, toMinimize)
includedDependencies.files.forEach {
unusedTracker.addDependency(it)
}
unusedTracker.findUnused()
} else {
null
emptySet()
}
return ShadowCopyAction(
archiveFile.get().asFile,
Expand All @@ -288,8 +292,7 @@ public abstract class ShadowJar :
rootPatternSet,
stats,
isPreserveFileTimestamps,
minimizeJar.get(),
unusedTracker,
unusedClasses,
)
}

Expand Down