From ecb4eaf8613211173ee713d376d1f840482cef7a Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 29 Jun 2015 17:29:42 +0200 Subject: [PATCH] Compiler: additional files with allowed extentions are recursively copied to build folder. Fixes #3080 --- arduino-core/src/cc/arduino/utils/Pair.java | 42 +++++++++++++++++ .../src/processing/app/debug/Compiler.java | 45 ++++++++++++------- 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 arduino-core/src/cc/arduino/utils/Pair.java diff --git a/arduino-core/src/cc/arduino/utils/Pair.java b/arduino-core/src/cc/arduino/utils/Pair.java new file mode 100644 index 000000000..2586fd62f --- /dev/null +++ b/arduino-core/src/cc/arduino/utils/Pair.java @@ -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 { + + public final K key; + public final V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + +} diff --git a/arduino-core/src/processing/app/debug/Compiler.java b/arduino-core/src/processing/app/debug/Compiler.java index 9609dd65e..a26f6e59f 100644 --- a/arduino-core/src/processing/app/debug/Compiler.java +++ b/arduino-core/src/processing/app/debug/Compiler.java @@ -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 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.