Commit 4a23cd07 authored by Massimiliano Pippi's avatar Massimiliano Pippi

added MergeSketchSources + tests

parent 8feeb6a6
......@@ -19,6 +19,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/arduino/arduino-cli/arduino/globals"
......@@ -27,6 +28,17 @@ import (
"github.com/pkg/errors"
)
var includesArduinoH = regexp.MustCompile(`^\s*#\s*include\s*[<\"]Arduino\.h[>\"]`)
// QuoteCppString returns the given string as a quoted string for use with the C
// preprocessor. This adds double quotes around it and escapes any
// double quotes and backslashes in the string.
func QuoteCppString(str string) string {
str = strings.Replace(str, "\\", "\\\\", -1)
str = strings.Replace(str, "\"", "\\\"", -1)
return "\"" + str + "\""
}
// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
......@@ -120,3 +132,26 @@ func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) {
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
}
// MergeSketchSources merges all the source files included in a sketch
func MergeSketchSources(sketch *sketch.Sketch) (int, string) {
lineOffset := 0
mergedSource := ""
// add Arduino.h inclusion directive if missing
if !includesArduinoH.MatchString(sketch.MainFile.GetSourceStr()) {
mergedSource += "#include <Arduino.h>\n"
lineOffset++
}
mergedSource += "#line 1 " + QuoteCppString(sketch.MainFile.Path) + "\n"
mergedSource += sketch.MainFile.GetSourceStr() + "\n"
lineOffset++
for _, item := range sketch.OtherSketchFiles {
mergedSource += "#line 1 " + QuoteCppString(item.Path) + "\n"
mergedSource += item.GetSourceStr() + "\n"
}
return lineOffset, mergedSource
}
......@@ -89,3 +89,21 @@ func TestLoadSketchFolderWrongMain(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "no such file or directory")
}
func TestMergeSketchSources(t *testing.T) {
// borrow the sketch from TestLoadSketchFolder to avoid boilerplate
s, err := builder.LoadSketch(filepath.Join("testdata", "TestLoadSketchFolder"), "")
assert.Nil(t, err)
assert.NotNil(t, s)
// load expected result
mergedPath := filepath.Join("testdata", t.Name()+".txt")
mergedBytes, err := ioutil.ReadFile(mergedPath)
if err != nil {
t.Fatalf("unable to read golden file %s: %v", mergedPath, err)
}
offset, source := builder.MergeSketchSources(s)
assert.Equal(t, 2, offset)
assert.Equal(t, string(mergedBytes), source)
}
#include <Arduino.h>
#line 1 {{QuoteCppString .sketch.MainFile.Name}}
#line 1 {{QuoteCppString .sketch.MainFile.Name}}
void setup() {
}
void loop() {
}
#line 1 {{QuoteCppString (index .sketch.OtherSketchFiles 0).Name}}
#line 1 {{QuoteCppString (index .sketch.OtherSketchFiles 1).Name}}
String hello() {
return "world";
}
#include <Arduino.h>
#line 1 "testdata/TestLoadSketchFolder/TestLoadSketchFolder.ino"
void setup() {
}
void loop() {
}
#line 1 "testdata/TestLoadSketchFolder/old.pde"
#line 1 "testdata/TestLoadSketchFolder/other.ino"
String hello() {
return "world";
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment