Skip to content

Commit 5b84aef

Browse files
author
Federico Fissore
committed
Build path is now a function of sketch path. This allows to recycle previously
compiled files even when working with different sketches at the same time. In such cases, recompiling is way faster
1 parent c3c59f2 commit 5b84aef

File tree

8 files changed

+46
-80
lines changed

8 files changed

+46
-80
lines changed

app/src/processing/app/Base.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static public void guardedMain(String args[]) throws Exception {
217217
}
218218

219219
// Create a location for untitled sketches
220-
untitledFolder = BaseNoGui.createTempFolder("untitled");
220+
untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp");
221221
DeleteFilesOnShutdown.add(untitledFolder);
222222

223223
INSTANCE = new Base(args);

app/src/processing/app/EditorHeader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.awt.*;
3030
import java.awt.event.*;
31+
import java.io.IOException;
3132

3233
import javax.swing.*;
3334

@@ -317,8 +318,12 @@ public void actionPerformed(ActionEvent e) {
317318

318319
item = new JMenuItem(tr("Delete"));
319320
item.addActionListener(new ActionListener() {
320-
public void actionPerformed(ActionEvent e) {
321-
editor.getSketch().handleDeleteCode();
321+
public void actionPerformed(ActionEvent event) {
322+
try {
323+
editor.getSketch().handleDeleteCode();
324+
} catch (IOException e) {
325+
e.printStackTrace();
326+
}
322327
}
323328
});
324329
menu.add(item);

app/src/processing/app/Sketch.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
* Stores information about files in the current sketch
5858
*/
5959
public class Sketch {
60-
static private File tempBuildFolder;
61-
6260
private final Editor editor;
6361

6462
/** true if any of the files have been modified. */
@@ -76,26 +74,6 @@ public class Sketch {
7674
public Sketch(Editor _editor, File file) throws IOException {
7775
editor = _editor;
7876
data = new SketchData(file);
79-
80-
// lib/build must exist when the application is started
81-
// it is added to the CLASSPATH by default, but if it doesn't
82-
// exist when the application is started, then java will remove
83-
// the entry from the CLASSPATH, causing Runner to fail.
84-
//
85-
/*
86-
tempBuildFolder = new File(TEMP_BUILD_PATH);
87-
if (!tempBuildFolder.exists()) {
88-
tempBuildFolder.mkdirs();
89-
Base.showError("Required folder missing",
90-
"A required folder was missing from \n" +
91-
"from your installation of Processing.\n" +
92-
"It has now been replaced, please restart \n" +
93-
"the application to complete the repair.", null);
94-
}
95-
*/
96-
tempBuildFolder = BaseNoGui.getBuildFolder();
97-
//Base.addBuildFolderToClassPath();
98-
9977
load();
10078
}
10179

@@ -439,7 +417,7 @@ protected void nameCode(String newName) {
439417
/**
440418
* Remove a piece of code from the sketch and from the disk.
441419
*/
442-
public void handleDeleteCode() {
420+
public void handleDeleteCode() throws IOException {
443421
editor.status.clearState();
444422
// make sure the user didn't hide the sketch folder
445423
ensureExistence();
@@ -485,7 +463,7 @@ public void handleDeleteCode() {
485463

486464
} else {
487465
// delete the file
488-
if (!current.getCode().deleteFile(tempBuildFolder)) {
466+
if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data))) {
489467
Base.showMessage(tr("Couldn't do it"),
490468
I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName()));
491469
return;
@@ -1102,7 +1080,7 @@ public void prepare() throws IOException {
11021080
* @throws RunnerException
11031081
*/
11041082
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
1105-
return build(tempBuildFolder.getAbsolutePath(), verbose, save);
1083+
return build(BaseNoGui.getBuildFolder(data).getAbsolutePath(), verbose, save);
11061084
}
11071085

11081086
/**
@@ -1143,7 +1121,7 @@ private String saveSketchInTempFolder() throws IOException {
11431121
}
11441122

11451123
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
1146-
return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer);
1124+
return exportApplet(BaseNoGui.getBuildFolder(data).getAbsolutePath(), usingProgrammer);
11471125
}
11481126

11491127

app/test/processing/app/AbstractGUITest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.junit.After;
3737
import org.junit.Before;
3838
import processing.app.helpers.ArduinoFrameFixture;
39+
import processing.app.helpers.FileUtils;
3940

4041
import javax.swing.*;
42+
import java.util.Random;
4143

4244
public abstract class AbstractGUITest {
4345

@@ -56,7 +58,7 @@ public void startUpTheIDE() throws Exception {
5658
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
5759
Theme.init();
5860
BaseNoGui.getPlatform().setLookAndFeel();
59-
Base.untitledFolder = BaseNoGui.createTempFolder("untitled");
61+
Base.untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp");
6062
DeleteFilesOnShutdown.add(Base.untitledFolder);
6163

6264
window = GuiActionRunner.execute(new GuiQuery<ArduinoFrameFixture>() {

app/test/processing/app/AbstractWithPreferencesTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
import cc.arduino.files.DeleteFilesOnShutdown;
3333
import org.junit.Before;
34+
import processing.app.helpers.FileUtils;
35+
36+
import java.util.Random;
3437

3538
public abstract class AbstractWithPreferencesTest {
3639

@@ -44,7 +47,7 @@ public void init() throws Exception {
4447

4548
BaseNoGui.initPackages();
4649

47-
Base.untitledFolder = BaseNoGui.createTempFolder("untitled");
50+
Base.untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp");
4851
DeleteFilesOnShutdown.add(Base.untitledFolder);
4952
}
5053

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

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import cc.arduino.packages.DiscoveryManager;
1313
import cc.arduino.packages.Uploader;
1414
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import org.apache.commons.codec.digest.DigestUtils;
1516
import org.apache.commons.compress.utils.IOUtils;
1617
import org.apache.commons.logging.impl.LogFactoryImpl;
1718
import org.apache.commons.logging.impl.NoOpLog;
@@ -27,6 +28,7 @@
2728
import java.io.FileOutputStream;
2829
import java.io.FileWriter;
2930
import java.io.IOException;
31+
import java.nio.file.Files;
3032
import java.util.*;
3133
import java.util.logging.Level;
3234
import java.util.logging.Logger;
@@ -59,8 +61,6 @@ public class BaseNoGui {
5961
VERSION_NAME_LONG = versionNameLong;
6062
}
6163

62-
static File buildFolder;
63-
6464
private static DiscoveryManager discoveryManager = new DiscoveryManager();
6565

6666
// these are static because they're used by Sketch
@@ -111,28 +111,6 @@ static public int countLines(String what) {
111111
return count;
112112
}
113113

114-
/**
115-
* Get the path to the platform's temporary folder, by creating
116-
* a temporary temporary file and getting its parent folder.
117-
* <br/>
118-
* Modified for revision 0094 to actually make the folder randomized
119-
* to avoid conflicts in multi-user environments. (Bug 177)
120-
*/
121-
static public File createTempFolder(String name) {
122-
try {
123-
File folder = File.createTempFile(name, null);
124-
//String tempPath = ignored.getParent();
125-
//return new File(tempPath);
126-
folder.delete();
127-
folder.mkdirs();
128-
return folder;
129-
130-
} catch (Exception e) {
131-
e.printStackTrace();
132-
}
133-
return null;
134-
}
135-
136114
static public String getAvrBasePath() {
137115
String path = getHardwarePath() + File.separator + "tools" +
138116
File.separator + "avr" + File.separator + "bin" + File.separator;
@@ -142,19 +120,14 @@ static public String getAvrBasePath() {
142120
return path;
143121
}
144122

145-
static public File getBuildFolder() {
146-
if (buildFolder == null) {
147-
String buildPath = PreferencesData.get("build.path");
148-
if (buildPath != null) {
149-
buildFolder = absoluteFile(buildPath);
150-
if (!buildFolder.exists())
151-
buildFolder.mkdirs();
152-
} else {
153-
//File folder = new File(getTempFolder(), "build");
154-
//if (!folder.exists()) folder.mkdirs();
155-
buildFolder = createTempFolder("build");
156-
DeleteFilesOnShutdown.add(buildFolder);
157-
}
123+
static public File getBuildFolder(SketchData data) throws IOException {
124+
File buildFolder;
125+
if (PreferencesData.get("build.path") != null) {
126+
buildFolder = absoluteFile(PreferencesData.get("build.path"));
127+
Files.createDirectories(buildFolder.toPath());
128+
} else {
129+
buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(data.getMainFilePath()) + ".tmp");
130+
DeleteFilesOnShutdown.add(buildFolder);
158131
}
159132
return buildFolder;
160133
}
@@ -495,7 +468,7 @@ static public void init(String[] args) throws Exception {
495468
// File tempBuildFolder = getBuildFolder();
496469
// data.load();
497470
SketchData data = new SketchData(absoluteFile(parser.getFilenames().get(0)));
498-
File tempBuildFolder = getBuildFolder();
471+
File tempBuildFolder = getBuildFolder(data);
499472
data.load();
500473

501474
// Sketch.exportApplet()
@@ -542,7 +515,7 @@ static public void init(String[] args) throws Exception {
542515
// File tempBuildFolder = getBuildFolder();
543516
// data.load();
544517
SketchData data = new SketchData(absoluteFile(path));
545-
File tempBuildFolder = getBuildFolder();
518+
File tempBuildFolder = getBuildFolder(data);
546519
data.load();
547520

548521
// Sketch.prepare() calls Sketch.ensureExistence()
@@ -1109,10 +1082,6 @@ public static void selectSerialPort(String port) {
11091082
PreferencesData.set("serial.port.file", portFile);
11101083
}
11111084

1112-
public static void setBuildFolder(File newBuildFolder) {
1113-
buildFolder = newBuildFolder;
1114-
}
1115-
11161085
static public void showError(String title, String message, int exit_code) {
11171086
showError(title, message, null, exit_code);
11181087
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,26 @@ protected boolean deleteFile(File tempBuildFolder) {
9797
return false;
9898
}
9999

100-
File[] compiledFiles = tempBuildFolder.listFiles(new FileFilter() {
101-
public boolean accept(File pathname) {
102-
return pathname.getName().startsWith(getFileName());
103-
}
100+
if (!deleteCompiledFilesFrom(tempBuildFolder)) {
101+
return false;
102+
}
103+
104+
if (!deleteCompiledFilesFrom(new File(tempBuildFolder, "sketch"))) {
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
111+
private boolean deleteCompiledFilesFrom(File tempBuildFolder) {
112+
File[] compiledFiles = tempBuildFolder.listFiles(pathname -> {
113+
return pathname.getName().startsWith(getFileName());
104114
});
105115
for (File compiledFile : compiledFiles) {
106116
if (!compiledFile.delete()) {
107117
return false;
108118
}
109119
}
110-
111120
return true;
112121
}
113122

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void parseArgumentsPhase1() {
161161
if (!buildFolder.isDirectory()) {
162162
BaseNoGui.showError(null, "The build path is not a folder", 3);
163163
}
164-
BaseNoGui.setBuildFolder(buildFolder);
164+
PreferencesData.set("build.path", buildFolder.getAbsolutePath());
165165
continue;
166166
}
167167
if (args[i].equals("--pref")) {

0 commit comments

Comments
 (0)