Skip to content

chore: relace project.exec with ExecOperations #110

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

Merged
merged 1 commit into from
Jun 22, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.File
import java.nio.charset.StandardCharsets
import javax.inject.Inject

open class BaseGettextEditTask @Inject constructor(
abstract class BaseGettextEditTask @Inject constructor(
objects: ObjectFactory
) : BaseGettextTask(objects) {
@Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import org.gradle.api.DefaultTask
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.kotlin.dsl.property
import org.gradle.process.ExecOperations
import javax.inject.Inject

open class BaseGettextTask @Inject constructor(
abstract class BaseGettextTask @Inject constructor(
objects: ObjectFactory
) : DefaultTask() {
@Input
val executable = objects.property<String>()

@get:Inject
protected abstract val execOperations: ExecOperations
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class GettextTask @Inject constructor(
}

val cmd = executable.get()
project.exec {
execOperations.exec {
executable = cmd
for (keyword in keywords.get()) {
args("-k$keyword")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.gradle.work.InputChanges
import java.io.File
import javax.inject.Inject

open class MsgAttribTask @Inject constructor(
abstract class MsgAttribTask @Inject constructor(
objects: ObjectFactory
) : BaseGettextEditTask(objects) {
@InputFiles
Expand Down Expand Up @@ -67,7 +67,7 @@ open class MsgAttribTask @Inject constructor(
continue
}
logger.debug("Processing {} with {} {}", po.file, cmd, arg)
project.exec {
execOperations.exec {
executable = cmd
args("--output-file=${outFile.absolutePath}")
args(arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import java.io.File
import java.nio.charset.Charset
import javax.inject.Inject

open class MsgFmtTask @Inject constructor(
abstract class MsgFmtTask @Inject constructor(
objects: ObjectFactory
) : BaseGettextTask(objects) {
@InputFiles
Expand Down Expand Up @@ -102,7 +102,7 @@ open class MsgFmtTask @Inject constructor(
logger.debug("Processing {} with {} {}", po.file, cmd, arg)
project.delete(tmpDir)
tmpDir.mkdirs()
project.exec {
execOperations.exec {
executable = cmd
if (format.get() == OutputFormat.JAVA) {
args("--java2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.gradle.work.InputChanges
import java.io.File
import javax.inject.Inject

open class MsgMergeTask @Inject constructor(
abstract class MsgMergeTask @Inject constructor(
objects: ObjectFactory
) : BaseGettextEditTask(objects) {
@InputFiles
Expand Down Expand Up @@ -72,7 +72,7 @@ open class MsgMergeTask @Inject constructor(
continue
}
logger.debug("Processing {} with {} {}", po.file, cmd, arg)
project.exec {
execOperations.exec {
executable = cmd
args("--output-file=${outFile.absolutePath}")
args("--quiet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.github.vlsi.gradle.release

import com.github.vlsi.gradle.release.svn.LsDepth
import com.github.vlsi.gradle.release.svn.Svn
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.the
import org.gradle.work.InputChanges
Expand All @@ -31,21 +31,22 @@ abstract class PromoteSvnRelease : SvnmuccTask() {
@Input
val useCpWorkaround = project.objects.property<Boolean>().convention(true)

private val ext = project.the<ReleaseExtension>()

override fun message() =
project.the<ReleaseExtension>().run {
"Promoting ${componentName.get()} ${rcTag.get()} -> ${releaseTag.get()} to release area"
}

override fun operations(inputChanges: InputChanges): List<SvnOperation> {
return mutableListOf<SvnOperation>().apply {
val ext = project.the<ReleaseExtension>()
val svnDist = ext.svnDist
val stageFolder = svnDist.stageFolder.get()
val releaseFolder = svnDist.releaseFolder.get()

val subfolders = svnDist.releaseSubfolder.get()

val entries = Svn(project, repository.get()).ls {
val entries = svnClient(repository.get()).ls {
withCredentials()
folders.add(stageFolder)
depth = LsDepth.INFINITY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class RemoveStaleArtifactsTask @Inject constructor(

override fun operations(inputChanges: InputChanges): List<SvnOperation> {
val svnUri = repository.get()
val entries = Svn(project, svnUri).ls {
val entries = svnClient(svnUri).ls {
withCredentials()
folders.addAll(foldersToList.get())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.logging.Logger
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.api.publish.maven.tasks.GenerateMavenPom
Expand All @@ -55,12 +56,16 @@ import org.gradle.kotlin.dsl.withType
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.plugins.signing.SigningExtension
import org.gradle.plugins.signing.SigningPlugin
import org.gradle.process.ExecOperations
import java.io.File
import java.net.URI
import java.nio.charset.StandardCharsets
import javax.inject.Inject

class StageVoteReleasePlugin @Inject constructor(private val instantiator: Instantiator) :
class StageVoteReleasePlugin @Inject constructor(
private val instantiator: Instantiator,
private val execOperations: ExecOperations,
) :
Plugin<Project> {
companion object {
@Deprecated(replaceWith = ReplaceWith("StageVoteReleasePlugin.RELEASE_PARAMS_EXTENSION_NAME"), message = "There are multiple extensions, so prefer clarified name")
Expand Down Expand Up @@ -653,6 +658,7 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
val releaseExt = project.the<ReleaseExtension>()

val voteMailFile = layout.buildDirectory.file("$PREPARE_VOTE_TASK_NAME/mail.txt")
val projectDir = layout.projectDirectory
outputs.file(file(voteMailFile))
doLast {
val nexusPublish = project.the<NexusPublishExtension>()
Expand All @@ -670,7 +676,7 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta
.let { it.replacePath(it.path + "/" + svnDist.stageFolder.get()) }

val (stagedFiles, checksums) = if (releaseExt.svnDistEnabled.get()) {
fetchSvnArtifacts(project, svnStagingUri, svnDist)
fetchSvnArtifacts(project, execOperations, logger, projectDir.asFile, svnStagingUri, svnDist)
} else {
Pair(listOf(), mapOf())
}
Expand Down Expand Up @@ -708,10 +714,13 @@ class StageVoteReleasePlugin @Inject constructor(private val instantiator: Insta

private fun fetchSvnArtifacts(
project: Project,
execOperations: ExecOperations,
logger: Logger,
projectDir: File,
svnStagingUri: URI,
svnDist: SvnDistConfig
): Pair<List<SvnEntry>, Map<String, String>> {
val svn = Svn(project, svnStagingUri).apply {
val svn = Svn(execOperations, logger, projectDir, svnStagingUri).apply {
username = svnDist.credentials.username(project)
password = svnDist.credentials.password(project)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,49 @@
*/
package com.github.vlsi.gradle.release

import com.github.vlsi.gradle.properties.dsl.stringProperty
import com.github.vlsi.gradle.properties.dsl.toBool
import com.github.vlsi.gradle.release.svn.Svn
import com.github.vlsi.gradle.release.svn.SvnCredentials
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.URI
import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.the
import org.gradle.process.ExecOperations
import org.gradle.process.ExecSpec
import org.gradle.work.InputChanges

abstract class SvnmuccTask @Inject constructor() : DefaultTask() {
@get:Inject
abstract val execOperations: ExecOperations

@Input
val repository = project.objects.property<URI>()
.convention(project.provider {
project.the<ReleaseExtension>().svnDist.url.get()
})

@InputDirectory
protected val projectDir = project.layout.projectDirectory

abstract fun operations(inputChanges: InputChanges): List<SvnOperation>
abstract fun message(): String

// TODO: remove project access at execution time
protected fun SvnCredentials.withCredentials() {
project.the<ReleaseExtension>().svnDist.credentials {
this@withCredentials.username = username(project)
this@withCredentials.password = password(project)
}
}

// TODO: remove project access at execution time
protected fun ExecSpec.svnCredentials() {
project.the<ReleaseExtension>().svnDist.credentials {
username(project)?.let { args("--username", it) }
Expand All @@ -60,31 +69,38 @@ abstract class SvnmuccTask @Inject constructor() : DefaultTask() {
fun exists(path: String): Boolean {
val os = ByteArrayOutputStream()
val absolutePath = "${repository.get()}/$path"
val result = project.exec {
workingDir = project.projectDir
val result = execOperations.exec {
workingDir = projectDir.asFile
commandLine("svn", "ls", "--depth", "empty", absolutePath)
svnCredentials()
isIgnoreExitValue = true
errorOutput = os
}
if (result.exitValue == 0) {
project.logger.debug("Directory {} exists in SVN", absolutePath)
logger.debug("Directory {} exists in SVN", absolutePath)
return true
}

val message = os.toString() // Default encoding is expected
if (message.contains("E200009")) {
// E200009: Could not list all targets because some targets don't exist
project.logger.debug("Directory {} does not exist in SVN", absolutePath)
logger.debug("Directory {} does not exist in SVN", absolutePath)
} else {
project.logger.warn("Unable to check existence of {}. Error: {}", absolutePath, message)
logger.warn("Unable to check existence of {}. Error: {}", absolutePath, message)
}
return false
}

@Internal
protected val commandsFile = project.layout.buildDirectory.file("svnmucc/$name.txt")

@Input
protected val asfDryRun = project.objects.property<Boolean>()
.convention(project.providers.gradleProperty("asfDryRun").map { it.toBool() })

protected fun svnClient(uri: URI) =
Svn(execOperations, logger, projectDir.asFile, uri)

@TaskAction
fun mucc(inputChanges: InputChanges) {
logger.debug(
Expand Down Expand Up @@ -120,7 +136,7 @@ abstract class SvnmuccTask @Inject constructor() : DefaultTask() {
commandsFile.writeText(commands)

val commitMessage = message()
if (project.stringProperty("asfDryRun").toBool()) {
if (asfDryRun.get()) {
logger.lifecycle(
"Dry run svnmucc. root={}, message={}, commands:\n{}",
repository.get(),
Expand All @@ -139,8 +155,8 @@ abstract class SvnmuccTask @Inject constructor() : DefaultTask() {
commitMessage,
commands
)
project.exec {
workingDir = project.projectDir
execOperations.exec {
workingDir = projectDir.asFile
commandLine("svnmucc", "--non-interactive", "--root-url", repository.get())
svnCredentials()
args("--extra-args", commandsFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import java.net.URI
import java.time.OffsetDateTime
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.logging.Logger
import org.gradle.process.ExecOperations
import java.io.File

class Svn(val project: Project, val uri: URI) : SvnCredentials {
class Svn(val execOperations: ExecOperations, val logger: Logger, val projectDir: File, val uri: URI) : SvnCredentials {
override var username: String? = null
override var password: String? = null

Expand Down Expand Up @@ -64,12 +67,12 @@ class Svn(val project: Project, val uri: URI) : SvnCredentials {
val file = opts.file

val revisionSuffix = opts.revision?.let { "@$it" } ?: ""
project.logger.lifecycle("Fetching {}/{}{}", uri, file, revisionSuffix)
logger.lifecycle("Fetching {}/{}{}", uri, file, revisionSuffix)

val stdout = ByteArrayOutputStream()
val stderr = ByteArrayOutputStream()
val result = project.exec {
workingDir = project.projectDir
val result = execOperations.exec {
workingDir = projectDir
commandLine("svn", "cat")
opts.username?.let { args("--username", it) }
opts.password?.let { args("--password", it) }
Expand Down Expand Up @@ -97,12 +100,12 @@ class Svn(val project: Project, val uri: URI) : SvnCredentials {
} else {
"contents"
}
project.logger.lifecycle("Listing SVN {} at {}{}", contents, uri, revisionSuffix)
logger.lifecycle("Listing SVN {} at {}{}", contents, uri, revisionSuffix)

val stdout = ByteArrayOutputStream()
val stderr = ByteArrayOutputStream()
val result = project.exec {
workingDir = project.projectDir
val result = execOperations.exec {
workingDir = projectDir
commandLine("svn", "ls", "--xml", "--depth", opts.depth.name.toLowerCase())
for (folder in opts.folders) {
args("$uri/$folder/")
Expand Down
Loading