From 959fba78650f8c01f5086882a75cb0aa84a446e1 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Fri, 18 Dec 2015 15:21:30 +0100 Subject: [PATCH] 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. --- app/src/processing/app/SketchController.java | 2 +- arduino-core/src/cc/arduino/Compiler.java | 2 +- arduino-core/src/processing/app/Sketch.java | 18 ++++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 43de0d137..cee9b34dd 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -864,7 +864,7 @@ public class SketchController { Files.write(Paths.get(tempFolder.getAbsolutePath(), file.getFileName()), file.getProgram().getBytes()); } - return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getName()).toString(); + return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getFileName()).toString(); } protected boolean exportApplet(boolean usingProgrammer) throws Exception { diff --git a/arduino-core/src/cc/arduino/Compiler.java b/arduino-core/src/cc/arduino/Compiler.java index 338396d84..440f0f6bf 100644 --- a/arduino-core/src/cc/arduino/Compiler.java +++ b/arduino-core/src/cc/arduino/Compiler.java @@ -157,7 +157,7 @@ public class Compiler implements MessageConsumer { size(prefs); - return sketch.getPrimaryFile().getName(); + return sketch.getPrimaryFile().getFileName(); } private String VIDPID() { diff --git a/arduino-core/src/processing/app/Sketch.java b/arduino-core/src/processing/app/Sketch.java index 5873f4bed..beb2f3c75 100644 --- a/arduino-core/src/processing/app/Sketch.java +++ b/arduino-core/src/processing/app/Sketch.java @@ -20,11 +20,6 @@ public class Sketch { public static final List OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s"); public static final List EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList()); - /** - * main pde file for this sketch. - */ - private File primaryFile; - /** * folder that contains this sketch */ @@ -57,8 +52,6 @@ public class Sketch { * The primary file for this sketch. */ Sketch(File file) throws IOException { - primaryFile = file; - // get the name of the sketch by chopping .pde or .java // off of the main file name String mainFilename = primaryFile.getName(); @@ -122,7 +115,9 @@ public class Sketch { Set result = new TreeSet<>(CODE_DOCS_COMPARATOR); for (File file : FileUtils.listFiles(folder, false, EXTENSIONS)) { if (BaseNoGui.isSanitaryName(file.getName())) { - result.add(new SketchFile(file, file.equals(primaryFile))); + FileUtils.SplitFile split = FileUtils.splitFilename(file); + boolean isPrimary = split.basename.equals(folder.getName()) && SKETCH_EXTENSIONS.contains(split.extension); + result.add(new SketchFile(file, isPrimary)); } else if (showWarnings) { System.err.println(I18n.format(tr("File name {0} is invalid: ignored"), file.getName())); } @@ -165,16 +160,15 @@ public class Sketch { /** * Returns a file object for the primary .pde of this sketch. */ - public File getPrimaryFile() { - return primaryFile; + public SketchFile getPrimaryFile() { + return files.get(0); } /** * Returns path to the main .pde file for this sketch. */ public String getMainFilePath() { - return primaryFile.getAbsolutePath(); - //return code[0].file.getAbsolutePath(); + return getPrimaryFile().getFile().getAbsolutePath(); } public void addFile(SketchFile file) {