Skip to content

Commit 139c2df

Browse files
committed
Change board list output sorting
Calling board list command now returns the detected boards sorted by FQBN alphabetically, Arduino boards are always shown before all the others.
1 parent d8cca4b commit 139c2df

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

Diff for: commands/board/list.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"net/http"
2323
"regexp"
24+
"sort"
2425
"sync"
2526

2627
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -117,11 +118,16 @@ func identify(pm *packagemanager.PackageManager, port *commands.BoardPort) ([]*r
117118
// first query installed cores through the Package Manager
118119
logrus.Debug("Querying installed cores for board identification...")
119120
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
121+
// We need the Platform maintaner for sorting so we set it here
122+
platform := &rpc.Platform{
123+
Maintainer: board.PlatformRelease.Platform.Package.Maintainer,
124+
}
120125
boards = append(boards, &rpc.BoardListItem{
121-
Name: board.Name(),
122-
Fqbn: board.FQBN(),
123-
Vid: port.IdentificationPrefs.Get("vid"),
124-
Pid: port.IdentificationPrefs.Get("pid"),
126+
Name: board.Name(),
127+
Fqbn: board.FQBN(),
128+
Platform: platform,
129+
Vid: port.IdentificationPrefs.Get("vid"),
130+
Pid: port.IdentificationPrefs.Get("pid"),
125131
})
126132
}
127133

@@ -142,6 +148,25 @@ func identify(pm *packagemanager.PackageManager, port *commands.BoardPort) ([]*r
142148
// boards)
143149
boards = items
144150
}
151+
152+
// Sort by FQBN alphabetically
153+
sort.Slice(boards, func(i, j int) bool {
154+
return boards[i].Fqbn < boards[j].Fqbn
155+
})
156+
157+
// Put Arduino boards before others in case there are non Arduino boards with identical VID:PID combination
158+
sort.SliceStable(boards, func(i, j int) bool {
159+
if boards[i].Platform.Maintainer == "Arduino" && boards[i].Platform.Maintainer != "Arduino" {
160+
return true
161+
}
162+
return false
163+
})
164+
165+
// We need the Board's Platform only for sorting but it shouldn't be present in the output
166+
for _, board := range boards {
167+
board.Platform = nil
168+
}
169+
145170
return boards, nil
146171
}
147172

Diff for: commands/board/list_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ import (
1919
"fmt"
2020
"net/http"
2121
"net/http/httptest"
22+
"os"
2223
"testing"
2324

25+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2426
"github.com/arduino/arduino-cli/commands"
2527
"github.com/arduino/arduino-cli/configuration"
28+
"github.com/arduino/go-paths-helper"
2629
"github.com/arduino/go-properties-orderedmap"
2730
"github.com/stretchr/testify/require"
31+
semver "go.bug.st/relaxed-semver"
2832
)
2933

3034
func init() {
@@ -109,3 +113,53 @@ func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
109113
require.Equal(t, err, ErrNotFound)
110114
require.Empty(t, items)
111115
}
116+
117+
func TestBoardIdentifySorting(t *testing.T) {
118+
dataDir := paths.TempDir().Join("test", "data_dir")
119+
os.Setenv("ARDUINO_DATA_DIR", dataDir.String())
120+
dataDir.MkdirAll()
121+
defer paths.TempDir().Join("test").RemoveAll()
122+
123+
// We don't really care about the paths in this case
124+
pm := packagemanager.NewPackageManager(dataDir, dataDir, dataDir, dataDir)
125+
126+
// Create some boards with identical VID:PID combination
127+
pack := pm.Packages.GetOrCreatePackage("packager")
128+
pack.Maintainer = "NotArduino"
129+
platform := pack.GetOrCreatePlatform("platform")
130+
platformRelease := platform.GetOrCreateRelease(semver.MustParse("0.0.0"))
131+
platformRelease.InstallDir = dataDir
132+
board := platformRelease.GetOrCreateBoard("boardA")
133+
board.Properties.Set("vid", "0x0000")
134+
board.Properties.Set("pid", "0x0000")
135+
board = platformRelease.GetOrCreateBoard("boardB")
136+
board.Properties.Set("vid", "0x0000")
137+
board.Properties.Set("pid", "0x0000")
138+
139+
// Create some Arduino boards with same VID:PID combination as boards created previously
140+
pack = pm.Packages.GetOrCreatePackage("arduino")
141+
pack.Maintainer = "Arduino"
142+
platform = pack.GetOrCreatePlatform("avr")
143+
platformRelease = platform.GetOrCreateRelease(semver.MustParse("0.0.0"))
144+
platformRelease.InstallDir = dataDir
145+
board = platformRelease.GetOrCreateBoard("nessuno")
146+
board.Properties.Set("vid", "0x0000")
147+
board.Properties.Set("pid", "0x0000")
148+
board = platformRelease.GetOrCreateBoard("assurdo")
149+
board.Properties.Set("vid", "0x0000")
150+
board.Properties.Set("pid", "0x0000")
151+
152+
idPrefs := properties.NewMap()
153+
idPrefs.Set("vid", "0x0000")
154+
idPrefs.Set("pid", "0x0000")
155+
res, err := identify(pm, &commands.BoardPort{IdentificationPrefs: idPrefs})
156+
require.NoError(t, err)
157+
require.NotNil(t, res)
158+
require.Len(t, res, 4)
159+
160+
// Verify expected sorting
161+
require.Equal(t, res[0].Fqbn, "arduino:avr:assurdo")
162+
require.Equal(t, res[1].Fqbn, "arduino:avr:nessuno")
163+
require.Equal(t, res[2].Fqbn, "packager:platform:boardA")
164+
require.Equal(t, res[3].Fqbn, "packager:platform:boardB")
165+
}

0 commit comments

Comments
 (0)