mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
import library also import folder (already expanded libraries)
Conflicts: app/src/processing/app/helpers/FileUtils.java app/test/processing/app/tools/ZipDeflaterTest.java
This commit is contained in:
parent
62a8a0149f
commit
8eab205166
@ -31,6 +31,7 @@ import javax.swing.*;
|
||||
|
||||
import processing.app.debug.Compiler;
|
||||
import processing.app.debug.Target;
|
||||
import processing.app.helpers.FileUtils;
|
||||
import processing.app.tools.ZipDeflater;
|
||||
import processing.core.*;
|
||||
import static processing.app.I18n._;
|
||||
@ -2364,24 +2365,43 @@ public class Base {
|
||||
}
|
||||
}
|
||||
|
||||
public void handleAddLibrary(Editor editor) {
|
||||
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
|
||||
fileChooser.setDialogTitle(_("Select a zip file or a folder containing the library you'd like to add"));
|
||||
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
||||
|
||||
public void handleAddZipLibrary(Editor editor) {
|
||||
String prompt = _("Select a zip file containing the library you'd like to add");
|
||||
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
|
||||
fd.setDirectory(System.getProperty("user.home"));
|
||||
fd.setVisible(true);
|
||||
Dimension preferredSize = fileChooser.getPreferredSize();
|
||||
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
|
||||
|
||||
int returnVal = fileChooser.showOpenDialog(editor);
|
||||
|
||||
String directory = fd.getDirectory();
|
||||
String filename = fd.getFile();
|
||||
if (filename == null) return;
|
||||
|
||||
File sourceFile = new File(directory, filename);
|
||||
try {
|
||||
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
|
||||
zipDeflater.deflate();
|
||||
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
||||
} catch (IOException e) {
|
||||
editor.statusError(e);
|
||||
if (returnVal != JFileChooser.APPROVE_OPTION) {
|
||||
return;
|
||||
}
|
||||
|
||||
File sourceFile = fileChooser.getSelectedFile();
|
||||
|
||||
if (sourceFile.isDirectory()) {
|
||||
File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName());
|
||||
if (!destinationFolder.mkdir()) {
|
||||
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
FileUtils.copy(sourceFile, destinationFolder);
|
||||
} catch (IOException e) {
|
||||
editor.statusError(e);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
|
||||
zipDeflater.deflate();
|
||||
} catch (IOException e) {
|
||||
editor.statusError(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
||||
}
|
||||
}
|
||||
|
@ -631,10 +631,10 @@ public class Editor extends JFrame implements RunnerListener {
|
||||
}
|
||||
sketchMenu.add(importMenu);
|
||||
|
||||
item = new JMenuItem(_("Add Library from ZIP"));
|
||||
item = new JMenuItem(_("Add Library..."));
|
||||
item.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
base.handleAddZipLibrary(Editor.this);
|
||||
base.handleAddLibrary(Editor.this);
|
||||
base.onBoardOrPortChange();
|
||||
base.rebuildImportMenu(Editor.importMenu);
|
||||
}
|
||||
|
81
app/src/processing/app/helpers/FileUtils.java
Normal file
81
app/src/processing/app/helpers/FileUtils.java
Normal file
@ -0,0 +1,81 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* Checks, whether the child directory is a subdirectory of the base directory.
|
||||
*
|
||||
* @param base
|
||||
* the base directory.
|
||||
* @param child
|
||||
* the suspected child directory.
|
||||
* @return true, if the child is a subdirectory of the base directory.
|
||||
*/
|
||||
public static boolean isSubDirectory(File base, File child) {
|
||||
try {
|
||||
base = base.getCanonicalFile();
|
||||
child = child.getCanonicalFile();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File parentFile = child;
|
||||
while (parentFile != null) {
|
||||
if (base.equals(parentFile)) {
|
||||
return true;
|
||||
}
|
||||
parentFile = parentFile.getParentFile();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void copy(File sourceFolder, File destFolder) throws IOException {
|
||||
for (File file : sourceFolder.listFiles()) {
|
||||
File destFile = new File(destFolder, file.getName());
|
||||
if (file.isDirectory()) {
|
||||
if (!destFile.mkdir()) {
|
||||
throw new IOException("Unable to create folder: " + destFile);
|
||||
}
|
||||
copy(file, destFile);
|
||||
} else {
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
fos = new FileOutputStream(destFile);
|
||||
byte[] buf = new byte[4096];
|
||||
int readBytes = -1;
|
||||
while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
|
||||
fos.write(buf, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
fis.close();
|
||||
}
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void recursiveDelete(File file) {
|
||||
if (file.isDirectory()) {
|
||||
for (File current : file.listFiles()) {
|
||||
if (current.isDirectory()) {
|
||||
recursiveDelete(current);
|
||||
} else {
|
||||
current.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
}
|
@ -10,6 +10,8 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import processing.app.helpers.FileUtils;
|
||||
|
||||
public class ZipDeflater {
|
||||
|
||||
private final ZipFile zipFile;
|
||||
@ -71,26 +73,13 @@ public class ZipDeflater {
|
||||
private void deleteUndesiredFoldersAndFiles(File folder) {
|
||||
for (File file : folder.listFiles()) {
|
||||
if (file.isDirectory() && "__MACOSX".equals(file.getName())) {
|
||||
recursiveDelete(file);
|
||||
FileUtils.recursiveDelete(file);
|
||||
} else if (file.getName().startsWith(".")) {
|
||||
recursiveDelete(file);
|
||||
FileUtils.recursiveDelete(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recursiveDelete(File file) {
|
||||
if (file.isDirectory()) {
|
||||
for (File current : file.listFiles()) {
|
||||
if (current.isDirectory()) {
|
||||
recursiveDelete(current);
|
||||
} else {
|
||||
current.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
private void ensureFoldersOfEntryExist(File folder, ZipEntry entry) {
|
||||
String[] parts = entry.getName().split("/");
|
||||
File current = folder;
|
||||
@ -109,7 +98,7 @@ public class ZipDeflater {
|
||||
}
|
||||
|
||||
files[0].renameTo(new File(folder.getParentFile(), files[0].getName()));
|
||||
recursiveDelete(folder);
|
||||
FileUtils.recursiveDelete(folder);
|
||||
}
|
||||
|
||||
private String folderNameFromZip() {
|
||||
|
Loading…
Reference in New Issue
Block a user