11package com .minecrafttas .discombobulator .tasks ;
22
3- import java .io .File ;
3+ import java .io .IOException ;
4+ import java .nio .file .Files ;
5+ import java .nio .file .Path ;
6+ import java .nio .file .StandardCopyOption ;
47import java .util .HashMap ;
58import java .util .List ;
69import java .util .Map ;
710import java .util .Map .Entry ;
11+ import java .util .stream .Stream ;
812
913import org .gradle .api .DefaultTask ;
1014import org .gradle .api .Project ;
@@ -21,17 +25,43 @@ public class TaskCollectBuilds extends DefaultTask {
2125 /**
2226 * List of all build dirs
2327 */
24- private Map <String , File > buildDirs = new HashMap <>();
28+ private Map <String , Path > buildDirs = new HashMap <>();
2529
2630 @ TaskAction
2731 public void collectBuilds () {
28- File collectDir = getBuildDir (getProject ());
29- collectDir .mkdirs ();
30- for (Entry <String , File > entry : buildDirs .entrySet ()) {
31- File buildDir = entry .getValue ();
32- for (File artifact : buildDir .listFiles ()) {
33- artifact .renameTo (new File (collectDir , artifact .getName ()));
32+ Path collectDir = getBuildDir (getProject ());
33+
34+ try {
35+ if (!Files .exists (collectDir ))
36+ Files .createDirectory (collectDir );
37+ } catch (IOException e ) {
38+ e .printStackTrace ();
39+ return ;
40+ }
41+
42+ for (Entry <String , Path > entry : buildDirs .entrySet ()) {
43+ Path buildDir = entry .getValue ();
44+ Stream <Path > stream ;
45+
46+ try {
47+ stream = Files .list (buildDir );
48+ } catch (IOException e ) {
49+ e .printStackTrace ();
50+ return ;
3451 }
52+
53+ stream .forEach (path -> {
54+ Path targetFile = collectDir .resolve (path .getFileName ());
55+
56+ try {
57+ Files .move (path , targetFile , StandardCopyOption .REPLACE_EXISTING );
58+ } catch (IOException e ) {
59+ e .printStackTrace ();
60+ return ;
61+ }
62+ });
63+
64+ stream .close ();
3565 }
3666 }
3767
@@ -43,7 +73,7 @@ public void collectBuilds() {
4373 public void updateCompileTasks (List <Task > compileTasks ) {
4474 for (Task task : compileTasks ) {
4575 Project project = task .getProject ();
46- this .buildDirs .put (project .getName (), new File ( getBuildDir (project ), "libs" ));
76+ this .buildDirs .put (project .getName (), getBuildDir (project ). resolve ( "libs" ));
4777 }
4878 this .setDependsOn (compileTasks );
4979 }
@@ -52,7 +82,7 @@ public void updateCompileTasks(List<Task> compileTasks) {
5282 * @param project The project to use
5383 * @return The build directory from the project
5484 */
55- private File getBuildDir (Project project ) {
56- return project .getLayout ().getBuildDirectory ().getAsFile ().get ();
85+ private Path getBuildDir (Project project ) {
86+ return project .getLayout ().getBuildDirectory ().get (). getAsFile ().toPath ();
5787 }
5888}
0 commit comments