Closed lindsayvallen closed 4 years ago
@jwalthour suggested that we have something that gets 'git status', saves it to a file, and pushes that to the robot during the code build process.
First find, haven't dug into it yet, but looks like it will facilitate what we want: Git-Version Gradle Plugin https://github.com/palantir/gradle-git-version
Challenge I was trying to figure out is how to get a git version, which is a hash of the source code, into the source code, since altering the code after calculating the hash invalidates the hash. The only thought I came up with on my own was to get Gradle to create a small java file that would be gitignore'd, yet compiled in, which had the sole purpose of providing a public final static string of the version to the project.
However, there appears to be cleaner method. Java can reference attributes stored in the manifest portion of the Jar file, and Gradle can add attributes to the manifest.
A stack overflow answer lays it out nicely: Gradle: Make build version available to Java
So we would add the Git-Version Gradle Plugin, then use it to define the Implementation-Version attribute to contain the git version information. That attribute can be read within java code with
BuildVersion.class.getPackage().getImplementationVersion();
Full git status info is an interesting idea I'll have to keep digging for.
riffing on the Manifest attribute theme, I searched for
How do I add a Build-Host attribute to a JAR manifest using Gradle?
to find that Gradle doesn't have a public API for the build host, but that it's available in Java using
InetAddress.localHost.hostName
Haven't figured out if Gradle can call java, or if gradlew.bat could be used to pass in the output of the hostname command.
Branch origin/feature/add-git-info-to-JarManifest now has an updated build.gradle which pulis in the plug-in:
id "com.palantir.git-version" version "0.12.2" // used to add git commit ID and branch name to Jar Manifest - zaaj
(com.palantir.git-version now at version 0.13.0 as of 19Jan2022)
Then grabs the git commit details with:
def gitDetails = versionDetails()
for use in the jar manifest. The manifest was originally created with:
manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS)
which produces:
Main-Class: frc.robot.Main
and has been changed to:
manifest {
attributes(
'Main-Class': ROBOT_MAIN_CLASS,
'Implementation-Title': InetAddress.getLocalHost().getHostName() + ":" + gitDetails.branchName,
'Implementation-Version': gitVersion() + " built on " + new java.text.SimpleDateFormat("yyyy-MM-dd' at 'HH:mm:ss").format(new Date())
)
}
which produces attributes like:
Main-Class: frc.robot.Main
Implementation-Title: DEVHOSTNAME:feature/add-git-info-to-JarManifest
Implementation-Version: 7135a59.dirty built on 2020-01-25 at 13:43:12
Added the two attributes from the Jar Manifest to the SmartDashboard with the following two lines in robotInit() in Robot.java:
SmartDashboard.putString("BuildHost-BranchName", Robot.class.getPackage().getImplementationTitle()); SmartDashboard.putString("GitCommitID-BuildTimestamp", Robot.class.getPackage().getImplementationVersion());
(you can even see which commit ID it was done in ;)
When we run the robot, it would be great to have the Git info (e.g. Git status) printed to the driver station or Smart Dashboard so we know what's on the robot when we run it, not just by re-pushing code to it.