Skip to content

Commit 74daa4c

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 262085e commit 74daa4c

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
@@ -1807,7 +1807,14 @@ protected boolean handleOpenInternal(File sketchFile) {
18071807
}
18081808

18091809
// create properly named folder
1810-
File properFolder = new File(sketchFile.getParent(), properParent);
1810+
File properFolder;
1811+
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1812+
// Move the whole folder, so properFolder needs to be created one level above
1813+
properFolder = new File(new File(sketchFile.getParent()).getParent(), properParent);
1814+
} else {
1815+
properFolder = new File(sketchFile.getParent(), properParent);
1816+
}
1817+
18111818
if (properFolder.exists()) {
18121819
Base.showWarning(tr("Error"), I18n.format(tr("A folder named \"{0}\" already exists. " +
18131820
"Can't open sketch."), properParent), null);
@@ -1821,15 +1828,21 @@ protected boolean handleOpenInternal(File sketchFile) {
18211828
// copy the sketch inside
18221829
File properPdeFile = new File(properFolder, sketchFile.getName());
18231830
try {
1824-
FileUtils.copy(new File(sketchFile.getParent()), properFolder);
1831+
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1832+
File dir = new File(sketchFile.getParent());
1833+
FileUtils.copy(dir, properFolder);
1834+
// remove the original folder, so user doesn't get confused
1835+
FileUtils.recursiveDelete(dir);
1836+
} else {
1837+
Base.copyFile(sketchFile, properPdeFile);
1838+
// remove the original file, so user doesn't get confused
1839+
sketchFile.delete();
1840+
}
18251841
} catch (IOException e) {
18261842
Base.showWarning(tr("Error"), tr("Could not copy to a proper location."), e);
18271843
return false;
18281844
}
18291845

1830-
// remove the original file, so user doesn't get confused
1831-
sketchFile.delete();
1832-
18331846
// update with the new path
18341847
file = properPdeFile;
18351848

@@ -1871,6 +1884,40 @@ public void run() {
18711884
return true;
18721885
}
18731886

1887+
private boolean allowedExtension(String fileName, String[] validExtensions) {
1888+
int i = fileName.lastIndexOf('.');
1889+
if (i > 0) {
1890+
String ext = fileName.substring(i+1).toLowerCase();
1891+
if (!Arrays.asList(validExtensions).contains(ext)) {
1892+
return false;
1893+
}
1894+
}
1895+
// no extension or valid extension
1896+
return true;
1897+
}
1898+
1899+
private boolean onlyContainsSketchFiles(String path) {
1900+
File folder = new File(path);
1901+
File[] listOfFiles = folder.listFiles();
1902+
1903+
for (int i = 0; i < listOfFiles.length; i++) {
1904+
String name = listOfFiles[i].getName();
1905+
if (listOfFiles[i].isFile()) {
1906+
// allowed files extensions are only .ino, .h, .hpp, .c, .cpp (all cases)
1907+
String[] valid = {"ino", "h", "hpp" , "c" , "cpp", "pde"};
1908+
if (!allowedExtension(name, valid)) {
1909+
return false;
1910+
}
1911+
} else if (listOfFiles[i].isDirectory()) {
1912+
// allowed dir names are only src and extras , plus source control folders
1913+
if (name != "src" && name != "extras" && !FileUtils.SOURCE_CONTROL_FOLDERS.contains(name)) {
1914+
return false;
1915+
}
1916+
}
1917+
}
1918+
return true;
1919+
}
1920+
18741921
public void updateTitle() {
18751922
if (sketchController == null) {
18761923
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)