forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.go
268 lines (220 loc) · 7.76 KB
/
sketch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
package builder
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/pkg/errors"
)
var includesArduinoH = regexp.MustCompile(`(?m)^\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 + "\""
}
// SketchSaveItemCpp saves a preprocessed .cpp sketch file on disk
func SketchSaveItemCpp(item *sketch.Item, destPath string) error {
sketchName := filepath.Base(item.Path)
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create a folder to save the sketch")
}
destFile := filepath.Join(destPath, sketchName+".cpp")
if err := ioutil.WriteFile(destFile, item.Source, os.FileMode(0644)); err != nil {
return errors.Wrap(err, "unable to save the sketch on disk")
}
return nil
}
func simpleLocalWalkRecursive(root string, maxDepth int, walkFn func(path string, info os.FileInfo, err error) error) error {
info, err := os.Stat(root)
if err != nil {
return walkFn(root, nil, err)
}
err = walkFn(root, info, err)
if err == filepath.SkipDir {
return nil
}
if info.IsDir() {
if maxDepth <= 0 {
return walkFn(root, info, errors.New("Filesystem bottom is too deep (directory recursion or filesystem really deep): "+root))
}
maxDepth--
files, err := ioutil.ReadDir(root)
if err == nil {
for _, file := range files {
err = simpleLocalWalkRecursive(root+string(os.PathSeparator)+file.Name(), maxDepth, walkFn)
if err == filepath.SkipDir {
return nil
}
}
}
}
return nil
}
// SimpleLocalWalk locally replaces filepath.Walk and/but goes through symlinks
func SimpleLocalWalk(root string, walkFn func(path string, info os.FileInfo, err error) error) error {
// see discussion in https://github.com/arduino/arduino-cli/pull/421
return simpleLocalWalkRecursive(root, 40, walkFn)
}
// SketchLoad collects all the files composing a sketch.
// The parameter `sketchPath` holds a path pointing to a single sketch file or a sketch folder,
// the path must be absolute.
func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) {
stat, err := os.Stat(sketchPath)
if err != nil {
return nil, errors.Wrap(err, "unable to stat Sketch location")
}
var sketchFolder, mainSketchFile string
// if a sketch folder was passed, save the parent and point sketchPath to the main .ino file
if stat.IsDir() {
sketchFolder = sketchPath
mainSketchFile = filepath.Join(sketchPath, stat.Name()+".ino")
// in the case a dir was passed, ensure the main file exists and is readable
f, err := os.Open(mainSketchFile)
if err != nil {
return nil, errors.Wrap(err, "unable to find the main sketch file")
}
f.Close()
// ensure it is not a directory
info, err := os.Stat(mainSketchFile)
if err != nil {
return nil, errors.Wrap(err, "unable to check the main sketch file")
}
if info.IsDir() {
return nil, errors.Wrap(errors.New(mainSketchFile), "sketch must not be a directory")
}
} else {
sketchFolder = filepath.Dir(sketchPath)
mainSketchFile = sketchPath
}
// collect all the sketch files
var files []string
err = SimpleLocalWalk(sketchFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("\nerror: %+v\n\n", err)
return filepath.SkipDir
}
// ignore hidden files and skip hidden directories
if strings.HasPrefix(info.Name(), ".") {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// skip legacy SCM directories
if info.IsDir() && strings.HasPrefix(info.Name(), "CVS") || strings.HasPrefix(info.Name(), "RCS") {
return filepath.SkipDir
}
// ignore directory entries
if info.IsDir() {
return nil
}
// ignore if file extension doesn't match
ext := strings.ToLower(filepath.Ext(path))
_, isMain := globals.MainFileValidExtensions[ext]
_, isAdditional := globals.AdditionalFileValidExtensions[ext]
if !(isMain || isAdditional) {
return nil
}
// check if file is readable
f, err := os.Open(path)
if err != nil {
return nil
}
f.Close()
// collect the file
files = append(files, path)
// done
return nil
})
if err != nil {
return nil, errors.Wrap(err, "there was an error while collecting the sketch files")
}
return sketch.New(sketchFolder, mainSketchFile, buildPath, files)
}
// SketchMergeSources merges all the source files included in a sketch
func SketchMergeSources(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
}
// SketchCopyAdditionalFiles copies the additional files for a sketch to the
// specified destination directory.
func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath string) error {
if err := os.MkdirAll(destPath, os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create a folder to save the sketch files")
}
for _, item := range sketch.AdditionalFiles {
relpath, err := filepath.Rel(sketch.LocationPath, item.Path)
if err != nil {
return errors.Wrap(err, "unable to compute relative path to the sketch for the item")
}
targetPath := filepath.Join(destPath, relpath)
// create the directory containing the target
if err = os.MkdirAll(filepath.Dir(targetPath), os.FileMode(0755)); err != nil {
return errors.Wrap(err, "unable to create the folder containing the item")
}
err = writeIfDifferent(item.Path, targetPath)
if err != nil {
return errors.Wrap(err, "unable to write to destination file")
}
}
return nil
}
func writeIfDifferent(sourcePath, destPath string) error {
// read the source file
newbytes, err := ioutil.ReadFile(sourcePath)
if err != nil {
return errors.Wrap(err, "unable to read contents of the source item")
}
// check whether the destination file exists
_, err = os.Stat(destPath)
if os.IsNotExist(err) {
// write directly
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644))
}
// read the destination file if it ex
existingBytes, err := ioutil.ReadFile(destPath)
if err != nil {
return errors.Wrap(err, "unable to read contents of the destination item")
}
// overwrite if contents are different
if bytes.Compare(existingBytes, newbytes) != 0 {
return ioutil.WriteFile(destPath, newbytes, os.FileMode(0644))
}
// source and destination are the same, don't write anything
return nil
}