Skip to content

Commit 4a23cd0

Browse files
committed
added MergeSketchSources + tests
1 parent 8feeb6a commit 4a23cd0

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

Diff for: arduino/builder/sketch.go

+35
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"io/ioutil"
2020
"os"
2121
"path/filepath"
22+
"regexp"
2223
"strings"
2324

2425
"github.com/arduino/arduino-cli/arduino/globals"
@@ -27,6 +28,17 @@ import (
2728
"github.com/pkg/errors"
2829
)
2930

31+
var includesArduinoH = regexp.MustCompile(`^\s*#\s*include\s*[<\"]Arduino\.h[>\"]`)
32+
33+
// QuoteCppString returns the given string as a quoted string for use with the C
34+
// preprocessor. This adds double quotes around it and escapes any
35+
// double quotes and backslashes in the string.
36+
func QuoteCppString(str string) string {
37+
str = strings.Replace(str, "\\", "\\\\", -1)
38+
str = strings.Replace(str, "\"", "\\\"", -1)
39+
return "\"" + str + "\""
40+
}
41+
3042
// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk
3143
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
3244

@@ -120,3 +132,26 @@ func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) {
120132

121133
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
122134
}
135+
136+
// MergeSketchSources merges all the source files included in a sketch
137+
func MergeSketchSources(sketch *sketch.Sketch) (int, string) {
138+
lineOffset := 0
139+
mergedSource := ""
140+
141+
// add Arduino.h inclusion directive if missing
142+
if !includesArduinoH.MatchString(sketch.MainFile.GetSourceStr()) {
143+
mergedSource += "#include <Arduino.h>\n"
144+
lineOffset++
145+
}
146+
147+
mergedSource += "#line 1 " + QuoteCppString(sketch.MainFile.Path) + "\n"
148+
mergedSource += sketch.MainFile.GetSourceStr() + "\n"
149+
lineOffset++
150+
151+
for _, item := range sketch.OtherSketchFiles {
152+
mergedSource += "#line 1 " + QuoteCppString(item.Path) + "\n"
153+
mergedSource += item.GetSourceStr() + "\n"
154+
}
155+
156+
return lineOffset, mergedSource
157+
}

Diff for: arduino/builder/sketch_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ func TestLoadSketchFolderWrongMain(t *testing.T) {
8989
assert.Error(t, err)
9090
assert.Contains(t, err.Error(), "no such file or directory")
9191
}
92+
93+
func TestMergeSketchSources(t *testing.T) {
94+
// borrow the sketch from TestLoadSketchFolder to avoid boilerplate
95+
s, err := builder.LoadSketch(filepath.Join("testdata", "TestLoadSketchFolder"), "")
96+
assert.Nil(t, err)
97+
assert.NotNil(t, s)
98+
99+
// load expected result
100+
mergedPath := filepath.Join("testdata", t.Name()+".txt")
101+
mergedBytes, err := ioutil.ReadFile(mergedPath)
102+
if err != nil {
103+
t.Fatalf("unable to read golden file %s: %v", mergedPath, err)
104+
}
105+
106+
offset, source := builder.MergeSketchSources(s)
107+
assert.Equal(t, 2, offset)
108+
assert.Equal(t, string(mergedBytes), source)
109+
}

Diff for: arduino/builder/testdata/TestLoadSketchFolder/merged_sketch.txt

-16
This file was deleted.

Diff for: arduino/builder/testdata/TestMergeSketchSources.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <Arduino.h>
2+
#line 1 "testdata/TestLoadSketchFolder/TestLoadSketchFolder.ino"
3+
void setup() {
4+
5+
}
6+
7+
void loop() {
8+
9+
}
10+
#line 1 "testdata/TestLoadSketchFolder/old.pde"
11+
12+
#line 1 "testdata/TestLoadSketchFolder/other.ino"
13+
String hello() {
14+
return "world";
15+
}

0 commit comments

Comments
 (0)