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 taskGraph.afterTask with an operation completion listener in Gradle 6.1+ #389

Merged
merged 1 commit into from
Jul 8, 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
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

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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this change?
I see that you added

    @Inject
    abstract protected ObjectFactory getObjects();

    @Inject
    abstract protected ProviderFactory getProviders();

But I don't get the why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I use them at

objects.newInstance(BuildEventsListenerRegistryProvider)
and

Of course you can get those from project too, but that is bad / outdated practice and I didn't want to add to the existing ones but doing it right. :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Thanks for the explanation 👍

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
Loading