Skip to content

Commit ecb4eaf

Browse files
author
Federico Fissore
committed
Compiler: additional files with allowed extentions are recursively copied to build folder. Fixes #3080
1 parent 149f906 commit ecb4eaf

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

Diff for: arduino-core/src/cc/arduino/utils/Pair.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.utils;
31+
32+
public class Pair<K, V> {
33+
34+
public final K key;
35+
public final V value;
36+
37+
public Pair(K key, V value) {
38+
this.key = key;
39+
this.value = value;
40+
}
41+
42+
}

Diff for: arduino-core/src/processing/app/debug/Compiler.java

+30-15
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@
2626
import static processing.app.I18n._;
2727

2828
import java.io.*;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.nio.file.Paths;
2932
import java.util.*;
33+
import java.util.stream.Stream;
3034

3135
import cc.arduino.MyStreamPumper;
3236
import cc.arduino.packages.BoardPort;
3337
import cc.arduino.packages.Uploader;
3438
import cc.arduino.packages.UploaderFactory;
3539

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

6167
private SketchData sketch;
6268
private PreferencesMap prefs;
@@ -1384,29 +1390,38 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
13841390
}
13851391
}
13861392

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

1395+
// 3. then loop over the code[] and save each .java file
13891396
for (SketchCode sc : sketch.getCodes()) {
1390-
if (sc.isExtension(SketchData.OTHER_ALLOWED_EXTENSIONS)) {
1391-
// no pre-processing services necessary for java files
1392-
// just write the the contents of 'program' to a .java file
1393-
// into the build directory. uses byte stream and reader/writer
1394-
// shtuff so that unicode bunk is properly handled
1395-
String filename = sc.getFileName(); //code[i].name + ".java";
1396-
try {
1397-
BaseNoGui.saveFile(sc.getProgram(), new File(buildPath, filename));
1398-
} catch (IOException e) {
1399-
e.printStackTrace();
1400-
throw new RunnerException(I18n.format(_("Problem moving {0} to the build folder"), filename));
1401-
}
1402-
1403-
} else if (sc.isExtension("ino") || sc.isExtension("pde")) {
1397+
if (sc.isExtension("ino") || sc.isExtension("pde")) {
14041398
// The compiler and runner will need this to have a proper offset
14051399
sc.addPreprocOffset(headerOffset);
14061400
}
14071401
}
14081402
}
14091403

1404+
private void copyAdditionalFilesToBuildFolderSavingOriginalFolderStructure(SketchData sketch, String buildPath) throws RunnerException {
1405+
Path sketchPath = Paths.get(sketch.getFolder().getAbsolutePath());
1406+
Stream<Path> otherFilesStream;
1407+
try {
1408+
otherFilesStream = Files.find(sketchPath, ADDITIONAL_FILES_COPY_MAX_DEPTH, (path, attribs) -> !attribs.isDirectory() && FileUtils.hasExtension(path.toFile(), SketchData.OTHER_ALLOWED_EXTENSIONS));
1409+
} catch (IOException e) {
1410+
throw new RunnerException(e);
1411+
}
1412+
otherFilesStream.map((path) -> new Pair<>(path, Paths.get(buildPath, sketchPath.relativize(path).toString())))
1413+
.filter((pair) -> !Files.exists(pair.value))
1414+
.forEach((pair) -> {
1415+
try {
1416+
Files.createDirectories(pair.value.getParent());
1417+
Files.copy(pair.key, pair.value);
1418+
} catch (IOException e) {
1419+
e.printStackTrace();
1420+
throw new RuntimeException(I18n.format(_("Problem moving {0} to the build folder"), sketchPath.relativize(pair.key).toString()));
1421+
}
1422+
});
1423+
}
1424+
14101425

14111426
/**
14121427
* List of library folders.

0 commit comments

Comments
 (0)