Skip to content

Library configuration file management (issue #1734) #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 114 additions & 7 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public class Sketch {

/** code folder location for this sketch (may not exist yet) */
private File codeFolder;


/** configuration folder location for this sketch (may not exist yet) */
private File configFolder;

private SketchCode current;
private int currentIndex;
/**
Expand Down Expand Up @@ -160,15 +163,24 @@ public Sketch(Editor editor, String path) throws IOException {
protected void load() throws IOException {
codeFolder = new File(folder, "code");
dataFolder = new File(folder, "data");
configFolder = new File(folder, "configuration");

// get list of files in the sketch folder
String list[] = folder.list();
// get list of files in the library configuration folder
String list1[] = configFolder.list();

// reset these because load() may be called after an
// external editor event. (fix for 0099)
codeCount = 0;

code = new SketchCode[list.length];

// Add files from library configuration folder if exists
if (configFolder.exists()) {
code = new SketchCode[list.length+list1.length];
}
else {
code = new SketchCode[list.length];
}

String[] extensions = getExtensions();

Expand Down Expand Up @@ -203,6 +215,38 @@ protected void load() throws IOException {
if (codeCount == 0)
throw new IOException(_("No valid code files found"));

//Check library configuration folder to create tabs for library configuration files
if (configFolder.exists()) {
for (String filename : list1) {
// Ignoring the dot prefix files is especially important to avoid files
// with the ._ prefix on Mac OS X. (You'll see this with Mac files on
// non-HFS drives, i.e. a thumb drive formatted FAT32.)
if (filename.startsWith(".")) continue;

// Don't let some wacko name a directory blah.pde or bling.java.
if (new File(configFolder, filename).isDirectory()) continue;

// figure out the name without any extension
String base = filename;
// now strip off the .pde and .java extensions
for (String extension : extensions) {
if (base.toLowerCase().endsWith("." + extension)) {
base = base.substring(0, base.length() - (extension.length() + 1));

// Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116]
if (Sketch.isSanitaryName(base)) {
code[codeCount++] = new SketchCode(new File(configFolder, filename), extension);
// Don't forget to declare it as configuration file
code[codeCount-1].setConfig(true);
} else {
editor.console.message(I18n.format("File name {0} is invalid: ignored", filename), true, false);
}
}
}
}
}

// Remove any code that wasn't proper
code = (SketchCode[]) PApplet.subset(code, 0, codeCount);

Expand Down Expand Up @@ -878,7 +922,14 @@ protected boolean saveAs() throws IOException {

// now make a fresh copy of the folder
newFolder.mkdirs();


// If exists, make a copy of the configuration folder
File newConfigFolder= new File(newFolder, "configuration");
if (configFolder.exists()) {
Base.copyDir(configFolder, newConfigFolder);
}


// grab the contents of the current tab before saving
// first get the contents of the editor text area
if (current.isModified()) {
Expand All @@ -887,10 +938,17 @@ protected boolean saveAs() throws IOException {

// save the other tabs to their new location
for (int i = 1; i < codeCount; i++) {
File newFile = new File(newFolder, code[i].getFileName());
// Library configuration file is not saved in the sketch folder but in the configuration folder
File newFile;
if (code[i].isConfig() && configFolder.exists()) {
newFile = new File(newConfigFolder, code[i].getFileName());
}
else {
newFile = new File(newFolder, code[i].getFileName());
}
code[i].saveAs(newFile);
}

// re-copy the data folder (this may take a while.. add progress bar?)
if (dataFolder.exists()) {
File newDataFolder = new File(newFolder, "data");
Expand Down Expand Up @@ -1117,12 +1175,28 @@ public void importLibrary(File jarPath) throws IOException {

String list[] = Base.headerListFromIncludePath(jarPath);

// If exists, make a copy of the library configuration file in the sketch folder
File LibConfigFolder = new File(jarPath, "configuration");
if (LibConfigFolder.exists()) {
System.out.println("Library configuration files found" );
File NewLibConfigFolder = (new File(folder,"configuration"));
Base.copyDir(LibConfigFolder, NewLibConfigFolder);
// Update editor tabs
load();
// Need to switch between tabs to refresh the current content
if (code[1].fileExists()) {
setCurrentCode(1);
}
setCurrentCode(0);
}

// import statements into the main sketch file (code[0])
// if the current code is a .java file, insert into current
//if (current.flavor == PDE) {
if (hasDefaultExtension(current)) {
setCurrentCode(0);
}

// could also scan the text in the file to see if each import
// statement is already in there, but if the user has the import
// commented out, then this will be a problem.
Expand All @@ -1136,6 +1210,7 @@ public void importLibrary(File jarPath) throws IOException {
buffer.append(editor.getText());
editor.setText(buffer.toString());
editor.setSelection(0, 0); // scroll to start

setModified(true);
}

Expand Down Expand Up @@ -1405,7 +1480,20 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
// shtuff so that unicode bunk is properly handled
String filename = sc.getFileName(); //code[i].name + ".java";
try {
Base.saveFile(sc.getProgram(), new File(buildPath, filename));
// Is it a Library configuration file ?
if (sc.isConfig()) {
File TempLibConfigFolder = new File(buildPath, "configuration");
// Create the Library Configuration folder if it doesn't exist
if(!TempLibConfigFolder.exists()){
TempLibConfigFolder.mkdir();
}
// Copy file in configuration folder
Base.saveFile(sc.getProgram(), new File(TempLibConfigFolder.getAbsolutePath(), filename));
}
else {
// Copy file in build folder
Base.saveFile(sc.getProgram(), new File(buildPath, filename));
}
} catch (IOException e) {
e.printStackTrace();
throw new RunnerException(I18n.format(_("Problem moving {0} to the build folder"), filename));
Expand Down Expand Up @@ -1962,6 +2050,25 @@ public File prepareCodeFolder() {
return codeFolder;
}

/**
* Returns the location of the sketch's configuration folder. (It may not exist yet.)
*/
public File getConfigFolder() {
return configFolder;
}


/**
* Create the config folder if it does not exist already. As a convenience,
* it also returns the config folder, since it's likely about to be used.
*/
public File prepareConfigFolder() {
if (!configFolder.exists()) {
configFolder.mkdirs();
}
return configFolder;
}


public String getClassPath() {
return classPath;
Expand Down
25 changes: 20 additions & 5 deletions app/src/processing/app/SketchCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class SketchCode {
private int scrollPosition;

private boolean modified;
private boolean config = false; // This attribute allows to differentiate Library configuration files (if true) from other files ( if false)

/** name of .java file after preproc */
// private String preprocName;
Expand Down Expand Up @@ -126,11 +127,15 @@ public boolean accept(File pathname) {


protected boolean renameTo(File what, String ext) {
boolean success = file.renameTo(what);
if (success) {
file = what;
extension = ext;
makePrettyName();
boolean success = false;
// rename is not allowed for Library configuration files
if (!config) {
success = file.renameTo(what);
if (success) {
file = what;
extension = ext;
makePrettyName();
}
}
return success;
}
Expand Down Expand Up @@ -186,6 +191,16 @@ public boolean isModified() {
}


public void setConfig(boolean _config) {
this.config = _config;
}


public boolean isConfig() {
return config;
}


// public void setPreprocName(String preprocName) {
// this.preprocName = preprocName;
// }
Expand Down
10 changes: 10 additions & 0 deletions app/src/processing/app/debug/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ private void compileLibrary(Library lib, List<File> includeFolders)
throws RunnerException {
File libFolder = lib.getSrcFolder();
File libBuildFolder = prefs.getFile(("build.path"), lib.getName());
// Manage library configuration files
File libConfigFolder = prefs.getFile(("build.path"), "configuration");
if (libConfigFolder.exists()) {
includeFolders.add(libConfigFolder);
}

if (lib.useRecursion()) {
// libBuildFolder == {build.path}/LibName
Expand All @@ -654,6 +659,11 @@ private void compileLibrary(Library lib, List<File> includeFolders)
// other libraries should not see this library's utility/ folder
includeFolders.remove(utilityFolder);
}
// other libraries should not see this library configuration folder
if (libConfigFolder.exists()) {
includeFolders.remove(libConfigFolder);
}

}

private void recursiveCompileFilesInFolder(File srcBuildFolder, File srcFolder, List<File> includeFolders) throws RunnerException {
Expand Down
22 changes: 22 additions & 0 deletions libraries/Morse/examples/MorseFast/Configuration/LibMorseConf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
LibMorseConf.h - Morse library configuration file.
Created by Gilles De Vos, December 10, 2013.
Released into the public domain.
*/

#ifndef LIBMORSECONF_H
#define LIBMORSECONF_H


#define LIB_MORSE_UNIT 100 //!< Time duration (ms) of elementary Morse code

// International Morse code elements
#define LIB_MORSE_DOT_DURATION LIB_MORSE_UNIT //!< Time duration (ms) of a dot
#define LIB_MORSE_DASH_DURATION 3*LIB_MORSE_UNIT //!< Time duration (ms) of a dash
#define LIB_MORSE_INTR_CHAR_GAP LIB_MORSE_UNIT //!< Time duration (ms) between the dots and dashes within a character
#define LIB_MORSE_SHORT_GAP 3*LIB_MORSE_UNIT //!< Time duration (ms) between characters
#define LIB_MORSE_MEDIUM_GAP 7*LIB_MORSE_UNIT //!< Time duration (ms) between words

#define LIB_MORSE_ALPHABET_AVAILABLE //!< Encoded characters are available

#endif //LIBMORSECONF_H
18 changes: 18 additions & 0 deletions libraries/Morse/examples/MorseFast/MorseFast.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include <Morse.h>

Morse morse(13);

void setup()
{
}

void loop()
{
morse._S();
morse._O();
morse._S();
morse.endword();

}

22 changes: 22 additions & 0 deletions libraries/Morse/examples/MorseSlow/Configuration/LibMorseConf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
LibMorseConf.h - Morse library configuration file.
Created by Gilles De Vos, December 10, 2013.
Released into the public domain.
*/

#ifndef LIBMORSECONF_H
#define LIBMORSECONF_H


#define LIB_MORSE_UNIT 250 //!< Time duration (ms) of elementary Morse code

// International Morse code elements
#define LIB_MORSE_DOT_DURATION LIB_MORSE_UNIT //!< Time duration (ms) of a dot
#define LIB_MORSE_DASH_DURATION 3*LIB_MORSE_UNIT //!< Time duration (ms) of a dash
#define LIB_MORSE_INTR_CHAR_GAP LIB_MORSE_UNIT //!< Time duration (ms) between the dots and dashes within a character
#define LIB_MORSE_SHORT_GAP 3*LIB_MORSE_UNIT //!< Time duration (ms) between characters
#define LIB_MORSE_MEDIUM_GAP 7*LIB_MORSE_UNIT //!< Time duration (ms) between words

//#define LIB_MORSE_ALPHABET_AVAILABLE //!< Encoded characters are available

#endif //LIBMORSECONF_H
17 changes: 17 additions & 0 deletions libraries/Morse/examples/MorseSlow/MorseSlow.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include <Morse.h>

Morse morse(13);

void setup()
{
}

void loop()
{
morse.dot(); morse.dot(); morse.dot(); morse.endchar();
morse.dash(); morse.dash(); morse.dash(); morse.endchar();
morse.dot(); morse.dot(); morse.dot(); morse.endchar();
morse.endword();
}

Loading