From 6ccb032de62f76fb04936d6e64156311d6d40a9c Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Fri, 25 Feb 2022 21:43:56 -0600 Subject: [PATCH 1/6] Update repotool to work with subprojects --- .../javacg/stat/support/RepoTool.java | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index 9508d4ab..5a7b6497 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -20,26 +20,38 @@ public class RepoTool { private static final Logger LOGGER = LoggerFactory.getLogger(RepoTool.class); - private String name; - private String URL; - private String checkoutID; - private String patchName; + final private String name; + final private String URL; + final private String checkoutID; + final private String patchName; + final private String subProject; + final private String mvnOptions; private List> properties; private Git git; - private RepoTool(String name, String URL, String checkoutID, String patchName){ + private RepoTool(String name, String URL, String checkoutID, String patchName, String subProject, String mvnOptions){ this.name = name; this.URL = URL; this.checkoutID = checkoutID; this.patchName = patchName; + this.subProject = subProject; + this.mvnOptions = mvnOptions; } public RepoTool(String name) throws FileNotFoundException { - this.name = name; + // @todo Perhaps using objects to store configuration data so we don't have to have unchecked casts e.g. https://www.baeldung.com/java-snake-yaml + Yaml yaml = new Yaml(); - InputStream inputStream = new FileInputStream(new File("artifacts/configs/" + this.name + "/" + this.name + ".yaml")); - Map>> data = yaml.load(inputStream); - this.properties = data.get("properties"); + InputStream inputStream = new FileInputStream("artifacts/configs/" + name + "/" + name + ".yaml"); + Map data = yaml.load(inputStream); + + this.name = name; + URL = (String) data.get("URL"); + checkoutID = (String) data.get("checkoutID"); + patchName = (String) data.get("patchName"); + subProject = (String) data.getOrDefault("subProject", ""); + mvnOptions = (String) data.getOrDefault("mvnOptions", ""); + properties = (List>) data.get("properties"); } public void cloneRepo() throws GitAPIException, JGitInternalException { @@ -81,9 +93,9 @@ public void buildJars() throws IOException, InterruptedException { public void testProperty(String property) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(); if(isWindows()) - pb.command("cmd.exe", "/c", "mvn", "test", "-Dtest=" + property); + pb.command("cmd.exe", "/c", "mvn", "test", mvnOptions, "-Dtest=" + property); else - pb.command("bash", "-c", "mvn test -Dtest=" + property); + pb.command("bash", "-c", "mvn test " + mvnOptions + " -Dtest=" + property); pb.directory(new File(this.name)); long start = System.nanoTime(); Process process = pb.start(); @@ -115,7 +127,7 @@ public void cleanTarget() throws IOException, InterruptedException { public List> obtainCoverageFilesAndEntryPoints(){ List> coverageFiles = new LinkedList<>(); for(Map m : properties) - coverageFiles.add(new Pair<>("artifacts/results/" + this.name + "/" + m.get("name") + ".xml", m.get("entryPoint"))); + coverageFiles.add(new Pair<>("artifacts/results/" + getProjectDir() + "/" + m.get("name") + ".xml", m.get("entryPoint"))); return coverageFiles; } @@ -124,7 +136,7 @@ public static Optional obtainTool(String folderName){ Yaml yaml = new Yaml(); InputStream inputStream = new FileInputStream(new File("artifacts/configs/" + folderName + "/" + folderName + ".yaml")); Map data = yaml.load(inputStream); - return Optional.of(new RepoTool(data.get("name"), data.get("URL"), data.get("checkoutID"), data.get("patchName"))); + return Optional.of(new RepoTool(data.get("name"), data.get("URL"), data.get("checkoutID"), data.get("patchName"), data.getOrDefault("subProject", ""), data.getOrDefault("mvnOptions", ""))); } catch(IOException e){ LOGGER.error("IOException: " + e.getMessage()); @@ -145,10 +157,12 @@ private void moveJars() throws IOException{ } private void moveJacoco(String property, long timeElapsed) throws IOException{ - String jacocoPath = System.getProperty("user.dir") + "/" + this.name + "/target/site/jacoco/jacoco.xml"; - String jacocoTargetPath = System.getProperty("user.dir") + "/artifacts/results/"+ this.name + "/" + property + ".xml"; - String statisticsPath = System.getProperty("user.dir") + "/" + this.name + "/target/site/jacoco/index.html"; - String statisticsTargetPath = System.getProperty("user.dir") + "/artifacts/results/"+ this.name + "/" + property + ".html"; + String projectName = getProjectDir(); + + String jacocoPath = System.getProperty("user.dir") + "/" + projectName + "/target/site/jacoco/jacoco.xml"; + String jacocoTargetPath = System.getProperty("user.dir") + "/artifacts/results/"+ projectName + "/" + property + ".xml"; + String statisticsPath = System.getProperty("user.dir") + "/" + projectName + "/target/site/jacoco/index.html"; + String statisticsTargetPath = System.getProperty("user.dir") + "/artifacts/results/"+ projectName + "/" + property + ".html"; Path jacoco = Files.move( Paths.get(jacocoPath), Paths.get(jacocoTargetPath), @@ -175,4 +189,11 @@ private boolean isWindows() { return System.getProperty("os.name") .toLowerCase().startsWith("windows"); } + + private String getProjectDir() { + if (subProject.equals("")) { + return name; + } + return this.name + "/" + this.subProject; + } } From ac7c9ffbc7cd489e9c16c769ee8faee31bc2f0c6 Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Fri, 25 Feb 2022 21:43:56 -0600 Subject: [PATCH 2/6] Update repotool to work with subprojects --- .../javacg/stat/support/RepoTool.java | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index 714c27b6..83717589 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -22,26 +22,38 @@ public class RepoTool { private static final Logger LOGGER = LoggerFactory.getLogger(RepoTool.class); - private String name; - private String URL; - private String checkoutID; - private String patchName; + final private String name; + final private String URL; + final private String checkoutID; + final private String patchName; + final private String subProject; + final private String mvnOptions; private List> properties; private Git git; - private RepoTool(String name, String URL, String checkoutID, String patchName){ + private RepoTool(String name, String URL, String checkoutID, String patchName, String subProject, String mvnOptions){ this.name = name; this.URL = URL; this.checkoutID = checkoutID; this.patchName = patchName; + this.subProject = subProject; + this.mvnOptions = mvnOptions; } public RepoTool(String name) throws FileNotFoundException { - this.name = name; + // @todo Perhaps using objects to store configuration data so we don't have to have unchecked casts e.g. https://www.baeldung.com/java-snake-yaml + Yaml yaml = new Yaml(); - InputStream inputStream = new FileInputStream(new File("artifacts/configs/" + this.name + "/" + this.name + ".yaml")); - Map>> data = yaml.load(inputStream); - this.properties = data.get("properties"); + InputStream inputStream = new FileInputStream("artifacts/configs/" + name + "/" + name + ".yaml"); + Map data = yaml.load(inputStream); + + this.name = name; + URL = (String) data.get("URL"); + checkoutID = (String) data.get("checkoutID"); + patchName = (String) data.get("patchName"); + subProject = (String) data.getOrDefault("subProject", ""); + mvnOptions = (String) data.getOrDefault("mvnOptions", ""); + properties = (List>) data.get("properties"); } public void cloneRepo() throws GitAPIException, JGitInternalException { @@ -83,9 +95,9 @@ public void buildJars() throws IOException, InterruptedException { public void testProperty(String property) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(); if(isWindows()) - pb.command("cmd.exe", "/c", "mvn", "test", "-Dtest=" + property); + pb.command("cmd.exe", "/c", "mvn", "test", mvnOptions, "-Dtest=" + property); else - pb.command("bash", "-c", "mvn test -Dtest=" + property); + pb.command("bash", "-c", "mvn test " + mvnOptions + " -Dtest=" + property); pb.directory(new File(this.name)); long start = System.nanoTime(); Process process = pb.start(); @@ -117,7 +129,7 @@ public void cleanTarget() throws IOException, InterruptedException { public List> obtainCoverageFilesAndEntryPoints(){ List> coverageFiles = new LinkedList<>(); for(Map m : properties) - coverageFiles.add(new Pair<>("artifacts/results/" + this.name + "/" + m.get("name") + ".xml", m.get("entryPoint"))); + coverageFiles.add(new Pair<>("artifacts/results/" + getProjectDir() + "/" + m.get("name") + ".xml", m.get("entryPoint"))); return coverageFiles; } @@ -126,7 +138,7 @@ public static Optional obtainTool(String folderName){ Yaml yaml = new Yaml(); InputStream inputStream = new FileInputStream(new File("artifacts/configs/" + folderName + "/" + folderName + ".yaml")); Map data = yaml.load(inputStream); - return Optional.of(new RepoTool(data.get("name"), data.get("URL"), data.get("checkoutID"), data.get("patchName"))); + return Optional.of(new RepoTool(data.get("name"), data.get("URL"), data.get("checkoutID"), data.get("patchName"), data.getOrDefault("subProject", ""), data.getOrDefault("mvnOptions", ""))); } catch(IOException e){ LOGGER.error("IOException: " + e.getMessage()); @@ -148,10 +160,11 @@ private void moveJars() throws IOException{ private void moveJacoco(String property, long timeElapsed) throws IOException{ String timeStamp = String.valueOf(java.time.LocalDateTime.now()); - String directoryPath = System.getProperty("user.dir") + "/artifacts/results/"+ this.name + timeStamp; - String jacocoPath = System.getProperty("user.dir") + "/" + this.name + "/target/site/jacoco/jacoco.xml"; + String projectDir = (subProject.equals("")) ? name : (name + "/" + subProject); + String directoryPath = System.getProperty("user.dir") + "/artifacts/results/" + projectDir + timeStamp; + String jacocoPath = System.getProperty("user.dir") + "/" + projectDir + "/target/site/jacoco/jacoco.xml"; String jacocoTargetPath = directoryPath + "/" + property + ".xml"; - String statisticsPath = System.getProperty("user.dir") + "/" + this.name + "/target/site/jacoco/index.html"; + String statisticsPath = System.getProperty("user.dir") + "/" + projectDir + "/target/site/jacoco/index.html"; String statisticsTargetPath = directoryPath + "/" + property + ".html"; File directory = new File(directoryPath); directory.mkdir(); From 4557dbf7180da4e642de90dd732bfa93573adce3 Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Fri, 25 Feb 2022 22:22:14 -0600 Subject: [PATCH 3/6] Fix code analysis flagged warnings --- .../gr/gousiosg/javacg/stat/support/RepoTool.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index 83717589..4de7d937 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -136,7 +136,7 @@ public List> obtainCoverageFilesAndEntryPoints(){ public static Optional obtainTool(String folderName){ try { Yaml yaml = new Yaml(); - InputStream inputStream = new FileInputStream(new File("artifacts/configs/" + folderName + "/" + folderName + ".yaml")); + InputStream inputStream = new FileInputStream("artifacts/configs/" + folderName + "/" + folderName + ".yaml"); Map data = yaml.load(inputStream); return Optional.of(new RepoTool(data.get("name"), data.get("URL"), data.get("checkoutID"), data.get("patchName"), data.getOrDefault("subProject", ""), data.getOrDefault("mvnOptions", ""))); } @@ -177,16 +177,9 @@ private void moveJacoco(String property, long timeElapsed) throws IOException{ Paths.get(statisticsTargetPath), StandardCopyOption.REPLACE_EXISTING); double timeElapsedInSeconds = (double) timeElapsed / 1_000_000_000; - FileWriter fileWriter = null; - BufferedWriter bufferedWriter = null; - try{ - fileWriter = new FileWriter(statisticsTargetPath, true); - bufferedWriter = new BufferedWriter(fileWriter); - bufferedWriter.append("

Total Time Elapsed: "+ timeElapsedInSeconds +" seconds

"); + try (FileWriter fileWriter = new FileWriter(statisticsTargetPath, true); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { + bufferedWriter.append("

Total Time Elapsed: ").append(String.valueOf(timeElapsedInSeconds)).append(" seconds

"); bufferedWriter.flush(); - } finally{ - fileWriter.close(); - bufferedWriter.close(); } } From 2837cd8aef2b9945ee7a36c83a949837c71b87b6 Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Fri, 25 Feb 2022 22:23:52 -0600 Subject: [PATCH 4/6] Move getProjectDir back to function (since used in one other place in the code) --- src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index 4de7d937..d23ed981 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -160,7 +160,7 @@ private void moveJars() throws IOException{ private void moveJacoco(String property, long timeElapsed) throws IOException{ String timeStamp = String.valueOf(java.time.LocalDateTime.now()); - String projectDir = (subProject.equals("")) ? name : (name + "/" + subProject); + String projectDir = getProjectDir(); String directoryPath = System.getProperty("user.dir") + "/artifacts/results/" + projectDir + timeStamp; String jacocoPath = System.getProperty("user.dir") + "/" + projectDir + "/target/site/jacoco/jacoco.xml"; String jacocoTargetPath = directoryPath + "/" + property + ".xml"; @@ -187,4 +187,8 @@ private boolean isWindows() { return System.getProperty("os.name") .toLowerCase().startsWith("windows"); } + + private String getProjectDir() { + return (subProject.equals("")) ? name : (name + "/" + subProject); + } } From b66d787b21ea3563b77d5c1c39aa779b8b3bae9e Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Mon, 14 Mar 2022 20:22:44 -0500 Subject: [PATCH 5/6] Move timeStamp to constructor as a class property --- .../gr/gousiosg/javacg/stat/support/RepoTool.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index d23ed981..6e29b614 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -8,10 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; +import java.nio.file.*; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -30,6 +27,7 @@ public class RepoTool { final private String mvnOptions; private List> properties; private Git git; + final private String timeStamp; private RepoTool(String name, String URL, String checkoutID, String patchName, String subProject, String mvnOptions){ this.name = name; @@ -38,6 +36,8 @@ private RepoTool(String name, String URL, String checkoutID, String patchName, S this.patchName = patchName; this.subProject = subProject; this.mvnOptions = mvnOptions; + + this.timeStamp = String.valueOf(java.time.LocalDateTime.now()); } public RepoTool(String name) throws FileNotFoundException { @@ -54,6 +54,8 @@ public RepoTool(String name) throws FileNotFoundException { subProject = (String) data.getOrDefault("subProject", ""); mvnOptions = (String) data.getOrDefault("mvnOptions", ""); properties = (List>) data.get("properties"); + + this.timeStamp = String.valueOf(java.time.LocalDateTime.now()); } public void cloneRepo() throws GitAPIException, JGitInternalException { @@ -129,7 +131,7 @@ public void cleanTarget() throws IOException, InterruptedException { public List> obtainCoverageFilesAndEntryPoints(){ List> coverageFiles = new LinkedList<>(); for(Map m : properties) - coverageFiles.add(new Pair<>("artifacts/results/" + getProjectDir() + "/" + m.get("name") + ".xml", m.get("entryPoint"))); + coverageFiles.add(new Pair<>("artifacts/results/" + getProjectDir() + timeStamp + "/" + m.get("name") + ".xml", m.get("entryPoint"))); return coverageFiles; } @@ -159,7 +161,6 @@ private void moveJars() throws IOException{ } private void moveJacoco(String property, long timeElapsed) throws IOException{ - String timeStamp = String.valueOf(java.time.LocalDateTime.now()); String projectDir = getProjectDir(); String directoryPath = System.getProperty("user.dir") + "/artifacts/results/" + projectDir + timeStamp; String jacocoPath = System.getProperty("user.dir") + "/" + projectDir + "/target/site/jacoco/jacoco.xml"; From cc39689b660e22ea0da537d47e6612917df896dc Mon Sep 17 00:00:00 2001 From: Jesse Coultas Date: Mon, 14 Mar 2022 20:23:53 -0500 Subject: [PATCH 6/6] Add moveFiles that uses a source glob for figuring out which source files should be copied. --- .../javacg/stat/support/RepoTool.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java index 6e29b614..652e1785 100644 --- a/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java +++ b/src/main/java/gr/gousiosg/javacg/stat/support/RepoTool.java @@ -149,15 +149,27 @@ public static Optional obtainTool(String folderName){ return Optional.empty(); } - private void moveJars() throws IOException{ - Path jar = Files.move( - Paths.get(System.getProperty("user.dir") + "/" + this.name + "/target/" + this.name + "-1.0.6-SNAPSHOT.jar"), - Paths.get(System.getProperty("user.dir") + "/artifacts/output/" + this.name + "-1.0.6-SNAPSHOT.jar"), - StandardCopyOption.REPLACE_EXISTING); - Path testJar = Files.move( - Paths.get(System.getProperty("user.dir") + "/" + this.name + "/target/" + this.name + "-1.0.6-SNAPSHOT-tests.jar"), - Paths.get(System.getProperty("user.dir") + "/artifacts/output/" + this.name + "-1.0.6-SNAPSHOT-tests.jar"), - StandardCopyOption.REPLACE_EXISTING); + private void moveJars() throws IOException { + Path sourceDir = Paths.get(System.getProperty("user.dir"), getProjectDir(), "target"); + Path targetDir = Paths.get(System.getProperty("user.dir"), "artifacts", "output"); + + // @todo may want to be able to override this in the yaml file on a project basis... + String depGlob = this.name + "*-with-dependencies.jar"; + String testGlob = this.name + "*-tests.jar"; + + moveFiles(sourceDir, targetDir, depGlob); + moveFiles(sourceDir, targetDir, testGlob); + } + + private void moveFiles(Path sourceDir, Path targetDir, String glob) throws IOException { + try (DirectoryStream dirStream = Files.newDirectoryStream(sourceDir, glob)) { + for (Path source: dirStream) { + Files.move( + source, + targetDir.resolve(source.getFileName()), + StandardCopyOption.REPLACE_EXISTING); + } + } } private void moveJacoco(String property, long timeElapsed) throws IOException{