Skip to content

Commit 47f5de8

Browse files
Clean up pde to ino renaming
This makes a few related changes: - `FileUtils.replaceExtension()` is introduced to handle replacing the .pde extension with .ino. - Instead of iterating .pde files on disk, this iterates SketchFiles in memory, saving another lookup from filename -> SketchFile later. - `SketchController.renameCodeToInoExtension()` is removed. Now it no longer needs to look up the SketchFile and FileUtils handles the extension replacement, this method did not have any reason to exist anymore. - Instead of hardcoding the .pde extension, a new Sketch.OLD_SKETCH_EXTENSIONS constant is introduced.
1 parent 7fafad4 commit 47f5de8

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

app/src/processing/app/SketchController.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.IOException;
4343
import java.nio.file.Files;
4444
import java.nio.file.Paths;
45+
import java.util.ArrayList;
4546
import java.util.LinkedList;
4647
import java.util.List;
4748
import java.util.Optional;
@@ -449,13 +450,13 @@ public boolean save() throws IOException {
449450
}
450451

451452
// rename .pde files to .ino
452-
File mainFile = new File(sketch.getMainFilePath());
453-
File mainFolder = mainFile.getParentFile();
454-
File[] pdeFiles = mainFolder.listFiles((dir, name) -> {
455-
return name.toLowerCase().endsWith(".pde");
456-
});
453+
List<SketchFile> oldFiles = new ArrayList<>();
454+
for (SketchFile file : sketch.getFiles()) {
455+
if (file.isExtension(Sketch.OLD_SKETCH_EXTENSIONS))
456+
oldFiles.add(file);
457+
}
457458

458-
if (pdeFiles != null && pdeFiles.length > 0) {
459+
if (oldFiles.size() > 0) {
459460
if (PreferencesData.get("editor.update_extension") == null) {
460461
Object[] options = {tr("OK"), tr("Cancel")};
461462
int result = JOptionPane.showOptionDialog(editor,
@@ -480,29 +481,17 @@ public boolean save() throws IOException {
480481

481482
if (PreferencesData.getBoolean("editor.update_extension")) {
482483
// Do rename of all .pde files to new .ino extension
483-
for (File pdeFile : pdeFiles)
484-
renameCodeToInoExtension(pdeFile);
484+
for (SketchFile file : oldFiles) {
485+
File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION);
486+
file.renameTo(newName);
487+
}
485488
}
486489
}
487490

488491
sketch.save();
489492
return true;
490493
}
491494

492-
493-
private boolean renameCodeToInoExtension(File pdeFile) {
494-
for (SketchFile file : sketch.getFiles()) {
495-
if (!file.getFile().equals(pdeFile))
496-
continue;
497-
498-
String pdeName = pdeFile.getPath();
499-
pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino";
500-
return file.renameTo(new File(pdeName));
501-
}
502-
return false;
503-
}
504-
505-
506495
/**
507496
* Handles 'Save As' for a sketch.
508497
* <P>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* This represents a single sketch, consisting of one or more files.
1515
*/
1616
public class Sketch {
17-
1817
public static final String DEFAULT_SKETCH_EXTENSION = "ino";
19-
public static final List<String> SKETCH_EXTENSIONS = Arrays.asList(DEFAULT_SKETCH_EXTENSION, "pde");
18+
public static final List<String> OLD_SKETCH_EXTENSIONS = Arrays.asList("pde");
19+
public static final List<String> SKETCH_EXTENSIONS = Stream.concat(Stream.of(DEFAULT_SKETCH_EXTENSION), OLD_SKETCH_EXTENSIONS.stream()).collect(Collectors.toList());
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

arduino-core/src/processing/app/helpers/FileUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ public static boolean hasExtension(File file, List<String> extensions) {
245245
return extensions.contains(extension.toLowerCase());
246246
}
247247

248+
/**
249+
* Returns the given filename with the extension replaced by the one
250+
* given. If the filename does not have an extension yet, one is
251+
* added.
252+
*/
253+
public static String replaceExtension(String filename, String extension) {
254+
SplitFile split = splitFilename(filename);
255+
split.extension = extension;
256+
return split.join();
257+
}
258+
259+
/**
260+
* Returns the given filename with the extension replaced by the one
261+
* given. If the filename does not have an extension yet, one is
262+
* added.
263+
*/
264+
public static File replaceExtension(File file, String extension) {
265+
return new File(file.getParentFile(), replaceExtension(file.getName(), extension));
266+
}
267+
248268
/**
249269
* The result of a splitFilename call.
250270
*/
@@ -256,6 +276,13 @@ public SplitFile(String basename, String extension) {
256276

257277
public String basename;
258278
public String extension;
279+
280+
public String join() {
281+
if (extension.equals(""))
282+
return basename;
283+
else
284+
return basename + "." + extension;
285+
}
259286
}
260287

261288
/**

0 commit comments

Comments
 (0)