Skip to content

Commit

Permalink
fix: use exec providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChipWolf committed Dec 17, 2024
1 parent a8aa516 commit 5197b3f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build_snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
with:
distribution: 'temurin'
java-version: '17'
cache: 'gradle'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Grant execute permission for gradlew
run: chmod +x gradlew
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package xyz.wagyourtail.unimined.internal.minecraft.patch.forge.fg3.mcpconfig
import com.google.common.base.Stopwatch
import com.google.common.hash.Hashing
import org.gradle.api.Action
import org.gradle.api.provider.Provider
import org.gradle.process.ExecResult
import org.gradle.api.Project
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.Logger
Expand Down Expand Up @@ -211,11 +213,23 @@ data class McpExecutor(
return path
}

override fun javaexec(configurator: Action<in JavaExecSpec>) {
SubprocessExecutor.exec(project, configurator).rethrowFailure().assertNormalExitValue()
override fun javaexec(configurator: Action<JavaExecSpec>) {
// Execute the subprocess and obtain a Provider<ExecResult>
val resultProvider: Provider<ExecResult> = SubprocessExecutor.exec(project, configurator)

// Transform the Provider to include error checking
val validatedResultProvider: Provider<ExecResult> = resultProvider.map { execResult: ExecResult ->
// Handle potential execution failures
execResult.rethrowFailure()
execResult.assertNormalExitValue()
execResult // Return the ExecResult if needed for further processing
}

// Trigger the execution by calling get() synchronously
validatedResultProvider.get()
}

override val minecraftLibraries: Set<File>
get() = provider.minecraftLibraries.resolve()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,46 @@ import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.ShowStacktrace
import org.gradle.process.ExecResult
import org.gradle.process.JavaExecSpec
import org.gradle.process.ExecOperations
import org.gradle.api.provider.Provider

object SubprocessExecutor {
fun shouldShowVerboseStdout(project: Project): Boolean {

private fun shouldShowVerboseStdout(project: Project): Boolean {
// if running with INFO or DEBUG logging
return project.gradle.startParameter.logLevel < LogLevel.LIFECYCLE
}

fun shouldShowVerboseStderr(project: Project): Boolean {
private fun shouldShowVerboseStderr(project: Project): Boolean {
// if stdout is shown or stacktraces are visible so that errors printed to stderr show up
return shouldShowVerboseStdout(project) || project.gradle.startParameter.showStacktrace != ShowStacktrace.INTERNAL_EXCEPTIONS
}

/**
* Executes a [javaexec][Project.javaexec] action with suppressed output.
* Executes a [javaexec][ExecOperations.javaexec] action with suppressed output.
*
* @param project the project
* @param configurator the `javaexec` configuration action
* @return the execution result
* @return a Provider of the execution result
*/
fun exec(project: Project, configurator: Action<in JavaExecSpec>): ExecResult {
return project.javaexec { spec: JavaExecSpec ->
spec.workingDir(project.rootProject.projectDir)
configurator.execute(spec)
if (shouldShowVerboseStdout(project)) {
spec.standardOutput = System.out
} else {
spec.standardOutput = NullOutputStream.NULL_OUTPUT_STREAM
}
if (shouldShowVerboseStderr(project)) {
spec.errorOutput = System.err
} else {
spec.errorOutput = NullOutputStream.NULL_OUTPUT_STREAM
fun exec(project: Project, configurator: Action<JavaExecSpec>): Provider<ExecResult> {
val execOperations = project.extensions.getByType(ExecOperations::class.java)

return project.provider {
execOperations.javaexec { spec: JavaExecSpec ->
spec.workingDir(project.rootProject.projectDir)
configurator.execute(spec)
spec.standardOutput = if (shouldShowVerboseStdout(project)) {
System.out
} else {
NullOutputStream.NULL_OUTPUT_STREAM
}
spec.errorOutput = if (shouldShowVerboseStderr(project)) {
System.err
} else {
NullOutputStream.NULL_OUTPUT_STREAM
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package xyz.wagyourtail.unimined.internal.minecraft.patch.jarmod

import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.process.ExecResult
import org.gradle.api.artifacts.ExternalDependency
import org.gradle.api.Action
import org.gradle.process.JavaExecSpec
import xyz.wagyourtail.unimined.api.minecraft.patch.jarmod.JarModAgentPatcher
import xyz.wagyourtail.unimined.api.runs.RunConfig
import xyz.wagyourtail.unimined.api.minecraft.task.AbstractRemapJarTask
Expand Down Expand Up @@ -119,22 +123,26 @@ open class JarModAgentMinecraftTransformer(
Files.copy(input, output)
try {
val classpath = (task as RemapJarTaskImpl).provider.sourceSet.runtimeClasspath.files.toMutableSet()

val result = SubprocessExecutor.exec(project) {
val resultProvider: Provider<ExecResult> = SubprocessExecutor.exec(project) {
it.jvmArgs = listOf(
"-D${JMA_TRANSFORMERS}=${transforms.joinToString(File.pathSeparator)}",
"-D${JMA_DEBUG}=true")
"-D${JMA_DEBUG}=true"
)
it.args = listOf(
input.absolutePathString(),
classpath.joinToString(File.pathSeparator) { it.absolutePath },
output.absolutePathString(),
output.absolutePathString()
)
it.mainClass.set("xyz.wagyourtail.unimined.jarmodagent.JarModAgent")
it.classpath = jarModAgent
}
if (result.exitValue != 0) {
throw IOException("Failed to run JarModAgent transformer: ${result.exitValue}")
}

resultProvider.map { execResult: ExecResult ->
if (execResult.exitValue != 0) {
throw IOException("Failed to run JarModAgent transformer: ${execResult.exitValue}")
}
execResult
}.get()
} catch (e: Exception) {
project.logger.error("[Unimined/JarModAgentTransformer] Failed to transform $input")
output.deleteIfExists()
Expand Down

0 comments on commit 5197b3f

Please sign in to comment.