Skip to content

Commit 0d7a6ea

Browse files
authored
Merge pull request #60 from DeepBlueRobotics/add-build-info
Add build info to SmartDashboard.
2 parents 2b7f1fa + 3bbe964 commit 0d7a6ea

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,4 @@ simgui-ds.json
224224
simgui.json
225225
*.wpilog
226226
*.dat
227+
src/main/deploy/BuildInfo.properties

build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id "java"
33
id "edu.wpi.first.GradleRIO" version "2024.3.2"
4+
id "com.peterabeles.gversion" version "1.10"
45
}
56

67
java {
@@ -110,3 +111,13 @@ wpi.java.configureTestTasks(test)
110111
tasks.withType(JavaCompile) {
111112
options.compilerArgs.add '-XDstringConcat=inline'
112113
}
114+
115+
project.compileJava.dependsOn(createVersionFile)
116+
gversion {
117+
language = "Properties"
118+
srcDir = "src/main/deploy/"
119+
className = "BuildInfo.properties"
120+
dateFormat = "yyyy-MM-dd HH:mm:ss z"
121+
timeZone = "America/Los_Angeles"
122+
indent = " "
123+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.carlmontrobotics;
2+
3+
import java.util.Properties;
4+
import java.io.File;
5+
import java.io.InputStream;
6+
import java.nio.file.Path;
7+
import java.nio.file.Files;
8+
9+
import edu.wpi.first.util.sendable.Sendable;
10+
import edu.wpi.first.util.sendable.SendableBuilder;
11+
import edu.wpi.first.wpilibj.Filesystem;
12+
13+
public class BuildInfo implements Sendable {
14+
private Properties props = new Properties();
15+
16+
static private BuildInfo instance = null;
17+
18+
public static BuildInfo getInstance() {
19+
if (instance == null) {
20+
instance = new BuildInfo();
21+
}
22+
return instance;
23+
}
24+
25+
private BuildInfo() {
26+
Path path = Path
27+
.of(Filesystem.getDeployDirectory().getAbsolutePath() + File.separator + "BuildInfo.properties");
28+
try (InputStream is = Files.newInputStream(path)) {
29+
props.load(is);
30+
} catch (Exception ex) {
31+
System.err.println("Error reading build properties from %s".formatted(path));
32+
}
33+
}
34+
35+
@Override
36+
public void initSendable(SendableBuilder builder) {
37+
props.stringPropertyNames().forEach(name -> {
38+
var value = props.getProperty(name);
39+
// Workaround bug (https://github.com/lessthanoptimal/gversion-plugin/pull/14)
40+
// where the gversion plugin surrounds values with quotes.
41+
value = value.replaceAll("\"", "");
42+
builder.publishConstString(name, value);
43+
});
44+
}
45+
}

src/main/java/org/carlmontrobotics/RobotContainer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public RobotContainer() {
123123
SmartDashboard.putData("CONFIG overrides", Config.CONFIG);
124124
System.out.println(Config.CONFIG);
125125

126+
SmartDashboard.putData("BuildConstants", BuildInfo.getInstance());
127+
126128
SmartDashboard.setDefaultBoolean("babymode", babyMode);
127129
SmartDashboard.setPersistent("babymode");
128130
//safe auto setup... stuff in setupAutos() is not safe to run here - will break robot
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.carlmontrobotics;
2+
3+
import java.util.HashMap;
4+
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
import edu.wpi.first.util.sendable.SendableBuilder;
10+
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilderImpl;
11+
12+
public class BuildInfoTest {
13+
@Test
14+
void testGetInstance() {
15+
assertNotNull(BuildInfo.getInstance());
16+
}
17+
18+
@Test
19+
void testInitSendable() throws Exception {
20+
var publishedStrings = new HashMap<String, String>();
21+
try (SendableBuilder testBuilder = new SendableBuilderImpl() {
22+
@Override
23+
public void publishConstString(String key, String value) {
24+
publishedStrings.put(key, value);
25+
}
26+
}) {
27+
BuildInfo.getInstance().initSendable(testBuilder);
28+
String actualBuildDate = publishedStrings.get("build_date");
29+
assertNotNull(actualBuildDate);
30+
assertTrue(actualBuildDate.matches("[^\"']+"),
31+
"build_date (%s) must not contain quotes".formatted(actualBuildDate));
32+
String actualSha = publishedStrings.get("sha");
33+
assertNotNull(actualSha);
34+
assertTrue(actualSha.matches("(UNKNOWN|[a-f0-9]+)"), "sha (%s) is not valid".formatted(actualSha));
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)