Merge pull request #3435 from ffissore/copy-other-files-recursively

Recusively copying additional files
This commit is contained in:
Federico Fissore 2015-06-30 09:38:28 +02:00
commit ddd19f6057
2 changed files with 72 additions and 15 deletions

View File

@ -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;
}
}

View File

@ -26,7 +26,11 @@ package processing.app.debug;
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;
@ -34,6 +38,7 @@ 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;
@ -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;
@ -1384,29 +1390,38 @@ public class Compiler implements MessageConsumer {
}
}
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.