Skip to content

Commit e009eef

Browse files
committed
Refactor BCInfo to load from a file
Still cheap and helps gradle stop unnecessary recompiles
1 parent c188758 commit e009eef

File tree

4 files changed

+73
-54
lines changed

4 files changed

+73
-54
lines changed

BotCommands-core/build.gradle.kts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,16 @@ val generateInfo by tasks.registering(GenerateBCInfoTask::class) {
131131
outputs.upToDateWhen { false }
132132
}
133133

134-
ksp {
135-
excludedSources.from(generateInfo)
136-
}
137-
138134
sourceSets {
139135
main {
140-
java {
136+
resources {
141137
srcDir(generateInfo)
142-
exclude("**/\$BCInfo.java")
143138
}
144139
}
145140
}
146141

147142
dokka {
148143
dokkaSourceSets.configureEach {
149-
suppressedFiles.from("src/main/java/io/github/freya022/botcommands/api/\$BCInfo.java")
150144
suppressGeneratedFiles = false
151145
}
152146
}

BotCommands-core/src/main/java/io/github/freya022/botcommands/api/$BCInfo.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.freya022.botcommands.api;
2+
3+
import io.github.freya022.botcommands.api.core.Logging;
4+
5+
import javax.annotation.Nonnull;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.time.Instant;
10+
import java.util.Properties;
11+
12+
public class BCInfo {
13+
14+
public static final Instant BUILD_TIME;
15+
public static final String VERSION_MAJOR;
16+
public static final String VERSION_MINOR;
17+
public static final String VERSION_REVISION;
18+
public static final String VERSION_CLASSIFIER;
19+
public static final String GITHUB = "https://github.com/freya022/BotCommands";
20+
/** May be "null", may also be a full commit hash in Jitpack builds */
21+
public static final String BRANCH_NAME;
22+
/** May be "null" */
23+
public static final String COMMIT_HASH;
24+
public static final String BUILD_JDA_VERSION;
25+
26+
@SuppressWarnings("ConstantConditions")
27+
public static final String VERSION;
28+
29+
static {
30+
final Properties properties = loadProperties();
31+
32+
BUILD_TIME = Instant.ofEpochSecond(Long.parseLong(properties.getProperty("BUILD_TIME", "0")));
33+
VERSION_MAJOR = properties.getProperty("VERSION_MAJOR", "<major>");
34+
VERSION_MINOR = properties.getProperty("VERSION_MINOR", "<minor>");
35+
VERSION_REVISION = properties.getProperty("VERSION_REVISION", "<revision>");
36+
VERSION_CLASSIFIER = properties.getProperty("VERSION_CLASSIFIER", "<classifier>");
37+
BRANCH_NAME = properties.getProperty("BRANCH_NAME", "<branch>");
38+
COMMIT_HASH = properties.getProperty("COMMIT_HASH", "<commit>");
39+
BUILD_JDA_VERSION = properties.getProperty("BUILD_JDA_VERSION", "<JDA_version>");
40+
VERSION = "%s.%s.%s%s%s".formatted(VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION,
41+
VERSION_CLASSIFIER.equals("null") ? "" : "-" + VERSION_CLASSIFIER,
42+
COMMIT_HASH.equals("null") ? "" : "_" + COMMIT_HASH);
43+
}
44+
45+
@Nonnull
46+
private static Properties loadProperties() {
47+
final Properties properties = new Properties(8);
48+
try (InputStream stream = BCInfo.class.getResourceAsStream("/BCInfo.properties")) {
49+
if (stream == null) throw new IOException("Unable to find BCInfo.properties");
50+
try (InputStreamReader reader = new InputStreamReader(stream)) {
51+
properties.load(reader);
52+
}
53+
} catch (Exception e) {
54+
Logging.getLogger().error("Could not load properties file!", e);
55+
}
56+
return properties;
57+
}
58+
}
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import org.gradle.api.DefaultTask
22
import org.gradle.api.provider.ProviderFactory
33
import org.gradle.api.tasks.Input
4-
import org.gradle.api.tasks.InputFile
54
import org.gradle.api.tasks.OutputDirectory
65
import org.gradle.api.tasks.TaskAction
76
import org.gradle.work.DisableCachingByDefault
@@ -11,11 +10,8 @@ import javax.inject.Inject
1110
@DisableCachingByDefault
1211
abstract class GenerateBCInfoTask : DefaultTask() {
1312

14-
@get:InputFile
15-
val inputFile = project.layout.projectDirectory.file($$"src/main/java/io/github/freya022/botcommands/api/$BCInfo.java")
16-
1713
@get:OutputDirectory
18-
val outputDir = project.layout.buildDirectory.dir("generated/sources/BotCommands/main/java")
14+
val outputDir = project.layout.buildDirectory.dir("generated/sources/BotCommands/main/resources")
1915

2016
@get:Input
2117
val projectDir: String = project.projectDir.absolutePath
@@ -32,26 +28,19 @@ abstract class GenerateBCInfoTask : DefaultTask() {
3228

3329
@TaskAction
3430
fun run() {
35-
val attributes = mapOf(
36-
"version-major" to version.major,
37-
"version-minor" to version.minor,
38-
"version-revision" to version.revision,
39-
"version-classifier" to (version.classifier ?: "null"),
40-
"branch-name" to (GitUtils.getCommitBranch(logger, providers, projectDir) ?: "null"),
41-
"commit-hash" to (GitUtils.getCommitHash(logger, providers, projectDir)?.take(10) ?: "null"),
42-
"build-jda-version" to jdaVersion,
43-
"build-time" to Instant.now().toEpochMilli().toString(),
44-
)
45-
46-
val initialContent = inputFile.asFile.readText()
47-
val filteredContent = initialContent.replaceTokens(attributes).replace($$"$BCInfo", "BCInfo")
48-
49-
val bcInfoFile = outputDir.get().file("io/github/freya022/botcommands/api/BCInfo.java").asFile
31+
val content = """
32+
BUILD_TIME = ${Instant.now().toEpochMilli()}
33+
VERSION_MAJOR = ${version.major}
34+
VERSION_MINOR = ${version.minor}
35+
VERSION_REVISION = ${version.revision}
36+
VERSION_CLASSIFIER = ${version.classifier ?: "null"}
37+
BRANCH_NAME = ${GitUtils.getCommitBranch(logger, providers, projectDir) ?: "null"}
38+
COMMIT_HASH = ${GitUtils.getCommitHash(logger, providers, projectDir)?.take(10) ?: "null"}
39+
BUILD_JDA_VERSION = $jdaVersion
40+
""".trimIndent()
41+
42+
val bcInfoFile = outputDir.get().file("BCInfo.properties").asFile
5043
bcInfoFile.parentFile.mkdirs()
51-
bcInfoFile.writeText(filteredContent)
52-
}
53-
54-
private fun String.replaceTokens(map: Map<String, String>): String {
55-
return map.entries.fold(this) { current, (key, value) -> current.replace("@$key@", value) }
44+
bcInfoFile.writeText(content)
5645
}
5746
}

0 commit comments

Comments
 (0)