From 682a5ec3314c01f24a778fa313881e6069239291 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Wed, 16 Sep 2015 14:51:37 +0200 Subject: [PATCH] Faster recompilation of unsaved sketches --- app/src/processing/app/Sketch.java | 3 +- .../libraries/LibraryInstaller.java | 2 +- .../src/processing/app/helpers/FileUtils.java | 28 ++++++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index f8c8adf38..d4d110987 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -28,6 +28,7 @@ import cc.arduino.CompilerProgressListener; import cc.arduino.UploaderUtils; import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.Uploader; +import org.apache.commons.codec.digest.DigestUtils; import processing.app.debug.RunnerException; import processing.app.forms.PasswordAuthorizationDialog; import processing.app.helpers.FileUtils; @@ -1130,7 +1131,7 @@ public class Sketch { } private String saveSketchInTempFolder() throws IOException { - File tempFolder = FileUtils.createTempFolder(); + File tempFolder = FileUtils.createTempFolder("arduino_", DigestUtils.md5Hex(data.getMainFilePath())); DeleteFilesOnShutdown.add(tempFolder); FileUtils.copy(getFolder(), tempFolder); diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java b/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java index c01b56f20..03710ac8e 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java @@ -109,7 +109,7 @@ public class LibraryInstaller { progress.setStatus(I18n.format(tr("Installing library: {0}"), lib.getName())); progressListener.onProgress(progress); File libsFolder = indexer.getSketchbookLibrariesFolder(); - File tmpFolder = FileUtils.createTempFolderIn(libsFolder); + File tmpFolder = FileUtils.createTempFolder(libsFolder); try { new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1); } catch (Exception e) { diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java index 981d3cb69..77ef6ead9 100644 --- a/arduino-core/src/processing/app/helpers/FileUtils.java +++ b/arduino-core/src/processing/app/helpers/FileUtils.java @@ -3,6 +3,8 @@ package processing.app.helpers; import org.apache.commons.compress.utils.IOUtils; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.*; import java.util.regex.Pattern; @@ -84,15 +86,27 @@ public class FileUtils { } public static File createTempFolder() throws IOException { - return createTempFolderIn(new File(System.getProperty("java.io.tmpdir"))); + return createTempFolder(new File(System.getProperty("java.io.tmpdir"))); } - public static File createTempFolderIn(File parent) throws IOException { - File tmpFolder = new File(parent, "arduino_" + new Random().nextInt(1000000)); - if (!tmpFolder.mkdir()) { - throw new IOException("Unable to create temp folder " + tmpFolder); - } - return tmpFolder; + public static File createTempFolder(File parent) throws IOException { + return createTempFolder(parent, "arduino_"); + } + + public static File createTempFolder(File parent, String prefix) throws IOException { + return createTempFolder(parent, prefix, Integer.toString(new Random().nextInt(1000000))); + } + + public static File createTempFolder(String prefix) throws IOException { + return createTempFolder(new File(System.getProperty("java.io.tmpdir")), prefix); + } + + public static File createTempFolder(String prefix, String suffix) throws IOException { + return createTempFolder(new File(System.getProperty("java.io.tmpdir")), prefix, suffix); + } + + public static File createTempFolder(File parent, String prefix, String suffix) throws IOException { + return Files.createDirectories(Paths.get(parent.getAbsolutePath(), prefix + suffix)).toFile(); } //