diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 7cc5c2a89..d9474b9ec 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -217,7 +217,7 @@ public class Base { } // Create a location for untitled sketches - untitledFolder = BaseNoGui.createTempFolder("untitled"); + untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp"); DeleteFilesOnShutdown.add(untitledFolder); INSTANCE = new Base(args); diff --git a/app/src/processing/app/EditorHeader.java b/app/src/processing/app/EditorHeader.java index 561054654..a1e100aea 100644 --- a/app/src/processing/app/EditorHeader.java +++ b/app/src/processing/app/EditorHeader.java @@ -28,6 +28,7 @@ import static processing.app.I18n.tr; import java.awt.*; import java.awt.event.*; +import java.io.IOException; import javax.swing.*; @@ -317,8 +318,12 @@ public class EditorHeader extends JComponent { item = new JMenuItem(tr("Delete")); item.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - editor.getSketch().handleDeleteCode(); + public void actionPerformed(ActionEvent event) { + try { + editor.getSketch().handleDeleteCode(); + } catch (IOException e) { + e.printStackTrace(); + } } }); menu.add(item); diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index d4d110987..2544e204c 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -57,8 +57,6 @@ import static processing.app.I18n.tr; * Stores information about files in the current sketch */ public class Sketch { - static private File tempBuildFolder; - private final Editor editor; /** true if any of the files have been modified. */ @@ -76,26 +74,6 @@ public class Sketch { public Sketch(Editor _editor, File file) throws IOException { editor = _editor; data = new SketchData(file); - - // lib/build must exist when the application is started - // it is added to the CLASSPATH by default, but if it doesn't - // exist when the application is started, then java will remove - // the entry from the CLASSPATH, causing Runner to fail. - // - /* - tempBuildFolder = new File(TEMP_BUILD_PATH); - if (!tempBuildFolder.exists()) { - tempBuildFolder.mkdirs(); - Base.showError("Required folder missing", - "A required folder was missing from \n" + - "from your installation of Processing.\n" + - "It has now been replaced, please restart \n" + - "the application to complete the repair.", null); - } - */ - tempBuildFolder = BaseNoGui.getBuildFolder(); - //Base.addBuildFolderToClassPath(); - load(); } @@ -439,7 +417,7 @@ public class Sketch { /** * Remove a piece of code from the sketch and from the disk. */ - public void handleDeleteCode() { + public void handleDeleteCode() throws IOException { editor.status.clearState(); // make sure the user didn't hide the sketch folder ensureExistence(); @@ -485,7 +463,7 @@ public class Sketch { } else { // delete the file - if (!current.getCode().deleteFile(tempBuildFolder)) { + if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data))) { Base.showMessage(tr("Couldn't do it"), I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName())); return; @@ -1102,7 +1080,7 @@ public class Sketch { * @throws RunnerException */ public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException { - return build(tempBuildFolder.getAbsolutePath(), verbose, save); + return build(BaseNoGui.getBuildFolder(data).getAbsolutePath(), verbose, save); } /** @@ -1143,7 +1121,7 @@ public class Sketch { } protected boolean exportApplet(boolean usingProgrammer) throws Exception { - return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer); + return exportApplet(BaseNoGui.getBuildFolder(data).getAbsolutePath(), usingProgrammer); } diff --git a/app/test/processing/app/AbstractGUITest.java b/app/test/processing/app/AbstractGUITest.java index 75c9b864f..d1db60d98 100644 --- a/app/test/processing/app/AbstractGUITest.java +++ b/app/test/processing/app/AbstractGUITest.java @@ -36,8 +36,10 @@ import org.fest.swing.edt.GuiQuery; import org.junit.After; import org.junit.Before; import processing.app.helpers.ArduinoFrameFixture; +import processing.app.helpers.FileUtils; import javax.swing.*; +import java.util.Random; public abstract class AbstractGUITest { @@ -56,7 +58,7 @@ public abstract class AbstractGUITest { JPopupMenu.setDefaultLightWeightPopupEnabled(false); Theme.init(); BaseNoGui.getPlatform().setLookAndFeel(); - Base.untitledFolder = BaseNoGui.createTempFolder("untitled"); + Base.untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp"); DeleteFilesOnShutdown.add(Base.untitledFolder); window = GuiActionRunner.execute(new GuiQuery() { diff --git a/app/test/processing/app/AbstractWithPreferencesTest.java b/app/test/processing/app/AbstractWithPreferencesTest.java index e78eea58b..f0d2f3a2b 100644 --- a/app/test/processing/app/AbstractWithPreferencesTest.java +++ b/app/test/processing/app/AbstractWithPreferencesTest.java @@ -31,6 +31,9 @@ package processing.app; import cc.arduino.files.DeleteFilesOnShutdown; import org.junit.Before; +import processing.app.helpers.FileUtils; + +import java.util.Random; public abstract class AbstractWithPreferencesTest { @@ -44,7 +47,7 @@ public abstract class AbstractWithPreferencesTest { BaseNoGui.initPackages(); - Base.untitledFolder = BaseNoGui.createTempFolder("untitled"); + Base.untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp"); DeleteFilesOnShutdown.add(Base.untitledFolder); } diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index f607820b8..d7af3eaf4 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -12,6 +12,7 @@ import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.DiscoveryManager; import cc.arduino.packages.Uploader; import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.logging.impl.LogFactoryImpl; import org.apache.commons.logging.impl.NoOpLog; @@ -27,6 +28,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -59,8 +61,6 @@ public class BaseNoGui { VERSION_NAME_LONG = versionNameLong; } - static File buildFolder; - private static DiscoveryManager discoveryManager = new DiscoveryManager(); // these are static because they're used by Sketch @@ -111,28 +111,6 @@ public class BaseNoGui { return count; } - /** - * Get the path to the platform's temporary folder, by creating - * a temporary temporary file and getting its parent folder. - *
- * Modified for revision 0094 to actually make the folder randomized - * to avoid conflicts in multi-user environments. (Bug 177) - */ - static public File createTempFolder(String name) { - try { - File folder = File.createTempFile(name, null); - //String tempPath = ignored.getParent(); - //return new File(tempPath); - folder.delete(); - folder.mkdirs(); - return folder; - - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - static public String getAvrBasePath() { String path = getHardwarePath() + File.separator + "tools" + File.separator + "avr" + File.separator + "bin" + File.separator; @@ -142,19 +120,14 @@ public class BaseNoGui { return path; } - static public File getBuildFolder() { - if (buildFolder == null) { - String buildPath = PreferencesData.get("build.path"); - if (buildPath != null) { - buildFolder = absoluteFile(buildPath); - if (!buildFolder.exists()) - buildFolder.mkdirs(); - } else { - //File folder = new File(getTempFolder(), "build"); - //if (!folder.exists()) folder.mkdirs(); - buildFolder = createTempFolder("build"); - DeleteFilesOnShutdown.add(buildFolder); - } + static public File getBuildFolder(SketchData data) throws IOException { + File buildFolder; + if (PreferencesData.get("build.path") != null) { + buildFolder = absoluteFile(PreferencesData.get("build.path")); + Files.createDirectories(buildFolder.toPath()); + } else { + buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(data.getMainFilePath()) + ".tmp"); + DeleteFilesOnShutdown.add(buildFolder); } return buildFolder; } @@ -495,7 +468,7 @@ public class BaseNoGui { // File tempBuildFolder = getBuildFolder(); // data.load(); SketchData data = new SketchData(absoluteFile(parser.getFilenames().get(0))); - File tempBuildFolder = getBuildFolder(); + File tempBuildFolder = getBuildFolder(data); data.load(); // Sketch.exportApplet() @@ -542,7 +515,7 @@ public class BaseNoGui { // File tempBuildFolder = getBuildFolder(); // data.load(); SketchData data = new SketchData(absoluteFile(path)); - File tempBuildFolder = getBuildFolder(); + File tempBuildFolder = getBuildFolder(data); data.load(); // Sketch.prepare() calls Sketch.ensureExistence() @@ -1109,10 +1082,6 @@ public class BaseNoGui { PreferencesData.set("serial.port.file", portFile); } - public static void setBuildFolder(File newBuildFolder) { - buildFolder = newBuildFolder; - } - static public void showError(String title, String message, int exit_code) { showError(title, message, null, exit_code); } diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java index c9e893878..d02f9606d 100644 --- a/arduino-core/src/processing/app/SketchCode.java +++ b/arduino-core/src/processing/app/SketchCode.java @@ -97,17 +97,26 @@ public class SketchCode { return false; } - File[] compiledFiles = tempBuildFolder.listFiles(new FileFilter() { - public boolean accept(File pathname) { - return pathname.getName().startsWith(getFileName()); - } + if (!deleteCompiledFilesFrom(tempBuildFolder)) { + return false; + } + + if (!deleteCompiledFilesFrom(new File(tempBuildFolder, "sketch"))) { + return false; + } + + return true; + } + + private boolean deleteCompiledFilesFrom(File tempBuildFolder) { + File[] compiledFiles = tempBuildFolder.listFiles(pathname -> { + return pathname.getName().startsWith(getFileName()); }); for (File compiledFile : compiledFiles) { if (!compiledFile.delete()) { return false; } } - return true; } diff --git a/arduino-core/src/processing/app/helpers/CommandlineParser.java b/arduino-core/src/processing/app/helpers/CommandlineParser.java index 6d93dcc7e..3c093d19d 100644 --- a/arduino-core/src/processing/app/helpers/CommandlineParser.java +++ b/arduino-core/src/processing/app/helpers/CommandlineParser.java @@ -161,7 +161,7 @@ public class CommandlineParser { if (!buildFolder.isDirectory()) { BaseNoGui.showError(null, "The build path is not a folder", 3); } - BaseNoGui.setBuildFolder(buildFolder); + PreferencesData.set("build.path", buildFolder.getAbsolutePath()); continue; } if (args[i].equals("--pref")) {