Skip to content

Commit

Permalink
Merge pull request #115 from outfoxx/task/jbang-rel
Browse files Browse the repository at this point in the history
Add JBang scripts
  • Loading branch information
kdubb authored Oct 28, 2024
2 parents cb0ccba + 26f8a09 commit d0f547f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Git Short SHA
run: |
calculatedSha=$(git rev-parse --short ${{ github.sha }})
echo "GIT_COMMIT_SHA=$calculatedSha" >> $GITHUB_ENV
- name: Build Artifacts & Documentation
run: >-
./gradlew
-PreleaseVersion=${{ github.ref_name }}
build dokkaHtmlMultiModule
-x test
env:
GIT_COMMIT_SHA: ${{ env.GIT_COMMIT_SHORT_SHA }}

- name: Publish Maven Release
run: >-
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Git Short SHA
run: |
calculatedSha=$(git rev-parse --short ${{ github.sha }})
echo "GIT_COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
- name: Build Artifacts & Documentation
run: ./gradlew build dokkaHtmlMultiModule -x test
env:
GIT_COMMIT_SHA: ${{ env.GIT_COMMIT_SHORT_SHA }}

- name: Publish Maven Artifacts (Snapshot)
run: >-
Expand Down
38 changes: 34 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dev.yumi.gradle.licenser.YumiLicenserGradleExtension
import org.apache.tools.ant.filters.ReplaceTokens
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
Expand All @@ -19,7 +20,7 @@ plugins {

val ignoreCheckFailures = project.findProperty("ignoreCheckFailures")?.toString()?.toBoolean() ?: false

val moduleNames = listOf( "generator", "cli", "gradle-plugin")
val moduleNames = listOf("generator", "cli", "gradle-plugin")

val releaseVersion: String by project
val isSnapshot = releaseVersion.endsWith("SNAPSHOT")
Expand Down Expand Up @@ -70,7 +71,7 @@ configure(moduleNames.map { project(it) }) {
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions{
compilerOptions {
apiVersion.set(KotlinVersion.fromVersion(kotlinVersion))
languageVersion.set(KotlinVersion.fromVersion(kotlinVersion))
jvmTarget.set(JvmTarget.fromTarget(javaVersion))
Expand Down Expand Up @@ -182,6 +183,23 @@ tasks {
include("sunday.raml")
}
}
// Copy JBang script
copy {
into(relDocDir)
from("scripts") {
if (isSnapshot) {
include("sunday-snapshot")
rename("sunday-snapshot", "sunday")
} else {
include("sunday")
}
}
filter<ReplaceTokens>(
"tokens" to mapOf("env.VER" to releaseVersion),
"beginToken" to "\${",
"endToken" to "}"
)
}
// For major.minor.patch releases, add sunday.raml as current
// and add docs in "current" directory
if (releaseVersion.matches("""^\d+.\d+.\d+$""".toRegex())) {
Expand Down Expand Up @@ -223,6 +241,18 @@ sonar {
//

githubRelease {
copy {
into(layout.buildDirectory.dir("scripts"))
from("scripts") {
include("sunday")
}
filter<ReplaceTokens>(
"tokens" to mapOf("env.VER" to releaseVersion),
"beginToken" to "\${",
"endToken" to "}"
)
}

owner("outfoxx")
repo("sunday-generator")
tagName(releaseVersion)
Expand All @@ -236,11 +266,11 @@ githubRelease {
listOf("", "-javadoc", "-sources").map { suffix ->
file("$rootDir/$moduleName/build/libs/$moduleName-$releaseVersion$suffix.jar")
}
}
} + layout.buildDirectory.dir("scripts").get().file("sunday"),
)
overwrite(true)
authorization(
"Token " + (project.findProperty("github.token") as String? ?: System.getenv("GITHUB_TOKEN"))
"Token " + (project.findProperty("github.token") as String? ?: System.getenv("GITHUB_TOKEN")),
)
}

Expand Down
18 changes: 14 additions & 4 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.time.OffsetDateTime
import java.time.ZoneOffset

plugins {
application
Expand Down Expand Up @@ -30,12 +32,20 @@ application {
applicationDefaultJvmArgs = listOf("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}

val releaseVersion: String by project

fun Manifest.updateAttributes() {
val title = "Sunday Generator"
val version = releaseVersion
val build = System.getenv("GIT_COMMIT_SHA") ?: OffsetDateTime.now(ZoneOffset.UTC).toString()
attributes["Implementation-Title"] = title
attributes["Implementation-Version"] = version
attributes["Implementation-Build"] = build
}

tasks {
shadowJar.configure {
manifest {
attributes["Implementation-Title"] = "Sunday Generator"
attributes["Implementation-Version"] = project.version
}
manifest { updateAttributes() }
dependsOn(assembleDist)
archiveClassifier.set("")
minimize {
Expand Down
31 changes: 29 additions & 2 deletions cli/src/main/kotlin/io/outfoxx/sunday/generator/Version.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.PrintMessage
import com.github.ajalt.clikt.parameters.options.eagerOption
import com.github.ajalt.clikt.parameters.transform.theme
import java.util.jar.JarFile

fun CliktCommand.versionOption() =
eagerOption(setOf("--version"), help = "Show version information and exit") {
Expand All @@ -28,7 +29,7 @@ fun CliktCommand.versionOption() =
${theme.style("warning")("Sunday")} ${theme.style("info")("- Generator")} ver. ${
theme.style("danger")(
versionString
versionString,
)
}
Expand All @@ -47,4 +48,30 @@ fun CliktCommand.versionOption() =
}

val versionString: String
get() = GenerateCommand::class.java.`package`.implementationVersion ?: "unknown"
get() {
val version = GenerateCommand::class.java.`package`.implementationVersion ?: "unknown"
val commit = readImplBuild()
return if (commit != null) {
"$version (build: $commit)"
} else {
version
}
}

fun readImplBuild(): String? =
try {
val classPath = GenerateCommand::class.java.protectionDomain.codeSource.location.path
JarFile(classPath).use { jarFile ->

val manifest = jarFile.manifest
val mainAttributes = manifest.mainAttributes

return try {
mainAttributes.getValue("Implementation-Build").ifBlank { null }
} catch (e: Exception) {
null
}
}
} catch (e: Exception) {
null
}
12 changes: 12 additions & 0 deletions scripts/sunday
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//DEPS io.outfoxx.sunday:cli:${env.VER}

import io.outfoxx.sunday.generator.MainKt;

public class sunday {

public static void main(String... args) {
MainKt.main(args);
}
}
15 changes: 15 additions & 0 deletions scripts/sunday-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//REPOS https://oss.sonatype.org/content/repositories/snapshots
//DEPS io.outfoxx.sunday:cli:${env.VER}

import io.outfoxx.sunday.generator.MainKt;
import io.outfoxx.sunday.generator.VersionKt;

class sunday {

public static void main(String... args) {
System.out.println("Sunday CLI (SNAPSHOT) - " + VersionKt.getVersionString());
MainKt.main(args);
}
}

0 comments on commit d0f547f

Please sign in to comment.