Skip to content

Commit

Permalink
Add build info to SmartDashboard.
Browse files Browse the repository at this point in the history
Includes build date, current branch, and most recent commit hash.
  • Loading branch information
brettle committed Apr 26, 2024
1 parent 2b7f1fa commit 3bbe964
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,4 @@ simgui-ds.json
simgui.json
*.wpilog
*.dat
src/main/deploy/BuildInfo.properties
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id "java"
id "edu.wpi.first.GradleRIO" version "2024.3.2"
id "com.peterabeles.gversion" version "1.10"
}

java {
Expand Down Expand Up @@ -110,3 +111,13 @@ wpi.java.configureTestTasks(test)
tasks.withType(JavaCompile) {
options.compilerArgs.add '-XDstringConcat=inline'
}

project.compileJava.dependsOn(createVersionFile)
gversion {
language = "Properties"
srcDir = "src/main/deploy/"
className = "BuildInfo.properties"
dateFormat = "yyyy-MM-dd HH:mm:ss z"
timeZone = "America/Los_Angeles"
indent = " "
}
45 changes: 45 additions & 0 deletions src/main/java/org/carlmontrobotics/BuildInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.carlmontrobotics;

import java.util.Properties;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Files;

import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.wpilibj.Filesystem;

public class BuildInfo implements Sendable {
private Properties props = new Properties();

static private BuildInfo instance = null;

public static BuildInfo getInstance() {
if (instance == null) {
instance = new BuildInfo();
}
return instance;
}

private BuildInfo() {
Path path = Path
.of(Filesystem.getDeployDirectory().getAbsolutePath() + File.separator + "BuildInfo.properties");
try (InputStream is = Files.newInputStream(path)) {
props.load(is);
} catch (Exception ex) {
System.err.println("Error reading build properties from %s".formatted(path));
}
}

@Override
public void initSendable(SendableBuilder builder) {
props.stringPropertyNames().forEach(name -> {
var value = props.getProperty(name);
// Workaround bug (https://github.com/lessthanoptimal/gversion-plugin/pull/14)
// where the gversion plugin surrounds values with quotes.
value = value.replaceAll("\"", "");
builder.publishConstString(name, value);
});
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/carlmontrobotics/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public RobotContainer() {
SmartDashboard.putData("CONFIG overrides", Config.CONFIG);
System.out.println(Config.CONFIG);

SmartDashboard.putData("BuildConstants", BuildInfo.getInstance());

SmartDashboard.setDefaultBoolean("babymode", babyMode);
SmartDashboard.setPersistent("babymode");
//safe auto setup... stuff in setupAutos() is not safe to run here - will break robot
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/carlmontrobotics/BuildInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.carlmontrobotics;

import java.util.HashMap;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilderImpl;

public class BuildInfoTest {
@Test
void testGetInstance() {
assertNotNull(BuildInfo.getInstance());
}

@Test
void testInitSendable() throws Exception {
var publishedStrings = new HashMap<String, String>();
try (SendableBuilder testBuilder = new SendableBuilderImpl() {
@Override
public void publishConstString(String key, String value) {
publishedStrings.put(key, value);
}
}) {
BuildInfo.getInstance().initSendable(testBuilder);
String actualBuildDate = publishedStrings.get("build_date");
assertNotNull(actualBuildDate);
assertTrue(actualBuildDate.matches("[^\"']+"),
"build_date (%s) must not contain quotes".formatted(actualBuildDate));
String actualSha = publishedStrings.get("sha");
assertNotNull(actualSha);
assertTrue(actualSha.matches("(UNKNOWN|[a-f0-9]+)"), "sha (%s) is not valid".formatted(actualSha));
}
}
}

0 comments on commit 3bbe964

Please sign in to comment.