Skip to content

Commit 03c70fa

Browse files
committed
start switching to proper gradle way
- grgit not fully used see ajoberstar/grgit#398 - doesn't fully use tasks properly I don't think - file needs to be properly set to output dir
1 parent e6fa247 commit 03c70fa

File tree

3 files changed

+90
-111
lines changed

3 files changed

+90
-111
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ dependencies {
2121

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

24+
implementation 'org.ajoberstar.grgit:grgit-core:5.0.0'
25+
26+
2427
testImplementation('org.spockframework:spock-core:2.0-M4-groovy-3.0') {
2528
exclude group: 'org.codehaus.groovy'
2629
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package edu.wpi.first.gradlerio.deploy;
2+
3+
import com.google.gson.GsonBuilder;
4+
import com.google.gson.Gson;
5+
6+
import org.ajoberstar.grgit.Grgit;
7+
8+
import org.gradle.api.DefaultTask;
9+
import org.gradle.api.GradleException;
10+
import org.gradle.api.Project;
11+
import org.gradle.api.tasks.TaskAction;
12+
13+
import java.util.HashMap;
14+
import java.io.IOException;
15+
import java.io.File;
16+
import java.lang.Runtime;
17+
import java.time.LocalDateTime;
18+
19+
public class CreateLogFileTask extends DefaultTask {
20+
public static final String[] DEPLOY_ITEMS = {
21+
"deployHost",
22+
"deployUser",
23+
"deployDate",
24+
"codePath",
25+
"gitHash",
26+
"gitBranch",
27+
"gitDesc",
28+
};
29+
30+
Gson builder = new GsonBuilder().create();
31+
HashMap<String, String> data = new HashMap<String, String>();
32+
33+
@TaskAction
34+
public void execute(Project project) {
35+
Grgit grgit = Grgit.open();
36+
boolean inGitRepo = false;
37+
String[] command = { "git", "rev-parse", "--is-inside-work-tree" };
38+
39+
try {
40+
// TODO! use grgit for this
41+
Runtime.getRuntime().exec(command);
42+
} catch (IOException e) {}
43+
44+
try {
45+
data.put(DEPLOY_ITEMS[0], Runtime.getRuntime().exec("hostname").getOutputStream().toString().strip());
46+
} catch (IOException e) {
47+
throw new GradleException("Couldn't get hostname", e);
48+
}
49+
50+
data.put(DEPLOY_ITEMS[1], System.getProperty("user.name"));
51+
data.put(DEPLOY_ITEMS[2], LocalDateTime.now().toString());
52+
data.put(DEPLOY_ITEMS[3], System.getProperty("user.dir"));
53+
54+
if (inGitRepo) {
55+
String[] command2 = { "git", "rev-parse", "HEAD" };
56+
try {
57+
// TODO! use grgit for this
58+
data.put(DEPLOY_ITEMS[4], Runtime.getRuntime().exec(command2).getOutputStream().toString().strip());
59+
} catch (IOException e) {
60+
throw new GradleException("Couldn't get git hash", e);
61+
}
62+
63+
String[] command3 = { "git", "rev-parse", "--abbrev-ref", "HEAD" };
64+
try {
65+
data.put(DEPLOY_ITEMS[5], Runtime.getRuntime().exec(command3).getOutputStream().toString().strip());
66+
} catch (IOException e) {
67+
throw new GradleException("Couldn't get git branch", e);
68+
}
69+
70+
try {
71+
HashMap<String, Object> args = new HashMap<String, Object>();
72+
args.put("dirty", "-dirty");
73+
args.put("always", true);
74+
75+
data.put(DEPLOY_ITEMS[6], grgit.describe(args));
76+
} catch (Exception e) {
77+
throw new GradleException("Couldn't get git description", e);
78+
}
79+
}
80+
81+
try {
82+
new File("build/deploy.json", builder.toJson(data)).createNewFile();
83+
} catch (IOException e) {
84+
throw new GradleException("Couldn't write deploy log file", e);
85+
}
86+
}
87+
}

src/main/java/edu/wpi/first/gradlerio/deploy/DeployData.java

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)