Skip to content

Commit 5e934ce

Browse files
use the same implementation of the libraries engine
1 parent 693e6d1 commit 5e934ce

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

Diff for: internal/arduino/libraries/loader.go

+37-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package libraries
1818
import (
1919
"errors"
2020
"fmt"
21+
"regexp"
2122
"strings"
2223

2324
"github.com/arduino/arduino-cli/internal/arduino/globals"
@@ -130,21 +131,12 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
130131
library.LDflags = strings.TrimSpace(libProperties.Get("ldflags"))
131132
library.Properties = libProperties
132133
library.InDevelopment = libraryDir.Join(".development").Exist()
133-
if dependencies := libProperties.Get("depends"); dependencies != "" {
134-
for dep := range strings.SplitSeq(strings.TrimSpace(libProperties.Get("depends")), ",") {
135-
var depConstraint semver.Constraint
136-
depName := strings.TrimSpace(dep)
137-
idx := strings.LastIndex(dep, " (")
138-
if idx != -1 {
139-
depName = dep[:idx]
140-
depConstraint, _ = semver.ParseConstraint(dep[idx+1:])
141-
}
142-
library.Dependencies = append(library.Dependencies, &Dependency{
143-
Name: depName,
144-
VersionConstraint: depConstraint,
145-
})
146-
}
134+
135+
dependencies, err := extractDependenciesList(libProperties.Get("depends"))
136+
if err != nil {
137+
return nil, fmt.Errorf("%s: %w", i18n.Tr("scanning library dependencies"), err)
147138
}
139+
library.Dependencies = dependencies
148140
return library, nil
149141
}
150142

@@ -235,3 +227,34 @@ func filterExamplesDirs(dir *paths.Path) bool {
235227
}
236228
return false
237229
}
230+
231+
var libraryDependsRegex = regexp.MustCompile("^([^()]+?) *(?: \\((.*)\\))?$")
232+
233+
// extractDependenciesList extracts dependencies from the "depends" field of library.properties
234+
func extractDependenciesList(depends string) ([]*Dependency, error) {
235+
deps := []*Dependency{}
236+
depends = strings.TrimSpace(depends)
237+
if depends == "" {
238+
return deps, nil
239+
}
240+
for dep := range strings.SplitSeq(depends, ",") {
241+
dep = strings.TrimSpace(dep)
242+
if dep == "" {
243+
return nil, fmt.Errorf("invalid dep: %s", dep)
244+
}
245+
matches := libraryDependsRegex.FindAllStringSubmatch(dep, -1)
246+
if matches == nil {
247+
return nil, fmt.Errorf("invalid dep: %s", dep)
248+
}
249+
250+
depConstraint, err := semver.ParseConstraint(matches[0][2])
251+
if err != nil {
252+
return nil, fmt.Errorf("invalid dep constraint: %s", matches[0][2])
253+
}
254+
deps = append(deps, &Dependency{
255+
Name: matches[0][1],
256+
VersionConstraint: depConstraint,
257+
})
258+
}
259+
return deps, nil
260+
}

Diff for: internal/arduino/libraries/loader_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package libraries
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestDependencyExtract(t *testing.T) {
10+
check := func(depDefinition string, name []string, ver []string) {
11+
dep, err := extractDependenciesList(depDefinition)
12+
require.NoError(t, err)
13+
require.NotNil(t, dep)
14+
require.Len(t, dep, len(name))
15+
for i := range name {
16+
require.Equal(t, name[i], dep[i].Name, depDefinition)
17+
require.Equal(t, ver[i], dep[i].VersionConstraint.String(), depDefinition)
18+
}
19+
}
20+
invalid := func(depends string) {
21+
dep, err := extractDependenciesList(depends)
22+
require.Nil(t, dep)
23+
require.Error(t, err)
24+
}
25+
check("ciao", []string{"ciao"}, []string{""})
26+
check("MyLib (>1.2.3)", []string{"MyLib"}, []string{">1.2.3"})
27+
check("MyLib (>=1.2.3)", []string{"MyLib"}, []string{">=1.2.3"})
28+
check("MyLib (<1.2.3)", []string{"MyLib"}, []string{"<1.2.3"})
29+
check("MyLib (<=1.2.3)", []string{"MyLib"}, []string{"<=1.2.3"})
30+
check("MyLib (!=1.2.3)", []string{"MyLib"}, []string{"!(=1.2.3)"})
31+
check("MyLib (>1.0.0 && <2.1.0)", []string{"MyLib"}, []string{"(>1.0.0 && <2.1.0)"})
32+
check("MyLib (<1.0.0 || >2.0.0)", []string{"MyLib"}, []string{"(<1.0.0 || >2.0.0)"})
33+
check("MyLib ((>0.1.0 && <2.0.0) || >2.1.0)", []string{"MyLib"}, []string{"((>0.1.0 && <2.0.0) || >2.1.0)"})
34+
check("MyLib ()", []string{"MyLib"}, []string{""})
35+
check("MyLib (>=1.2.3),AnotherLib, YetAnotherLib (=1.0.0)",
36+
[]string{"MyLib", "AnotherLib", "YetAnotherLib"},
37+
[]string{">=1.2.3", "", "=1.0.0"})
38+
invalid("MyLib,,AnotherLib")
39+
invalid("(MyLib)")
40+
invalid("MyLib(=1.2.3)")
41+
check("Arduino Uno WiFi Dev Ed Library, LoRa Node (^2.1.2)",
42+
[]string{"Arduino Uno WiFi Dev Ed Library", "LoRa Node"},
43+
[]string{"", "^2.1.2"})
44+
check("Arduino Uno WiFi Dev Ed Library , LoRa Node (^2.1.2)",
45+
[]string{"Arduino Uno WiFi Dev Ed Library", "LoRa Node"},
46+
[]string{"", "^2.1.2"})
47+
check("Arduino_OAuth, ArduinoHttpClient (<0.3.0), NonExistentLib",
48+
[]string{"Arduino_OAuth", "ArduinoHttpClient", "NonExistentLib"},
49+
[]string{"", "<0.3.0", ""})
50+
check("", []string{}, []string{})
51+
}

0 commit comments

Comments
 (0)