8000 Add board search command and gRPC interface function by silvanocerza · Pull Request #1210 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

Add board search command and gRPC interface function #1210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove fuzzy search from board search command
  • Loading branch information
silvanocerza committed Mar 9, 2021
commit 181252c30b6f54395bd046cee5a65bc072021bd8
66 changes: 66 additions & 0 deletions arduino/utils/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package utils

import (
"strings"
"unicode"

"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)

// removeDiatrics removes accents and similar diatrics from unicode characters.
// An empty string is returned in case of errors.
// This might not be the best solution but it works well enough for our usecase,
// in the future we might want to use the golang.org/x/text/secure/precis package
// when its API will be finalized.
// From https://stackoverflow.com/a/26722698
func removeDiatrics(s string) (string, error) {
transformer := transform.Chain(
norm.NFD,
runes.Remove(runes.In(unicode.Mn)),
norm.NFC,
)
s, _, err := transform.String(transformer, s)
if err != nil {
return "", err
}
return s, nil
}

// Match returns true if all substrings are contained in str.
// Both str and substrings are transforms to lower case and have their
// accents and other unicode diatrics removed.
// If strings transformation fails an error is returned.
func Match(str string, substrings []string) (bool, error) {
str, err := removeDiatrics(strings.ToLower(str))
if err != nil {
return false, err
}

for _, sub := range substrings {
cleanSub, err := removeDiatrics(strings.ToLower(sub))
if err != nil {
return false, err
}
if !strings.Contains(str, cleanSub) {
return false, nil
}
}
return true, nil
}
29 changes: 18 additions & 11 deletions commands/board/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"sort"
"strings"

"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/lithammer/fuzzysearch/fuzzy"
)

// Search returns all boards that match the search arg.
Expand All @@ -36,20 +36,23 @@ func Search(ctx context.Context, req *rpc.BoardSearchReq) (*rpc.BoardSearchResp,
return nil, errors.New("invalid instance")
}

searchArgs := strings.Trim(req.SearchArgs, " ")
searchArgs := strings.Split(strings.Trim(req.SearchArgs, " "), " ")

match := func(toTest []string) bool {
match := func(toTest []string) (bool, error) {
if len(searchArgs) == 0 {
return true
return true, nil
}
const maximumSearchDistance = 20

for _, rank := range fuzzy.RankFindNormalizedFold(searchArgs, toTest) {
if rank.Distance < maximumSearchDistance {
return true
for _, t := range toTest {
matches, err := utils.Match(t, searchArgs)
if err != nil {
return false, err
}
if matches {
return matches, nil
}
}
return false
return false, nil
}

res := &rpc.BoardSearchResp{Boards: []*rpc.BoardListItem{}}
Expand Down Expand Up @@ -87,7 +90,9 @@ func Search(ctx context.Context, req *rpc.BoardSearchReq) (*rpc.BoardSearchResp,
}

toTest := append(strings.Split(board.Name(), " "), board.Name(), board.FQBN())
if !match(toTest) {
if ok, err := match(toTest); err != nil {
return nil, err
} else if !ok {
continue
}

Expand All @@ -101,7 +106,9 @@ func Search(ctx context.Context, req *rpc.BoardSearchReq) (*rpc.BoardSearchResp,
} else {
for _, board := range latestPlatformRelease.BoardsManifest {
toTest := append(strings.Split(board.Name, " "), board.Name)
if !match(toTest) {
if ok, err := match(toTest); err != nil {
return nil, err
} else if !ok {
continue
}

Expand Down
52 changes: 20 additions & 32 deletions test/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,18 @@ def test_board_search(run_command, data_dir):
assert "Arduino Nano 33 BLE" in names
assert "Arduino Portenta H7" in names

# Search in non installed boards
res = run_command("board search --format json nano 33")
assert res.ok
data = json.loads(res.stdout)
# Verifies boards are returned
assert len(data) > 0
# Verifies no board has FQBN set since no platform is installed
assert len([board["FQBN"] for board in data if "FQBN" in board]) == 0
names = [board["name"] for board in data if "name" in board]
assert "Arduino Nano 33 BLE" in names
assert "Arduino Nano 33 IoT" in names

# Install a platform from index
assert run_command("core install arduino:avr@1.8.3")

Expand All @@ -621,6 +633,14 @@ def test_board_search(run_command, data_dir):
assert "arduino:avr:yun" in installed_boards
assert "Arduino Yún" == installed_boards["arduino:avr:yun"]["name"]

res = run_command("board search --format json arduino yun")
assert res.ok
data = json.loads(res.stdout)
assert len(data) > 0
installed_boards = {board["FQBN"]: board for board in data if "FQBN" in board}
assert "arduino:avr:yun" in installed_boards
assert "Arduino Yún" == installed_boards["arduino:avr:yun"]["name"]

# Manually installs a core in sketchbooks hardware folder
git_url = "https://github.com/arduino/ArduinoCore-samd.git"
repo_dir = Path(data_dir, "hardware", "arduino-beta-development", "samd")
Expand All @@ -647,38 +667,6 @@ def test_board_search(run_command, data_dir):
assert "Arduino NANO 33 IoT" == installed_boards["arduino-beta-development:samd:nano_33_iot"]["name"]
assert "arduino-beta-development:samd:arduino_zero_native" in installed_boards


def test_board_search_fuzzy(run_command, data_dir):
assert run_command("update")

# Search in non installed boards
res = run_command("board search --format json nano 33")
assert res.ok
data = json.loads(res.stdout)
# Verifies boards are returned
assert len(data) > 0
# Verifies no board has FQBN set since no platform is installed
assert len([board["FQBN"] for board in data if "FQBN" in board]) == 0
names = [board["name"] for board in data if "name" in board]
assert "Arduino Nano 33 BLE" in names
assert "Arduino Nano 33 IoT" in names

# Install a platform from index
assert run_command("core install arduino:avr@1.8.3")

res = run_command("board search --format json arduino yun")
assert res.ok
data = json.loads(res.stdout)
assert len(data) > 0
installed_boards = {board["FQBN"]: board for board in data if "FQBN" in board}
assert "arduino:avr:yun" in installed_boards
assert "Arduino Yún" == installed_boards["arduino:avr:yun"]["name"]

# Manually installs a core in sketchbooks hardware folder
git_url = "https://github.com/arduino/ArduinoCore-samd.git"
repo_dir = Path(data_dir, "hardware", "arduino-beta-development", "samd")
assert Repo.clone_from(git_url, repo_dir, multi_options=["-b 1.8.11"])

res = run_command("board search --format json mkr1000")
assert res.ok
data = json.loads(res.stdout)
Expand Down
0