|
| 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 | +} |
0 commit comments