Skip to content

Commit

Permalink
Print git info (#87)
Browse files Browse the repository at this point in the history
Adds a gradle task which writes information about the build environment (device hostname, project path, and if available information about the commit and changed files) to `git-info.txt` in the deploy directory and adds code to print the file contents.
  • Loading branch information
KangarooKoala authored Oct 23, 2023
1 parent 6ad2340 commit f251917
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,6 @@ local.properties

# Don't know what's creating this
.project

# Generated git info file
src/main/deploy/git-info.txt
72 changes: 72 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.net.InetAddress;

plugins {
id "java"
id "com.diffplug.spotless" version "6.15.0"
Expand Down Expand Up @@ -134,6 +136,76 @@ spotless {
trimTrailingWhitespace();
}
}

task writeGitInfo {
def hasGit = { ->
try {
exec {
executable 'git'
delegate.args '--version'
standardOutput = OutputStream.nullOutputStream()
}
} catch (Exception e) {
return false
}
return true
}

def addGitOutput = { PrintStream output, String name, String... args ->
output.println("### $name (git ${String.join(' ', args)})")
// Temporary stream so that we don't close output
def execOutput = new ByteArrayOutputStream()
def ex = exec {
workingDir projectDir
executable 'git'
delegate.args args
standardOutput = execOutput
}
output.print(execOutput)
}

def convertPath = { String path ->
String username = System.getenv('USERNAME')
if (username != null) {
return path.replace(username, "\$USERNAME")
}
username = System.getProperty('user.name')
if (username != null) {
return path.replace(username, '<sys prop user.name>')
}
return path
}

doLast {
try (PrintStream output = new PrintStream('src/main/deploy/git-info.txt')) {
output.println("### Host name (InetAddress.localHost.hostName)")
output.println(InetAddress.localHost.hostName)
output.println("### Project path (convertPath(projectDir.path))")
output.println(convertPath(projectDir.path))
if (!hasGit()) {
// ANSI escape codes
def WARNINGFMT = "\033[43;30m"
def CLEAR = "\033[m"
println("${WARNINGFMT}WARNING: Could not find git! Git info will not be sent to the robot.${CLEAR}")
output.println("### <could not run git>")
} else {
addGitOutput(output, 'Commit hash', 'log', '-1', '--format=%H')
addGitOutput(output, 'Refs to latest commit', 'log', '-1', '--format=%D')
addGitOutput(output, 'Commit date', 'log', '-1', '--format=%ad')
addGitOutput(output, 'Commit message', 'log', '-1', '--format=%B')
addGitOutput(output, 'Remotes', 'remote', '-v')
addGitOutput(output, 'Changed files', 'diff-index', '--name-only', 'HEAD', '--')
addGitOutput(output, 'Untracked files', 'ls-files', '--exclude-standard', '--others')
}
output.println('### (END)')
}
}
}
if (getProjectBooleanProperty('jarWriteGitInfo', true)) {
jar.dependsOn(writeGitInfo)
}

// Set up spotless
tasks.named("build") { finalizedBy("spotlessApply") }
tasks.named("deploy") { finalizedBy("spotlessApply") }
spotlessCheck.mustRunAfter(compileJava);
14 changes: 14 additions & 0 deletions src/main/java/frc/team2412/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import frc.team2412.robot.sim.PhysicsSim;
import frc.team2412.robot.util.MACAddress;
import frc.team2412.robot.util.auto.AutonomousChooser;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Optional;

Expand Down Expand Up @@ -103,6 +106,17 @@ public void robotInit() {
DriverStation.silenceJoystickConnectionWarning(true);

PathPlannerServer.startServer(5811);

logRobotInfo();
}

private void logRobotInfo() {
try {
File gitInfoFile = new File(Filesystem.getDeployDirectory(), "git-info.txt");
System.out.println("Git info:\n" + Files.readString(gitInfoFile.toPath()));
} catch (IOException e) {
DriverStation.reportWarning("Could not open git info file", true);
}
}

public SwerveAutoBuilder getAutoBuilder(HashMap<String, Command> eventMap) {
Expand Down

0 comments on commit f251917

Please sign in to comment.