Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-cli
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Operations
Operations
Metrics
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-cli
Commits
e404a3ba
Unverified
Commit
e404a3ba
authored
Sep 28, 2022
by
Cristian Maglie
Committed by
GitHub
Sep 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved library search (#1896)
parent
baa322f9
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
513184 additions
and
18 deletions
+513184
-18
commands/lib/search.go
commands/lib/search.go
+30
-5
commands/lib/search_test.go
commands/lib/search_test.go
+41
-13
commands/lib/testdata/full/library_index.json
commands/lib/testdata/full/library_index.json
+513113
-0
commands/lib/testdata/test1/library_index.json
commands/lib/testdata/test1/library_index.json
+0
-0
No files found.
commands/lib/search.go
View file @
e404a3ba
...
@@ -17,11 +17,11 @@ package lib
...
@@ -17,11 +17,11 @@ package lib
import
(
import
(
"context"
"context"
"strings"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands"
rpc
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
rpc
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
semver
"go.bug.st/relaxed-semver"
semver
"go.bug.st/relaxed-semver"
...
@@ -40,12 +40,37 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
...
@@ -40,12 +40,37 @@ func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.Libraries
res
:=
[]
*
rpc
.
SearchedLibrary
{}
res
:=
[]
*
rpc
.
SearchedLibrary
{}
status
:=
rpc
.
LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
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
==
':'
)
})
for
_
,
lib
:=
range
lm
.
Index
.
Libraries
{
for
_
,
lib
:=
range
lm
.
Index
.
Libraries
{
toTest
:=
[]
string
{
lib
.
Name
,
lib
.
Latest
.
Paragraph
,
lib
.
Latest
.
Sentence
}
matchTerm
:=
func
(
x
string
)
bool
{
if
!
utils
.
MatchAny
(
req
.
GetQuery
(),
toTest
)
{
if
strings
.
Contains
(
strings
.
ToLower
(
lib
.
Name
),
x
)
||
continue
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
}
if
match
()
{
res
=
append
(
res
,
indexLibraryToRPCSearchLibrary
(
lib
))
}
}
res
=
append
(
res
,
indexLibraryToRPCSearchLibrary
(
lib
))
}
}
return
&
rpc
.
LibrarySearchResponse
{
Libraries
:
res
,
Status
:
status
}
return
&
rpc
.
LibrarySearchResponse
{
Libraries
:
res
,
Status
:
status
}
...
...
commands/lib/search_test.go
View file @
e404a3ba
package
lib
package
lib
import
(
import
(
"sort"
"strings"
"strings"
"testing"
"testing"
...
@@ -8,20 +9,17 @@ import (
...
@@ -8,20 +9,17 @@ import (
rpc
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
rpc
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths
"github.com/arduino/go-paths-helper"
paths
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
)
var
customIndexPath
=
paths
.
New
(
"testdata"
)
var
customIndexPath
=
paths
.
New
(
"testdata"
,
"test1"
)
var
fullIndexPath
=
paths
.
New
(
"testdata"
,
"full"
)
func
TestSearchLibrary
(
t
*
testing
.
T
)
{
func
TestSearchLibrary
(
t
*
testing
.
T
)
{
lm
:=
librariesmanager
.
NewLibraryManager
(
customIndexPath
,
nil
)
lm
:=
librariesmanager
.
NewLibraryManager
(
customIndexPath
,
nil
)
lm
.
LoadIndex
()
lm
.
LoadIndex
()
req
:=
&
rpc
.
LibrarySearchRequest
{
resp
:=
searchLibrary
(
&
rpc
.
LibrarySearchRequest
{
Query
:
"test"
},
lm
)
Instance
:
&
rpc
.
Instance
{
Id
:
1
},
Query
:
"test"
,
}
resp
:=
searchLibrary
(
req
,
lm
)
assert
:=
assert
.
New
(
t
)
assert
:=
assert
.
New
(
t
)
assert
.
Equal
(
resp
.
GetStatus
(),
rpc
.
LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
)
assert
.
Equal
(
resp
.
GetStatus
(),
rpc
.
LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
)
assert
.
Equal
(
len
(
resp
.
GetLibraries
()),
2
)
assert
.
Equal
(
len
(
resp
.
GetLibraries
()),
2
)
...
@@ -33,12 +31,7 @@ func TestSearchLibrarySimilar(t *testing.T) {
...
@@ -33,12 +31,7 @@ func TestSearchLibrarySimilar(t *testing.T) {
lm
:=
librariesmanager
.
NewLibraryManager
(
customIndexPath
,
nil
)
lm
:=
librariesmanager
.
NewLibraryManager
(
customIndexPath
,
nil
)
lm
.
LoadIndex
()
lm
.
LoadIndex
()
req
:=
&
rpc
.
LibrarySearchRequest
{
resp
:=
searchLibrary
(
&
rpc
.
LibrarySearchRequest
{
Query
:
"arduino"
},
lm
)
Instance
:
&
rpc
.
Instance
{
Id
:
1
},
Query
:
"arduino"
,
}
resp
:=
searchLibrary
(
req
,
lm
)
assert
:=
assert
.
New
(
t
)
assert
:=
assert
.
New
(
t
)
assert
.
Equal
(
resp
.
GetStatus
(),
rpc
.
LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
)
assert
.
Equal
(
resp
.
GetStatus
(),
rpc
.
LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
)
assert
.
Equal
(
len
(
resp
.
GetLibraries
()),
2
)
assert
.
Equal
(
len
(
resp
.
GetLibraries
()),
2
)
...
@@ -49,3 +42,38 @@ func TestSearchLibrarySimilar(t *testing.T) {
...
@@ -49,3 +42,38 @@ func TestSearchLibrarySimilar(t *testing.T) {
assert
.
Contains
(
libs
,
"ArduinoTestPackage"
)
assert
.
Contains
(
libs
,
"ArduinoTestPackage"
)
assert
.
Contains
(
libs
,
"Arduino"
)
assert
.
Contains
(
libs
,
"Arduino"
)
}
}
func
TestSearchLibraryFields
(
t
*
testing
.
T
)
{
lm
:=
librariesmanager
.
NewLibraryManager
(
fullIndexPath
,
nil
)
lm
.
LoadIndex
()
query
:=
func
(
q
string
)
[]
string
{
libs
:=
[]
string
{}
for
_
,
lib
:=
range
searchLibrary
(
&
rpc
.
LibrarySearchRequest
{
Query
:
q
},
lm
)
.
Libraries
{
libs
=
append
(
libs
,
lib
.
Name
)
}
sort
.
Strings
(
libs
)
return
libs
}
res
:=
query
(
"SparkFun_u-blox_GNSS"
)
require
.
Len
(
t
,
res
,
3
)
require
.
Equal
(
t
,
"SparkFun u-blox Arduino Library"
,
res
[
0
])
require
.
Equal
(
t
,
"SparkFun u-blox GNSS Arduino Library"
,
res
[
1
])
require
.
Equal
(
t
,
"SparkFun u-blox SARA-R5 Arduino Library"
,
res
[
2
])
res
=
query
(
"SparkFun u-blox GNSS"
)
require
.
Len
(
t
,
res
,
3
)
require
.
Equal
(
t
,
"SparkFun u-blox Arduino Library"
,
res
[
0
])
require
.
Equal
(
t
,
"SparkFun u-blox GNSS Arduino Library"
,
res
[
1
])
require
.
Equal
(
t
,
"SparkFun u-blox SARA-R5 Arduino Library"
,
res
[
2
])
res
=
query
(
"painlessMesh"
)
require
.
Len
(
t
,
res
,
1
)
require
.
Equal
(
t
,
"Painless Mesh"
,
res
[
0
])
res
=
query
(
"cristian maglie"
)
require
.
Len
(
t
,
res
,
2
)
require
.
Equal
(
t
,
"Arduino_ConnectionHandler"
,
res
[
0
])
require
.
Equal
(
t
,
"FlashStorage_SAMD"
,
res
[
1
])
}
commands/lib/testdata/full/library_index.json
0 → 100644
View file @
e404a3ba
This diff is collapsed.
Click to expand it.
commands/lib/testdata/library_index.json
→
commands/lib/testdata/
test1/
library_index.json
View file @
e404a3ba
File moved
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment