
com.freeletics.gradle.monorepo.tasks.ComputeGitShaTask.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugins Show documentation
Show all versions of plugins Show documentation
A set of reusable Gradle plugins
package com.freeletics.gradle.monorepo.tasks
import com.android.build.api.variant.BuildConfigField
import com.freeletics.gradle.monorepo.util.RealGit
import com.freeletics.gradle.monorepo.util.computeInfoFromGit
import java.io.File
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider
public abstract class ComputeGitShaTask : DefaultTask() {
@get:Input
public abstract val computeFromGit: Property
@get:Input
public abstract val gitRootDirectory: Property
@get:OutputFile
public abstract val outputFile: RegularFileProperty
@TaskAction
public fun action() {
val gitSha = if (computeFromGit.get()) {
val git = RealGit(gitRootDirectory.get())
git.commitSha()
} else {
"sha-not-computed"
}
outputFile.get().asFile.writeText(gitSha)
}
internal companion object {
fun Project.registerComputeGitShaTask(): TaskProvider {
return tasks.register("computeGitSha", ComputeGitShaTask::class.java) { task ->
task.computeFromGit.set(computeInfoFromGit)
task.gitRootDirectory.set(rootDir)
task.outputFile.set(layout.buildDirectory.file("intermediates/git/sha.txt"))
}
}
fun TaskProvider.mapOutput(): Provider> {
return flatMap { task ->
task.outputFile.map {
BuildConfigField(
type = "String",
value = "\"${it.asFile.readText()}\"",
comment = null,
)
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy