Skip to content

Commit 1ed3b66

Browse files
matteosuppofacchinm
authored andcommitted
add resolve function
1 parent 9c190d2 commit 1ed3b66

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

programmer/programmer.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,33 @@ type Extra struct {
4242
}
4343

4444
// Do performs a command on a port with a board attached to it
45-
func Do(port, board, path, commandline string, extra Extra, l logger) {
46-
debug(l, port, board, path, commandline)
45+
func Do(port, board, file, commandline string, extra Extra, l logger) {
46+
debug(l, port, board, file, commandline)
4747
if extra.Network {
4848
doNetwork()
4949
} else {
50-
doSerial(port, extra, l)
50+
doSerial(port, board, file, commandline, extra, l)
5151
}
5252
}
5353

5454
func doNetwork() {}
5555

56-
func doSerial(port string, extra Extra, l logger) {
56+
func doSerial(port, board, file, commandline string, extra Extra, l logger) error {
5757
// some boards needs to be resetted
5858
if extra.Use1200bpsTouch {
59-
port, _ = reset(port, extra.WaitForUploadPort, l)
59+
var err error
60+
port, err = reset(port, extra.WaitForUploadPort, l)
61+
if err != nil {
62+
return errors.Wrapf(err, "Reset before upload")
63+
}
6064
}
65+
66+
// resolve commandline
67+
info(l, "unresolved commandline ", commandline)
68+
commandline = resolve(port, board, file, commandline, extra)
69+
info(l, "resolved commandline ", commandline)
70+
71+
return nil
6172
}
6273

6374
// reset opens the port at 1200bps. It returns the new port name (which could change

programmer/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package programmer
22

3+
import (
4+
"path/filepath"
5+
"strings"
6+
)
7+
38
// differ returns the first item that differ between the two input slices
49
func differ(slice1 []string, slice2 []string) string {
510
m := map[string]int{}
@@ -19,3 +24,18 @@ func differ(slice1 []string, slice2 []string) string {
1924

2025
return ""
2126
}
27+
28+
// resolve replaces some symbols in the commandline with the appropriate values
29+
func resolve(port, board, file, commandline string, extra Extra) string {
30+
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
31+
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
32+
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
33+
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
34+
35+
if extra.Verbose == true {
36+
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)
37+
} else {
38+
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsQuiet, -1)
39+
}
40+
return commandline
41+
}

programmer/utils_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package programmer
22

3-
import "testing"
3+
import (
4+
"log"
5+
"testing"
6+
)
47

58
var TestDifferData = []struct {
69
Before []string
@@ -9,6 +12,7 @@ var TestDifferData = []struct {
912
}{
1013
{[]string{}, []string{}, ""},
1114
{[]string{"/dev/ttyACM0", "/dev/sys"}, []string{"/dev/sys"}, "/dev/ttyACM0"},
15+
{[]string{"/dev/sys"}, []string{"/dev/ttyACM0", "/dev/sys"}, "/dev/ttyACM0"},
1216
}
1317

1418
func TestDiffer(t *testing.T) {
@@ -20,3 +24,28 @@ func TestDiffer(t *testing.T) {
2024
}
2125
}
2226
}
27+
28+
var TestResolveData = []struct {
29+
Port string
30+
Board string
31+
File string
32+
Commandline string
33+
Extra Extra
34+
Result string
35+
}{
36+
{"/dev/ttyACM0", "arduino:avr:leonardo", "./programmer_test.hex",
37+
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verbose} {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`, Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
38+
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./programmer_test.hex:i"`},
39+
}
40+
41+
func TestResolve(t *testing.T) {
42+
for _, test := range TestResolveData {
43+
result := resolve(test.Port, test.Board, test.File, test.Commandline, test.Extra)
44+
if result != test.Result {
45+
log.Println(result)
46+
log.Println(test.Result)
47+
t.Error("expected " + test.Result + ", got " + result)
48+
continue
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)