Skip to content

Commit 4f0af2a

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 c4e77a7 commit 4f0af2a

File tree

5 files changed

+50
-78
lines changed

5 files changed

+50
-78
lines changed

app/src/processing/app/SketchController.java

+8-52
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void handleDeleteCode() throws IOException {
253253
editor.base.handleClose(editor);
254254
} else {
255255
// delete the file
256-
if (!current.delete(BaseNoGui.getBuildFolder(sketch).toPath())) {
256+
if (!current.delete(sketch.getBuildPath().toPath())) {
257257
Base.showMessage(tr("Couldn't do it"),
258258
I18n.format(tr("Could not delete \"{0}\"."), current.getFileName()));
259259
return;
@@ -614,43 +614,6 @@ public void importLibrary(UserLibrary lib) throws IOException {
614614
editor.getCurrentTab().setSelection(0, 0); // scroll to start
615615
}
616616

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

@@ -678,7 +641,7 @@ private String build(String buildPath, boolean verbose, boolean save) throws Run
678641
}
679642

680643
try {
681-
return new Compiler(pathToSketch, sketch, buildPath).build(progressListener, save);
644+
return new Compiler(pathToSketch, sketch).build(progressListener, save);
682645
} finally {
683646
// Make sure we clean up any temporary sketch copy
684647
if (deleteTemp)
@@ -697,20 +660,13 @@ private File saveSketchInTempFolder() throws IOException {
697660
return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getFileName()).toFile();
698661
}
699662

700-
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
701-
return exportApplet(BaseNoGui.getBuildFolder(sketch).getAbsolutePath(), usingProgrammer);
702-
}
703-
704-
705663
/**
706664
* Handle export to applet.
707665
*/
708-
private boolean exportApplet(String appletPath, boolean usingProgrammer)
709-
throws Exception {
710-
666+
protected boolean exportApplet(boolean usingProgrammer) throws Exception {
711667
// build the sketch
712668
editor.status.progressNotice(tr("Compiling sketch..."));
713-
String foundName = build(appletPath, false, false);
669+
String foundName = build(false, false);
714670
// (already reported) error during export, exit this function
715671
if (foundName == null) return false;
716672

@@ -724,12 +680,12 @@ private boolean exportApplet(String appletPath, boolean usingProgrammer)
724680
// }
725681

726682
editor.status.progressNotice(tr("Uploading..."));
727-
boolean success = upload(appletPath, foundName, usingProgrammer);
683+
boolean success = upload(foundName, usingProgrammer);
728684
editor.status.progressUpdate(100);
729685
return success;
730686
}
731687

732-
private boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws Exception {
688+
private boolean upload(String suggestedClassName, boolean usingProgrammer) throws Exception {
733689

734690
UploaderUtils uploaderInstance = new UploaderUtils();
735691
Uploader uploader = uploaderInstance.getUploaderByPreferences(false);
@@ -751,7 +707,7 @@ private boolean upload(String buildPath, String suggestedClassName, boolean usin
751707

752708
List<String> warningsAccumulator = new LinkedList<>();
753709
try {
754-
success = uploaderInstance.upload(sketch, uploader, buildPath, suggestedClassName, usingProgrammer, false, warningsAccumulator);
710+
success = uploaderInstance.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

+6-5
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,23 @@ enum BuilderAction {
112112

113113
private final File pathToSketch;
114114
private final Sketch sketch;
115-
private final String buildPath;
115+
private String buildPath;
116116
private final boolean verbose;
117117
private RunnerException exception;
118118

119-
public Compiler(Sketch data, String buildPath) {
120-
this(data.getPrimaryFile().getFile(), data, buildPath);
119+
public Compiler(Sketch data) {
120+
this(data.getPrimaryFile().getFile(), data);
121121
}
122122

123-
public Compiler(File pathToSketch, Sketch sketch, String buildPath) {
123+
public Compiler(File pathToSketch, Sketch sketch) {
124124
this.pathToSketch = pathToSketch;
125125
this.sketch = sketch;
126-
this.buildPath = buildPath;
127126
this.verbose = PreferencesData.getBoolean("build.verbose");
128127
}
129128

130129
public String build(CompilerProgressListener progListener, boolean exportHex) throws RunnerException, PreferencesMapException, IOException {
130+
this.buildPath = sketch.getBuildPath().getAbsolutePath();
131+
131132
TargetBoard board = BaseNoGui.getTargetBoard();
132133
if (board == null) {
133134
throw new RunnerException("Board is not selected");

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

+2-2
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

+3-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import cc.arduino.packages.DiscoveryManager;
1515
import cc.arduino.packages.Uploader;
1616
import com.fasterxml.jackson.core.JsonProcessingException;
17-
import org.apache.commons.codec.digest.DigestUtils;
1817
import org.apache.commons.compress.utils.IOUtils;
1918
import org.apache.commons.logging.impl.LogFactoryImpl;
2019
import org.apache.commons.logging.impl.NoOpLog;
@@ -29,7 +28,6 @@
2928
import java.io.File;
3029
import java.io.FileWriter;
3130
import java.io.IOException;
32-
import java.nio.file.Files;
3331
import java.util.*;
3432
import java.util.logging.Level;
3533
import java.util.logging.Logger;
@@ -124,18 +122,6 @@ static public String getAvrBasePath() {
124122
return path;
125123
}
126124

127-
static public File getBuildFolder(Sketch data) throws IOException {
128-
File buildFolder;
129-
if (PreferencesData.get("build.path") != null) {
130-
buildFolder = absoluteFile(PreferencesData.get("build.path"));
131-
Files.createDirectories(buildFolder.toPath());
132-
} else {
133-
buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(data.getMainFilePath()) + ".tmp");
134-
DeleteFilesOnShutdown.add(buildFolder);
135-
}
136-
return buildFolder;
137-
}
138-
139125
static public PreferencesMap getBoardPreferences() {
140126
TargetBoard board = getTargetBoard();
141127
if (board == null)
@@ -508,7 +494,6 @@ static public void init(String[] args) throws Exception {
508494
// SketchData data = new SketchData(file);
509495
// File tempBuildFolder = getBuildFolder();
510496
Sketch data = new Sketch(absoluteFile(parser.getFilenames().get(0)));
511-
File tempBuildFolder = getBuildFolder(data);
512497

513498
// Sketch.exportApplet()
514499
// - calls Sketch.prepare() that calls Sketch.ensureExistence()
@@ -517,7 +502,7 @@ static public void init(String[] args) throws Exception {
517502
if (!data.getFolder().exists()) {
518503
showError(tr("No sketch"), tr("Can't find the sketch in the specified path"), null);
519504
}
520-
String suggestedClassName = new Compiler(data, tempBuildFolder.getAbsolutePath()).build(null, false);
505+
String suggestedClassName = new Compiler(data).build(null, false);
521506
if (suggestedClassName == null) {
522507
showError(tr("Error while verifying"), tr("An error occurred while verifying the sketch"), null);
523508
}
@@ -526,7 +511,7 @@ static public void init(String[] args) throws Exception {
526511
Uploader uploader = new UploaderUtils().getUploaderByPreferences(parser.isNoUploadPort());
527512
if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) showError("...", "...", null);
528513
try {
529-
success = new UploaderUtils().upload(data, uploader, tempBuildFolder.getAbsolutePath(), suggestedClassName, parser.isDoUseProgrammer(), parser.isNoUploadPort(), warningsAccumulator);
514+
success = new UploaderUtils().upload(data, uploader, suggestedClassName, parser.isDoUseProgrammer(), parser.isNoUploadPort(), warningsAccumulator);
530515
showMessage(tr("Done uploading"), tr("Done uploading"));
531516
} finally {
532517
if (uploader.requiresAuthorization() && !success) {
@@ -554,15 +539,14 @@ static public void init(String[] args) throws Exception {
554539
// File tempBuildFolder = getBuildFolder();
555540
// data.load();
556541
Sketch data = new Sketch(absoluteFile(path));
557-
File tempBuildFolder = getBuildFolder(data);
558542

559543
// Sketch.prepare() calls Sketch.ensureExistence()
560544
// Sketch.build(verbose) calls Sketch.ensureExistence() and set progressListener and, finally, calls Compiler.build()
561545
// This translates here as:
562546
// if (!data.getFolder().exists()) showError(...);
563547
// String ... = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, verbose);
564548
if (!data.getFolder().exists()) showError(tr("No sketch"), tr("Can't find the sketch in the specified path"), null);
565-
String suggestedClassName = new Compiler(data, tempBuildFolder.getAbsolutePath()).build(null, false);
549+
String suggestedClassName = new Compiler(data).build(null, false);
566550
if (suggestedClassName == null) showError(tr("Error while verifying"), tr("An error occurred while verifying the sketch"), null);
567551
showMessage(tr("Done compiling"), tr("Done compiling"));
568552
} catch (Exception e) {

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

+31
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)