1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

Portable Arduino

Use Arduino on USB flash stick and without affecting anything on your PC.

To use this feature, simply make a folder named "portable" in the application
root folder where there are "hardware", "examples", "lib" and others.
In this mode, file association setting is disabled in the preferences dialog.

The sketch book folder is inside the "portable" folder. This may be confusing
especially for Mac OSX "Arduino.app" folder.
This commit is contained in:
Shigeru KANEMOTO
2012-06-09 17:07:39 +09:00
committed by Federico Fissore
parent c596edd083
commit 02447a0170
4 changed files with 150 additions and 5 deletions

View File

@ -111,6 +111,9 @@ public class Base {
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>()); List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
Editor activeEditor; Editor activeEditor;
static File portableFolder = null;
static final String portableSketchbookFolder = "sketchbook";
static public void main(String args[]) throws Exception { static public void main(String args[]) throws Exception {
initPlatform(); initPlatform();
@ -156,6 +159,13 @@ public class Base {
} }
*/ */
initPlatform();
// Portable folder
portableFolder = getContentFile("portable");
if (!portableFolder.exists())
portableFolder = null;
// // Set the look and feel before opening the window // // Set the look and feel before opening the window
// try { // try {
// platform.setLookAndFeel(); // platform.setLookAndFeel();
@ -249,8 +259,12 @@ public class Base {
// If a value is at least set, first check to see if the folder exists. // If a value is at least set, first check to see if the folder exists.
// If it doesn't, warn the user that the sketchbook folder is being reset. // If it doesn't, warn the user that the sketchbook folder is being reset.
if (sketchbookPath != null) { if (sketchbookPath != null) {
File skechbookFolder = new File(sketchbookPath); File sketchbookFolder;
if (!skechbookFolder.exists()) { if (portableFolder != null)
sketchbookFolder = new File(portableFolder, sketchbookPath);
else
sketchbookFolder = new File(sketchbookPath);
if (!sketchbookFolder.exists()) {
Base.showWarning(_("Sketchbook folder disappeared"), Base.showWarning(_("Sketchbook folder disappeared"),
_("The sketchbook folder no longer exists.\n" + _("The sketchbook folder no longer exists.\n" +
"Arduino will switch to the default sketchbook\n" + "Arduino will switch to the default sketchbook\n" +
@ -264,7 +278,10 @@ public class Base {
// If no path is set, get the default sketchbook folder for this platform // If no path is set, get the default sketchbook folder for this platform
if (sketchbookPath == null) { if (sketchbookPath == null) {
File defaultFolder = getDefaultSketchbookFolder(); File defaultFolder = getDefaultSketchbookFolder();
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath()); if (portableFolder != null)
Preferences.set("sketchbook.path", portableSketchbookFolder);
else
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
if (!defaultFolder.exists()) { if (!defaultFolder.exists()) {
defaultFolder.mkdirs(); defaultFolder.mkdirs();
} }
@ -422,6 +439,15 @@ public class Base {
int opened = 0; int opened = 0;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
String path = Preferences.get("last.sketch" + i + ".path"); String path = Preferences.get("last.sketch" + i + ".path");
if (portableFolder != null) {
File absolute = new File(portableFolder, path);
try {
path = absolute.getCanonicalPath();
}
catch (IOException e) {
// path unchanged.
}
}
int[] location; int[] location;
if (windowPositionValid) { if (windowPositionValid) {
String locationStr = Preferences.get("last.sketch" + i + ".location"); String locationStr = Preferences.get("last.sketch" + i + ".location");
@ -460,6 +486,11 @@ public class Base {
!editor.getSketch().isModified()) { !editor.getSketch().isModified()) {
continue; continue;
} }
if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path);
if (path == null)
continue;
}
Preferences.set("last.sketch" + index + ".path", path); Preferences.set("last.sketch" + index + ".path", path);
int[] location = editor.getPlacement(); int[] location = editor.getPlacement();
@ -478,6 +509,11 @@ public class Base {
String untitledPath = untitledFolder.getAbsolutePath(); String untitledPath = untitledFolder.getAbsolutePath();
if (path.startsWith(untitledPath)) { if (path.startsWith(untitledPath)) {
path = ""; path = "";
} else
if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path);
if (path == null)
path = "";
} }
Preferences.set("last.sketch" + index + ".path", path); Preferences.set("last.sketch" + index + ".path", path);
} }
@ -1759,6 +1795,9 @@ public class Base {
static public File getSettingsFolder() { static public File getSettingsFolder() {
if (portableFolder != null)
return portableFolder;
File settingsFolder = null; File settingsFolder = null;
String preferencesPath = Preferences.get("settings.path"); String preferencesPath = Preferences.get("settings.path");
@ -1938,7 +1977,19 @@ public class Base {
return boardPreferences; return boardPreferences;
} }
static public File getPortableFolder() {
return portableFolder;
}
static public String getPortableSketchbookFolder() {
return portableSketchbookFolder;
}
static public File getSketchbookFolder() { static public File getSketchbookFolder() {
if (portableFolder != null)
return new File(portableFolder, Preferences.get("sketchbook.path"));
return new File(Preferences.get("sketchbook.path")); return new File(Preferences.get("sketchbook.path"));
} }
@ -1971,6 +2022,9 @@ public class Base {
protected File getDefaultSketchbookFolder() { protected File getDefaultSketchbookFolder() {
if (portableFolder != null)
return new File(portableFolder, portableSketchbookFolder);
File sketchbookFolder = null; File sketchbookFolder = null;
try { try {
sketchbookFolder = platform.getDefaultSketchbookFolder(); sketchbookFolder = platform.getDefaultSketchbookFolder();

View File

@ -326,7 +326,14 @@ public class Preferences {
File file = File file =
Base.selectFolder(_("Select new sketchbook location"), dflt, dialog); Base.selectFolder(_("Select new sketchbook location"), dflt, dialog);
if (file != null) { if (file != null) {
sketchbookLocationField.setText(file.getAbsolutePath()); String path = file.getAbsolutePath();
if (Base.getPortableFolder() != null) {
path = RelativePath.relativePath(Base.getPortableFolder().toString(), path);
if (path == null) {
path = Base.getPortableSketchbookFolder();
}
}
sketchbookLocationField.setText(path);
} }
} }
}); });
@ -439,6 +446,10 @@ public class Preferences {
autoAssociateBox.setBounds(left, top, d.width + 10, d.height); autoAssociateBox.setBounds(left, top, d.width + 10, d.height);
right = Math.max(right, left + d.width); right = Math.max(right, left + d.width);
top += d.height + GUI_BETWEEN; top += d.height + GUI_BETWEEN;
// If using portable mode, it's bad manner to change PC setting.
if (Base.getPortableFolder() != null)
autoAssociateBox.setEnabled(false);
} }
// More preferences are in the ... // More preferences are in the ...
@ -591,6 +602,12 @@ public class Preferences {
// if the sketchbook path has changed, rebuild the menus // if the sketchbook path has changed, rebuild the menus
String oldPath = get("sketchbook.path"); String oldPath = get("sketchbook.path");
String newPath = sketchbookLocationField.getText(); String newPath = sketchbookLocationField.getText();
if (newPath.isEmpty()) {
if (Base.getPortableFolder() == null)
newPath = editor.base.getDefaultSketchbookFolder().toString();
else
newPath = Base.getPortableSketchbookFolder();
}
if (!newPath.equals(oldPath)) { if (!newPath.equals(oldPath)) {
editor.base.rebuildSketchbookMenus(); editor.base.rebuildSketchbookMenus();
set("sketchbook.path", newPath); set("sketchbook.path", newPath);

View File

@ -0,0 +1,74 @@
/*
* by Shigeru KANEMOTO at SWITCHSCIENCE.
*/
package processing.app;
import java.io.File;
import java.io.IOException;
class RelativePath {
//
// Compute relative path to "target" from a directory "origin".
//
// If "origin" is not absolute, it is relative from the current directory.
// If "target" is not absolute, it is relative from "origin".
//
public static String relativePath(String origin, String target) {
try {
origin = (new File(origin)).getCanonicalPath();
File targetFile = new File(target);
if (targetFile.isAbsolute())
target = targetFile.getCanonicalPath();
else
target = (new File(origin, target)).getCanonicalPath();
}
catch (IOException e) {
return null;
}
if (origin.equals(target)) {
// origin and target is identical.
return ".";
}
if (origin.equals(File.separator)) {
// origin is root.
return "." + target;
}
String prefix = "";
String root = File.separator;
if (System.getProperty("os.name").indexOf("Windows") != -1) {
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
// Windows UNC path not supported.
return null;
}
char originLetter = origin.charAt(0);
char targetLetter = target.charAt(0);
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
// Windows only
if (originLetter != targetLetter) {
// Drive letters differ
return null;
}
}
prefix = "" + originLetter + ':';
root = prefix + File.separator;
}
String relative = "";
while (!target.startsWith(origin + File.separator)) {
origin = (new File(origin)).getParent();
if (origin.equals(root))
origin = prefix;
relative += "..";
relative += File.separator;
}
return relative + target.substring(origin.length() + 1);
}
}

View File

@ -826,7 +826,7 @@ public class Sketch {
FileDialog.SAVE); FileDialog.SAVE);
if (isReadOnly() || isUntitled()) { if (isReadOnly() || isUntitled()) {
// default to the sketchbook folder // default to the sketchbook folder
fd.setDirectory(Preferences.get("sketchbook.path")); fd.setDirectory(Base.getSketchbookFolder().getAbsolutePath());
} else { } else {
// default to the parent folder of where this was // default to the parent folder of where this was
fd.setDirectory(folder.getParent()); fd.setDirectory(folder.getParent());