|
| 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 | +} |
0 commit comments