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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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 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.Project;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.OutputFile;

import java.util.HashMap;

import javax.inject.Inject;

import java.io.IOException;
import java.net.InetAddress;
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 RegularFileProperty deployFile;
private String json;
private String gitDirectory;

@Inject
public CreateLogFileTask(Project project) {
deployFile = project.getObjects().fileProperty();
}

@TaskAction
public void execute() throws IOException {
deployFile.getAsFile().get().getParentFile().mkdirs();
HashMap<String, String> data = new HashMap<String, String>();
spacey-sooty marked this conversation as resolved.
Show resolved Hide resolved
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) {
getLogger().log(LogLevel.WARN, "Couldn't get git hash");
}

try {
data.put(DEPLOY_ITEMS[5], repository.getBranch());
} catch (Exception e) {
getLogger().log(LogLevel.WARN, "Couldn't get git branch");
}
try {
data.put(DEPLOY_ITEMS[6], repository.getGitwebDescription());
} catch (Exception e) {
getLogger().log(LogLevel.WARN, "Couldn't get git description");
}
} catch (Exception e) {
getLogger().log(LogLevel.WARN, "Couldn't find git");
}

try {
data.put(DEPLOY_ITEMS[0], InetAddress.getLocalHost().getHostName());
} catch (IOException e) {
getLogger().log(LogLevel.WARN, "Couldn't get host name");
}

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);
ResourceGroovyMethods.setText(deployFile.getAsFile().get(), json);
}

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 RegularFileProperty 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.getDeployFile().set(project.getLayout().getBuildDirectory().file("debug/deploy_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