Skip to content

Commit

Permalink
Implement suggestions on fqbn
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Jul 27, 2021
1 parent fc367b7 commit 771583d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/schollz/closestmatch"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
)
Expand Down Expand Up @@ -129,6 +130,22 @@ func (pm *PackageManager) FindBoardWithFQBN(fqbnIn string) (*cores.Board, error)
return board, err
}

func (pm *PackageManager) findClosestMatchFqbn(fqbn *cores.FQBN) string {
// Create closestmatch DB
wordsToTest := []string{}
name := fqbn.StringWithoutConfig()
for _, board := range pm.InstalledBoards() {
wordsToTest = append(wordsToTest, board.FQBN())
}
// Choose a set of bag sizes, more is more accurate but slower
bagSizes := []int{2}

// Create a closestmatch object and find the best matching name
cm := closestmatch.New(wordsToTest, bagSizes)
closestName := cm.Closest(name)
return closestName
}

// ResolveFQBN returns, in order:
//
// - the Package pointed by the fqbn
Expand Down Expand Up @@ -174,8 +191,10 @@ func (pm *PackageManager) ResolveFQBN(fqbn *cores.FQBN) (
// Find board
board := platformRelease.Boards[fqbn.BoardID]
if board == nil {
// Try looking for the closest match; if found, suggest it
suggestion := pm.findClosestMatchFqbn(fqbn)
return targetPackage, platformRelease, nil, nil, nil,
fmt.Errorf("board %s:%s not found", platformRelease, fqbn.BoardID)
fmt.Errorf("board %s not found, did you mean %s ?", fqbn.StringWithoutConfig(), suggestion)
}

buildProperties, err := board.GetBuildProperties(fqbn.Configs)
Expand Down

0 comments on commit 771583d

Please sign in to comment.