From 59173dd93c4d9f9253b1cd3f19e2d062af3729d6 Mon Sep 17 00:00:00 2001 From: Loes Immens Date: Mon, 16 Sep 2024 16:42:10 +0200 Subject: [PATCH] FDP-2318: spotless Signed-off-by: Loes Immens --- .../simulator/response/CommandService.kt | 5 ++- build.gradle.kts | 26 ++++++++++++++- license-template.kt | 3 ++ scripts/pre-commit | 33 +++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 license-template.kt create mode 100755 scripts/pre-commit diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt index 328d393..4568e16 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt @@ -1,13 +1,12 @@ // SPDX-FileCopyrightText: Contributors to the GXF project // // SPDX-License-Identifier: Apache-2.0 - package org.gxf.crestdevicesimulator.simulator.response import org.springframework.stereotype.Service @Service class CommandService { - fun hasRebootCommand(command: String) = - command.contains("CMD:REBOOT") + // todo remove + fun hasRebootCommand(command: String) = command.contains("CMD:REBOOT") } diff --git a/build.gradle.kts b/build.gradle.kts index 4b12da6..449d244 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 +import com.diffplug.gradle.spotless.SpotlessExtension import com.github.davidmc24.gradle.plugin.avro.GenerateAvroJavaTask import io.spring.gradle.dependencymanagement.internal.dsl.StandardDependencyManagementExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension @@ -15,6 +16,7 @@ plugins { kotlin("plugin.spring") version "2.0.0" apply false kotlin("plugin.jpa") version "2.0.0" apply false id("com.github.davidmc24.gradle.plugin.avro") version "1.9.1" apply false + id("com.diffplug.spotless") version "6.25.0" id("org.sonarqube") version "5.0.0.4638" id("eclipse") } @@ -34,6 +36,7 @@ subprojects { apply(plugin = "org.jetbrains.kotlin.jvm") apply(plugin = "org.jetbrains.kotlin.plugin.spring") apply(plugin = "io.spring.dependency-management") + apply(plugin = "com.diffplug.spotless") apply(plugin = "eclipse") apply(plugin = "org.jetbrains.kotlin.plugin.jpa") apply(plugin = "jacoco") @@ -46,6 +49,17 @@ subprojects { mavenCentral() } + extensions.configure { + kotlin { + // by default the target is every '.kt' and '.kts' file in the java source sets + ktfmt().dropboxStyle() + licenseHeaderFile( + "${project.rootDir}/license-template.kt", + "package") + .updateYearWithLatest(false) + } + } + extensions.configure { imports { mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES) } } @@ -59,8 +73,18 @@ subprojects { } } + tasks.register("updateGitHooks") { + description = "Copies the pre-commit Git Hook to the .git/hooks folder." + group = "verification" + from("${project.rootDir}/scripts/pre-commit") + into("${project.rootDir}/.git/hooks") + } + tasks.withType { - dependsOn(tasks.withType()) + dependsOn( + tasks.withType(), + tasks.named("updateGitHooks") + ) } tasks.withType { diff --git a/license-template.kt b/license-template.kt new file mode 100644 index 0000000..878d7ea --- /dev/null +++ b/license-template.kt @@ -0,0 +1,3 @@ +// SPDX-FileCopyrightText: Contributors to the GXF project +// +// SPDX-License-Identifier: Apache-2.0 diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 0000000..7c2770c --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,33 @@ +#!/bin/bash +echo "*********************************************************" +echo "Running git pre-commit hook. Running Spotless Apply... " +echo "*********************************************************" + +# Gather the staged files - to make sure changes are saved only for these files. +stagedFiles=$(git diff --staged --name-only) + +# run spotless apply +./gradlew spotlessApply + +status=$? + +if [ "$status" = 0 ] ; then + echo "Static analysis found no problems." + # Add staged file changes to git + for file in $stagedFiles; do + if test -f "$file"; then + git add $file + fi + done + #Exit + exit 0 +else + echo "*********************************************************" + echo " ******************************************** " + echo 1>&2 "Spotless Apply found violations it could not fix." + echo "Run spotless apply in your terminal and fix the issues before trying to commit again." + echo " ******************************************** " + echo "*********************************************************" + #Exit + exit 1 +fi