Skip to content

Commit

Permalink
Upgrade Buf (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet authored Mar 19, 2022
1 parent 2ca9712 commit 1117811
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/parmet/buf/gradle/BufExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class BufExtension {
/**
* Specify the version of Buf.
*/
var toolVersion: String = "1.0.0-rc10"
var toolVersion: String = "1.1.0"

internal var imageArtifactDetails: ArtifactDetails? = null

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/parmet/buf/gradle/BufPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class BufPlugin : Plugin<Project> {

project.afterEvaluate {
project.configureCreateSymLinksToModules()
project.configureCopyBufConfig(ext)
project.configureWriteWorkspaceYaml()
project.configureLint(ext)
project.configureBuild(ext)
project.getArtifactDetails(ext)?.let {
if (ext.publishSchema) {
project.configureBuild(ext, it)
project.configureImagePublication(it)
}
if (ext.runBreakageCheck()) {
project.configureBreaking(ext, it)
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/com/parmet/buf/gradle/Build.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const val BUF_BUILD_TASK_NAME = "bufBuild"
const val BUF_BUILD_PUBLICATION_FILENAME = "image.json"
const val BUF_IMAGE_PUBLICATION_NAME = "bufImagePublication"

internal fun Project.configureBuild(ext: BufExtension, artifactDetails: ArtifactDetails) {
logger.info("Publishing buf schema image to ${artifactDetails.groupAndArtifact()}:${artifactDetails.version}")

internal fun Project.configureBuild(ext: BufExtension) {
tasks.register<Exec>(BUF_BUILD_TASK_NAME) {
bufTask(ext, "build", "--output", BUF_BUILD_PUBLICATION_FILENAME)
}
}

internal fun Project.configureImagePublication(artifactDetails: ArtifactDetails) {
logger.info("Publishing buf schema image to ${artifactDetails.groupAndArtifact()}:${artifactDetails.version}")

the<PublishingExtension>().publications {
create<MavenPublication>(BUF_IMAGE_PUBLICATION_NAME) {
Expand Down
47 changes: 47 additions & 0 deletions src/main/kotlin/com/parmet/buf/gradle/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.parmet.buf.gradle

import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.kotlin.dsl.register
import java.io.File

const val COPY_BUF_CONFIG_TASK_NAME = "copyBufConfig"

internal fun Project.configureCopyBufConfig(ext: BufExtension) {
tasks.register<Copy>(COPY_BUF_CONFIG_TASK_NAME) {
from(listOfNotNull(bufConfigFile(ext)))
into(bufbuildDir)
rename { "buf.yaml" }
}
}

private fun Project.bufConfigFile(ext: BufExtension) =
project.resolveConfig(ext).let {
if (it != null) {
logger.info("Using buf config from $it")
it
} else {
val configFile = project.file("buf.yaml")
if (configFile.exists()) {
logger.info("Using buf config from default location (project directory)")
configFile
} else {
logger.info("Using default buf config")
null
}
}
}

private fun Project.resolveConfig(ext: BufExtension): File? =
configurations.getByName(BUF_CONFIGURATION_NAME).let {
if (it.dependencies.isNotEmpty()) {
check(ext.configFileLocation == null) {
"Buf lint configuration specified with a config file location and a dependency; pick one."
}
checkNotNull(it.files.singleOrNull()) {
"Buf lint configuration should have exactly one file; had ${it.files}."
}
} else {
ext.configFileLocation
}
}
35 changes: 2 additions & 33 deletions src/main/kotlin/com/parmet/buf/gradle/DockerSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,16 @@ package com.parmet.buf.gradle

import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import java.io.File

internal fun Exec.bufTask(ext: BufExtension, vararg args: Any) {
dependsOn(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME)
dependsOn(WRITE_WORKSPACE_YAML_TASK_NAME)
dependsOn(COPY_BUF_CONFIG_TASK_NAME)

commandLine("docker")
setArgs(project.baseDockerArgs(ext) + args + bufTaskConfigOption(ext))
setArgs(project.baseDockerArgs(ext) + args)
}

private fun Exec.bufTaskConfigOption(ext: BufExtension) =
project.resolveConfig(ext).let {
if (it != null) {
logger.info("Using buf config from $it")
listOf("--config", it.readText())
} else {
val configFile = project.file("buf.yaml")
if (configFile.exists()) {
logger.info("Using buf config from default location (project directory)")
listOf("--config", configFile.readText())
} else {
logger.info("Using default buf config")
emptyList()
}
}
}

private fun Project.resolveConfig(ext: BufExtension): File? =
configurations.getByName(BUF_CONFIGURATION_NAME).let {
if (it.dependencies.isNotEmpty()) {
check(ext.configFileLocation == null) {
"Buf lint configuration specified with a config file location and a dependency; pick one."
}
checkNotNull(it.files.singleOrNull()) {
"Buf lint configuration should have exactly one file; had ${it.files}."
}
} else {
ext.configFileLocation
}
}

private fun Project.baseDockerArgs(ext: BufExtension) =
listOf(
"run",
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/parmet/buf/gradle/Lint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const val BUF_LINT_TASK_NAME = "bufLint"

internal fun Project.configureLint(ext: BufExtension) {
tasks.register<Exec>(BUF_LINT_TASK_NAME) {
dependsOn(BUF_BUILD_TASK_NAME)
group = CHECK_TASK_NAME
bufTask(ext, "lint")
bufTask(ext, "lint", BUF_BUILD_PUBLICATION_FILENAME)
}

tasks.named(CHECK_TASK_NAME).dependsOn(BUF_LINT_TASK_NAME)
Expand Down

0 comments on commit 1117811

Please sign in to comment.