From ea7d8045b2d49d08d30ba7d326b94f2194776b93 Mon Sep 17 00:00:00 2001 From: Tyler Wilcox Date: Thu, 29 Jan 2026 22:55:45 -0500 Subject: [PATCH] Version file in deploy folder --- .gitignore | 3 +++ build.gradle | 32 ++++++++++++++++++++++++++++++ src/main/java/frc/robot/Robot.java | 21 ++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/.gitignore b/.gitignore index 9a9ca7b..0eaa4d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # This gitignore has been specially created by the WPILib team. # If you remove items from this file, intellisense might break. +### Version Files ### +src/main/deploy/versionfile.txt + ### C++ ### # Prerequisites *.d diff --git a/build.gradle b/build.gradle index 7640d58..9a4bb94 100644 --- a/build.gradle +++ b/build.gradle @@ -101,9 +101,41 @@ deployArtifact.jarTask = jar wpi.java.configureExecutableTasks(jar) wpi.java.configureTestTasks(test) +def saveCommitTask = tasks.register("saveCommit") { + println "Current Version: " + getCheckedOutGitCommitHash(); + + def deployFolder = "$projectDir/src/main/deploy/" + def commitFile = new FileWriter(deployFolder + "versionfile.txt"); + doLast { + commitFile.write(getCheckedOutGitCommitHash() + " - " + new Date().toString()); + commitFile.close(); + println "versionfile.txt written"; + } +} + +def getCheckedOutGitCommitHash() { + def gitFolder = "$projectDir/.git/" + def takeFromHash = 12 + /* + * '.git/HEAD' contains either + * in case of detached head: the currently checked out commit hash + * otherwise: a reference to a file containing the current commit hash + */ + def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD + def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd + // def isRef = head.length > 1 // ref: refs/heads/master + + if(isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb + + def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master + refHead.text.trim().take takeFromHash +} + // Configure string concat to always inline compile tasks.withType(JavaCompile) { options.compilerArgs.add '-XDstringConcat=inline' + dependsOn saveCommitTask } + apply from: 'libs.gradle' diff --git a/src/main/java/frc/robot/Robot.java b/src/main/java/frc/robot/Robot.java index 2f9e6c3..9971fa1 100644 --- a/src/main/java/frc/robot/Robot.java +++ b/src/main/java/frc/robot/Robot.java @@ -4,6 +4,10 @@ package frc.robot; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + import org.fairportrobotics.frc.posty.PostyManager; import org.littletonrobotics.junction.LogFileUtil; import org.littletonrobotics.junction.LoggedRobot; @@ -12,6 +16,7 @@ import org.littletonrobotics.junction.networktables.NT4Publisher; import org.littletonrobotics.junction.wpilog.WPILOGReader; import org.littletonrobotics.junction.wpilog.WPILOGWriter; +import edu.wpi.first.wpilibj.Filesystem; import edu.wpi.first.wpilibj.PowerDistribution; import edu.wpi.first.wpilibj.PowerDistribution.ModuleType; import edu.wpi.first.wpilibj2.command.Command; @@ -31,6 +36,19 @@ public class Robot extends LoggedRobot { private RobotContainer m_robotContainer; + public static String getVersion() { + try { + Scanner scan = new Scanner(new File(Filesystem.getDeployDirectory().getAbsolutePath() + "/versionfile.txt")); + String ver = scan.nextLine(); + scan.close(); + + return ver; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return ""; + } + /** * This function is run when the robot is first started up and should be used * for any @@ -40,6 +58,7 @@ public class Robot extends LoggedRobot { public void robotInit() { Logger.recordMetadata("ProjectName", "MyProject"); // Set a metadata value + Logger.recordMetadata("Software Version", getVersion()); if (isReal()) { Logger.addDataReceiver(new WPILOGWriter()); // Log to a USB stick ("/U/logs") @@ -58,6 +77,8 @@ public class Robot extends LoggedRobot { Logger.start(); // Start logging! No more data receivers, replay sources, or metadata values may // be added. + Logger.recordOutput("Software Version", getVersion()); + // Instantiate our RobotContainer. This will perform all our button bindings, // and put our // autonomous chooser on the dashboard.