Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version constant injection #576

Merged
merged 9 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions buildSrc/src/main/kotlin/Projects.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import org.gradle.kotlin.dsl.DependencyHandlerScope
import org.gradle.kotlin.dsl.project
import org.gradle.api.Project
import java.io.ByteArrayOutputStream

/**
* whether the process has been invoked by JitPack
*/
val isJitPack get() = "true" == System.getenv("JITPACK")

val Project.commitHash: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github allows you to get the commit hash, why is this logic code introduced.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are referring to the GITHUB_SHA environment variable, right? This one is the full hash, @DRSchlaubi wanted to have the short one if available (retrieved by git rev-parse --short HEAD).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github doesn't provide that? in the environment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't search too long, maybe they do. Do you know more?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github doesn't provide that? in the environment?

https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables only lists the long SHA

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should compute the short hash in the actions instead (like here).
It would probably also be good to provide KORD_COMMIT_HASH and KORD_SHORT_COMMIT_HASH.

get() = try {
ByteArrayOutputStream().use { out ->
exec {
commandLine("git", "rev-parse", "--short", "HEAD")
standardOutput = out
}
out.toString().trim()
}
} catch (e: Throwable) {
System.getenv("GITHUB_SHA") ?: "unknown"
}

object Library {
const val name = "kord"
const val group = "dev.kord"
Expand Down
30 changes: 30 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
buildscript {
repositories {
mavenCentral()
}
}

plugins {
`kord-module`
`kord-sampled-module`
`kord-publishing`

// see https://github.com/gmazzo/gradle-buildconfig-plugin
id("com.github.gmazzo.buildconfig") version "3.0.3"
}

dependencies {
Expand All @@ -11,3 +20,24 @@ dependencies {
testImplementation(libs.bundles.test.implementation)
testRuntimeOnly(libs.bundles.test.runtime)
}

/*
This will generate a file named "BuildConfigGenerated.kt" that looks like:

package dev.kord.common

internal const val BUILD_CONFIG_GENERATED_LIBRARY_VERSION: String = "<version>"
internal const val BUILD_CONFIG_GENERATED_COMMIT_HASH: String = "<commit hash>"
*/
buildConfig {
packageName("dev.kord.common")
className("BuildConfigGenerated")

useKotlinOutput {
topLevelConstants = true
internalVisibility = true
}

buildConfigField(type = "String", name = "BUILD_CONFIG_GENERATED_LIBRARY_VERSION", value = "\"${Library.version}\"")
buildConfigField(type = "String", name = "BUILD_CONFIG_GENERATED_COMMIT_HASH", value = "\"$commitHash\"")
}
14 changes: 13 additions & 1 deletion common/src/main/kotlin/KordConstants.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
// No const vals, they are inlined, so recompiling would be required when values change.
@file:Suppress("MayBeConstant")

package dev.kord.common

public object KordConstants {

/** Kord's version. */
public val KORD_VERSION: String = BUILD_CONFIG_GENERATED_LIBRARY_VERSION

/** The hash of the commit from which this Kord version was built. */
public val KORD_COMMIT_HASH: String = BUILD_CONFIG_GENERATED_COMMIT_HASH

/** URL for Kord's GitHub repository. */
public val KORD_GITHUB_URL: String = "https://github.com/kordlib/kord"

/** Kord's value for the [User Agent header](https://discord.com/developers/docs/reference#user-agent). */
public const val USER_AGENT: String = "DiscordBot (https://github.com/kordlib/kord, 0.8.0-M12)"
public val USER_AGENT: String = "DiscordBot ($KORD_GITHUB_URL, $KORD_VERSION)"
}