@@ -37,7 +37,8 @@ public static void main(String[] args) throws IOException {
3737
3838 /**
3939 * Download AVRDude for the current OS architecture.
40- * @return the path to the extracted avrdude executable.
40+ * @return the path to the first folder. If the target is c:\a\b\ and the extracted files are in c:\a\b\c\d\
41+ * then this will return c:\a\b\c
4142 * @throws IOException if the download fails.
4243 */
4344 public static String downloadAVRDude () throws IOException {
@@ -52,22 +53,20 @@ public static String downloadAVRDude() throws IOException {
5253 */
5354 public static String downloadAVRDude (String arch ) throws IOException {
5455 String url = getURLforOS (arch );
55- if (url != null ) {
56- try {
57- return downloadAndExtract ( url );
58- } catch ( IOException e ) {
59- logger . error ( "Error downloading avrdude" , e );
60- throw e ;
61- }
56+ if (url == null ) return null ;
57+
58+ try {
59+ return downloadAndExtract ( url );
60+ } catch ( IOException e ) {
61+ logger . error ( "Error downloading avrdude" , e ) ;
62+ throw e ;
6263 }
63- return null ;
6464 }
6565
6666 public static String getArch () {
67- String arch = LINUX ;
68- if (OSHelper .isWindows ()) arch = WINDOWS ;
69- if (OSHelper .isOSX ()) arch = MACOS ;
70- return arch ;
67+ if (OSHelper .isWindows ()) return WINDOWS ;
68+ if (OSHelper .isOSX ()) return MACOS ;
69+ return LINUX ;
7170 }
7271
7372 /**
@@ -160,7 +159,7 @@ private static JSONObject getJSONFromURL(String url) throws IOException {
160159 public static String downloadAndExtract (String urlStr ) throws IOException {
161160 File toDeleteLater = downloadFileToTemp (urlStr );
162161 String path = extractFile (toDeleteLater , urlStr );
163- logger .info ("new path: " + path );
162+ logger .info ("new path: {}" , path );
164163 makeExecutable (path );
165164
166165 return path ;
@@ -210,6 +209,13 @@ private static File downloadFileToTemp(String urlStr) throws IOException {
210209 return toDeleteLater ;
211210 }
212211
212+ /**
213+ * Extract the downloaded file to ~/.makelangelo
214+ * @param toDeleteLater the file to delete after extraction
215+ * @param urlStr the URL of the file
216+ * @return the path to the extracted file, which should be ~/.makelangelo/firstSubDirectory
217+ * @throws IOException if the extraction fails
218+ */
213219 private static String extractFile (File toDeleteLater , String urlStr ) throws IOException {
214220 String targetDirStr = System .getProperty ("user.home" ) + File .separator + ".makelangelo" + File .separator ;
215221 String newFolderName = "" ;
@@ -225,18 +231,26 @@ private static String extractFile(File toDeleteLater, String urlStr) throws IOEx
225231 newFolderName = extractBz2File (toDeleteLater , targetDirStr );
226232 }
227233
228- if (!newFolderName .isEmpty ()) {
229- newFolderName = targetDirStr + newFolderName ;
234+ // sometimes the path returned might be /a/b/... and we only want /a/
235+ if (newFolderName .contains ("/" )) {
236+ newFolderName = newFolderName .split ("/" )[0 ];
230237 }
231238
232- return newFolderName ;
239+ return targetDirStr + newFolderName ;
233240 }
234241
235242 private static String getFileExtension (String fileName ) {
236243 int dotIndex = fileName .lastIndexOf ('.' );
237244 return (dotIndex == -1 ) ? "" : fileName .substring (dotIndex );
238245 }
239246
247+ /**
248+ * Extract a zip file to the target directory.
249+ * @param file the zip file
250+ * @param targetDirStr the target directory
251+ * @return the name of the first directory in the zip file
252+ * @throws IOException if the extraction fails
253+ */
240254 private static String extractZipFile (File file , String targetDirStr ) throws IOException {
241255 try (ZipInputStream zipIn = new ZipInputStream (new FileInputStream (file ))) {
242256 ZipEntry entry = zipIn .getNextEntry ();
@@ -245,6 +259,10 @@ private static String extractZipFile(File file, String targetDirStr) throws IOEx
245259 Path filePath = Paths .get (targetDirStr , entry .getName ());
246260 if (entry .isDirectory ()) {
247261 Files .createDirectories (filePath );
262+ // the first directory is the one we want to return
263+ if (newFolderName .isEmpty ()) {
264+ newFolderName = entry .getName ();
265+ }
248266 } else {
249267 Files .createDirectories (filePath .getParent ());
250268 try (OutputStream outputStream = new FileOutputStream (filePath .toFile ())) {
@@ -256,15 +274,19 @@ private static String extractZipFile(File file, String targetDirStr) throws IOEx
256274 }
257275 }
258276 zipIn .closeEntry ();
259- if (newFolderName .isEmpty () && entry .isDirectory ()) {
260- newFolderName = entry .getName ();
261- }
262277 entry = zipIn .getNextEntry ();
263278 }
264279 return newFolderName ;
265280 }
266281 }
267282
283+ /**
284+ * Extract a bz2 file to the target directory.
285+ * @param file the zip file
286+ * @param targetDirStr the target directory
287+ * @return the name of the first directory in the bz2 file
288+ * @throws IOException if the extraction fails
289+ */
268290 private static String extractBz2File (File file , String targetDirStr ) throws IOException {
269291 try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream (new FileInputStream (file ))) {
270292 Path targetFilePath = Paths .get (targetDirStr , file .getName ().replace (".bz2" , "" ));
@@ -280,15 +302,25 @@ private static String extractBz2File(File file, String targetDirStr) throws IOEx
280302 }
281303 }
282304
305+ /**
306+ * Extract a .tar.bz2 file to the target directory.
307+ * @param file the zip file
308+ * @param targetDirStr the target directory
309+ * @return the name of the first directory in the bz2 file
310+ * @throws IOException if the extraction fails
311+ */
283312 private static String extractTarBz2File (File file , String targetDirStr ) throws IOException {
284313 try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream (new FileInputStream (file ));
285314 TarArchiveInputStream tarIn = new TarArchiveInputStream (bzIn )) {
286315 TarArchiveEntry entry ;
287316 String newFolderName = "" ;
288- while ((entry = ( TarArchiveEntry ) tarIn .getNextEntry ()) != null ) {
317+ while ((entry = tarIn .getNextEntry ()) != null ) {
289318 Path filePath = Paths .get (targetDirStr , entry .getName ());
290319 if (entry .isDirectory ()) {
291320 Files .createDirectories (filePath );
321+ if (newFolderName .isEmpty ()) {
322+ newFolderName = getFolderNameFromEntry (entry .getName ());
323+ }
292324 } else {
293325 Files .createDirectories (filePath .getParent ());
294326 try (OutputStream outputStream = new FileOutputStream (filePath .toFile ())) {
@@ -299,9 +331,6 @@ private static String extractTarBz2File(File file, String targetDirStr) throws I
299331 }
300332 }
301333 }
302- if (newFolderName .isEmpty ()) {
303- newFolderName = getFolderNameFromEntry (entry .getName ());
304- }
305334 }
306335 return newFolderName ;
307336 }
0 commit comments