Skip to content

Commit

Permalink
chore: only suppress output of executing proguard task if '-i' is not…
Browse files Browse the repository at this point in the history
… set
  • Loading branch information
Ellet committed Jul 3, 2024
1 parent a372684 commit 171cd1b
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions buildSrc/src/main/kotlin/BuildMinimizedJarTask.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.LogLevel
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
Expand Down Expand Up @@ -138,26 +139,17 @@ open class BuildMinimizedJarTask : DefaultTask() {
generatedProguardConfigurationFiles.forEach { configuration(it) }
}

// Execute the Proguard task

// A workaround for executing ProGuard without getting the notes by disabling the logging
// when the `-i` or `--info` is not set

val oldStandardOut = System.out
val oldStandardErr = System.err
val noOpOutputStream =
object : OutputStream() {
override fun write(b: Int) {
// Do nothing
}
if (project.gradle.startParameter.logLevel != LogLevel.INFO) {
suppressOutputAndExecute {
proguardTask.actions.forEach { it.execute(proguardTask) }
}

try {
System.setOut(PrintStream(noOpOutputStream))
System.setErr(PrintStream(noOpOutputStream))

// Execute the Proguard task
} else {
proguardTask.actions.forEach { it.execute(proguardTask) }
} finally {
System.setOut(oldStandardOut)
System.setErr(oldStandardErr)
}

logResultMessage()
Expand All @@ -177,4 +169,25 @@ open class BuildMinimizedJarTask : DefaultTask() {
" The size has been reduced \uD83D\uDCC9 by $formattedPercentageDifference. Location: ${minimized.path}",
)
}

fun suppressOutputAndExecute(action: () -> Unit) {
val oldStandardOut = System.out
val oldStandardErr = System.err
val noOpOutputStream =
object : OutputStream() {
override fun write(b: Int) {
// Do nothing
}
}

try {
System.setOut(PrintStream(noOpOutputStream))
System.setErr(PrintStream(noOpOutputStream))

action()
} finally {
System.setOut(oldStandardOut)
System.setErr(oldStandardErr)
}
}
}

0 comments on commit 171cd1b

Please sign in to comment.