Skip to content

Commit 959fba7

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 86d20b8 commit 959fba7

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
@@ -864,7 +864,7 @@ private String saveSketchInTempFolder() throws IOException {
864864
Files.write(Paths.get(tempFolder.getAbsolutePath(), file.getFileName()), file.getProgram().getBytes());
865865
}
866866

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

870870
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
@@ -157,7 +157,7 @@ public String build(CompilerProgressListener progListener, boolean exportHex) th
157157

158158
size(prefs);
159159

160-
return sketch.getPrimaryFile().getName();
160+
return sketch.getPrimaryFile().getFileName();
161161
}
162162

163163
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(tr("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)