Skip to content

Commit 2ed118b

Browse files
committed
Upload and Compile now accept both Alias and FQBN
1 parent 23306ba commit 2ed118b

File tree

12 files changed

+144
-99
lines changed

12 files changed

+144
-99
lines changed

arduino/cores/packagemanager/package_manager.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,20 @@ func (pm *PackageManager) FindBoardsWithID(id string) []*cores.Board {
116116
return res
117117
}
118118

119-
// FindBoardWithFQBN returns the board identified by the fqbn, or an error
120-
func (pm *PackageManager) FindBoardWithFQBN(fqbnIn string) (*cores.Board, error) {
121-
fqbn, err := cores.ParseFQBN(fqbnIn)
119+
// FindBoard search the board identified by boardArg (board alias or board fqbn).
120+
// It returns FQBN, RegistereBoard and Board or an error if not found.
121+
// The board may be present in registry but not installed, in this case o
122+
func (pm *PackageManager) FindBoard(boardArg string) (*cores.FQBN, *RegisteredBoard, *cores.Board, error) {
123+
fqbn, registeredBoard, err := pm.Registry.FindBoard(boardArg)
122124
if err != nil {
123-
return nil, fmt.Errorf("parsing fqbn: %s", err)
125+
return fqbn, registeredBoard, nil, err
124126
}
127+
board, err := pm.FindBoardWithFQBN(fqbn)
128+
return fqbn, registeredBoard, board, err
129+
}
125130

131+
// FindBoardWithFQBN returns the board identified by the fqbn, or an error
132+
func (pm *PackageManager) FindBoardWithFQBN(fqbn *cores.FQBN) (*cores.Board, error) {
126133
_, _, board, _, _, err := pm.ResolveFQBN(fqbn)
127134
return board, err
128135
}

arduino/cores/packagemanager/package_manager_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ func TestFindBoardWithFQBN(t *testing.T) {
4141
pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
4242
pm.LoadHardwareFromDirectory(customHardware)
4343

44-
board, err := pm.FindBoardWithFQBN("arduino:avr:uno")
44+
_, _, board, err := pm.FindBoard("arduino:avr:uno")
4545
require.Nil(t, err)
4646
require.NotNil(t, board)
4747
require.Equal(t, board.Name(), "Arduino/Genuino Uno")
4848

49-
board, err = pm.FindBoardWithFQBN("arduino:avr:mega")
49+
_, _, board, err = pm.FindBoard("arduino:avr:mega")
5050
require.Nil(t, err)
5151
require.NotNil(t, board)
5252
require.Equal(t, board.Name(), "Arduino/Genuino Mega or Mega 2560")
@@ -178,7 +178,7 @@ func TestBoardOptionsFunctions(t *testing.T) {
178178
pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
179179
pm.LoadHardwareFromDirectory(customHardware)
180180

181-
nano, err := pm.FindBoardWithFQBN("arduino:avr:nano")
181+
_, _, nano, err := pm.FindBoard("arduino:avr:nano")
182182
require.Nil(t, err)
183183
require.NotNil(t, nano)
184184
require.Equal(t, nano.Name(), "Arduino Nano")
@@ -194,7 +194,7 @@ func TestBoardOptionsFunctions(t *testing.T) {
194194
expectedNanoCPUValues.Set("atmega168", "ATmega168")
195195
require.EqualValues(t, expectedNanoCPUValues, nanoCPUValues)
196196

197-
esp8266, err := pm.FindBoardWithFQBN("esp8266:esp8266:generic")
197+
_, _, esp8266, err := pm.FindBoard("esp8266:esp8266:generic")
198198
require.Nil(t, err)
199199
require.NotNil(t, esp8266)
200200
require.Equal(t, esp8266.Name(), "Generic ESP8266 Module")
@@ -230,7 +230,7 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
230230
loadIndex("http://arduino.esp8266.com/stable/package_esp8266com_index.json")
231231
loadIndex("https://adafruit.github.io/arduino-board-index/package_adafruit_index.json")
232232
require.NoError(t, pm.LoadHardware())
233-
esp32, err := pm.FindBoardWithFQBN("esp32:esp32:esp32")
233+
_, _, esp32, err := pm.FindBoard("esp32:esp32:esp32")
234234
require.NoError(t, err)
235235
esptool231 := pm.FindToolDependency(&cores.ToolDependency{
236236
ToolPackager: "esp32",
@@ -266,7 +266,7 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
266266
testConflictingToolsInDifferentPackages()
267267
testConflictingToolsInDifferentPackages()
268268

269-
feather, err := pm.FindBoardWithFQBN("adafruit:samd:adafruit_feather_m0_express")
269+
_, _, feather, err := pm.FindBoard("adafruit:samd:adafruit_feather_m0_express")
270270
require.NoError(t, err)
271271
require.NotNil(t, feather)
272272
featherTools, err := pm.FindToolsRequiredForBoard(feather)

arduino/cores/packagemanager/registry.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ func (r *BoardsRegistry) addBoard(board *RegisteredBoard) {
5050
r.aliastToBoard[board.Alias] = board
5151
}
5252

53-
func (r *BoardsRegistry) FindBoard(fqbnOrAlias string) *RegisteredBoard {
53+
func (r *BoardsRegistry) FindBoard(fqbnOrAlias string) (*cores.FQBN, *RegisteredBoard, error) {
5454
if found, ok := r.aliastToBoard[fqbnOrAlias]; ok {
55-
return found
55+
return found.FQBN, found, nil
5656
}
57-
if found, ok := r.fqbnToBoard[fqbnOrAlias]; ok {
58-
return found
57+
fqbn, err := cores.ParseFQBN(fqbnOrAlias)
58+
if err != nil {
59+
return nil, nil, err
5960
}
60-
return nil
61+
if found, ok := r.fqbnToBoard[fqbn.StringWithoutConfig()]; ok {
62+
return fqbn, found, nil
63+
}
64+
return fqbn, nil, nil
6165
}
6266

6367
func (r *BoardsRegistry) SearchBoards(query string) []*RegisteredBoard {

cli/compile/compile.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
var (
36-
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
36+
boardArg string // Board Alias or Board FQBN
3737
showProperties bool // Show all build preferences used instead of compiling.
3838
preprocess bool // Print preprocessed code to stdout.
3939
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
@@ -63,7 +63,9 @@ func NewCommand() *cobra.Command {
6363
Run: run,
6464
}
6565

66-
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
66+
command.Flags().StringVarP(&boardArg, "board", "b", "", "Board Alias or Board FQBN (e.g. 'arduino:avr:uno')")
67+
command.Flags().StringVarP(&boardArg, "fqbn", "", "", "")
68+
command.Flags().MarkDeprecated("fqbn", "use --board instead.")
6769
command.Flags().BoolVar(&showProperties, "show-properties", false, "Show all build properties used instead of compiling.")
6870
command.Flags().BoolVar(&preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
6971
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", "Builds of 'core.a' are saved into this path to be cached and reused.")
@@ -104,7 +106,7 @@ func run(cmd *cobra.Command, args []string) {
104106

105107
_, err = compile.Compile(context.Background(), &rpc.CompileReq{
106108
Instance: inst,
107-
Fqbn: fqbn,
109+
Board: boardArg,
108110
SketchPath: sketchPath.String(),
109111
ShowProperties: showProperties,
110112
Preprocess: preprocess,
@@ -129,7 +131,7 @@ func run(cmd *cobra.Command, args []string) {
129131
if uploadAfterCompile {
130132
_, err := upload.Upload(context.Background(), &rpc.UploadReq{
131133
Instance: inst,
132-
Fqbn: fqbn,
134+
Board: boardArg,
133135
SketchPath: sketchPath.String(),
134136
Port: port,
135137
Verbose: verbose,

cli/upload/upload.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
var (
33-
fqbn string
33+
boardArg string
3434
port string
3535
verbose bool
3636
verify bool
@@ -48,7 +48,9 @@ func NewCommand() *cobra.Command {
4848
Run: run,
4949
}
5050

51-
uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
51+
uploadCommand.Flags().StringVarP(&boardArg, "board", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
52+
uploadCommand.Flags().StringVarP(&boardArg, "fqbn", "", "", "")
53+
uploadCommand.Flags().MarkDeprecated("fqbn", "use --board instead.")
5254
uploadCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
5355
uploadCommand.Flags().StringVarP(&importFile, "input", "i", "", "Input file to be uploaded.")
5456
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
@@ -72,7 +74,7 @@ func run(command *cobra.Command, args []string) {
7274

7375
if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
7476
Instance: instance,
75-
Fqbn: fqbn,
77+
Board: boardArg,
7678
SketchPath: sketchPath.String(),
7779
Port: port,
7880
Verbose: verbose,

commands/board/install.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ func Install(ctx context.Context, req *rpc.BoardInstallReq,
3535
return nil, errors.New("invalid instance")
3636
}
3737

38-
board := pm.Registry.FindBoard(req.GetBoard())
39-
if board == nil {
40-
return nil, errors.Errorf("board '%s' not found", req.GetBoard())
38+
fqbn, _, _, err := pm.FindBoard(req.GetBoard())
39+
if err != nil {
40+
return nil, errors.Errorf("board '%s' not found: %s", req.GetBoard(), err)
4141
}
4242

4343
platformInstallReq := &rpc.PlatformInstallReq{
4444
Instance: req.GetInstance(),
45-
PlatformPackage: board.FQBN.Package,
46-
Architecture: board.FQBN.PlatformArch,
45+
PlatformPackage: fqbn.Package,
46+
Architecture: fqbn.PlatformArch,
4747
}
4848
if _, err := core.PlatformInstall(ctx, platformInstallReq, downloadCB, taskCB, downloaderHeaders); err != nil {
4949
return nil, errors.WithMessage(err, "installing board platforms")

commands/compile/compile.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ package compile
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"io"
2322
"path/filepath"
2423
"sort"
2524
"strconv"
2625
"strings"
2726

28-
"github.com/arduino/arduino-cli/arduino/cores"
2927
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
3028
"github.com/arduino/arduino-cli/arduino/sketches"
3129
"github.com/arduino/arduino-cli/commands"
@@ -37,6 +35,7 @@ import (
3735
"github.com/arduino/arduino-cli/telemetry"
3836
paths "github.com/arduino/go-paths-helper"
3937
properties "github.com/arduino/go-properties-orderedmap"
38+
"github.com/pkg/errors"
4039
"github.com/segmentio/stats/v4"
4140
"github.com/sirupsen/logrus"
4241
"github.com/spf13/viper"
@@ -46,6 +45,7 @@ import (
4645
func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResp, e error) {
4746

4847
tags := map[string]string{
48+
"board": req.Board,
4949
"fqbn": req.Fqbn,
5050
"sketchPath": telemetry.Sanitize(req.SketchPath),
5151
"showProperties": strconv.FormatBool(req.ShowProperties),
@@ -85,16 +85,20 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
8585
return nil, fmt.Errorf("opening sketch: %s", err)
8686
}
8787

88-
fqbnIn := req.GetFqbn()
89-
if fqbnIn == "" && sketch != nil && sketch.Metadata != nil {
90-
fqbnIn = sketch.Metadata.CPU.Fqbn
88+
boardArg := req.GetBoard()
89+
if boardArg == "" {
90+
boardArg = req.GetFqbn() // DEPRECATED: Keep Fqbn field working for old clients.
9191
}
92-
if fqbnIn == "" {
93-
return nil, fmt.Errorf("no FQBN provided")
92+
if boardArg == "" && sketch != nil && sketch.Metadata != nil {
93+
boardArg = sketch.Metadata.CPU.Fqbn
9494
}
95-
fqbn, err := cores.ParseFQBN(fqbnIn)
95+
if boardArg == "" {
96+
return nil, errors.Errorf("no board provided")
97+
}
98+
99+
fqbn, _, _, err := pm.FindBoard(boardArg)
96100
if err != nil {
97-
return nil, fmt.Errorf("incorrect FQBN: %s", err)
101+
return nil, errors.Errorf("board '%s' not found: %s", req.GetBoard(), err)
98102
}
99103

100104
targetPlatform := pm.FindPlatform(&packagemanager.PlatformReference{
@@ -254,6 +258,6 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
254258
}
255259
}
256260

257-
logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
261+
logrus.Tracef("Compile %s for %s successful", sketch.Name, boardArg)
258262
return &rpc.CompileResp{}, nil
259263
}

commands/upload/upload.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
rpc "github.com/arduino/arduino-cli/rpc/commands"
3434
paths "github.com/arduino/go-paths-helper"
3535
properties "github.com/arduino/go-properties-orderedmap"
36+
"github.com/pkg/errors"
3637
"github.com/sirupsen/logrus"
3738
"go.bug.st/serial"
3839
)
@@ -67,19 +68,22 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
6768
return nil, fmt.Errorf("no upload port provided")
6869
}
6970

70-
fqbnIn := req.GetFqbn()
71-
if fqbnIn == "" && sketch != nil && sketch.Metadata != nil {
72-
fqbnIn = sketch.Metadata.CPU.Fqbn
71+
boardArg := req.GetBoard()
72+
if boardArg == "" {
73+
boardArg = req.GetFqbn() // DEPRECATION: Keep compatiblity with old clients using Fqbn
7374
}
74-
if fqbnIn == "" {
75-
return nil, fmt.Errorf("no Fully Qualified Board Name provided")
75+
if boardArg == "" && sketch != nil && sketch.Metadata != nil {
76+
boardArg = sketch.Metadata.CPU.Fqbn
7677
}
77-
fqbn, err := cores.ParseFQBN(fqbnIn)
78-
if err != nil {
79-
return nil, fmt.Errorf("incorrect FQBN: %s", err)
78+
if boardArg == "" {
79+
return nil, errors.Errorf("no board specified")
8080
}
8181

8282
pm := commands.GetPackageManager(req.GetInstance().GetId())
83+
fqbn, _, _, err := pm.FindBoard(boardArg)
84+
if err != nil {
85+
return nil, errors.Errorf("board '%s' not found: %s", req.GetBoard(), err)
86+
}
8387

8488
// Find target board and board properties
8589
_, _, board, boardProperties, _, err := pm.ResolveFQBN(fqbn)
@@ -258,7 +262,7 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
258262
return nil, fmt.Errorf("uploading error: %s", err)
259263
}
260264

261-
logrus.Tracef("Upload %s on %s successful", sketch.Name, fqbnIn)
265+
logrus.Tracef("Upload %s on %s successful", sketch.Name, boardArg)
262266

263267
return &rpc.UploadResp{}, nil
264268
}

0 commit comments

Comments
 (0)