From 035f1d0c06a60a03ff40399e37afe0cb663d1cd0 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 1 Dec 2023 17:45:37 +0300 Subject: [PATCH 1/5] Make diktat-cli executable ### What's done: - insert shell script on top of jar file It closes #1842 --- diktat-cli/build.gradle.kts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/diktat-cli/build.gradle.kts b/diktat-cli/build.gradle.kts index f9addf8d21..39593a55a9 100644 --- a/diktat-cli/build.gradle.kts +++ b/diktat-cli/build.gradle.kts @@ -60,7 +60,7 @@ sourceSets.getByName("main") { ) } -tasks.named("shadowJar") { +val shadowJarTaskProvider = tasks.named("shadowJar") { archiveClassifier.set("") manifest { attributes["Main-Class"] = "com.saveourtool.diktat.DiktatMainKt" @@ -69,6 +69,35 @@ tasks.named("shadowJar") { duplicatesStrategy = DuplicatesStrategy.FAIL } +tasks.register("shadowExecutableJar") { + group = "Distribution" + dependsOn(shadowJarTaskProvider) + + val shadowJarFile = shadowJarTaskProvider.get().outputs.files.singleFile + val outputFile = project.layout.buildDirectory.file(shadowJarFile.name.removeSuffix(".jar")) + + inputs.file(shadowJarFile) + outputs.file(outputFile) + + doLast { + outputFile.get() + .asFile + .apply { + //language=shell script + writeText( + """ + #!/bin/sh + + exec java -jar "$0" "$@" + + """.trimIndent() + ) + appendBytes(shadowJarFile.readBytes()) + setExecutable(true, false) + } + } +} + // disable default jar tasks.named("jar") { enabled = false From 368e3a383f560a93cdd8215b02884d609c16a07d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 1 Dec 2023 18:28:01 +0300 Subject: [PATCH 2/5] diktatFix + fixed release.yml --- .github/workflows/release.yml | 43 +++++++++++++++++++++++------------ diktat-cli/build.gradle.kts | 17 +++++++++----- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 39efd158b9..aa2d8e00f3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,14 +33,6 @@ jobs: java-version: 11 distribution: temurin - - name: 'Publish a release to GitHub' - id: publish-github - uses: gradle/gradle-build-action@v2 - with: - gradle-version: wrapper - arguments: | - publishAllPublicationsToGitHubRepository - - name: 'Publish a release to Maven Central' id: publish-sonatype uses: gradle/gradle-build-action@v2 @@ -60,15 +52,38 @@ jobs: -Pgradle.publish.key=${{ secrets.GRADLE_KEY }} -Pgradle.publish.secret=${{ secrets.GRADLE_SECRET }} - github_release: - needs: release - name: 'Github Release' - runs-on: ubuntu-latest - steps: - - name: 'Github Release' + - name: 'Publish a release to GitHub' + id: publish-github + uses: gradle/gradle-build-action@v2 + with: + gradle-version: wrapper + arguments: | + publishAllPublicationsToGitHubRepository + - name: 'GitHub Release' + id: create_release uses: actions/create-release@v1 with: tag_name: ${{ github.ref }} release_name: Release ${{ github.ref }} draft: false prerelease: false + - name: Upload Diktat CLI to GitHub release + id: upload-release-asset-cli + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./diktat-cli/build/diktat-cli-${{ github.ref }} + asset_name: diktat-cli-${{ github.ref }} + asset_content_type: application/zip + - name: Upload Diktat ruleset for KtLint to GitHub release + id: upload-release-asset-ruleset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./diktat-ruleset/build/libs/diktat-${{ github.ref }}.jar + asset_name: diktat-${{ github.ref }}.jar + asset_content_type: application/zip diff --git a/diktat-cli/build.gradle.kts b/diktat-cli/build.gradle.kts index 39593a55a9..7d1950ef4e 100644 --- a/diktat-cli/build.gradle.kts +++ b/diktat-cli/build.gradle.kts @@ -1,6 +1,5 @@ import com.saveourtool.diktat.buildutils.configurePublications import com.github.jengelman.gradle.plugins.shadow.ShadowExtension -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import org.jetbrains.kotlin.incremental.createDirectory @Suppress("DSL_SCOPE_VIOLATION", "RUN_IN_SCRIPT") // https://github.com/gradle/gradle/issues/22797 @@ -60,7 +59,7 @@ sourceSets.getByName("main") { ) } -val shadowJarTaskProvider = tasks.named("shadowJar") { +tasks.shadowJar { archiveClassifier.set("") manifest { attributes["Main-Class"] = "com.saveourtool.diktat.DiktatMainKt" @@ -71,10 +70,16 @@ val shadowJarTaskProvider = tasks.named("shadowJar") { tasks.register("shadowExecutableJar") { group = "Distribution" - dependsOn(shadowJarTaskProvider) + dependsOn(tasks.shadowJar) - val shadowJarFile = shadowJarTaskProvider.get().outputs.files.singleFile - val outputFile = project.layout.buildDirectory.file(shadowJarFile.name.removeSuffix(".jar")) + val shadowJarFile = tasks.shadowJar + .get() + .outputs + .files + .singleFile + val outputFile = project.layout + .buildDirectory + .file(shadowJarFile.name.removeSuffix(".jar")) inputs.file(shadowJarFile) outputs.file(outputFile) @@ -83,7 +88,7 @@ tasks.register("shadowExecutableJar") { outputFile.get() .asFile .apply { - //language=shell script + // language=shell script writeText( """ #!/bin/sh From 54066701bf5318a50655bccf8112d218808db5a3 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 11 Dec 2023 13:52:02 +0300 Subject: [PATCH 3/5] copied diktat.sh from benedikt --- diktat-cli/build.gradle.kts | 12 +--- diktat-cli/src/main/script/diktat.sh | 91 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 diktat-cli/src/main/script/diktat.sh diff --git a/diktat-cli/build.gradle.kts b/diktat-cli/build.gradle.kts index 7d1950ef4e..a69cf24ea6 100644 --- a/diktat-cli/build.gradle.kts +++ b/diktat-cli/build.gradle.kts @@ -72,6 +72,7 @@ tasks.register("shadowExecutableJar") { group = "Distribution" dependsOn(tasks.shadowJar) + val scriptFile = project.file("src/main/script/diktat.sh") val shadowJarFile = tasks.shadowJar .get() .outputs @@ -81,7 +82,7 @@ tasks.register("shadowExecutableJar") { .buildDirectory .file(shadowJarFile.name.removeSuffix(".jar")) - inputs.file(shadowJarFile) + inputs.files(scriptFile, shadowJarFile) outputs.file(outputFile) doLast { @@ -89,14 +90,7 @@ tasks.register("shadowExecutableJar") { .asFile .apply { // language=shell script - writeText( - """ - #!/bin/sh - - exec java -jar "$0" "$@" - - """.trimIndent() - ) + writeBytes(scriptFile.readBytes()) appendBytes(shadowJarFile.readBytes()) setExecutable(true, false) } diff --git a/diktat-cli/src/main/script/diktat.sh b/diktat-cli/src/main/script/diktat.sh new file mode 100644 index 0000000000..512ef997ff --- /dev/null +++ b/diktat-cli/src/main/script/diktat.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# +# vim:ai et sw=4 si sta ts=4: +# +# External variables used: +# +# - JAVA_HOME +# - GITHUB_ACTIONS + +# Bash strict mode, +# see http://redsymbol.net/articles/unofficial-bash-strict-mode/. +set -euo pipefail + +function error() { + local message + message="$*" + + if [[ "${GITHUB_ACTIONS:=false}" == 'true' ]] + then + # Echoing to GitHub. + echo "::error::${message}" + elif [[ -t 1 ]] + then + # Echoing to a terminal. + echo -e "\e[1m$(basename "$0"): \e[31merror:\e[0m ${message}" >&2 + else + # Echoing to a pipe. + echo "$(basename "$0"): error: ${message}" >&2 + fi +} + +# Exit codes. +# The code of 1 is returned by ktlint in the event of failure. +declare -ir ERROR_JAVA_NOT_FOUND=2 +declare -ir ERROR_INCOMPATIBLE_BASH_VERSION=8 + +if (( BASH_VERSINFO[0] < 4 )) +then + error "bash version ${BASH_VERSION} is too old, version 4+ is required" + exit ${ERROR_INCOMPATIBLE_BASH_VERSION} +fi + +JAVA_ARGS=() +DIKTAT_JAR="$0" + +# Locates Java, preferring JAVA_HOME. +# +# The 1st variable expansion prevents the "unbound variable" error if JAVA_HOME +# is unset. +function find_java() { + if [[ -n "${JAVA_HOME:=}" ]] + then + case "$(uname -s)" in + 'MINGW32_NT-'* | 'MINGW64_NT-'* | 'MSYS_NT-'* ) + JAVA_HOME="$(cygpath "${JAVA_HOME}")" + ;; + esac + + JAVA="${JAVA_HOME}/bin/java" + # Update the PATH, just in case + export PATH="${JAVA_HOME}/bin:${PATH}" + elif [[ -x "$(which java 2>/dev/null)" ]] + then + JAVA="$(which java 2>/dev/null)" + else + error 'Java is not found' + exit ${ERROR_JAVA_NOT_FOUND} + fi +} + +# On Windows, converts a UNIX path to Windows. Should be invoked before a path +# is passed to any of the Windows-native tools (e.g.: `java`). +# +# On UNIX, just returns the 1st argument. +function native_path() { + case "$(uname -s)" in + 'MINGW32_NT-'* | 'MINGW64_NT-'* | 'MSYS_NT-'* ) + cygpath --windows "$1" + ;; + *) + echo "$1" + ;; + esac +} + +find_java + +JAVA_ARGS+=('-Xmx512m') +JAVA_ARGS+=('-jar' "$(native_path "${DIKTAT_JAR}")") + +exec "${JAVA}" "${JAVA_ARGS[@]}" "$@" From 4178c4bd6551237bd9effc395463ef675762a395 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 11 Dec 2023 14:47:39 +0300 Subject: [PATCH 4/5] override IFS --- diktat-cli/src/main/script/diktat.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/diktat-cli/src/main/script/diktat.sh b/diktat-cli/src/main/script/diktat.sh index 512ef997ff..b0f91b8f6e 100644 --- a/diktat-cli/src/main/script/diktat.sh +++ b/diktat-cli/src/main/script/diktat.sh @@ -10,6 +10,7 @@ # Bash strict mode, # see http://redsymbol.net/articles/unofficial-bash-strict-mode/. set -euo pipefail +IFS=$'\n' function error() { local message @@ -32,7 +33,7 @@ function error() { # Exit codes. # The code of 1 is returned by ktlint in the event of failure. declare -ir ERROR_JAVA_NOT_FOUND=2 -declare -ir ERROR_INCOMPATIBLE_BASH_VERSION=8 +declare -ir ERROR_INCOMPATIBLE_BASH_VERSION=3 if (( BASH_VERSINFO[0] < 4 )) then From 055d3d1c475c360300078d23f01b684476c86da6 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 11 Dec 2023 14:52:33 +0300 Subject: [PATCH 5/5] self review note --- diktat-cli/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/diktat-cli/build.gradle.kts b/diktat-cli/build.gradle.kts index a69cf24ea6..2c916a577f 100644 --- a/diktat-cli/build.gradle.kts +++ b/diktat-cli/build.gradle.kts @@ -89,7 +89,6 @@ tasks.register("shadowExecutableJar") { outputFile.get() .asFile .apply { - // language=shell script writeBytes(scriptFile.readBytes()) appendBytes(shadowJarFile.readBytes()) setExecutable(true, false)