Skip to content

Commit 57a2377

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 98c0e0f commit 57a2377

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

app/src/processing/app/SketchController.java

+11-22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.IOException;
4242
import java.nio.file.Files;
4343
import java.nio.file.Paths;
44+
import java.util.ArrayList;
4445
import java.util.LinkedList;
4546
import java.util.List;
4647
import java.util.Optional;
@@ -448,13 +449,13 @@ public boolean save() throws IOException {
448449
}
449450

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

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

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

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

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

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

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

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