Skip to content

Commit

Permalink
feat: Added Gradle Plugin to check if helpers modules version get upd…
Browse files Browse the repository at this point in the history
…ated (#2108)

Fixes #2063

## Test Plan
> How do we know the code works?

Version check for `:flank_wrapper` and `:flank_scripts` works same as previously
  • Loading branch information
piotradamczyk5 authored Jul 30, 2021
1 parent 2784a32 commit e2a2ef3
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 78 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object Plugins {
const val NEXUS_STAGING = "io.codearte.nexus-staging"
const val BEN_MANES_PLUGIN = "com.github.ben-manes.versions"
const val MAVEN_VERSION_CHECK = "maven-version-check"
const val CHECK_VERSION_UPDATED = "check-version-updated"
object Kotlin {
const val PLUGIN_JVM = "jvm"
const val PLUGIN_SERIALIZATION = "plugin.serialization"
Expand Down
25 changes: 0 additions & 25 deletions buildSrc/src/main/kotlin/Utils.kt

This file was deleted.

15 changes: 15 additions & 0 deletions check_version_updated/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repositories {
mavenCentral()
}

plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

gradlePlugin {
plugins.register("check-version-updated") {
id = "check-version-updated"
implementationClass = "com.github.flank.gradle.CheckVersionUpdated"
}
}
1 change: 1 addition & 0 deletions check_version_updated/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "check-version-updated"
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.flank.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.get
import java.io.ByteArrayOutputStream

class CheckVersionUpdated : Plugin<Project> {

private val taskName = "checkIfVersionUpdated"

override fun apply(project: Project) {

project.tasks.register(taskName, CheckVersionUpdatedTask::class.java)

project.afterEvaluate {
project.tasks["lintKotlin"].dependsOn(taskName)
}
}

private fun Project.execAndGetStdout(vararg args: String): String {
val stdout = ByteArrayOutputStream()
exec {
commandLine(*args)
standardOutput = stdout
workingDir = projectDir
}
return stdout.toString().trimEnd()
}

private fun Project.isVersionChangedInBuildGradle(): Boolean {

val localResultsStream = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--", "build.gradle.kts")
.split("\n")
val commitedResultsStream = execAndGetStdout("git", "diff", "origin/master", "--", "build.gradle.kts")
.split("\n")
return (commitedResultsStream + localResultsStream)
.filter { it.startsWith("-version = ") || it.startsWith("+version = ") }
.isNotEmpty()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.flank.gradle

import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.tasks.TaskAction
import java.io.ByteArrayOutputStream

open class CheckVersionUpdatedTask : DefaultTask() {

@TaskAction
fun action() {
project.execAndGetStdout("git", "fetch", "--no-tags")
val changedFiles = project.execAndGetStdout("git", "diff", "origin/master", "HEAD", "--name-only").split("\n") +
project.execAndGetStdout("git", "diff", "origin/master", "--name-only").split("\n")
val isVersionChanged = changedFiles.any { it.startsWith(project.name) }.not() ||
(changedFiles.contains("${project.name}/build.gradle.kts") && project.isVersionChangedInBuildGradle())

if (isVersionChanged.not()) {
throw GradleException(
"""
${project.path} version is not updated, but files changed.
Please update version according to schema: <breaking change>.<feature added>.<fix/minor change>
""".trimIndent()
)
}
}

private fun Project.execAndGetStdout(vararg args: String): String {
val stdout = ByteArrayOutputStream()
exec {
commandLine(*args)
standardOutput = stdout
workingDir = projectDir
}
return stdout.toString().trimEnd()
}

private fun Project.isVersionChangedInBuildGradle(): Boolean {
val localResultsStream = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--", "build.gradle.kts")
.split("\n")
val commitedResultsStream = execAndGetStdout("git", "diff", "origin/master", "--", "build.gradle.kts")
.split("\n")
return (commitedResultsStream + localResultsStream)
.any { it.startsWith("-version = ") || it.startsWith("+version = ") }
}
}
28 changes: 2 additions & 26 deletions flank-scripts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
kotlin(Plugins.Kotlin.PLUGIN_SERIALIZATION) version Versions.KOTLIN
id(Plugins.PLUGIN_SHADOW_JAR) version Versions.SHADOW
id(Plugins.MAVEN_PUBLISH)
id(Plugins.CHECK_VERSION_UPDATED)
}

val artifactID = "flank-scripts"
Expand All @@ -25,7 +26,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.9.24"
version = "1.9.25"
group = "com.github.flank"

application {
Expand Down Expand Up @@ -125,29 +126,6 @@ val download by tasks.registering(Exec::class) {
}
}

// TODO replace with plugin in #2063
val checkIfVersionUpdated by tasks.registering(Exec::class) {
group = "verification"
commandLine("git", "fetch", "--no-tags")

doLast {
val changedFiles = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--name-only").split("\n") +
execAndGetStdout("git", "diff", "origin/master", "--name-only").split("\n")
val isVersionChanged = changedFiles.any { it.startsWith("flank-scripts") }.not() ||
(changedFiles.contains("flank-scripts/build.gradle.kts") && isVersionChangedInBuildGradle())

if (isVersionChanged.not()) {
throw GradleException(
"""
Flank scripts version is not updated, but files changed.
Please update version according to schema: <breaking change>.<feature added>.<fix/minor change>
""".trimIndent()

)
}
}
}

val releaseFlankScripts by tasks.registering(Exec::class) {
dependsOn(":flank-scripts:publish")
commandLine(
Expand All @@ -157,5 +135,3 @@ val releaseFlankScripts by tasks.registering(Exec::class) {
"-p"
)
}

tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
28 changes: 2 additions & 26 deletions flank_wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
kotlin(Plugins.Kotlin.PLUGIN_JVM)
id(Plugins.PLUGIN_SHADOW_JAR) version Versions.SHADOW
id(Plugins.MAVEN_PUBLISH)
id(Plugins.CHECK_VERSION_UPDATED)
}

val artifactID = "flank_wrapper"
Expand All @@ -23,7 +24,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.2.0"
version = "1.2.1"
group = "com.github.flank"

repositories {
Expand Down Expand Up @@ -101,29 +102,6 @@ application {
mainClass.set(runnerClass)
}

// TODO replace with plugin in #2063
val checkIfVersionUpdated by tasks.registering(Exec::class) {
group = "verification"
commandLine("git", "fetch", "--no-tags")

doLast {
val changedFiles = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--name-only").split("\n") +
execAndGetStdout("git", "diff", "origin/master", "--name-only").split("\n")
val isVersionChanged = changedFiles.any { it.startsWith("flank_wrapper") }.not() ||
(changedFiles.contains("flank_wrapper/build.gradle.kts") && isVersionChangedInBuildGradle())

if (isVersionChanged.not()) {
throw GradleException(
"""
Flank wrapper version is not updated, but files changed.
Please update version according to schema: <breaking change>.<feature added>.<fix/minor change>
""".trimIndent()

)
}
}
}

val prepareJar by tasks.registering(Copy::class) {
dependsOn("shadowJar")
from("$buildDir/libs")
Expand All @@ -143,5 +121,3 @@ val setWrapperVersion by tasks.registering {
tasks.processResources {
dependsOn(setWrapperVersion)
}

tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
2 changes: 1 addition & 1 deletion flank_wrapper/src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.1
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.gradle.kotlin.dsl.support.serviceOf
rootProject.name = "flank"

includeBuild("maven_version_check")
includeBuild("check_version_updated")

include(
":test_runner",
Expand Down

0 comments on commit e2a2ef3

Please sign in to comment.