Skip to content

Commit f33d47c

Browse files
committed
Avoid copying whole directory on Open in all cases
Alternative to arduino#7904, tries to distinguish between * click on a single ino file downloaded from the internet (old behaviour applies, directory is created an the file is moved in it) * click on an ino file that for some reason is part of a project with the wrong name (eg. it was downloaded as zip from github, so the containing folder names becomes "projectName-master"). In this case if the directory is created one level above the selected file, all files are moved there and the original directory is recursively deleted)
1 parent 0885c61 commit f33d47c

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

app/src/processing/app/Editor.java

+52-5
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,14 @@ protected boolean handleOpenInternal(File sketchFile) {
17681768
}
17691769

17701770
// create properly named folder
1771-
File properFolder = new File(sketchFile.getParent(), properParent);
1771+
File properFolder;
1772+
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1773+
// Move the whole folder, so properFolder needs to be created one level above
1774+
properFolder = new File(new File(sketchFile.getParent()).getParent(), properParent);
1775+
} else {
1776+
properFolder = new File(sketchFile.getParent(), properParent);
1777+
}
1778+
17721779
if (properFolder.exists()) {
17731780
Base.showWarning(tr("Error"), I18n.format(tr("A folder named \"{0}\" already exists. " +
17741781
"Can't open sketch."), properParent), null);
@@ -1782,15 +1789,21 @@ protected boolean handleOpenInternal(File sketchFile) {
17821789
// copy the sketch inside
17831790
File properPdeFile = new File(properFolder, sketchFile.getName());
17841791
try {
1785-
FileUtils.copy(new File(sketchFile.getParent()), properFolder);
1792+
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1793+
File dir = new File(sketchFile.getParent());
1794+
FileUtils.copy(dir, properFolder);
1795+
// remove the original folder, so user doesn't get confused
1796+
FileUtils.recursiveDelete(dir);
1797+
} else {
1798+
Base.copyFile(sketchFile, properPdeFile);
1799+
// remove the original file, so user doesn't get confused
1800+
sketchFile.delete();
1801+
}
17861802
} catch (IOException e) {
17871803
Base.showWarning(tr("Error"), tr("Could not copy to a proper location."), e);
17881804
return false;
17891805
}
17901806

1791-
// remove the original file, so user doesn't get confused
1792-
sketchFile.delete();
1793-
17941807
// update with the new path
17951808
file = properPdeFile;
17961809

@@ -1813,6 +1826,40 @@ protected boolean handleOpenInternal(File sketchFile) {
18131826
return true;
18141827
}
18151828

1829+
private boolean allowedExtension(String fileName, String[] validExtensions) {
1830+
int i = fileName.lastIndexOf('.');
1831+
if (i > 0) {
1832+
String ext = fileName.substring(i+1).toLowerCase();
1833+
if (!Arrays.asList(validExtensions).contains(ext)) {
1834+
return false;
1835+
}
1836+
}
1837+
// no extension or valid extension
1838+
return true;
1839+
}
1840+
1841+
private boolean onlyContainsSketchFiles(String path) {
1842+
File folder = new File(path);
1843+
File[] listOfFiles = folder.listFiles();
1844+
1845+
for (int i = 0; i < listOfFiles.length; i++) {
1846+
String name = listOfFiles[i].getName();
1847+
if (listOfFiles[i].isFile()) {
1848+
// allowed files extensions are only .ino, .h, .hpp, .c, .cpp (all cases)
1849+
String[] valid = {"ino", "h", "hpp" , "c" , "cpp", "pde"};
1850+
if (!allowedExtension(name, valid)) {
1851+
return false;
1852+
}
1853+
} else if (listOfFiles[i].isDirectory()) {
1854+
// allowed dir names are only src and extras , plus source control folders
1855+
if (name != "src" && name != "extras" && !FileUtils.SOURCE_CONTROL_FOLDERS.contains(name)) {
1856+
return false;
1857+
}
1858+
}
1859+
}
1860+
return true;
1861+
}
1862+
18161863
public void updateTitle() {
18171864
if (sketchController == null) {
18181865
return;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class FileUtils {
1212

13-
private static final List<String> SOURCE_CONTROL_FOLDERS = Arrays.asList("CVS", "RCS", ".git", ".svn", ".hg", ".bzr");
13+
public static final List<String> SOURCE_CONTROL_FOLDERS = Arrays.asList("CVS", "RCS", ".git", ".svn", ".hg", ".bzr");
1414
private static final Pattern BACKSLASH = Pattern.compile("\\\\");
1515

1616
/**
@@ -62,7 +62,7 @@ public static void copy(File sourceFolder, File destFolder) throws IOException {
6262
// Avoid recursive copy of folders
6363
continue;
6464
}
65-
if (file.isDirectory() && !SOURCE_CONTROL_FOLDERS.contains(file.getName())) {
65+
if (file.isDirectory()) {
6666
if (!destFile.exists() && !destFile.mkdir()) {
6767
throw new IOException("Unable to create folder: " + destFile);
6868
}

0 commit comments

Comments
 (0)