12
12
import org .gradle .api .tasks .TaskAction ;
13
13
import org .gradle .api .tasks .options .Option ;
14
14
15
+ import com .google .gson .Gson ;
15
16
import com .google .gson .GsonBuilder ;
16
17
17
18
import de .undercouch .gradle .tasks .download .DownloadAction ;
22
23
*/
23
24
public class VendorDepTask extends DefaultTask {
24
25
private String url ;
26
+ private boolean update ;
25
27
private DownloadAction downloadAction = new DownloadAction (getProject ());
26
28
private WPIVendorDepsExtension wpiExt = getProject ().getExtensions ().getByType (WPIVendorDepsExtension .class );
27
29
@@ -30,41 +32,68 @@ public void setURL(String url) {
30
32
this .url = url ;
31
33
}
32
34
35
+ @ Option (option = "update" , description = "Update the existing vendordeps" )
36
+ public void update () {
37
+ update = true ;
38
+ }
39
+
33
40
/**
34
41
* Installs the JSON file
35
42
* @throws java.io.IOException throws on ioexception
36
43
*/
37
44
@ TaskAction
38
45
public void install () throws IOException {
39
- String filename = findFileName (url );
40
- Path dest = computeDest (filename );
41
- if (url .startsWith ("FRCLOCAL/" )) {
42
- getLogger ().info ("Locally fetching $filename" );
43
- copyLocal (filename , dest );
44
- } else {
45
- getLogger ().info ("Remotely fetching " + filename );
46
- downloadRemote (dest );
47
- }
48
-
49
- var destString = dest .toString ();
50
- String newFilename ;
51
- try (BufferedReader reader = Files .newBufferedReader (dest )) {
52
- newFilename = new GsonBuilder ().create ().fromJson (reader , JsonDependency .class ).fileName ;
53
- if (newFilename == null ) {
54
- getLogger ().warn ("Couldn't find fileName field in " + destString + "\n Aborting" );
55
- return ;
46
+ if (update ) {
47
+ Gson gson = new GsonBuilder ().create ();
48
+ Object property = getProject ().findProperty (WPIVendorDepsExtension .NATIVEUTILS_VENDOR_FOLDER_PROPERTY );
49
+ File destfolder = new File (property != null ? (String )property : WPIVendorDepsExtension .DEFAULT_VENDORDEPS_FOLDER_NAME );
50
+ File [] vendordeps = destfolder .listFiles ();
51
+ if (vendordeps != null ) {
52
+ for (File vendordep : vendordeps ) {
53
+ getLogger ().info ("Remotely fetching " + vendordep .toString ());
54
+ BufferedReader reader = Files .newBufferedReader (Path .of (vendordep .getPath ()));
55
+ var jsonUrl = gson .fromJson (reader , JsonDependency .class ).jsonUrl ;
56
+ if (jsonUrl != null ) {
57
+ url = jsonUrl ;
58
+ downloadRemote (Path .of (vendordep .getPath ()));
59
+ } else {
60
+ getLogger ().warn ("Couldn't get jsonUrl for " + vendordep );
61
+ }
56
62
}
57
- } catch (IOException e ) {
58
- throw new RuntimeException (e );
59
- }
60
- File file = new File (destString );
61
- int lastPathSeparator = dest .toString ().lastIndexOf ('/' );
62
- File newFile = new File (dest .toString ().substring (0 , lastPathSeparator + 1 ) + newFilename );
63
- boolean didRename = file .renameTo (newFile );
64
- if (didRename ) {
65
- getLogger ().info ("Succesfully renamed " + file .toString () + " to " + newFile .toString ());
63
+ } else {
64
+ getLogger ().warn ("Couldn't update vendordeps, invalid directory." );
65
+ }
66
66
} else {
67
- getLogger ().warn ("Failed to rename file " + file .toString () + " to " + newFile .toString ());
67
+ String filename = findFileName (url );
68
+ Path dest = computeDest (filename );
69
+ if (url .startsWith ("FRCLOCAL/" )) {
70
+ getLogger ().info ("Locally fetching $filename" );
71
+ copyLocal (filename , dest );
72
+ } else {
73
+ getLogger ().info ("Remotely fetching " + filename );
74
+ downloadRemote (dest );
75
+ }
76
+
77
+ var destString = dest .toString ();
78
+ String newFilename ;
79
+ try (BufferedReader reader = Files .newBufferedReader (dest )) {
80
+ newFilename = new GsonBuilder ().create ().fromJson (reader , JsonDependency .class ).fileName ;
81
+ if (newFilename == null ) {
82
+ getLogger ().warn ("Couldn't find fileName field in " + destString + "\n Aborting" );
83
+ return ;
84
+ }
85
+ } catch (IOException e ) {
86
+ throw new RuntimeException (e );
87
+ }
88
+ File file = new File (destString );
89
+ int lastPathSeparator = dest .toString ().lastIndexOf ('/' );
90
+ File newFile = new File (dest .toString ().substring (0 , lastPathSeparator + 1 ) + newFilename );
91
+ boolean didRename = file .renameTo (newFile );
92
+ if (didRename ) {
93
+ getLogger ().info ("Succesfully renamed " + file .toString () + " to " + newFile .toString ());
94
+ } else {
95
+ getLogger ().warn ("Failed to rename file " + file .toString () + " to " + newFile .toString ());
96
+ }
68
97
}
69
98
}
70
99
0 commit comments