Version file in deploy folder

This commit is contained in:
Tyler Wilcox 2026-01-29 22:55:45 -05:00
parent fb326a5465
commit ea7d8045b2
3 changed files with 56 additions and 0 deletions

3
.gitignore vendored
View File

@ -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

View File

@ -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'

View File

@ -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.