Skip to content

Recusively copying additional files #3435

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

Merged
merged 1 commit into from
Jun 30, 2015
Merged
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
42 changes: 42 additions & 0 deletions arduino-core/src/cc/arduino/utils/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of Arduino.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/

package cc.arduino.utils;

public class Pair<K, V> {

public final K key;
public final V value;

public Pair(K key, V value) {
this.key = key;
this.value = value;
}

}
45 changes: 30 additions & 15 deletions arduino-core/src/processing/app/debug/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@
import static processing.app.I18n._;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;

import cc.arduino.MyStreamPumper;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Uploader;
import cc.arduino.packages.UploaderFactory;

import cc.arduino.packages.uploaders.MergeSketchWithBooloader;
import cc.arduino.utils.Pair;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.exec.*;
import processing.app.BaseNoGui;
Expand All @@ -57,6 +62,7 @@ public class Compiler implements MessageConsumer {
* used for the last build.
*/
static final public String BUILD_PREFS_FILE = "buildprefs.txt";
private static final int ADDITIONAL_FILES_COPY_MAX_DEPTH = 5;

private SketchData sketch;
private PreferencesMap prefs;
Expand Down Expand Up @@ -1384,29 +1390,38 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
}
}

// 3. then loop over the code[] and save each .java file
copyAdditionalFilesToBuildFolderSavingOriginalFolderStructure(sketch, buildPath);

// 3. then loop over the code[] and save each .java file
for (SketchCode sc : sketch.getCodes()) {
if (sc.isExtension(SketchData.OTHER_ALLOWED_EXTENSIONS)) {
// no pre-processing services necessary for java files
// just write the the contents of 'program' to a .java file
// into the build directory. uses byte stream and reader/writer
// shtuff so that unicode bunk is properly handled
String filename = sc.getFileName(); //code[i].name + ".java";
try {
BaseNoGui.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));
}

} else if (sc.isExtension("ino") || sc.isExtension("pde")) {
if (sc.isExtension("ino") || sc.isExtension("pde")) {
// The compiler and runner will need this to have a proper offset
sc.addPreprocOffset(headerOffset);
}
}
}

private void copyAdditionalFilesToBuildFolderSavingOriginalFolderStructure(SketchData sketch, String buildPath) throws RunnerException {
Path sketchPath = Paths.get(sketch.getFolder().getAbsolutePath());
Stream<Path> otherFilesStream;
try {
otherFilesStream = Files.find(sketchPath, ADDITIONAL_FILES_COPY_MAX_DEPTH, (path, attribs) -> !attribs.isDirectory() && FileUtils.hasExtension(path.toFile(), SketchData.OTHER_ALLOWED_EXTENSIONS));
} catch (IOException e) {
throw new RunnerException(e);
}
otherFilesStream.map((path) -> new Pair<>(path, Paths.get(buildPath, sketchPath.relativize(path).toString())))
.filter((pair) -> !Files.exists(pair.value))
.forEach((pair) -> {
try {
Files.createDirectories(pair.value.getParent());
Files.copy(pair.key, pair.value);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(I18n.format(_("Problem moving {0} to the build folder"), sketchPath.relativize(pair.key).toString()));
}
});
}


/**
* List of library folders.
Expand Down