Skip to content

Commit

Permalink
Make diktat-cli executable (#1843)
Browse files Browse the repository at this point in the history
- insert shell script on top of jar file, copied from benedikt
- fixed release.yml
  • Loading branch information
nulls authored Dec 11, 2023
1 parent c7cad50 commit bb23743
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 16 deletions.
43 changes: 29 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
31 changes: 29 additions & 2 deletions diktat-cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,7 +59,7 @@ sourceSets.getByName("main") {
)
}

tasks.named<ShadowJar>("shadowJar") {
tasks.shadowJar {
archiveClassifier.set("")
manifest {
attributes["Main-Class"] = "com.saveourtool.diktat.DiktatMainKt"
Expand All @@ -69,6 +68,34 @@ tasks.named<ShadowJar>("shadowJar") {
duplicatesStrategy = DuplicatesStrategy.FAIL
}

tasks.register<DefaultTask>("shadowExecutableJar") {
group = "Distribution"
dependsOn(tasks.shadowJar)

val scriptFile = project.file("src/main/script/diktat.sh")
val shadowJarFile = tasks.shadowJar
.get()
.outputs
.files
.singleFile
val outputFile = project.layout
.buildDirectory
.file(shadowJarFile.name.removeSuffix(".jar"))

inputs.files(scriptFile, shadowJarFile)
outputs.file(outputFile)

doLast {
outputFile.get()
.asFile
.apply {
writeBytes(scriptFile.readBytes())
appendBytes(shadowJarFile.readBytes())
setExecutable(true, false)
}
}
}

// disable default jar
tasks.named("jar") {
enabled = false
Expand Down
92 changes: 92 additions & 0 deletions diktat-cli/src/main/script/diktat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/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
IFS=$'\n'

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=3

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[@]}" "$@"

0 comments on commit bb23743

Please sign in to comment.