Skip to content

Commit

Permalink
Swap to gradle plugin for git info (#97)
Browse files Browse the repository at this point in the history
* Switch git info to use WPILib recommended plugin

* Cleanup
  • Loading branch information
pordonj authored Apr 28, 2024
1 parent 78d3d09 commit a19e80d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,4 @@ ctre_sim/
simgui.json
simgui-ds.json
networktables.json
src/main/deploy/version/*
src/main/java/frc/robot/generated/*
90 changes: 23 additions & 67 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id "java"
id "edu.wpi.first.GradleRIO" version "2024.3.2"
id 'com.diffplug.spotless' version '6.20.0'
id "com.peterabeles.gversion" version "1.10"
}

java {
Expand Down Expand Up @@ -106,6 +107,11 @@ tasks.withType(JavaCompile) {
options.compilerArgs.add '-XDstringConcat=inline'
}

// -------------------------
// --- Additions by 1675 ---
// -------------------------

// Configure spotless for styling and checks
spotless {
enforceCheck = false
java {
Expand Down Expand Up @@ -150,79 +156,29 @@ spotless {
}
}

def getRobotVersionData() {
def robotVersion = new StringBuilder()
robotVersion.append("Commit: @"+"git rev-parse --short HEAD".execute().text.replace('\n', '')+" by "+"git config user.name".execute().text+"\n")
robotVersion.append("Branch: ")
robotVersion.append('git branch --show-current'.execute().text)
robotVersion.append("Uncommitted Changes: ")
robotVersion.append('git diff --shortstat'.execute().text)
robotVersion.append("--------------------\n")
robotVersion.append('git status -s'.execute().text)
return robotVersion.toString()
}

def getMinifiedRobotVersionData() {
def miniData = new StringBuilder()
miniData.append("git config user.name".execute().text + "@")
miniData.append("git branch --show-current".execute().text)
miniData.append(" | "+"git rev-parse --short HEAD".execute().text)
miniData.append(" | "+("git status -s".execute().text.length() > 0 ? "*" : ""))
return miniData.toString().replace('\n', '')
}

def writeVersionToFile = {
->
if("git config user.name".execute().text.length() < 1) {
println "Git username not configured, skipping robot version file writing. We are probably on the build server."
return;
}

def versionDeployDir = new File("./src/main/deploy/version");

if(!versionDeployDir.exists()) {
if(! versionDeployDir.mkdir()) {
println "Version deploy directory could not be created."
}
}

def versionFile = new File(versionDeployDir, ".robotVersion")
def versionFileMini = new File(versionDeployDir, ".robotVersionMini")
if(versionFile.exists()){
versionFile.delete()
}

if(versionFileMini.exists()) {
versionFileMini.delete()
}
versionFile << getRobotVersionData()
versionFileMini << getMinifiedRobotVersionData()
println("Wrote version information to .robotVersion")
}

tasks.named("build") {
doFirst {
writeVersionToFile()
}
}

tasks.named("deploy") {
doFirst {
writeVersionToFile()
}
// Configure gversion for generating build/git info
// Credit to 2024 group led by Michael S for writing a standalong implementation before we learned about the gradle plugin
gversion {
srcDir = "src/main/java/"
classPackage = "frc.robot.generated.git"
className = "BuildGitInfo"
dateFormat = "yyyy-MM-dd HH:mm:ss z"
timeZone = "America/Chicago"
indent = " "
}

tasks.named("simulateExternalJavaRelease") {
doFirst {
writeVersionToFile()
}
}


// Installs/updates git hook on build
// Credit to Michael S 2024 for coming up with git hook idea to enforce spotless
task installLocalGitHook(type: Copy){
from new File(rootProject.rootDir, 'scripts/pre-commit')
into { new File(rootProject.rootDir, '.git/hooks')}
fileMode 0775
}

// Set up custom task dependencies

// install local git hooks on build
build.dependsOn installLocalGitHook

// create git info file before compile
compileJava.dependsOn createVersionFile
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import frc.robot.util.AllianceUtil;
import frc.robot.util.Dashboards;
import frc.robot.util.RobotContext;
import frc.robot.util.VersionFile;

public class RobotContainer {
private final PoseScheduler poseScheduler;
Expand Down Expand Up @@ -78,7 +77,7 @@ public RobotContainer() {
Dashboards.initVoltageDashboard();
Dashboards.initCurrentDashboard();
Dashboards.initMemoryDashboard();
VersionFile.getInstance().putToDashboard();
Dashboards.initGitInfoDashboard();

// Comment the below out when not testing.
// initTestingOnlyTab();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/frc/robot/util/Dashboards.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import edu.wpi.first.wpilibj.RobotController;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab;
import frc.robot.generated.git.BuildGitInfo;

public class Dashboards {
private static boolean voltageNeedsInit = true;
private static boolean currentNeedsInit = true;
private static boolean memoryNeedsInit = true;
private static boolean gitInfoNeedsInit = true;

private static final PowerDistribution PDH = new PowerDistribution(1, ModuleType.kRev);

public static void initVoltageDashboard() {
Expand Down Expand Up @@ -50,6 +53,18 @@ public static void initMemoryDashboard() {
Runtime r = Runtime.getRuntime();
Shuffleboard.getTab("Memory Usage")
.addDouble("Usage percent", () -> r.freeMemory() / (double) r.totalMemory());
memoryNeedsInit = false;
}
}

public static void initGitInfoDashboard() {
if (gitInfoNeedsInit) {
ShuffleboardTab tab = Shuffleboard.getTab("Git Info");
tab.addString("Branch", () -> BuildGitInfo.GIT_BRANCH);
tab.addString("Commit", () -> BuildGitInfo.GIT_SHA.substring(0, 8));
tab.addBoolean("Dirty?", () -> BuildGitInfo.DIRTY != 0); // true if any uncommitted changes
tab.addString("Build Time", () -> BuildGitInfo.BUILD_DATE);
gitInfoNeedsInit = false;
}
}
}
71 changes: 0 additions & 71 deletions src/main/java/frc/robot/util/VersionFile.java

This file was deleted.

0 comments on commit a19e80d

Please sign in to comment.