From 55b5dfdb2fc111301c122a7a440fe027d6122ee3 Mon Sep 17 00:00:00 2001
From: Piotr Adamczyk <piotr.adamczyk@gogoapps.io>
Date: Thu, 29 Jul 2021 22:54:35 +0200
Subject: [PATCH 1/2] feat: Added Gradle Plugin to check if helpers version
 updated

---
 buildSrc/src/main/kotlin/Plugins.kt           |  1 +
 buildSrc/src/main/kotlin/Utils.kt             | 25 ----------
 check_version_updated/build.gradle.kts        | 15 ++++++
 check_version_updated/settings.gradle.kts     |  1 +
 .../flank/gradle/CheckVersionUpdated.kt       | 42 +++++++++++++++++
 .../flank/gradle/CheckVersionUpdatedTask.kt   | 47 +++++++++++++++++++
 flank-scripts/build.gradle.kts                | 28 +----------
 flank_wrapper/build.gradle.kts                | 28 +----------
 settings.gradle.kts                           |  1 +
 9 files changed, 111 insertions(+), 77 deletions(-)
 delete mode 100644 buildSrc/src/main/kotlin/Utils.kt
 create mode 100644 check_version_updated/build.gradle.kts
 create mode 100644 check_version_updated/settings.gradle.kts
 create mode 100644 check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdated.kt
 create mode 100644 check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdatedTask.kt

diff --git a/buildSrc/src/main/kotlin/Plugins.kt b/buildSrc/src/main/kotlin/Plugins.kt
index c8a21c6f86..232a873778 100644
--- a/buildSrc/src/main/kotlin/Plugins.kt
+++ b/buildSrc/src/main/kotlin/Plugins.kt
@@ -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"
diff --git a/buildSrc/src/main/kotlin/Utils.kt b/buildSrc/src/main/kotlin/Utils.kt
deleted file mode 100644
index df1ac57161..0000000000
--- a/buildSrc/src/main/kotlin/Utils.kt
+++ /dev/null
@@ -1,25 +0,0 @@
-import org.gradle.api.Project
-import java.io.ByteArrayOutputStream
-
-fun Project.execAndGetStdout(vararg args: String): String {
-    val stdout = ByteArrayOutputStream()
-    exec {
-        commandLine(*args)
-        standardOutput = stdout
-        workingDir = projectDir
-    }
-    return stdout.toString().trimEnd()
-}
-
-// TODO replace with plugin in #2063
-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()
-}
-
diff --git a/check_version_updated/build.gradle.kts b/check_version_updated/build.gradle.kts
new file mode 100644
index 0000000000..5e95fdad57
--- /dev/null
+++ b/check_version_updated/build.gradle.kts
@@ -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"
+    }
+}
diff --git a/check_version_updated/settings.gradle.kts b/check_version_updated/settings.gradle.kts
new file mode 100644
index 0000000000..cd1a5448d4
--- /dev/null
+++ b/check_version_updated/settings.gradle.kts
@@ -0,0 +1 @@
+rootProject.name = "check-version-updated"
diff --git a/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdated.kt b/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdated.kt
new file mode 100644
index 0000000000..c98dc1e21c
--- /dev/null
+++ b/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdated.kt
@@ -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()
+    }
+
+}
diff --git a/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdatedTask.kt b/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdatedTask.kt
new file mode 100644
index 0000000000..cef9326503
--- /dev/null
+++ b/check_version_updated/src/main/kotlin/com/github/flank/gradle/CheckVersionUpdatedTask.kt
@@ -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 = ") }
+    }
+}
diff --git a/flank-scripts/build.gradle.kts b/flank-scripts/build.gradle.kts
index 47f5da2e38..0e0f2a1fe0 100644
--- a/flank-scripts/build.gradle.kts
+++ b/flank-scripts/build.gradle.kts
@@ -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"
@@ -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 {
@@ -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(
@@ -157,5 +135,3 @@ val releaseFlankScripts by tasks.registering(Exec::class) {
         "-p"
     )
 }
-
-tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
diff --git a/flank_wrapper/build.gradle.kts b/flank_wrapper/build.gradle.kts
index 3fcc0bc27e..e5cb9cff8f 100644
--- a/flank_wrapper/build.gradle.kts
+++ b/flank_wrapper/build.gradle.kts
@@ -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"
@@ -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 {
@@ -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")
@@ -143,5 +121,3 @@ val setWrapperVersion by tasks.registering {
 tasks.processResources {
     dependsOn(setWrapperVersion)
 }
-
-tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 2f7589d4a4..465c193985 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -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",

From 12616c0670084a498d4a0359562a9dc59c203a67 Mon Sep 17 00:00:00 2001
From: piotradamczyk5 <piotradamczyk5@users.noreply.github.com>
Date: Thu, 29 Jul 2021 21:08:28 +0000
Subject: [PATCH 2/2] [Auto-commit] Added files fixed by Detekt

---
 flank_wrapper/src/main/resources/version.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flank_wrapper/src/main/resources/version.txt b/flank_wrapper/src/main/resources/version.txt
index 867e52437a..cb174d58a5 100644
--- a/flank_wrapper/src/main/resources/version.txt
+++ b/flank_wrapper/src/main/resources/version.txt
@@ -1 +1 @@
-1.2.0
\ No newline at end of file
+1.2.1
\ No newline at end of file