Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-cli
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Operations
Operations
Metrics
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-cli
Commits
4a23cd07
Commit
4a23cd07
authored
Jul 08, 2019
by
Massimiliano Pippi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added MergeSketchSources + tests
parent
8feeb6a6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
16 deletions
+68
-16
arduino/builder/sketch.go
arduino/builder/sketch.go
+35
-0
arduino/builder/sketch_test.go
arduino/builder/sketch_test.go
+18
-0
arduino/builder/testdata/TestLoadSketchFolder/merged_sketch.txt
...o/builder/testdata/TestLoadSketchFolder/merged_sketch.txt
+0
-16
arduino/builder/testdata/TestMergeSketchSources.txt
arduino/builder/testdata/TestMergeSketchSources.txt
+15
-0
No files found.
arduino/builder/sketch.go
View file @
4a23cd07
...
...
@@ -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
}
arduino/builder/sketch_test.go
View file @
4a23cd07
...
...
@@ -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
)
}
arduino/builder/testdata/TestLoadSketchFolder/merged_sketch.txt
deleted
100644 → 0
View file @
8feeb6a6
#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";
}
arduino/builder/testdata/TestMergeSketchSources.txt
0 → 100644
View file @
4a23cd07
#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";
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment