Inclure les données Git dans Deploy

This article will explain how to include metadata from Git in robot code using the gversion Gradle plugin. This generates a file which can be used for accessing Git metadata in robot code. This can be used to track what revision of code is on the robot, such as by printing or logging it.

Note

For Python teams, Git metadata is always copied to your robot during the deploy process. You can use wpilib.deployinfo.getDeployData() to retrieve the stored information.

Installing gversion

To install gversion add the following line to the plugins block of build.gradle. This tells Gradle to use gversion in the project.

plugins {
    // ...
    id "com.peterabeles.gversion" version "1.10"
}

In order to generate the file when building the project, add the following block to the bottom of build.gradle.

project.compileJava.dependsOn(createVersionFile)
gversion {
  srcDir       = "src/main/java/"
  classPackage = "frc.robot"
  className    = "BuildConstants"
  dateFormat   = "yyyy-MM-dd HH:mm:ss z"
  timeZone     = "America/New_York" // Use preferred time zone
  indent       = "  "
}

The srcDir, classPackage, and className parameters tell the plugin where to put the manifest file, and what to name it. The timeZone field can be set to your team’s time zone based on this list of timezone IDs. The dateFormat parameter is based on this Java class.

To test this, run a build of your project through the WPILib menu in the top right. Once the code has finished building, there should be a file called BuildConstants.java in your src/main/java folder. The following is an example of what this file should look like:

package frc.robot;

/**
* Automatically generated file containing build version information.
*/
public final class BuildConstants {
  public static final String MAVEN_GROUP = "";
  public static final String MAVEN_NAME = "GitVersionTest";
  public static final String VERSION = "unspecified";
  public static final int GIT_REVISION = 1;
  public static final String GIT_SHA = "fad108a4b1c1dcdfc8859c6295ea64e06d43f557";
  public static final String GIT_DATE = "2023-10-26 17:38:59 EDT";
  public static final String GIT_BRANCH = "main";
  public static final String BUILD_DATE = "2023-10-27 12:29:57 EDT";
  public static final long BUILD_UNIX_TIME = 1698424197122L;
  public static final int DIRTY = 0;

  private BuildConstants(){}
}

DIRTY indicates whether there are uncommitted changes in the project. A value of 0 indicates a clean repository, a value of 1 indicates that there are uncommitted changes, and a value -1 indicates an error.

Ignorer les fichiers générés avec Git

These files are regenerated every time code is built or deployed, so it isn’t necessary to track them with Git. The aptly named .gitignore file tells Git not to track any listed files and should exist by default in any project generated by the WPILib VS Code extension. Below is the line you should add to .gitignore to ignore the generated file:

src/main/java/frc/robot/BuildConstants.java