Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[robotpy parity] Write a deploy.json log file when deploying code #690

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ dependencies {

api 'edu.wpi.first:native-utils:2025.6.0'

api 'org.eclipse.jgit:org.eclipse.jgit:6.10.+'


api 'de.undercouch:gradle-download-task:4.1.2'

testImplementation('org.spockframework:spock-core:2.0-M4-groovy-3.0') {
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/edu/wpi/first/gradlerio/deploy/CreateLogFileTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.wpi.first.gradlerio.deploy;

import com.google.gson.GsonBuilder;
import com.google.gson.Gson;

import javax.inject.Inject;

import org.codehaus.groovy.runtime.ResourceGroovyMethods;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.OutputFile;

import java.util.HashMap;
import java.io.IOException;
import java.io.File;
import java.time.LocalDateTime;

public class CreateLogFileTask extends DefaultTask {
public static final String[] DEPLOY_ITEMS = {
"deployHost",
"deployUser",
"deployDate",
"codePath",
"gitHash",
"gitBranch",
"gitDesc",
};
private File deployFile;
private String json;
private String gitDirectory;

@Inject
public CreateLogFileTask() {
HashMap<String, String> data = new HashMap<String, String>();
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson jsongen = builder.create();

try {
Repository repository = new FileRepositoryBuilder().setGitDir(new File(gitDirectory))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();

try {
data.put(DEPLOY_ITEMS[4], repository.resolve("HEAD").toString());
} catch (Exception e) {
throw new GradleException("Couldn't get git hash", e);
}

try {
data.put(DEPLOY_ITEMS[5], repository.getBranch());
} catch (Exception e) {
throw new GradleException("Couldn't get git branch", e);
}

try {
data.put(DEPLOY_ITEMS[6], repository.getGitwebDescription());
} catch (Exception e) {
throw new GradleException("Couldn't get git description", e);
}
} catch (Exception e) {
}

try {
var pb = new ProcessBuilder("hostname");
var process = pb.start();
data.put(DEPLOY_ITEMS[0], process.getOutputStream().toString());
} catch (IOException e) {
throw new GradleException("Couldn't get hostname", e);
}

data.put(DEPLOY_ITEMS[1], System.getProperty("user.name"));
data.put(DEPLOY_ITEMS[2], LocalDateTime.now().toString());
data.put(DEPLOY_ITEMS[3], System.getProperty("user.dir"));

json = jsongen.toJson(data);
}

@TaskAction
public void execute() throws IOException {
deployFile.getParentFile().mkdirs();
ResourceGroovyMethods.setText(deployFile, json);
}

public void setDeployFile(String path) {
deployFile = new File(path);
}

public void setGitDirectory(String dir) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a string property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find a StringProperty class in the Gradle API?

gitDirectory = dir;
}

@OutputFile
public File getDeployFile() {
return deployFile;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ public FRCExtension(Project project, DeployExtension deployExtension) {
t.getDebugFile().set(project.getLayout().getBuildDirectory().file("debug/debug_info.json"));
});

deployLogFile = project.getTasks().register("writeDeployFile", CreateLogFileTask.class, t -> {
t.setGitDirectory(project.getRootDir().toString());
t.setDeployFile(project.getLayout().getBuildDirectory().toString() + "debug/debug_info.json");
});

deployExtension.getDeployTask().configure(t -> {
t.dependsOn(debugFileTask);
t.dependsOn(deployLogFile);
});
}

private final TaskProvider<DebugFileTask> debugFileTask;
private final TaskProvider<CreateLogFileTask> deployLogFile;

public TaskProvider<DebugFileTask> getDebugFileTask() {
return debugFileTask;
Expand Down