From 465e343f77129a88b30b5c7495f73d1397cbfcce Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 4 Oct 2022 10:39:07 +0200 Subject: [PATCH 1/5] Factored function to get search terms --- commands/lib/search.go | 4 +--- rpc/cc/arduino/cli/commands/v1/search.go | 28 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 rpc/cc/arduino/cli/commands/v1/search.go diff --git a/commands/lib/search.go b/commands/lib/search.go index 25a420e72e4..7ea94978a47 100644 --- a/commands/lib/search.go +++ b/commands/lib/search.go @@ -41,9 +41,7 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS // Split on anything but 0-9, a-z or : - queryTerms := strings.FieldsFunc(strings.ToLower(req.GetQuery()), func(r rune) bool { - return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':') - }) + queryTerms := rpc.SearchTermsFromQueryString(req.GetQuery()) for _, lib := range lm.Index.Libraries { matchTerm := func(x string) bool { diff --git a/rpc/cc/arduino/cli/commands/v1/search.go b/rpc/cc/arduino/cli/commands/v1/search.go new file mode 100644 index 00000000000..b6a66ecc99c --- /dev/null +++ b/rpc/cc/arduino/cli/commands/v1/search.go @@ -0,0 +1,28 @@ +// 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 commands + +import "strings" + +// SearchTermsFromQueryString returns the terms inside the query string. +// All non alphanumeric characters (expect ':') are considered separators. +// All search terms are converted to lowercase. +func SearchTermsFromQueryString(query string) []string { + // Split on anything but 0-9, a-z or : + return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool { + return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':') + }) +} From 0e410368afb41ebb1ece9d24577117eadabca135 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 4 Oct 2022 11:27:47 +0200 Subject: [PATCH 2/5] Made 'core search' logic identical to Arduino IDE 1.8.x search dialog --- commands/core/search.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/commands/core/search.go b/commands/core/search.go index 48ba605d8a4..55e715901f3 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -29,8 +29,6 @@ import ( // PlatformSearch FIXMEDOC func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) { - searchArgs := strings.TrimSpace(req.SearchArgs) - allVersions := req.AllVersions pme, release := commands.GetPackageManagerExplorer(req) if pme == nil { return nil, &arduino.InvalidInstanceError{} @@ -38,14 +36,14 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse defer release() res := []*cores.PlatformRelease{} - if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", searchArgs); isUsb { - vid, pid := searchArgs[:4], searchArgs[5:] + if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", req.SearchArgs); isUsb { + vid, pid := req.SearchArgs[:4], req.SearchArgs[5:] res = pme.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid) } else { - + searchArgs := rpc.SearchTermsFromQueryString(req.SearchArgs) + allVersions := req.AllVersions for _, targetPackage := range pme.GetPackages() { for _, platform := range targetPackage.Platforms { - // discard invalid platforms // Users can install platforms manually in the Sketchbook hardware folder, // the core search command must operate only on platforms installed through // the PlatformManager, thus we skip the manually installed ones. @@ -53,34 +51,32 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse continue } - // discard invalid releases - platformRelease := platform.GetLatestRelease() - if platformRelease == nil { + // Discard platforms with no releases + latestRelease := platform.GetLatestRelease() + if latestRelease == nil { continue } // Gather all strings that can be used for searching - toTest := []string{ - platform.String(), - platform.Name, - platform.Architecture, - targetPackage.Name, - targetPackage.Maintainer, - targetPackage.WebsiteURL, - } - for _, board := range platformRelease.BoardsManifest { - toTest = append(toTest, board.Name) + toTest := platform.String() + " " + + platform.Name + " " + + platform.Architecture + " " + + targetPackage.Name + " " + + targetPackage.Maintainer + " " + + targetPackage.WebsiteURL + for _, board := range latestRelease.BoardsManifest { + toTest += board.Name + " " } // Search - if !utils.MatchAny(searchArgs, toTest) { + if !utils.Match(toTest, searchArgs) { continue } if allVersions { res = append(res, platform.GetAllReleases()...) } else { - res = append(res, platformRelease) + res = append(res, latestRelease) } } } From 90ed76fc99fe9cb76416080183c64e8391f73ee9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 4 Oct 2022 11:33:39 +0200 Subject: [PATCH 3/5] Drastically simplified search algorithm for libs --- commands/lib/search.go | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/commands/lib/search.go b/commands/lib/search.go index 7ea94978a47..ca8b0a48a3a 100644 --- a/commands/lib/search.go +++ b/commands/lib/search.go @@ -17,11 +17,11 @@ package lib import ( "context" - "strings" "github.com/arduino/arduino-cli/arduino" "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" + "github.com/arduino/arduino-cli/arduino/utils" "github.com/arduino/arduino-cli/commands" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" semver "go.bug.st/relaxed-semver" @@ -38,40 +38,23 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse { res := []*rpc.SearchedLibrary{} - status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS - - // Split on anything but 0-9, a-z or : queryTerms := rpc.SearchTermsFromQueryString(req.GetQuery()) for _, lib := range lm.Index.Libraries { - matchTerm := func(x string) bool { - if strings.Contains(strings.ToLower(lib.Name), x) || - strings.Contains(strings.ToLower(lib.Latest.Paragraph), x) || - strings.Contains(strings.ToLower(lib.Latest.Sentence), x) || - strings.Contains(strings.ToLower(lib.Latest.Author), x) { - return true - } - for _, include := range lib.Latest.ProvidesIncludes { - if strings.Contains(strings.ToLower(include), x) { - return true - } - } - return false - } - match := func() bool { - for _, term := range queryTerms { - if !matchTerm(term) { - return false - } - } - return true + toTest := lib.Name + " " + + lib.Latest.Paragraph + " " + + lib.Latest.Sentence + " " + + lib.Latest.Author + " " + for _, include := range lib.Latest.ProvidesIncludes { + toTest += include + " " } - if match() { + + if utils.Match(toTest, queryTerms) { res = append(res, indexLibraryToRPCSearchLibrary(lib)) } } - return &rpc.LibrarySearchResponse{Libraries: res, Status: status} + return &rpc.LibrarySearchResponse{Libraries: res, Status: rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS} } // indexLibraryToRPCSearchLibrary converts a librariindex.Library to rpc.SearchLibrary From ebb55e38bd121eef864a03b39155942f90b4b91c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 4 Oct 2022 12:03:08 +0200 Subject: [PATCH 4/5] Fixed SearchTermsFromQueryString function --- rpc/cc/arduino/cli/commands/v1/search.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rpc/cc/arduino/cli/commands/v1/search.go b/rpc/cc/arduino/cli/commands/v1/search.go index b6a66ecc99c..3dc6802ab5a 100644 --- a/rpc/cc/arduino/cli/commands/v1/search.go +++ b/rpc/cc/arduino/cli/commands/v1/search.go @@ -15,7 +15,10 @@ package commands -import "strings" +import ( + "strings" + "unicode" +) // SearchTermsFromQueryString returns the terms inside the query string. // All non alphanumeric characters (expect ':') are considered separators. @@ -23,6 +26,6 @@ import "strings" func SearchTermsFromQueryString(query string) []string { // Split on anything but 0-9, a-z or : return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool { - return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':') + return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != ':' }) } From 28dd57db4105a6ebaa80bc8eadf20c26c06b9d90 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 4 Oct 2022 12:03:50 +0200 Subject: [PATCH 5/5] Moving SearchTermsFromQueryString into proper place --- arduino/utils/search.go | 10 ++++++++ commands/core/search.go | 2 +- commands/lib/search.go | 2 +- rpc/cc/arduino/cli/commands/v1/search.go | 31 ------------------------ 4 files changed, 12 insertions(+), 33 deletions(-) delete mode 100644 rpc/cc/arduino/cli/commands/v1/search.go diff --git a/arduino/utils/search.go b/arduino/utils/search.go index 4e3de83611a..c0b48ed45ae 100644 --- a/arduino/utils/search.go +++ b/arduino/utils/search.go @@ -43,6 +43,16 @@ func removeDiatrics(s string) (string, error) { return s, nil } +// SearchTermsFromQueryString returns the terms inside the query string. +// All non alphanumeric characters (expect ':') are considered separators. +// All search terms are converted to lowercase. +func SearchTermsFromQueryString(query string) []string { + // Split on anything but 0-9, a-z or : + return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool { + return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != ':' + }) +} + // 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. diff --git a/commands/core/search.go b/commands/core/search.go index 55e715901f3..1d9a39c440a 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -40,7 +40,7 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse vid, pid := req.SearchArgs[:4], req.SearchArgs[5:] res = pme.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid) } else { - searchArgs := rpc.SearchTermsFromQueryString(req.SearchArgs) + searchArgs := utils.SearchTermsFromQueryString(req.SearchArgs) allVersions := req.AllVersions for _, targetPackage := range pme.GetPackages() { for _, platform := range targetPackage.Platforms { diff --git a/commands/lib/search.go b/commands/lib/search.go index ca8b0a48a3a..2766a2d6bba 100644 --- a/commands/lib/search.go +++ b/commands/lib/search.go @@ -38,7 +38,7 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse { res := []*rpc.SearchedLibrary{} - queryTerms := rpc.SearchTermsFromQueryString(req.GetQuery()) + queryTerms := utils.SearchTermsFromQueryString(req.GetQuery()) for _, lib := range lm.Index.Libraries { toTest := lib.Name + " " + diff --git a/rpc/cc/arduino/cli/commands/v1/search.go b/rpc/cc/arduino/cli/commands/v1/search.go deleted file mode 100644 index 3dc6802ab5a..00000000000 --- a/rpc/cc/arduino/cli/commands/v1/search.go +++ /dev/null @@ -1,31 +0,0 @@ -// 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 commands - -import ( - "strings" - "unicode" -) - -// SearchTermsFromQueryString returns the terms inside the query string. -// All non alphanumeric characters (expect ':') are considered separators. -// All search terms are converted to lowercase. -func SearchTermsFromQueryString(query string) []string { - // Split on anything but 0-9, a-z or : - return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool { - return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != ':' - }) -}