Skip to content

Commit 6753f4e

Browse files
committed
build: Refactor version info saving
1 parent 1ab34fc commit 6753f4e

File tree

5 files changed

+123
-25
lines changed

5 files changed

+123
-25
lines changed

buildSrc/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ repositories {
66
mavenCentral()
77
gradlePluginPortal()
88
}
9+
10+
dependencies {
11+
implementation(libs.kotlinx.serialization.json)
12+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import io.papermc.paperweight.util.Git
2+
import io.papermc.paperweight.util.createParentDirectories
3+
import io.papermc.paperweight.util.path
4+
import kotlinx.serialization.json.Json
5+
import kotlinx.serialization.json.buildJsonObject
6+
import kotlinx.serialization.json.put
7+
import org.gradle.api.DefaultTask
8+
import org.gradle.api.Project
9+
import org.gradle.api.file.RegularFileProperty
10+
import org.gradle.api.tasks.Input
11+
import org.gradle.api.tasks.OutputFile
12+
import org.gradle.api.tasks.TaskAction
13+
import org.gradle.kotlin.dsl.property
14+
import kotlin.io.path.writeText
15+
16+
abstract class IncludeVersionInfoTask : DefaultTask() {
17+
@get:Input
18+
val implementationName = project.objects.property<String>().convention(project.name)
19+
20+
@get:Input
21+
val implementationVersion = project.objects.property<String>().convention(project.version.toString())
22+
23+
@get:Input
24+
val implementationVendor = project.objects.property<String>().convention("ElectroPlay Development Team")
25+
26+
@get:Input
27+
val specificationName = project.objects.property<String>().convention("proxycheck.io")
28+
29+
@get:Input
30+
val specificationVersion = project.objects.property<String>()
31+
32+
@get:Input
33+
val specificationVendor = project.objects.property<String>().convention("https://proxycheck.io/api/")
34+
35+
@get:Input
36+
val gitBranch = project.objects
37+
.property<String>()
38+
.convention(project.git().exec(project.providers, "rev-parse", "--abbrev-ref", "HEAD").map { it.trim() })
39+
40+
@get:Input
41+
val gitCommit = project.objects
42+
.property<String>()
43+
.convention(project.git().exec(project.providers, "rev-parse", "--short=7", "HEAD").map { it.trim() })
44+
45+
@get:Input
46+
val gitTimestamp = project.objects
47+
.property<String>()
48+
.convention(gitCommit.flatMap { gitCommit -> project.git().exec(project.providers, "show", "-s", "--format=%cI", gitCommit) }.map { it.trim() })
49+
50+
@get:Input
51+
val contactWebsite = project.objects.property<String>().convention("https://epserv.ru/")
52+
53+
@get:Input
54+
val contactEmail = project.objects.property<String>().convention("admin@epserv.ru")
55+
56+
@get:OutputFile
57+
abstract val outputFile: RegularFileProperty
58+
59+
@TaskAction
60+
fun includeVersionInfo() {
61+
val outputPath = outputFile.get().path
62+
outputPath.createParentDirectories()
63+
64+
val versionInfo = buildJsonObject {
65+
put("implementation_name", implementationName.get())
66+
put("implementation_version", implementationVersion.get())
67+
put("implementation_vendor", implementationVendor.get())
68+
put("specification_name", specificationName.get())
69+
put("specification_version", specificationVersion.get())
70+
put("specification_vendor", specificationVendor.get())
71+
put("git_branch", gitBranch.get())
72+
put("git_commit", gitCommit.get())
73+
put("git_timestamp", gitTimestamp.get())
74+
put("contact_website", contactWebsite.get())
75+
put("contact_email", contactEmail.get())
76+
}
77+
78+
outputPath.writeText(Json.encodeToString(versionInfo))
79+
}
80+
81+
companion object {
82+
fun Project.git(): Git = Git(rootProject.layout.projectDirectory.path)
83+
}
84+
}

v3/impl-java/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ group = "${rootProject.group}.v3"
77
dependencies {
88
api(project(":proxy-check-v3-api"))
99
}
10+
11+
val includeVersionInfoTask = tasks.register<IncludeVersionInfoTask>("includeVersionInfo") {
12+
specificationVersion = "v3"
13+
outputFile = layout.buildDirectory.file("generated/resources/version_info.json")
14+
}
15+
16+
tasks.processResources {
17+
dependsOn(includeVersionInfoTask)
18+
from(includeVersionInfoTask.map { it.outputFile }) {
19+
into("ru/epserv/proxycheck/v3/impl")
20+
}
21+
}
22+

v3/impl-java/src/main/kotlin/ru/epserv/proxycheck/v3/impl/ProxyCheckApiImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import java.net.http.HttpClient
1717
import java.net.http.HttpRequest
1818
import java.net.http.HttpResponse
1919
import java.util.concurrent.CompletableFuture
20-
import java.util.jar.JarInputStream
2120
import kotlin.time.Duration
2221
import kotlin.time.Duration.Companion.minutes
2322
import kotlin.time.Duration.Companion.seconds
@@ -39,8 +38,9 @@ class ProxyCheckApiImpl(
3938
override val versionInfo: VersionInfo
4039

4140
init {
42-
val manifest = JarInputStream(this.javaClass.protectionDomain.codeSource.location.openStream()).use { it.manifest }
43-
this.versionInfo = VersionInfoImpl(manifest)
41+
val stream = checkNotNull(this.javaClass.getResourceAsStream("version_info.json")) { "Unable to find version_info.json resource" }
42+
val json = Json.decodeFromString<JsonObject>(stream.use { it.bufferedReader().readText() })
43+
this.versionInfo = VersionInfoImpl.CODEC.decode(KJsonOps, json).orThrow.first
4444
}
4545

4646
override fun checkAsync(

v3/impl-java/src/main/kotlin/ru/epserv/proxycheck/v3/impl/VersionInfoImpl.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
package ru.epserv.proxycheck.v3.impl
44

5+
import com.mojang.serialization.Codec
6+
import com.mojang.serialization.codecs.RecordCodecBuilder
57
import ru.epserv.proxycheck.v3.api.util.VersionInfo
6-
import java.util.jar.Attributes
7-
import java.util.jar.Manifest
88
import kotlin.time.ExperimentalTime
99
import kotlin.time.Instant
1010

@@ -26,26 +26,23 @@ data class VersionInfoImpl(
2626
) : VersionInfo {
2727
override val httpUserAgent by lazy { "${this.implementationName}/${this.implementationVersion} (+${this.contactWebsite}; <${this.contactEmail}>)" }
2828

29-
constructor(manifest: Manifest) : this(manifest.mainAttributes)
30-
31-
constructor(attributes: Attributes) : this(
32-
specificationName = attributes.getOrThrow("Specification-Title"),
33-
specificationVersion = attributes.getOrThrow("Specification-Version"),
34-
specificationVendor = attributes.getOrThrow("Specification-Vendor"),
35-
36-
implementationName = attributes.getOrThrow("Implementation-Title"),
37-
implementationVersion = attributes.getOrThrow("Implementation-Version"),
38-
implementationVendor = attributes.getOrThrow("Implementation-Vendor"),
39-
40-
gitCommit = attributes.getOrThrow("Git-Commit"),
41-
gitBranch = attributes.getOrThrow("Git-Branch"),
42-
gitTimestamp = Instant.parse(attributes.getOrThrow("Git-Timestamp")),
43-
44-
contactWebsite = attributes.getOrThrow("Contact-Website"),
45-
contactEmail = attributes.getOrThrow("Contact-Email"),
46-
)
47-
4829
companion object {
49-
private fun Attributes.getOrThrow(name: String): String = requireNotNull(this.getValue(name)) { "Unable to find manifest attribute: $name" }
30+
private val TIMESTAMP_CODEC = Codec.STRING.xmap(Instant::parse, Instant::toString)
31+
32+
internal val CODEC = RecordCodecBuilder.mapCodec {
33+
it.group(
34+
Codec.STRING.fieldOf("implementation_name").forGetter(VersionInfoImpl::implementationName),
35+
Codec.STRING.fieldOf("implementation_version").forGetter(VersionInfoImpl::implementationVersion),
36+
Codec.STRING.fieldOf("implementation_vendor").forGetter(VersionInfoImpl::implementationVendor),
37+
Codec.STRING.fieldOf("specification_name").forGetter(VersionInfoImpl::specificationName),
38+
Codec.STRING.fieldOf("specification_version").forGetter(VersionInfoImpl::specificationVersion),
39+
Codec.STRING.fieldOf("specification_vendor").forGetter(VersionInfoImpl::specificationVendor),
40+
Codec.STRING.fieldOf("git_branch").forGetter(VersionInfoImpl::gitBranch),
41+
Codec.STRING.fieldOf("git_commit").forGetter(VersionInfoImpl::gitCommit),
42+
TIMESTAMP_CODEC.fieldOf("git_timestamp").forGetter(VersionInfoImpl::gitTimestamp),
43+
Codec.STRING.fieldOf("contact_website").forGetter(VersionInfoImpl::contactWebsite),
44+
Codec.STRING.fieldOf("contact_email").forGetter(VersionInfoImpl::contactEmail),
45+
).apply(it, ::VersionInfoImpl)
46+
}.codec()
5047
}
5148
}

0 commit comments

Comments
 (0)