Skip to content

Commit

Permalink
WIP - use build service, not task listener for Gradle 8.3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnybot0 committed Oct 16, 2023
1 parent af4bf46 commit 3fb4e6f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.researchgate.release

import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener
import org.gradle.tooling.events.task.TaskFailureResult
import org.gradle.tooling.events.task.TaskFinishEvent
import org.slf4j.LoggerFactory

abstract class CleanupBuildService implements BuildService<Params>, OperationCompletionListener {

interface Params extends BuildServiceParameters {
Property<BaseScmAdapter> getScmAdapter()
Property<ReleaseExtension> getExtension()
Property<Project> getProject()
}

@Override
void onFinish(FinishEvent finishEvent) {
if (finishEvent instanceof TaskFinishEvent) {
def result = finishEvent.result
if (result instanceof TaskFailureResult) {
def extension = parameters.extension.get()
def scmAdapter = parameters.scmAdapter.get()
def project = parameters.project.get()
def log = project.logger ?: LoggerFactory.getLogger(this.getClass())

if (finishEvent.descriptor.name == "release") {
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.')
}
}
}
}
}
}
31 changes: 16 additions & 15 deletions src/main/groovy/net/researchgate/release/ReleasePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ import net.researchgate.release.tasks.UpdateVersion
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.GradleBuild
import org.gradle.api.tasks.TaskState
import org.gradle.build.event.BuildEventsListenerRegistry

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

@Inject
abstract BuildEventsListenerRegistry getBuildEventsListenerRegistry()

void apply(Project project) {
if (!project.plugins.hasPlugin(BasePlugin.class)) {
project.plugins.apply(BasePlugin.class)
Expand Down Expand Up @@ -157,19 +161,16 @@ 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.')
def cleanupServiceProvider = project.gradle.sharedServices
.registerIfAbsent('cleanupBuildService', CleanupBuildService) {
it.parameters.extension.set(extension)
it.parameters.project.set(project)
it.parameters.scmAdapter.set(project.provider {
createScmAdapter()
return scmAdapter
})
}
}
}
buildEventsListenerRegistry.onTaskCompletion(cleanupServiceProvider)
}

void createScmAdapter() {
Expand Down

0 comments on commit 3fb4e6f

Please sign in to comment.