Skip to content

Commit d201069

Browse files
committed
Merge pull request #2980 from ffissore/delete_on_exit
Delete temporary files on exit
2 parents e7dc30d + dabd6e4 commit d201069

File tree

8 files changed

+84
-10
lines changed

8 files changed

+84
-10
lines changed

app/src/processing/app/Base.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import cc.arduino.contributions.packages.ContributionsIndexer;
3333
import cc.arduino.contributions.DownloadableContributionVersionComparator;
3434
import cc.arduino.contributions.packages.ui.ContributionManagerUI;
35+
import cc.arduino.files.DeleteFilesOnShutdown;
3536
import cc.arduino.packages.DiscoveryManager;
3637
import cc.arduino.utils.Progress;
3738
import cc.arduino.view.SplashScreenHelper;
@@ -127,6 +128,8 @@ static public void main(String args[]) throws Exception {
127128
}
128129

129130
static public void guardedMain(String args[]) throws Exception {
131+
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
132+
130133
BaseNoGui.initLogger();
131134

132135
BaseNoGui.notifier = new GUIUserNotifier();
@@ -202,7 +205,7 @@ static public void guardedMain(String args[]) throws Exception {
202205

203206
// Create a location for untitled sketches
204207
untitledFolder = createTempFolder("untitled");
205-
untitledFolder.deleteOnExit();
208+
DeleteFilesOnShutdown.add(untitledFolder);
206209

207210
new Base(args);
208211
}
@@ -401,6 +404,7 @@ protected void onProgress(Progress progress) {
401404
// Set verbosity for command line build
402405
Preferences.set("build.verbose", "" + parser.isDoVerboseBuild());
403406
Preferences.set("upload.verbose", "" + parser.isDoVerboseUpload());
407+
Preferences.set("runtime.preserve.temp.files", Boolean.toString(parser.isPreserveTempFiles()));
404408

405409
// Make sure these verbosity preferences are only for the
406410
// current session

app/src/processing/app/EditorConsoleStream.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package processing.app;
22

3+
import cc.arduino.files.DeleteFilesOnShutdown;
4+
35
import static processing.app.I18n._;
46

57
import java.io.File;
@@ -33,19 +35,19 @@ public static void init() {
3335
// The files and folders are not deleted on exit because they may be
3436
// needed for debugging or bug reporting.
3537
tempFolder = Base.createTempFolder("console");
36-
tempFolder.deleteOnExit();
38+
DeleteFilesOnShutdown.add(tempFolder);
3739
try {
3840
String outFileName = Preferences.get("console.output.file");
3941
if (outFileName != null) {
4042
outFile = new File(tempFolder, outFileName);
41-
outFile.deleteOnExit();
43+
DeleteFilesOnShutdown.add(outFile);
4244
stdoutFile = new FileOutputStream(outFile);
4345
}
4446

4547
String errFileName = Preferences.get("console.error.file");
4648
if (errFileName != null) {
4749
errFile = new File(tempFolder, errFileName);
48-
errFile.deleteOnExit();
50+
DeleteFilesOnShutdown.add(errFile);
4951
stderrFile = new FileOutputStream(errFile);
5052
}
5153
} catch (IOException e) {

app/test/processing/app/AbstractGUITest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package processing.app;
22

3+
import cc.arduino.files.DeleteFilesOnShutdown;
34
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
45
import org.fest.swing.edt.GuiActionRunner;
56
import org.fest.swing.edt.GuiQuery;
@@ -16,6 +17,7 @@ public abstract class AbstractGUITest {
1617
@Before
1718
public void startUpTheIDE() throws Exception {
1819
System.setProperty("mrj.version", "whynot"); //makes sense only on osx. See https://github.com/alexruiz/fest-swing-1.x/issues/2#issuecomment-86532042
20+
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
1921

2022
FailOnThreadViolationRepaintManager.install();
2123

@@ -25,7 +27,7 @@ public void startUpTheIDE() throws Exception {
2527
Theme.init();
2628
Base.getPlatform().setLookAndFeel();
2729
Base.untitledFolder = Base.createTempFolder("untitled");
28-
Base.untitledFolder.deleteOnExit();
30+
DeleteFilesOnShutdown.add(Base.untitledFolder);
2931

3032
window = GuiActionRunner.execute(new GuiQuery<ArduinoFrameFixture>() {
3133
@Override
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package processing.app;
22

3+
import cc.arduino.files.DeleteFilesOnShutdown;
34
import org.junit.Before;
45

56
public abstract class AbstractWithPreferencesTest {
67

78
@Before
89
public void init() throws Exception {
10+
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
911
Base.initPlatform();
1012
Preferences.init(null);
1113
Theme.init();
1214

1315
Base.untitledFolder = Base.createTempFolder("untitled");
14-
Base.untitledFolder.deleteOnExit();
15-
16+
DeleteFilesOnShutdown.add(Base.untitledFolder);
1617
}
18+
1719
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cc.arduino.files;
2+
3+
import processing.app.PreferencesData;
4+
import processing.app.helpers.FileUtils;
5+
6+
import java.io.File;
7+
import java.util.Collections;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
public class DeleteFilesOnShutdown implements Runnable {
12+
13+
public static final DeleteFilesOnShutdown INSTANCE = new DeleteFilesOnShutdown();
14+
15+
public static void add(File file) {
16+
INSTANCE.addFile(file);
17+
}
18+
19+
private final List<File> files;
20+
21+
public DeleteFilesOnShutdown() {
22+
this.files = new LinkedList<File>();
23+
}
24+
25+
public synchronized void addFile(File file) {
26+
this.files.add(file);
27+
}
28+
29+
@Override
30+
public void run() {
31+
boolean preserveTempFiles = PreferencesData.getBoolean("runtime.preserve.temp.files");
32+
if (preserveTempFiles) {
33+
return;
34+
}
35+
List<File> copyOfFiles;
36+
synchronized (this) {
37+
copyOfFiles = new LinkedList<File>(files);
38+
}
39+
Collections.reverse(copyOfFiles);
40+
for (File file : copyOfFiles) {
41+
if (file.exists() && file.canWrite()) {
42+
FileUtils.recursiveDelete(file);
43+
}
44+
}
45+
}
46+
47+
}

arduino-core/src/processing/app/BaseNoGui.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package processing.app;
22

33
import cc.arduino.contributions.libraries.LibrariesIndexer;
4+
import cc.arduino.files.DeleteFilesOnShutdown;
45
import cc.arduino.packages.DiscoveryManager;
56
import cc.arduino.packages.Uploader;
67
import cc.arduino.contributions.packages.ContributedTool;
78
import cc.arduino.contributions.packages.ContributionsIndexer;
8-
import cc.arduino.utils.ArchiveExtractor;
99
import org.apache.commons.logging.impl.LogFactoryImpl;
1010
import org.apache.commons.logging.impl.NoOpLog;
1111
import processing.app.debug.Compiler;
@@ -133,7 +133,7 @@ static public File getBuildFolder() {
133133
//File folder = new File(getTempFolder(), "build");
134134
//if (!folder.exists()) folder.mkdirs();
135135
buildFolder = createTempFolder("build");
136-
buildFolder.deleteOnExit();
136+
DeleteFilesOnShutdown.add(buildFolder);
137137
}
138138
}
139139
return buildFolder;
@@ -703,6 +703,8 @@ static public void main(String args[]) throws Exception {
703703
if (args.length == 0)
704704
showError(_("No parameters"), _("No command line parameters found"), null);
705705

706+
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
707+
706708
initPlatform();
707709

708710
initPortableFolder();

arduino-core/src/processing/app/helpers/CommandlineParser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private enum ACTION {
3636
private boolean doVerboseBuild = false;
3737
private boolean doVerboseUpload = false;
3838
private boolean doUseProgrammer = false;
39+
private boolean preserveTempFiles;
3940
private boolean noUploadPort = false;
4041
private boolean forceSavePrefs = false;
4142
private String getPref;
@@ -105,6 +106,12 @@ private void parseArguments(String[] args) {
105106
action = ACTION.NOOP;
106107
continue;
107108
}
109+
if (args[i].equals("--preserve-temp-files")) {
110+
preserveTempFiles = true;
111+
if (action == ACTION.GUI)
112+
action = ACTION.NOOP;
113+
continue;
114+
}
108115
if (args[i].equals("--verbose-build")) {
109116
doVerboseBuild = true;
110117
if (action == ACTION.GUI)
@@ -330,4 +337,8 @@ public String getBoardToInstall() {
330337
public String getLibraryToInstall() {
331338
return libraryToInstall;
332339
}
340+
341+
public boolean isPreserveTempFiles() {
342+
return preserveTempFiles;
343+
}
333344
}

build/shared/manpage.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SYNOPSIS
2525
--------
2626
*arduino* ['FILE.ino'...]
2727

28-
*arduino* [*--verify*|*--upload*] [*--board* __package__:__arch__:__board__[:__parameters__]] [*--port* __portname__] [*--pref* __name__=__value__] [*-v*|*--verbose*] [__FILE.ino__]
28+
*arduino* [*--verify*|*--upload*] [*--board* __package__:__arch__:__board__[:__parameters__]] [*--port* __portname__] [*--pref* __name__=__value__] [*-v*|*--verbose*] [--preserve-temp-files] [__FILE.ino__]
2929

3030
*arduino* [*--get-pref* __preference__]
3131

@@ -117,6 +117,10 @@ OPTIONS
117117
verbose mode during build is *disabled* regardless of the current
118118
preferences.
119119

120+
*--preserve-temp-files*::
121+
Keep temporary files (preprocessed sketch, object files...) after termination.
122+
If omitted, temporary files are deleted.
123+
120124
{empty}::
121125
This option is only valid together with *--verify* or
122126
*--upload*.

0 commit comments

Comments
 (0)