1
1
package programmer
2
2
3
3
import (
4
+ "bufio"
5
+ "os/exec"
4
6
"path/filepath"
5
7
"regexp"
8
+ "runtime"
6
9
"strings"
7
10
"time"
8
11
12
+ "github.com/arduino/arduino-create-agent/tools"
9
13
"github.com/facchinm/go-serial"
10
14
"github.com/pkg/errors"
11
15
)
@@ -27,8 +31,8 @@ func info(l logger, args ...interface{}) {
27
31
}
28
32
}
29
33
30
- // tools can return the location of a tool in the system
31
- type tools interface {
34
+ // locater can return the location of a tool in the system
35
+ type locater interface {
32
36
GetLocation (command string ) (string , error )
33
37
}
34
38
@@ -50,8 +54,8 @@ type Extra struct {
50
54
}
51
55
52
56
// Resolve replaces some symbols in the commandline with the appropriate values
53
- // it can return an error when looking a variable in the tools
54
- func Resolve (port , board , file , commandline string , extra Extra , tools tools ) (string , error ) {
57
+ // it can return an error when looking a variable in the locater
58
+ func Resolve (port , board , file , commandline string , extra Extra , t locater ) (string , error ) {
55
59
commandline = strings .Replace (commandline , "{build.path}" , filepath .ToSlash (filepath .Dir (file )), - 1 )
56
60
commandline = strings .Replace (commandline , "{build.project_name}" , strings .TrimSuffix (filepath .Base (file ), filepath .Ext (filepath .Base (file ))), - 1 )
57
61
commandline = strings .Replace (commandline , "{serial.port}" , port , - 1 )
@@ -63,13 +67,13 @@ func Resolve(port, board, file, commandline string, extra Extra, tools tools) (s
63
67
commandline = strings .Replace (commandline , "{upload.verbose}" , extra .ParamsQuiet , - 1 )
64
68
}
65
69
66
- // search for runtime variables and replace with values from globalToolsMap
70
+ // search for runtime variables and replace with values from locater
67
71
var runtimeRe = regexp .MustCompile ("\\ {(.*?)\\ }" )
68
72
runtimeVars := runtimeRe .FindAllString (commandline , - 1 )
69
73
70
74
for _ , element := range runtimeVars {
71
75
72
- location , err := tools .GetLocation (element )
76
+ location , err := t .GetLocation (element )
73
77
if err != nil {
74
78
return "" , errors .Wrapf (err , "get location of %s" , element )
75
79
}
@@ -80,7 +84,7 @@ func Resolve(port, board, file, commandline string, extra Extra, tools tools) (s
80
84
}
81
85
82
86
// Do performs a command on a port with a board attached to it
83
- func Do (port , board , file , commandline string , extra Extra , t tools , l logger ) {
87
+ func Do (port , board , file , commandline string , extra Extra , t locater , l logger ) {
84
88
debug (l , port , board , file , commandline )
85
89
if extra .Network {
86
90
doNetwork ()
@@ -91,7 +95,7 @@ func Do(port, board, file, commandline string, extra Extra, t tools, l logger) {
91
95
92
96
func doNetwork () {}
93
97
94
- func doSerial (port , board , file , commandline string , extra Extra , t tools , l logger ) error {
98
+ func doSerial (port , board , file , commandline string , extra Extra , t locater , l logger ) error {
95
99
// some boards needs to be resetted
96
100
if extra .Use1200bpsTouch {
97
101
var err error
@@ -193,3 +197,58 @@ func waitReset(beforeReset []string, l logger) string {
193
197
194
198
return port
195
199
}
200
+
201
+ func program (binary string , args []string , l logger ) error {
202
+ // remove quotes form binary command and args
203
+ binary = strings .Replace (binary , "\" " , "" , - 1 )
204
+
205
+ for i := range args {
206
+ args [i ] = strings .Replace (args [i ], "\" " , "" , - 1 )
207
+ }
208
+
209
+ // find extension
210
+ extension := ""
211
+ if runtime .GOOS == "windows" {
212
+ extension = ".exe"
213
+ }
214
+
215
+ oscmd := exec .Command (binary , args ... )
216
+
217
+ tools .TellCommandNotToSpawnShell (oscmd )
218
+
219
+ stdout , err := oscmd .StdoutPipe ()
220
+ if err != nil {
221
+ return err
222
+ }
223
+
224
+ stderr , err := oscmd .StderrPipe ()
225
+ if err != nil {
226
+ return err
227
+ }
228
+
229
+ info (l , "Flashing with command:" + binary + extension + " " + strings .Join (args , " " ))
230
+
231
+ err = oscmd .Start ()
232
+
233
+ stdoutCopy := bufio .NewScanner (stdout )
234
+ stderrCopy := bufio .NewScanner (stderr )
235
+
236
+ stdoutCopy .Split (bufio .ScanLines )
237
+ stderrCopy .Split (bufio .ScanLines )
238
+
239
+ go func () {
240
+ for stdoutCopy .Scan () {
241
+ info (l , stdoutCopy .Text ())
242
+ }
243
+ }()
244
+
245
+ go func () {
246
+ for stderrCopy .Scan () {
247
+ info (l , stdoutCopy .Text ())
248
+ }
249
+ }()
250
+
251
+ err = oscmd .Wait ()
252
+
253
+ return err
254
+ }
0 commit comments