Skip to content

Commit

Permalink
Include commit hash in clientcommands build and use it in github link…
Browse files Browse the repository at this point in the history
… in villager rng invalid setup help
  • Loading branch information
Earthcomputer committed Dec 3, 2024
1 parent f75ae00 commit d1054fa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net.earthcomputer.clientcommands.buildscript.CheckLanguageFilesTask
import net.earthcomputer.clientcommands.buildscript.GenerateBuildInfoTask

plugins {
id 'fabric-loom' version '1.9-SNAPSHOT'
Expand Down Expand Up @@ -94,6 +95,10 @@ jar {
from("LICENSE")
}

tasks.register('generateBuildInfo', GenerateBuildInfoTask) {
outputFile = new File(temporaryDir, "build_info.json")
}

processResources {
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft_version_dependency
Expand All @@ -108,6 +113,8 @@ processResources {
from(sourceSets.main.resources.srcDirs) {
exclude "fabric.mod.json"
}

from generateBuildInfo
}

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.earthcomputer.clientcommands.buildscript

import com.google.gson.Gson
import com.google.gson.JsonObject
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecOperations
import java.io.ByteArrayOutputStream
import javax.inject.Inject

abstract class GenerateBuildInfoTask : DefaultTask() {
@get:OutputFile
abstract val outputFile: RegularFileProperty

@get:Inject
protected abstract val execOperations: ExecOperations

init {
outputs.upToDateWhen { false }
}

@TaskAction
fun run() {
val gitCommitHashBytes = ByteArrayOutputStream()
execOperations.exec {
commandLine("git", "rev-parse", "HEAD")
standardOutput = gitCommitHashBytes
}.rethrowFailure()
val commitHash = gitCommitHashBytes.toByteArray().decodeToString().trim()

val json = JsonObject()
json.addProperty("commit_hash", commitHash)

outputFile.asFile.get().writer().use {
Gson().toJson(json, it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.earthcomputer.clientcommands.command.VillagerCommand;
import net.earthcomputer.clientcommands.event.ClientConnectionEvents;
import net.earthcomputer.clientcommands.event.MoreClientEvents;
import net.earthcomputer.clientcommands.util.BuildInfo;
import net.earthcomputer.clientcommands.util.CUtil;
import net.earthcomputer.clientcommands.util.CombinedMedianEM;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -287,7 +288,17 @@ private static void checkVillagerSetup() {
}

private static void sendInvalidSetupHelp() {
ClientCommandHelper.sendHelp(Component.translatable("villagerManip.help.setup", Component.translatable("villagerManip.help.setup.link").withStyle(style -> style.withUnderlined(true).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://raw.githubusercontent.com/Earthcomputer/clientcommands/refs/heads/fabric/dpcs/villager_rng_setup.png")))));
ClientCommandHelper.sendHelp(
Component.translatable("villagerManip.help.setup",
Component.translatable("villagerManip.help.setup.link").withStyle(style -> style
.withUnderlined(true)
.withClickEvent(new ClickEvent(
ClickEvent.Action.OPEN_URL,
"https://github.com/Earthcomputer/clientcommands/blob/%s/docs/villager_rng_setup.png?raw=true".formatted(BuildInfo.INSTANCE.shortCommitHash(10))
))
)
)
);
}

private static void onDisconnect() {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/net/earthcomputer/clientcommands/util/BuildInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.earthcomputer.clientcommands.util;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.logging.LogUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.Util;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;

public record BuildInfo(String commitHash) {
private static final Logger LOGGER = LogUtils.getLogger();

private static final BuildInfo UNKNOWN = new BuildInfo("refs/heads/fabric");

public static final BuildInfo INSTANCE = Util.make(() -> {
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

Path buildInfoPath = FabricLoader.getInstance().getModContainer("clientcommands")
.orElseThrow()
.findPath("build_info.json")
.orElse(null);
if (buildInfoPath == null) {
LOGGER.warn("Couldn't find build_info.json");
return UNKNOWN;
}

try (BufferedReader reader = Files.newBufferedReader(buildInfoPath)) {
BuildInfo result = gson.fromJson(reader, BuildInfo.class);
if (result == null) {
LOGGER.warn("build_info.json was null or empty");
return UNKNOWN;
}
return result;
} catch (Throwable e) {
LOGGER.warn("Couldn't read build_info.json", e);
return UNKNOWN;
}
});

public String shortCommitHash(int length) {
return this == UNKNOWN || length >= commitHash.length() ? commitHash : commitHash.substring(0, length);
}
}

0 comments on commit d1054fa

Please sign in to comment.