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.
This commit is contained in:
Matthijs Kooijman 2015-12-21 16:48:59 +01:00 committed by Martino Facchin
parent 98c0e0f841
commit 57a237752d
3 changed files with 40 additions and 24 deletions

View File

@ -41,6 +41,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@ -448,13 +449,13 @@ public class SketchController {
}
// rename .pde files to .ino
File mainFile = new File(sketch.getMainFilePath());
File mainFolder = mainFile.getParentFile();
File[] pdeFiles = mainFolder.listFiles((dir, name) -> {
return name.toLowerCase().endsWith(".pde");
});
List<SketchFile> oldFiles = new ArrayList<>();
for (SketchFile file : sketch.getFiles()) {
if (file.isExtension(Sketch.OLD_SKETCH_EXTENSIONS))
oldFiles.add(file);
}
if (pdeFiles != null && pdeFiles.length > 0) {
if (oldFiles.size() > 0) {
if (PreferencesData.get("editor.update_extension") == null) {
Object[] options = {tr("OK"), tr("Cancel")};
int result = JOptionPane.showOptionDialog(editor,
@ -479,8 +480,10 @@ public class SketchController {
if (PreferencesData.getBoolean("editor.update_extension")) {
// Do rename of all .pde files to new .ino extension
for (File pdeFile : pdeFiles)
renameCodeToInoExtension(pdeFile);
for (SketchFile file : oldFiles) {
File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION);
file.renameTo(newName);
}
}
}
@ -488,20 +491,6 @@ public class SketchController {
return true;
}
private boolean renameCodeToInoExtension(File pdeFile) {
for (SketchFile file : sketch.getFiles()) {
if (!file.getFile().equals(pdeFile))
continue;
String pdeName = pdeFile.getPath();
pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino";
return file.renameTo(new File(pdeName));
}
return false;
}
/**
* Handles 'Save As' for a sketch.
* <P>

View File

@ -14,9 +14,9 @@ import static processing.app.I18n.tr;
* This represents a single sketch, consisting of one or more files.
*/
public class Sketch {
public static final String DEFAULT_SKETCH_EXTENSION = "ino";
public static final List<String> SKETCH_EXTENSIONS = Arrays.asList(DEFAULT_SKETCH_EXTENSION, "pde");
public static final List<String> OLD_SKETCH_EXTENSIONS = Arrays.asList("pde");
public static final List<String> SKETCH_EXTENSIONS = Stream.concat(Stream.of(DEFAULT_SKETCH_EXTENSION), OLD_SKETCH_EXTENSIONS.stream()).collect(Collectors.toList());
public static final List<String> OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s");
public static final List<String> EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList());

View File

@ -245,6 +245,26 @@ public class FileUtils {
return extensions.contains(extension.toLowerCase());
}
/**
* Returns the given filename with the extension replaced by the one
* given. If the filename does not have an extension yet, one is
* added.
*/
public static String replaceExtension(String filename, String extension) {
SplitFile split = splitFilename(filename);
split.extension = extension;
return split.join();
}
/**
* Returns the given filename with the extension replaced by the one
* given. If the filename does not have an extension yet, one is
* added.
*/
public static File replaceExtension(File file, String extension) {
return new File(file.getParentFile(), replaceExtension(file.getName(), extension));
}
/**
* The result of a splitFilename call.
*/
@ -256,6 +276,13 @@ public class FileUtils {
public String basename;
public String extension;
public String join() {
if (extension.equals(""))
return basename;
else
return basename + "." + extension;
}
}
/**