-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include commit hash in clientcommands build and use it in github link…
… in villager rng invalid setup help
- Loading branch information
1 parent
f75ae00
commit d1054fa
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...Src/src/main/kotlin/net/earthcomputer/clientcommands/buildscript/GenerateBuildInfoTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/net/earthcomputer/clientcommands/util/BuildInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |