Skip to content

Commit

Permalink
Replace taskGraph.afterTask with an operation completion listener in …
Browse files Browse the repository at this point in the history
…Gradle 6.1+
  • Loading branch information
Vampire authored and Hillkorn committed Jul 8, 2024
1 parent 7ce9b9d commit 57651e2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.gradle.api.internal.classpath.ModuleRegistry

This comment has been minimized.

Copy link
@Iwjsmwiwjd

This comment has been minimized.

Copy link
@Iwjsmwiwjd

Iwjsmwiwjd Oct 8, 2024

Passport

buildscript {
repositories {
maven {
Expand Down Expand Up @@ -33,6 +35,10 @@ dependencies {
testCompile "cglib:cglib-nodep:3.2.8"
testImplementation gradleTestKit()
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")

// work-around for https://github.com/gradle/gradle/issues/16774
testRuntimeOnly(files(project.services.get(ModuleRegistry).getModule('gradle-tooling-api-builders').classpath.asFiles.first()))
testRuntimeOnly('com.google.guava:guava:27.1-android')
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.researchgate.release

import org.gradle.build.event.BuildEventsListenerRegistry

import javax.inject.Inject

interface BuildEventsListenerRegistryProvider {
@Inject
BuildEventsListenerRegistry getBuildEventsListenerRegistry();
}
54 changes: 41 additions & 13 deletions src/main/groovy/net/researchgate/release/ReleasePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.GradleBuild
import org.gradle.api.tasks.TaskState
import org.gradle.build.event.BuildEventsListenerRegistry
import org.gradle.tooling.events.OperationCompletionListener
import org.gradle.tooling.events.task.TaskFailureResult
import org.gradle.util.GradleVersion

class ReleasePlugin extends PluginHelper implements Plugin<Project> {
import javax.inject.Inject

abstract class ReleasePlugin extends PluginHelper implements Plugin<Project> {
static final String RELEASE_GROUP = 'Release'

private BaseScmAdapter scmAdapter
Expand Down Expand Up @@ -74,7 +80,7 @@ class ReleasePlugin extends PluginHelper implements Plugin<Project> {
"${p}updateVersion" as String,
"${p}commitNewVersion" as String
]

// Gradle 6 workaround (https://github.com/gradle/gradle/issues/12872)
buildName = project.name + "-release"
}
Expand Down Expand Up @@ -119,7 +125,7 @@ class ReleasePlugin extends PluginHelper implements Plugin<Project> {
"${p}afterReleaseBuild" as String
].flatten()
}

// Gradle 6 workaround (https://github.com/gradle/gradle/issues/12872)
buildName = project.name + "-release"
}
Expand Down Expand Up @@ -161,18 +167,40 @@ class ReleasePlugin extends PluginHelper implements Plugin<Project> {
}
}

project.gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure && task.name == "release") {
try {
createScmAdapter()
} catch (Exception ignored) {}
if (scmAdapter && extension.revertOnFail && project.file(extension.versionPropertyFile)?.exists()) {
log.error('Release process failed, reverting back any changes made by Release Plugin.')
scmAdapter.revert()
} else {
log.error('Release process failed, please remember to revert any uncommitted changes made by the Release Plugin.')
if (GradleVersion.current() < GradleVersion.version('6.1')) {
project.gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure && task.name == 'release') {
revert()
}
}
} else {
objects.newInstance(BuildEventsListenerRegistryProvider)
.buildEventsListenerRegistry
.onTaskCompletion(providers.provider {
{ finishEvent ->
if ((finishEvent.result instanceof TaskFailureResult) && finishEvent.descriptor.taskPath.endsWith(':release')) {
revert()
}
} as OperationCompletionListener
})
}
}

@Inject
abstract protected ObjectFactory getObjects();

@Inject
abstract protected ProviderFactory getProviders();

protected revert() {
try {
createScmAdapter()
} catch (Exception ignored) {}
if (scmAdapter && extension.revertOnFail.get() && project.file(extension.versionPropertyFile)?.exists()) {
log.error('Release process failed, reverting back any changes made by Release Plugin.')
scmAdapter.revert()
} else {
log.error('Release process failed, please remember to revert any uncommitted changes made by the Release Plugin.')
}
}

Expand Down

0 comments on commit 57651e2

Please sign in to comment.