Skip to content

Commit dfe842d

Browse files
Store the build path used in Sketch
Previously, everywhere where it was needed, the path was requested from BaseNoGui. Because the path is based on a hash of the sketch filename, every caller would get the same path for the same sketch. However, it makes more sense to store the path used for a given sketch inside the Sketch object. This prevents having to pass around or regenerate the build path everywhere, and no longer requires the build path to be deterministic (though it still is in this commit). This allows removing some methods and constructors of which two versions were available - one with a build path argument and one without.
1 parent 39dea53 commit dfe842d

File tree

5 files changed

+50
-78
lines changed

5 files changed

+50
-78
lines changed

app/src/processing/app/SketchController.java

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void handleDeleteCode() throws IOException {
254254
editor.base.handleClose(editor);
255255
} else {
256256
// delete the file
257-
if (!current.delete(BaseNoGui.getBuildFolder(sketch).toPath())) {
257+
if (!current.delete(sketch.getBuildPath().toPath())) {
258258
Base.showMessage(tr("Couldn't do it"),
259259
I18n.format(tr("Could not delete \"{0}\"."), current.getFileName()));
260260
return;
@@ -615,43 +615,6 @@ private void importLibrary(File jarPath) throws IOException {
615615
editor.getCurrentTab().setSelection(0, 0); // scroll to start
616616
}
617617

618-
/**
619-
* Preprocess, Compile, and Run the current code.
620-
* <P>
621-
* There are three main parts to this process:
622-
* <PRE>
623-
* (0. if not java, then use another 'engine'.. i.e. python)
624-
*
625-
* 1. do the p5 language preprocessing
626-
* this creates a working .java file in a specific location
627-
* better yet, just takes a chunk of java code and returns a
628-
* new/better string editor can take care of saving this to a
629-
* file location
630-
*
631-
* 2. compile the code from that location
632-
* catching errors along the way
633-
* placing it in a ready classpath, or .. ?
634-
*
635-
* 3. run the code
636-
* needs to communicate location for window
637-
* and maybe setup presentation space as well
638-
* run externally if a code folder exists,
639-
* or if more than one file is in the project
640-
*
641-
* X. afterwards, some of these steps need a cleanup function
642-
* </PRE>
643-
*/
644-
//protected String compile() throws RunnerException {
645-
646-
/**
647-
* Run the build inside the temporary build folder.
648-
* @return null if compilation failed, main class name if not
649-
* @throws RunnerException
650-
*/
651-
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
652-
return build(BaseNoGui.getBuildFolder(sketch).getAbsolutePath(), verbose, save);
653-
}
654-
655618
/**
656619
* Preprocess and compile all the code for this sketch.
657620
*
@@ -661,7 +624,7 @@ public String build(boolean verbose, boolean save) throws RunnerException, Prefe
661624
*
662625
* @return null if compilation failed, main class name if not
663626
*/
664-
private String build(String buildPath, boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
627+
public String build(boolean verbose, boolean save) throws RunnerException, PreferencesMapException, IOException {
665628
// run the preprocessor
666629
editor.status.progressUpdate(20);
667630

@@ -679,7 +642,7 @@ private String build(String buildPath, boolean verbose, boolean save) throws Run
679642
}
680643

681644
try {
682-
return new Compiler(pathToSketch, sketch, buildPath).build(progressListener, save);
645+
return new Compiler(pathToSketch, sketch).build(progressListener, save);
683646
} finally {
684647
// Make sure we clean up any temporary sketch copy
685648
if (deleteTemp)
@@ -698,20 +661,13 @@ private File saveSketchInTempFolder() throws IOException {
698661
return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getFileName()).toFile();
699662
}
700663

701-
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
702-
return exportApplet(BaseNoGui.getBuildFolder(sketch).getAbsolutePath(), usingProgrammer);
703-
}
704-
705-
706664
/**
707665
* Handle export to applet.
708666
*/
709-
private boolean exportApplet(String appletPath, boolean usingProgrammer)
710-
throws Exception {
711-
667+
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
712668
// build the sketch
713669
editor.status.progressNotice(tr("Compiling sketch..."));
714-
String foundName = build(appletPath, false, false);
670+
String foundName = build(false, false);
715671
// (already reported) error during export, exit this function
716672
if (foundName == null) return false;
717673

@@ -725,12 +681,12 @@ private boolean exportApplet(String appletPath, boolean usingProgrammer)
725681
// }
726682

727683
editor.status.progressNotice(tr("Uploading..."));
728-
boolean success = upload(appletPath, foundName, usingProgrammer);
684+
boolean success = upload(foundName, usingProgrammer);
729685
editor.status.progressUpdate(100);
730686
return success;
731687
}
732688

733-
private boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws Exception {
689+
private boolean upload(String suggestedClassName, boolean usingProgrammer) throws Exception {
734690

735691
Uploader uploader = new UploaderUtils().getUploaderByPreferences(false);
736692

@@ -751,7 +707,7 @@ private boolean upload(String buildPath, String suggestedClassName, boolean usin
751707

752708
List<String> warningsAccumulator = new LinkedList<>();
753709
try {
754-
success = new UploaderUtils().upload(sketch, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
710+
success = new UploaderUtils().upload(sketch, uploader, suggestedClassName, usingProgrammer, false, warningsAccumulator);
755711
} finally {
756712
if (uploader.requiresAuthorization() && !success) {
757713
PreferencesData.remove(uploader.getAuthorizationKey());

arduino-core/src/cc/arduino/Compiler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,23 @@ enum BuilderAction {
110110

111111
private final File pathToSketch;
112112
private final Sketch sketch;
113-
private final String buildPath;
113+
private String buildPath;
114114
private final boolean verbose;
115115
private RunnerException exception;
116116

117-
public Compiler(Sketch data, String buildPath) {
118-
this(data.getPrimaryFile().getFile(), data, buildPath);
117+
public Compiler(Sketch data) {
118+
this(data.getPrimaryFile().getFile(), data);
119119
}
120120

121-
public Compiler(File pathToSketch, Sketch sketch, String buildPath) {
121+
public Compiler(File pathToSketch, Sketch sketch) {
122122
this.pathToSketch = pathToSketch;
123123
this.sketch = sketch;
124-
this.buildPath = buildPath;
125124
this.verbose = PreferencesData.getBoolean("build.verbose");
126125
}
127126

128127
public String build(CompilerProgressListener progListener, boolean exportHex) throws RunnerException, PreferencesMapException, IOException {
128+
this.buildPath = sketch.getBuildPath().getAbsolutePath();
129+
129130
TargetBoard board = BaseNoGui.getTargetBoard();
130131
if (board == null) {
131132
throw new RunnerException("Board is not selected");

arduino-core/src/cc/arduino/UploaderUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Uploader getUploaderByPreferences(boolean noUploadPort) {
5656
return new UploaderFactory().newUploader(target.getBoards().get(board), boardPort, noUploadPort);
5757
}
5858

59-
public boolean upload(Sketch data, Uploader uploader, String buildPath, String suggestedClassName, boolean usingProgrammer, boolean noUploadPort, List<String> warningsAccumulator) throws Exception {
59+
public boolean upload(Sketch data, Uploader uploader, String suggestedClassName, boolean usingProgrammer, boolean noUploadPort, List<String> warningsAccumulator) throws Exception {
6060

6161
if (uploader == null)
6262
uploader = getUploaderByPreferences(noUploadPort);
@@ -75,7 +75,7 @@ public boolean upload(Sketch data, Uploader uploader, String buildPath, String s
7575
}
7676

7777
try {
78-
success = uploader.uploadUsingPreferences(data.getFolder(), buildPath, suggestedClassName, usingProgrammer, warningsAccumulator);
78+
success = uploader.uploadUsingPreferences(data.getFolder(), data.getBuildPath().getAbsolutePath(), suggestedClassName, usingProgrammer, warningsAccumulator);
7979
} finally {
8080
if (uploader.requiresAuthorization() && !success) {
8181
PreferencesData.remove(uploader.getAuthorizationKey());

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
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;
1615
import org.apache.commons.compress.utils.IOUtils;
1716
import org.apache.commons.logging.impl.LogFactoryImpl;
1817
import org.apache.commons.logging.impl.NoOpLog;
@@ -28,7 +27,6 @@
2827
import java.io.FileOutputStream;
2928
import java.io.FileWriter;
3029
import java.io.IOException;
31-
import java.nio.file.Files;
3230
import java.util.*;
3331
import java.util.logging.Level;
3432
import java.util.logging.Logger;
@@ -121,18 +119,6 @@ static public String getAvrBasePath() {
121119
return path;
122120
}
123121

124-
static public File getBuildFolder(Sketch data) throws IOException {
125-
File buildFolder;
126-
if (PreferencesData.get("build.path") != null) {
127-
buildFolder = absoluteFile(PreferencesData.get("build.path"));
128-
Files.createDirectories(buildFolder.toPath());
129-
} else {
130-
buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(data.getMainFilePath()) + ".tmp");
131-
DeleteFilesOnShutdown.add(buildFolder);
132-
}
133-
return buildFolder;
134-
}
135-
136122
static public PreferencesMap getBoardPreferences() {
137123
TargetBoard board = getTargetBoard();
138124
if (board == null)
@@ -472,7 +458,6 @@ static public void init(String[] args) throws Exception {
472458
// SketchData data = new SketchData(file);
473459
// File tempBuildFolder = getBuildFolder();
474460
Sketch data = new Sketch(absoluteFile(parser.getFilenames().get(0)));
475-
File tempBuildFolder = getBuildFolder(data);
476461

477462
// Sketch.exportApplet()
478463
// - calls Sketch.prepare() that calls Sketch.ensureExistence()
@@ -481,7 +466,7 @@ static public void init(String[] args) throws Exception {
481466
if (!data.getFolder().exists()) {
482467
showError(tr("No sketch"), tr("Can't find the sketch in the specified path"), null);
483468
}
484-
String suggestedClassName = new Compiler(data, tempBuildFolder.getAbsolutePath()).build(null, false);
469+
String suggestedClassName = new Compiler(data).build(null, false);
485470
if (suggestedClassName == null) {
486471
showError(tr("Error while verifying"), tr("An error occurred while verifying the sketch"), null);
487472
}
@@ -490,7 +475,7 @@ static public void init(String[] args) throws Exception {
490475
Uploader uploader = new UploaderUtils().getUploaderByPreferences(parser.isNoUploadPort());
491476
if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) showError("...", "...", null);
492477
try {
493-
success = new UploaderUtils().upload(data, uploader, tempBuildFolder.getAbsolutePath(), suggestedClassName, parser.isDoUseProgrammer(), parser.isNoUploadPort(), warningsAccumulator);
478+
success = new UploaderUtils().upload(data, uploader, suggestedClassName, parser.isDoUseProgrammer(), parser.isNoUploadPort(), warningsAccumulator);
494479
showMessage(tr("Done uploading"), tr("Done uploading"));
495480
} finally {
496481
if (uploader.requiresAuthorization() && !success) {
@@ -518,15 +503,14 @@ static public void init(String[] args) throws Exception {
518503
// File tempBuildFolder = getBuildFolder();
519504
// data.load();
520505
Sketch data = new Sketch(absoluteFile(path));
521-
File tempBuildFolder = getBuildFolder(data);
522506

523507
// Sketch.prepare() calls Sketch.ensureExistence()
524508
// Sketch.build(verbose) calls Sketch.ensureExistence() and set progressListener and, finally, calls Compiler.build()
525509
// This translates here as:
526510
// if (!data.getFolder().exists()) showError(...);
527511
// String ... = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, verbose);
528512
if (!data.getFolder().exists()) showError(tr("No sketch"), tr("Can't find the sketch in the specified path"), null);
529-
String suggestedClassName = new Compiler(data, tempBuildFolder.getAbsolutePath()).build(null, false);
513+
String suggestedClassName = new Compiler(data).build(null, false);
530514
if (suggestedClassName == null) showError(tr("Error while verifying"), tr("An error occurred while verifying the sketch"), null);
531515
showMessage(tr("Done compiling"), tr("Done compiling"));
532516
} catch (Exception e) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.file.Files;
56
import java.util.*;
67
import java.util.stream.Collectors;
78
import java.util.stream.Stream;
89

10+
import org.apache.commons.codec.digest.DigestUtils;
11+
12+
import cc.arduino.files.DeleteFilesOnShutdown;
913
import processing.app.helpers.FileUtils;
1014

1115
import static processing.app.I18n.tr;
@@ -27,6 +31,8 @@ public class Sketch {
2731

2832
private List<SketchFile> files = new ArrayList<SketchFile>();
2933

34+
private File buildPath;
35+
3036
private static final Comparator<SketchFile> CODE_DOCS_COMPARATOR = new Comparator<SketchFile>() {
3137
@Override
3238
public int compare(SketchFile x, SketchFile y) {
@@ -161,6 +167,31 @@ public SketchFile getFile(int i) {
161167
return files.get(i);
162168
}
163169

170+
/**
171+
* Gets the build path for this sketch. The first time this is called,
172+
* a build path is generated and created and the same path is returned
173+
* on all subsequent calls.
174+
*
175+
* This takes into account the build.path preference. If it is set,
176+
* that path is always returned, and the directory is *not* deleted on
177+
* shutdown. If the preference is not set, a pathname in a
178+
* temporary directory is generated, which is automatically deleted on
179+
* shutdown.
180+
*/
181+
public File getBuildPath() throws IOException {
182+
if (buildPath == null) {
183+
if (PreferencesData.get("build.path") != null) {
184+
buildPath = BaseNoGui.absoluteFile(PreferencesData.get("build.path"));
185+
Files.createDirectories(buildPath.toPath());
186+
} else {
187+
buildPath = FileUtils.createTempFolder("build", DigestUtils.md5Hex(getMainFilePath()) + ".tmp");
188+
DeleteFilesOnShutdown.add(buildPath);
189+
}
190+
}
191+
192+
return buildPath;
193+
}
194+
164195
protected void removeFile(SketchFile which) {
165196
if (!files.remove(which))
166197
System.err.println("removeCode: internal error.. could not find code");

0 commit comments

Comments
 (0)