88import org .slf4j .Logger ;
99import org .slf4j .LoggerFactory ;
1010import java .io .*;
11- import java .nio .file .Files ;
12- import java .nio .file .Path ;
13- import java .nio .file .Paths ;
14- import java .nio .file .StandardCopyOption ;
11+ import java .nio .file .*;
1512import java .util .LinkedList ;
1613import java .util .List ;
1714import java .util .Map ;
2219public class RepoTool {
2320
2421 private static final Logger LOGGER = LoggerFactory .getLogger (RepoTool .class );
25- private String name ;
26- private String URL ;
27- private String checkoutID ;
28- private String patchName ;
22+ final private String name ;
23+ final private String URL ;
24+ final private String checkoutID ;
25+ final private String patchName ;
26+ final private String subProject ;
27+ final private String mvnOptions ;
2928 private List <Map <String , String >> properties ;
3029 private Git git ;
30+ final private String timeStamp ;
3131
32- private RepoTool (String name , String URL , String checkoutID , String patchName ){
32+ private RepoTool (String name , String URL , String checkoutID , String patchName , String subProject , String mvnOptions ){
3333 this .name = name ;
3434 this .URL = URL ;
3535 this .checkoutID = checkoutID ;
3636 this .patchName = patchName ;
37+ this .subProject = subProject ;
38+ this .mvnOptions = mvnOptions ;
39+
40+ this .timeStamp = String .valueOf (java .time .LocalDateTime .now ());
3741 }
3842
3943 public RepoTool (String name ) throws FileNotFoundException {
40- this .name = name ;
44+ // @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
45+
4146 Yaml yaml = new Yaml ();
42- InputStream inputStream = new FileInputStream (new File ("artifacts/configs/" + this .name + "/" + this .name + ".yaml" ));
43- Map <String , List <Map <String ,String >>> data = yaml .load (inputStream );
44- this .properties = data .get ("properties" );
47+ InputStream inputStream = new FileInputStream ("artifacts/configs/" + name + "/" + name + ".yaml" );
48+ Map <String , Object > data = yaml .load (inputStream );
49+
50+ this .name = name ;
51+ URL = (String ) data .get ("URL" );
52+ checkoutID = (String ) data .get ("checkoutID" );
53+ patchName = (String ) data .get ("patchName" );
54+ subProject = (String ) data .getOrDefault ("subProject" , "" );
55+ mvnOptions = (String ) data .getOrDefault ("mvnOptions" , "" );
56+ properties = (List <Map <String ,String >>) data .get ("properties" );
57+
58+ this .timeStamp = String .valueOf (java .time .LocalDateTime .now ());
4559 }
4660
4761 public void cloneRepo () throws GitAPIException , JGitInternalException {
@@ -83,9 +97,9 @@ public void buildJars() throws IOException, InterruptedException {
8397 public void testProperty (String property ) throws IOException , InterruptedException {
8498 ProcessBuilder pb = new ProcessBuilder ();
8599 if (isWindows ())
86- pb .command ("cmd.exe" , "/c" , "mvn" , "test" , "-Dtest=" + property );
100+ pb .command ("cmd.exe" , "/c" , "mvn" , "test" , mvnOptions , "-Dtest=" + property );
87101 else
88- pb .command ("bash" , "-c" , "mvn test -Dtest=" + property );
102+ pb .command ("bash" , "-c" , "mvn test " + mvnOptions + " -Dtest=" + property );
89103 pb .directory (new File (this .name ));
90104 long start = System .nanoTime ();
91105 Process process = pb .start ();
@@ -117,16 +131,16 @@ public void cleanTarget() throws IOException, InterruptedException {
117131 public List <Pair <String ,String >> obtainCoverageFilesAndEntryPoints (){
118132 List <Pair <String ,String >> coverageFiles = new LinkedList <>();
119133 for (Map <String , String > m : properties )
120- coverageFiles .add (new Pair <>("artifacts/results/" + this . name + "/" + m .get ("name" ) + ".xml" , m .get ("entryPoint" )));
134+ coverageFiles .add (new Pair <>("artifacts/results/" + getProjectDir () + timeStamp + "/" + m .get ("name" ) + ".xml" , m .get ("entryPoint" )));
121135 return coverageFiles ;
122136 }
123137
124138 public static Optional <RepoTool > obtainTool (String folderName ){
125139 try {
126140 Yaml yaml = new Yaml ();
127- InputStream inputStream = new FileInputStream (new File ( "artifacts/configs/" + folderName + "/" + folderName + ".yaml" ) );
141+ InputStream inputStream = new FileInputStream ("artifacts/configs/" + folderName + "/" + folderName + ".yaml" );
128142 Map <String , String > data = yaml .load (inputStream );
129- return Optional .of (new RepoTool (data .get ("name" ), data .get ("URL" ), data .get ("checkoutID" ), data .get ("patchName" )));
143+ return Optional .of (new RepoTool (data .get ("name" ), data .get ("URL" ), data .get ("checkoutID" ), data .get ("patchName" ), data . getOrDefault ( "subProject" , "" ), data . getOrDefault ( "mvnOptions" , "" ) ));
130144 }
131145 catch (IOException e ){
132146 LOGGER .error ("IOException: " + e .getMessage ());
@@ -135,23 +149,35 @@ public static Optional<RepoTool> obtainTool(String folderName){
135149 return Optional .empty ();
136150 }
137151
138- private void moveJars () throws IOException {
139- Path jar = Files .move (
140- Paths .get (System .getProperty ("user.dir" ) + "/" + this .name + "/target/" + this .name + "-1.0.6-SNAPSHOT.jar" ),
141- Paths .get (System .getProperty ("user.dir" ) + "/artifacts/output/" + this .name + "-1.0.6-SNAPSHOT.jar" ),
142- StandardCopyOption .REPLACE_EXISTING );
143- Path testJar = Files .move (
144- Paths .get (System .getProperty ("user.dir" ) + "/" + this .name + "/target/" + this .name + "-1.0.6-SNAPSHOT-tests.jar" ),
145- Paths .get (System .getProperty ("user.dir" ) + "/artifacts/output/" + this .name + "-1.0.6-SNAPSHOT-tests.jar" ),
146- StandardCopyOption .REPLACE_EXISTING );
152+ private void moveJars () throws IOException {
153+ Path sourceDir = Paths .get (System .getProperty ("user.dir" ), getProjectDir (), "target" );
154+ Path targetDir = Paths .get (System .getProperty ("user.dir" ), "artifacts" , "output" );
155+
156+ // @todo may want to be able to override this in the yaml file on a project basis...
157+ String depGlob = this .name + "*-with-dependencies.jar" ;
158+ String testGlob = this .name + "*-tests.jar" ;
159+
160+ moveFiles (sourceDir , targetDir , depGlob );
161+ moveFiles (sourceDir , targetDir , testGlob );
162+ }
163+
164+ private void moveFiles (Path sourceDir , Path targetDir , String glob ) throws IOException {
165+ try (DirectoryStream <Path > dirStream = Files .newDirectoryStream (sourceDir , glob )) {
166+ for (Path source : dirStream ) {
167+ Files .move (
168+ source ,
169+ targetDir .resolve (source .getFileName ()),
170+ StandardCopyOption .REPLACE_EXISTING );
171+ }
172+ }
147173 }
148174
149175 private void moveJacoco (String property , long timeElapsed ) throws IOException {
150- String timeStamp = String . valueOf ( java . time . LocalDateTime . now () );
151- String directoryPath = System .getProperty ("user.dir" ) + "/artifacts/results/" + this . name + timeStamp ;
152- String jacocoPath = System .getProperty ("user.dir" ) + "/" + this . name + "/target/site/jacoco/jacoco.xml" ;
176+ String projectDir = getProjectDir ( );
177+ String directoryPath = System .getProperty ("user.dir" ) + "/artifacts/results/" + projectDir + timeStamp ;
178+ String jacocoPath = System .getProperty ("user.dir" ) + "/" + projectDir + "/target/site/jacoco/jacoco.xml" ;
153179 String jacocoTargetPath = directoryPath + "/" + property + ".xml" ;
154- String statisticsPath = System .getProperty ("user.dir" ) + "/" + this . name + "/target/site/jacoco/index.html" ;
180+ String statisticsPath = System .getProperty ("user.dir" ) + "/" + projectDir + "/target/site/jacoco/index.html" ;
155181 String statisticsTargetPath = directoryPath + "/" + property + ".html" ;
156182 File directory = new File (directoryPath );
157183 directory .mkdir ();
@@ -164,21 +190,18 @@ private void moveJacoco(String property, long timeElapsed) throws IOException{
164190 Paths .get (statisticsTargetPath ),
165191 StandardCopyOption .REPLACE_EXISTING );
166192 double timeElapsedInSeconds = (double ) timeElapsed / 1_000_000_000 ;
167- FileWriter fileWriter = null ;
168- BufferedWriter bufferedWriter = null ;
169- try {
170- fileWriter = new FileWriter (statisticsTargetPath , true );
171- bufferedWriter = new BufferedWriter (fileWriter );
172- bufferedWriter .append ("<html><section><h1> Total Time Elapsed: " + timeElapsedInSeconds +" seconds</h1></section></html>" );
193+ try (FileWriter fileWriter = new FileWriter (statisticsTargetPath , true ); BufferedWriter bufferedWriter = new BufferedWriter (fileWriter )) {
194+ bufferedWriter .append ("<html><section><h1> Total Time Elapsed: " ).append (String .valueOf (timeElapsedInSeconds )).append (" seconds</h1></section></html>" );
173195 bufferedWriter .flush ();
174- } finally {
175- fileWriter .close ();
176- bufferedWriter .close ();
177196 }
178197 }
179198
180199 private boolean isWindows () {
181200 return System .getProperty ("os.name" )
182201 .toLowerCase ().startsWith ("windows" );
183202 }
203+
204+ private String getProjectDir () {
205+ return (subProject .equals ("" )) ? name : (name + "/" + subProject );
206+ }
184207}
0 commit comments