generated from DeepBlueRobotics/EmptyProject2024
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes build date, current branch, and most recent commit hash.
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,3 +224,4 @@ simgui-ds.json | |
simgui.json | ||
*.wpilog | ||
*.dat | ||
src/main/deploy/BuildInfo.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |