Skip to content

Commit 2a6a03e

Browse files
committed
use sketch.Item
1 parent 6fb3d48 commit 2a6a03e

File tree

6 files changed

+94
-7
lines changed

6 files changed

+94
-7
lines changed

Diff for: arduino/builder/sketch.go

+85-3
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,104 @@ import (
1919
"io/ioutil"
2020
"os"
2121
"path/filepath"
22+
"strings"
23+
24+
"github.com/arduino/arduino-cli/arduino/globals"
25+
"github.com/arduino/arduino-cli/arduino/sketch"
2226

2327
"github.com/pkg/errors"
2428
)
2529

26-
// SaveSketch saves a preprocessed .cpp sketch file on disk
27-
func SaveSketch(sketchName string, source string, buildPath string) error {
30+
// SaveSketchItemCpp saves a preprocessed .cpp sketch file on disk
31+
func SaveSketchItemCpp(item *sketch.Item, buildPath string) error {
32+
33+
sketchName := filepath.Base(item.Path)
2834

2935
if err := os.MkdirAll(buildPath, os.FileMode(0755)); err != nil {
3036
return errors.Wrap(err, "unable to create a folder to save the sketch")
3137
}
3238

3339
destFile := filepath.Join(buildPath, sketchName+".cpp")
3440

35-
if err := ioutil.WriteFile(destFile, []byte(source), os.FileMode(0644)); err != nil {
41+
if err := ioutil.WriteFile(destFile, item.Source, os.FileMode(0644)); err != nil {
3642
return errors.Wrap(err, "unable to save the sketch on disk")
3743
}
3844

3945
return nil
4046
}
47+
48+
// LoadSketch collects all the files composing a sketch.
49+
// The parameter `sketchPath` holds a path pointing to a single sketch file or a sketch folder,
50+
// the path must be absolute.
51+
func LoadSketch(sketchPath, buildPath string) (*sketch.Sketch, error) {
52+
stat, err := os.Stat(sketchPath)
53+
if err != nil {
54+
return nil, errors.Wrap(err, "unable to stat Sketch location")
55+
}
56+
57+
var sketchFolder, mainSketchFile string
58+
59+
// if a sketch folder was passed, save the parent and point sketchPath to the main .ino file
60+
if stat.IsDir() {
61+
sketchFolder = sketchPath
62+
mainSketchFile = filepath.Join(sketchPath, stat.Name()+".ino")
63+
// in the case a dir was passed, ensure the main file exists and is readable
64+
f, err := os.Open(mainSketchFile)
65+
if err != nil {
66+
return nil, errors.Wrap(err, "unable to find the main sketch file")
67+
}
68+
f.Close()
69+
} else {
70+
sketchFolder = filepath.Dir(sketchPath)
71+
mainSketchFile = sketchPath
72+
}
73+
74+
// collect all the sketch files
75+
var files []string
76+
err = filepath.Walk(sketchFolder, func(path string, info os.FileInfo, err error) error {
77+
// ignore hidden files and skip hidden directories
78+
if strings.HasPrefix(info.Name(), ".") {
79+
if info.IsDir() {
80+
return filepath.SkipDir
81+
}
82+
return nil
83+
}
84+
85+
// skip legacy SCM directories
86+
if info.IsDir() && strings.HasPrefix(info.Name(), "CVS") || strings.HasPrefix(info.Name(), "RCS") {
87+
return filepath.SkipDir
88+
}
89+
90+
// ignore directory entries
91+
if info.IsDir() {
92+
return nil
93+
}
94+
95+
// ignore if file extension doesn't match
96+
ext := strings.ToLower(filepath.Ext(path))
97+
_, isMain := globals.MainFileValidExtensions[ext]
98+
_, isAdditional := globals.AdditionalFileValidExtensions[ext]
99+
if !(isMain || isAdditional) {
100+
return nil
101+
}
102+
103+
// check if file is readable
104+
f, err := os.Open(path)
105+
if err != nil {
106+
return nil
107+
}
108+
f.Close()
109+
110+
// collect the file
111+
files = append(files, path)
112+
113+
// done
114+
return nil
115+
})
116+
117+
if err != nil {
118+
return nil, errors.Wrap(err, "there was an error while collecting the sketch files")
119+
}
120+
121+
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
122+
}

Diff for: arduino/builder/sketch_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323

2424
"github.com/arduino/arduino-cli/arduino/builder"
25+
"github.com/arduino/arduino-cli/arduino/sketch"
2526
"github.com/stretchr/testify/assert"
2627
)
2728

@@ -36,7 +37,7 @@ func TestSaveSketch(t *testing.T) {
3637
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
3738
}
3839

39-
builder.SaveSketch(sketchName, string(source), tmp)
40+
builder.SaveSketchItemCpp(&sketch.Item{Path: sketchName, Source: source}, tmp)
4041

4142
out, err := ioutil.ReadFile(filepath.Join(tmp, outName))
4243
if err != nil {

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U=
22
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
33
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
44
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
5+
github.com/arduino/arduino v0.0.0-20190521072917-00a7546fb43e h1:5TJufBg+nZAFruNEALBVHDPRMtMDbRg7Hox/WA4ZI40=
56
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c h1:agh2JT96G8egU7FEb13L4dq3fnCN7lxXhJ86t69+W7s=
67
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c/go.mod h1:HK7SpkEax/3P+0w78iRQx1sz1vCDYYw9RXwHjQTB5i8=
78
github.com/arduino/go-paths-helper v0.0.0-20190214132331-c3c98d1bf2e1 h1:S0NpDSqjlkNA510vmRCP5Cq9mPgu3rWDSdeN4SI1Mwc=

Diff for: legacy/builder/container_add_prototypes.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package builder
3131

3232
import (
3333
bldr "github.com/arduino/arduino-cli/arduino/builder"
34+
"github.com/arduino/arduino-cli/arduino/sketch"
3435
"github.com/arduino/arduino-cli/legacy/builder/constants"
3536
"github.com/arduino/arduino-cli/legacy/builder/i18n"
3637
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -67,7 +68,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
6768
}
6869
}
6970

70-
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
71+
if err := bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
7172
return i18n.WrapError(err)
7273
}
7374

Diff for: legacy/builder/container_merge_copy_sketch_files.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package builder
3131

3232
import (
3333
bldr "github.com/arduino/arduino-cli/arduino/builder"
34+
"github.com/arduino/arduino-cli/arduino/sketch"
3435
"github.com/arduino/arduino-cli/legacy/builder/i18n"
3536
"github.com/arduino/arduino-cli/legacy/builder/types"
3637
)
@@ -42,7 +43,7 @@ func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error {
4243
return i18n.WrapError(err)
4344
}
4445

45-
if err := bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String()); err != nil {
46+
if err := bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String()); err != nil {
4647
return i18n.WrapError(err)
4748
}
4849

Diff for: legacy/builder/preprocess_sketch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"strings"
3939

4040
bldr "github.com/arduino/arduino-cli/arduino/builder"
41+
"github.com/arduino/arduino-cli/arduino/sketch"
4142
"github.com/arduino/arduino-cli/legacy/builder/constants"
4243
"github.com/arduino/arduino-cli/legacy/builder/i18n"
4344
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -81,7 +82,7 @@ func (s *PreprocessSketchArduino) Run(ctx *types.Context) error {
8182
if ctx.CodeCompleteAt != "" {
8283
err = new(OutputCodeCompletions).Run(ctx)
8384
} else {
84-
err = bldr.SaveSketch(ctx.Sketch.MainFile.Name.Base(), ctx.Source, ctx.SketchBuildPath.String())
85+
err = bldr.SaveSketchItemCpp(&sketch.Item{ctx.Sketch.MainFile.Name.String(), []byte(ctx.Source)}, ctx.SketchBuildPath.String())
8586
}
8687

8788
return err

0 commit comments

Comments
 (0)