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

feat: Expose server build information #1336

Draft
wants to merge 8 commits into
base: dev/3.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ tasks {
attributes["Automatic-Module-Name"] = "com.velocitypowered.api"
}
}
// TEMP
withType<Checkstyle> {
exclude("**/com/velocitypowered/api/util/buildinfo/*ServerBuildInfo.java")
}

withType<Javadoc> {
exclude("com/velocitypowered/api/plugin/ap/**")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.util.buildinfo;

import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Information about the current server build.
*
* @apiNote to be separated later
*/
@SuppressWarnings({"checkstyle", "CheckStyle"}) // Temporarily
@ApiStatus.NonExtendable
public interface PaperServerBuildInfo extends ServerBuildInfo {

/**
* The brand id for Paper.
*/
Key BRAND_PAPER_ID = Key.key("papermc", "paper");

/**
* Gets the Minecraft version id.
*
* @return the Minecraft version id (e.g. "1.20.4", "1.20.2-pre2", "23w31a")
*/
@NotNull String minecraftVersionId();

/**
* Gets the Minecraft version name.
*
* @return the Minecraft version name (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a")
*/
@NotNull String minecraftVersionName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.util.buildinfo;

import java.time.Instant;
import java.util.Optional;
import java.util.OptionalInt;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.adventure.util.Services;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Information about the current server build.
*
* @apiNote to be separated later
*/
@SuppressWarnings({"checkstyle", "CheckStyle"}) // Temporarily
@ApiStatus.NonExtendable
public interface ServerBuildInfo {

/**
* Gets the {@code ServerBuildInfo}.
*
* @return the {@code ServerBuildInfo}
*/
// TODO: This works but, I have no clue if this is correct codewise
static <T extends ServerBuildInfo> @NotNull T buildInfo() {
//<editor-fold defaultstate="collapsed" desc="Holder">
/**
* This is a holder, it holds the serverbuildinfo :).
*/
final class Holder {
static final Optional<ServerBuildInfo> INSTANCE = Services.service(ServerBuildInfo.class);
}
//</editor-fold>

return (T) Holder.INSTANCE.orElseThrow();
}

/**
* Gets the brand id of the server.
*
* @return the brand id of the server (e.g. "papermc:velocity")
*/
@NotNull Key brandId();

/**
* Checks if the current server supports the specified brand.
*
* @param brandId the brand to check (e.g. "papermc:folia")
* @return {@code true} if the server supports the specified brand
*/
@ApiStatus.Experimental
boolean isBrandCompatible(final @NotNull Key brandId);

/**
* Gets the brand name of the server.
*
* @return the brand name of the server (e.g. "Velocity")
*/
@NotNull String brandName();

/**
* Gets the build number.
*
* @return the build number
*/
@NotNull OptionalInt buildNumber();

/**
* Gets the build time.
*
* @return the build time
*/
@NotNull Instant buildTime();

/**
* Gets the git commit branch.
*
* @return the git commit branch
*/
@NotNull Optional<String> gitBranch();

/**
* Gets the git commit hash.
*
* @return the git commit hash
*/
@NotNull Optional<String> gitCommit();

/**
* Creates a string representation of the server build information.
*
* @param representation the type of representation
* @return a string
*/
// This *could* be a PlainTextSerializer string of asComponent()?
@NotNull default String asString(final @NotNull StringRepresentation representation) {
return PlainTextComponentSerializer.plainText().serialize(asComponent(representation));
}

@NotNull Component asComponent(final @NotNull StringRepresentation representation);

/**
* String representation types.
*/
enum StringRepresentation {
/**
* A simple version string, in format {@code <versionName>-<buildNumber>-<gitCommit>}.
*/
VERSION_SIMPLE,
/**
* A simple version string, in format {@code <versionName>-<buildNumber>-<gitBranch>@<gitCommit> (<buildTime>)}.
*/
VERSION_FULL,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.util.buildinfo;

import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Information about the current server build.
*
* @apiNote to be separated later
*/
@SuppressWarnings({"checkstyle", "CheckStyle"}) // Temporarily
@ApiStatus.NonExtendable
public interface VelocityServerBuildInfo extends ServerBuildInfo {

/**
* The brand id for Velocity.
*/
Key BRAND_VELOCITY_ID = Key.key("papermc", "velocity");

/**
* Gets the Velocity version id.
*
* @return the Velocity version id (e.g. "3.3.0-SNAPSHOT", "3.3.0", "3.0.0")
*/
@NotNull
String velocityVersionId();
// one of these can probably go
/**
* Gets the Velocity version name.
*
* @return the Velocity version name (e.g. "3.3.0 Snapshot", "3.3.0", "3.0.0")
*/
@NotNull
String velocityVersionName();
}
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
// see https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
implementation("com.diffplug.spotless:spotless-plugin-gradle:${libs.plugins.spotless.get().version}")
implementation("net.kyori:indra-git:${libs.versions.indra.get()}")
}

spotless {
Expand Down
34 changes: 23 additions & 11 deletions build-logic/src/main/kotlin/velocity-init-manifest.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import net.kyori.indra.git.IndraGitExtension
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.withType
import java.io.ByteArrayOutputStream
import java.time.Instant

val currentShortRevision = ByteArrayOutputStream().use {
exec {
executable = "git"
args = listOf("rev-parse", "HEAD")
standardOutput = it
}
it.toString().trim().substring(0, 8)
plugins {
id("net.kyori.indra.git")
}

tasks.withType<Jar> {
manifest {
val indraGit = project.extensions.getByType(IndraGitExtension::class.java)
val buildNumber = System.getenv("BUILD_NUMBER")
val gitHash = indraGit.commit()?.name?.substring(0, 8) ?: "unknown"
val gitBranch = indraGit.branchName() ?: "unknown"
val velocityVersion = project.version.toString()
val velocityVersionButWithoutTheDashSnapshot = velocityVersion.replace("-SNAPSHOT", "")
val implementationVersion = "$velocityVersion-${buildNumber ?: "DEV"}-$gitHash"
val velocityHumanVersion: String =
if (project.version.toString().endsWith("-SNAPSHOT")) {
if (buildNumber == null) {
"${project.version} (git-$currentShortRevision)"
"${project.version} (git-$gitHash)"
} else {
"${project.version} (git-$currentShortRevision-b$buildNumber)"
"${project.version} (git-$gitHash-b$buildNumber)"
}
} else {
archiveVersion.get()
}
attributes["Implementation-Version"] = velocityHumanVersion
attributes["Implementation-Title"] = "Velocity"
attributes["Implementation-Vendor"] = "Velocity Contributors"
attributes["Multi-Release"] = "true"
attributes["Specification-Version"] = velocityHumanVersion
attributes["Implementation-Version"] = velocityVersionButWithoutTheDashSnapshot
attributes["Brand-Id"] = "papermc:velocity"
attributes["Brand-Name"] = "Velocity"
attributes["Build-Number"] = (buildNumber ?: "")
attributes["Build-Time"] = Instant.now().toString()
attributes["Git-Branch"] = gitBranch
attributes["Git-Commit"] = gitHash
}
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ configurate4 = "4.1.2"
flare = "2.0.1"
log4j = "2.22.1"
netty = "4.1.106.Final"
indra = "3.1.3"

[plugins]
indra-publishing = "net.kyori.indra.publishing:2.0.6"
shadow = "io.github.goooler.shadow:8.1.5"
spotless = "com.diffplug.spotless:6.25.0"
indra-git = { id = "net.kyori.indra.git", version.ref = "indra" }

[libraries]
adventure-bom = "net.kyori:adventure-bom:4.17.0"
Expand Down
12 changes: 4 additions & 8 deletions proxy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ application {
}

tasks {
withType<Checkstyle> {
exclude("**/com/velocitypowered/proxy/protocol/packet/**")
runShadow {
workingDir = project.file("run").also(File::mkdirs)
}

jar {
manifest {
attributes["Implementation-Title"] = "Velocity"
attributes["Implementation-Vendor"] = "Velocity Contributors"
attributes["Multi-Release"] = "true"
}
withType<Checkstyle> {
exclude("**/com/velocitypowered/proxy/protocol/packet/**")
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.velocitypowered.api.util.Favicon;
import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.api.util.buildinfo.ServerBuildInfo;
import com.velocitypowered.proxy.command.VelocityCommandManager;
import com.velocitypowered.proxy.command.builtin.CallbackCommand;
import com.velocitypowered.proxy.command.builtin.GlistCommand;
Expand Down Expand Up @@ -182,7 +183,9 @@ public VelocityConfiguration getConfiguration() {

@Override
public ProxyVersion getVersion() {
// TODO: this can likely also be changed to ServerBuildInfo
Package pkg = VelocityServer.class.getPackage();
final ServerBuildInfo buildInfo = ServerBuildInfo.buildInfo();
String implName;
String implVersion;
String implVendor;
Expand All @@ -196,7 +199,7 @@ public ProxyVersion getVersion() {
implVendor = "Velocity Contributors";
}

return new ProxyVersion(implName, implVendor, implVersion);
return new ProxyVersion(buildInfo.brandName(), implVendor, implVersion);
}

@Override
Expand All @@ -211,7 +214,8 @@ void awaitProxyShutdown() {
@EnsuresNonNull({"serverKeyPair", "servers", "pluginManager", "eventManager", "scheduler",
"console", "cm", "configuration"})
void start() {
logger.info("Booting up {} {}...", getVersion().getName(), getVersion().getVersion());
final ServerBuildInfo buildInfo = ServerBuildInfo.buildInfo();
logger.info("Booting up {} {}...", getVersion().getName(), buildInfo.asString(ServerBuildInfo.StringRepresentation.VERSION_FULL));
console.setupStreams();

registerTranslations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.api.util.buildinfo.ServerBuildInfo;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.util.InformationUtils;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -154,13 +155,15 @@ private record Info(ProxyServer server) implements Command<CommandSource> {
public int run(final CommandContext<CommandSource> context) {
final CommandSource source = context.getSource();
final ProxyVersion version = server.getVersion();
final ServerBuildInfo build = ServerBuildInfo.buildInfo();


final Component velocity = Component.text()
.content(version.getName() + " ")
.decoration(TextDecoration.BOLD, true)
.color(VELOCITY_COLOR)
.append(Component.text()
.content(version.getVersion())
.append(build.asComponent(ServerBuildInfo.StringRepresentation.VERSION_FULL))
.decoration(TextDecoration.BOLD, false))
.build();
final Component copyright = Component
Expand Down
Loading