Skip to content

Commit cf7498b

Browse files
Let Sketch.getPrimaryFile return a SketchFile
Previously, it returned a File object, which the Sketch separately stored from the primary SketchFile. By letting it just return the SketchFile, and let callers query that for the filename, Sketch does not need to store the File object itself and there is less chance of info getting out of sync.
1 parent b5911c3 commit cf7498b

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

app/src/processing/app/SketchController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ private String saveSketchInTempFolder() throws IOException {
865865
Files.write(Paths.get(tempFolder.getAbsolutePath(), file.getFileName()), file.getProgram().getBytes());
866866
}
867867

868-
return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getName()).toString();
868+
return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getFileName()).toString();
869869
}
870870

871871
protected boolean exportApplet(boolean usingProgrammer) throws Exception {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public String build(CompilerProgressListener progListener, boolean exportHex) th
155155

156156
size(prefs);
157157

158-
return sketch.getPrimaryFile().getName();
158+
return sketch.getPrimaryFile().getFileName();
159159
}
160160

161161
private String VIDPID() {

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ public class Sketch {
2020
public static final List<String> OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s");
2121
public static final List<String> EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList());
2222

23-
/**
24-
* main pde file for this sketch.
25-
*/
26-
private File primaryFile;
27-
2823
/**
2924
* folder that contains this sketch
3025
*/
@@ -57,8 +52,6 @@ public int compare(SketchFile x, SketchFile y) {
5752
* The primary file for this sketch.
5853
*/
5954
Sketch(File file) throws IOException {
60-
primaryFile = file;
61-
6255
// get the name of the sketch by chopping .pde or .java
6356
// off of the main file name
6457
String mainFilename = primaryFile.getName();
@@ -122,7 +115,9 @@ private List<SketchFile> listSketchFiles(boolean showWarnings) throws IOExceptio
122115
Set<SketchFile> result = new TreeSet<>(CODE_DOCS_COMPARATOR);
123116
for (File file : FileUtils.listFiles(folder, false, EXTENSIONS)) {
124117
if (BaseNoGui.isSanitaryName(file.getName())) {
125-
result.add(new SketchFile(file, file.equals(primaryFile)));
118+
FileUtils.SplitFile split = FileUtils.splitFilename(file);
119+
boolean isPrimary = split.basename.equals(folder.getName()) && SKETCH_EXTENSIONS.contains(split.extension);
120+
result.add(new SketchFile(file, isPrimary));
126121
} else if (showWarnings) {
127122
System.err.println(I18n.format("File name {0} is invalid: ignored", file.getName()));
128123
}
@@ -165,16 +160,15 @@ public SketchFile[] getFiles() {
165160
/**
166161
* Returns a file object for the primary .pde of this sketch.
167162
*/
168-
public File getPrimaryFile() {
169-
return primaryFile;
163+
public SketchFile getPrimaryFile() {
164+
return files.get(0);
170165
}
171166

172167
/**
173168
* Returns path to the main .pde file for this sketch.
174169
*/
175170
public String getMainFilePath() {
176-
return primaryFile.getAbsolutePath();
177-
//return code[0].file.getAbsolutePath();
171+
return getPrimaryFile().getFile().getAbsolutePath();
178172
}
179173

180174
public void addFile(SketchFile file) {

0 commit comments

Comments
 (0)