diff --git a/.github/workflows/gradle-build.yml b/.github/workflows/gradle-build.yml index e70c929..0e7913a 100644 --- a/.github/workflows/gradle-build.yml +++ b/.github/workflows/gradle-build.yml @@ -22,6 +22,11 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 + # Fetch tags and full project history + - run: | + git fetch --prune --unshallow --tags + git describe --tags + - name: Set Up JDK 1.8 uses: actions/setup-java@v1.4.0 with: diff --git a/build.gradle.kts b/build.gradle.kts index 45f2095..1383ef9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import java.io.BufferedInputStream +import java.io.BufferedReader +import java.io.ByteArrayOutputStream +import java.io.InputStreamReader +import kotlin.collections.toByteArray plugins { kotlin("jvm") @@ -9,7 +14,29 @@ plugins { } group = "com.virtuslab.dokka" -version = "0.1.3" + + +fun String.run(): String? { + val proc = Runtime.getRuntime()?.exec(this) + if (proc?.waitFor() != 0) return null + val os = proc.inputStream + return os.let { + val line = BufferedReader(InputStreamReader(it)).readLines().joinToString("\n") + os.close() + return line.trim() + } +} + +fun getVersion(): String { + val base = "git describe --tags --exact-match".run() ?: "git describe --tags".run() ?: "0.1.0-SNAPSHOT" + + val statusStr = "git status --porcelain".run()?.let { if (it.trim().length > 2) "-SNAPSHOT" else "" } ?: "-SNAPSHOT" + val v = base + statusStr + println("Using $v version.") + return v +} + +version = getVersion() tasks.withType(KotlinCompile::class).all { val language_version: String by project