Skip to content

Commit 4b4e1e7

Browse files
committed
Use sketch project file to determine default fqbn and port
1 parent b282fbe commit 4b4e1e7

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

Diff for: arduino/sketch/sketch.go

+12
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ func (s *Sketch) GetProjectPath() *paths.Path {
296296
return projectFile
297297
}
298298

299+
// GetDefaultFQBN returns the default FQBN for the sketch (from the sketch.yaml project file), or the
300+
// empty string if not set.
301+
func (s *Sketch) GetDefaultFQBN() string {
302+
return s.Project.DefaultFqbn
303+
}
304+
305+
// GetDefaultPortAddressAndProtocol returns the default port address and port protocol for the sketch
306+
// (from the sketch.yaml project file), or empty strings if not set.
307+
func (s *Sketch) GetDefaultPortAddressAndProtocol() (string, string) {
308+
return s.Project.DefaultPort, s.Project.DefaultProtocol
309+
}
310+
299311
// InvalidSketchFolderNameError is returned when the sketch directory doesn't match the sketch name
300312
type InvalidSketchFolderNameError struct {
301313
SketchFolder *paths.Path

Diff for: cli/arguments/fqbn.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,15 @@ func (f *Fqbn) Set(fqbn string) {
6969
// - it tries to autodetect the board connected to the given port flags
7070
// If all above methods fails, it returns the empty string.
7171
// The Port metadata are always returned except if:
72-
// - the port is not found, in this case nil is returned
73-
// - the FQBN autodetection fail, in this case the function prints an error and
74-
// terminates the execution
72+
// - the port is not found, in this case nil is returned
73+
// - the FQBN autodetection fail, in this case the function prints an error and
74+
// terminates the execution
7575
func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance, sk *sketch.Sketch) (string, *rpc.Port) {
7676
// TODO: REMOVE sketch.Sketch from here
7777

7878
fqbn := fqbnArg.String()
79-
if fqbn == "" && sk != nil && sk.Metadata != nil {
80-
// If the user didn't specify an FQBN and a sketch.json file is present
81-
// read it from there.
82-
fqbn = sk.Metadata.CPU.Fqbn
79+
if fqbn == "" && sk != nil {
80+
fqbn = sk.GetDefaultFQBN()
8381
}
8482
if fqbn == "" {
8583
if portArgs == nil || portArgs.address == "" {

Diff for: cli/arguments/port.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package arguments
1717

1818
import (
1919
"fmt"
20-
"net/url"
2120
"os"
2221
"time"
2322

@@ -80,18 +79,8 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
8079
address := p.address
8180
protocol := p.protocol
8281

83-
if address == "" && sk != nil && sk.Metadata != nil {
84-
// This is for compatibility with old sketch.json where the address
85-
// for serial ports was stored as URL like serial:///dev/ttyACM0, so we
86-
// check if we have a URI and the scheme is "serial". In all other
87-
// cases we pick the address as is.
88-
deviceURI, err := url.Parse(sk.Metadata.CPU.Port)
89-
if err == nil && deviceURI.Scheme == "serial" {
90-
address = deviceURI.Host + deviceURI.Path
91-
} else {
92-
address = sk.Metadata.CPU.Port
93-
}
94-
protocol = sk.Metadata.CPU.Protocol
82+
if address == "" && sk != nil {
83+
address, protocol = sk.GetDefaultPortAddressAndProtocol()
9584
}
9685
if address == "" {
9786
// If no address is provided we assume the user is trying to upload

Diff for: commands/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
7676
}
7777

7878
fqbnIn := req.GetFqbn()
79-
if fqbnIn == "" && sk != nil && sk.Metadata != nil {
80-
fqbnIn = sk.Metadata.CPU.Fqbn
79+
if fqbnIn == "" && sk != nil {
80+
fqbnIn = sk.GetDefaultFQBN()
8181
}
8282
if fqbnIn == "" {
8383
return nil, &arduino.MissingFQBNError{}

Diff for: commands/debug/debug_info.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func getDebugProperties(req *debug.DebugConfigRequest, pme *packagemanager.Explo
5454

5555
// XXX Remove this code duplication!!
5656
fqbnIn := req.GetFqbn()
57-
if fqbnIn == "" && sk != nil && sk.Metadata != nil {
58-
fqbnIn = sk.Metadata.CPU.Fqbn
57+
if fqbnIn == "" && sk != nil {
58+
fqbnIn = sk.GetDefaultFQBN()
5959
}
6060
if fqbnIn == "" {
6161
return nil, &arduino.MissingFQBNError{}

0 commit comments

Comments
 (0)