Skip to content

Commit e1be6e1

Browse files
committed
Added support for 'dependency:' field in profiles libraries
1 parent d3d20c2 commit e1be6e1

File tree

6 files changed

+102
-30
lines changed

6 files changed

+102
-30
lines changed

internal/arduino/sketch/profiles.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,16 @@ func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) err
348348

349349
// ProfileLibraryReference is a reference to a library
350350
type ProfileLibraryReference struct {
351-
Library string
352-
InstallDir *paths.Path
353-
Version *semver.Version
351+
Library string
352+
Version *semver.Version
353+
IsDependency bool
354+
InstallDir *paths.Path
354355
}
355356

356357
// UnmarshalYAML decodes a ProfileLibraryReference from YAML source.
357358
func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
358359
var dataMap map[string]any
359-
var data string
360+
var libReference string
360361
if err := unmarshal(&dataMap); err == nil {
361362
if installDir, ok := dataMap["dir"]; ok {
362363
if installDir, ok := installDir.(string); !ok {
@@ -366,15 +367,24 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
366367
l.Library = l.InstallDir.Base()
367368
return nil
368369
}
370+
} else if depLib, ok := dataMap["dependency"]; ok {
371+
if libReference, ok = depLib.(string); !ok {
372+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), dataMap)
373+
}
374+
l.IsDependency = true
375+
// Fallback
369376
} else {
370377
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), dataMap)
371378
}
372-
} else if err := unmarshal(&data); err != nil {
379+
} else if err := unmarshal(&libReference); err != nil {
373380
return err
381+
} else {
382+
l.IsDependency = false
374383
}
375384

376-
if libName, libVersion, ok := parseNameAndVersion(data); !ok {
377-
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), data)
385+
// Parse reference in the format "LIBRARY_NAME (VERSION)"
386+
if libName, libVersion, ok := parseNameAndVersion(libReference); !ok {
387+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), libReference)
378388
} else if v, err := semver.Parse(libVersion); err != nil {
379389
return fmt.Errorf("%s: %w", i18n.Tr("invalid version"), err)
380390
} else {
@@ -389,17 +399,25 @@ func (l *ProfileLibraryReference) AsYaml() string {
389399
if l.InstallDir != nil {
390400
return fmt.Sprintf(" - dir: %s\n", l.InstallDir)
391401
}
392-
return fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
402+
dep := ""
403+
if l.IsDependency {
404+
dep = "dependency: "
405+
}
406+
return fmt.Sprintf(" - %s%s (%s)\n", dep, l.Library, l.Version)
393407
}
394408

395409
func (l *ProfileLibraryReference) String() string {
396410
if l.InstallDir != nil {
397-
return fmt.Sprintf("%s@dir:%s", l.Library, l.InstallDir)
411+
return "@dir:" + l.InstallDir.String()
412+
}
413+
dep := ""
414+
if l.IsDependency {
415+
dep = " (dep)"
398416
}
399417
if l.Version == nil {
400-
return l.Library
418+
return l.Library + dep
401419
}
402-
return fmt.Sprintf("%s@%s", l.Library, l.Version)
420+
return fmt.Sprintf("%s@%s%s", l.Library, l.Version, dep)
403421
}
404422

405423
// Match checks if this library reference matches another one.
@@ -434,8 +452,9 @@ func (l *ProfileLibraryReference) ToRpc() *rpc.ProfileLibraryReference {
434452
return &rpc.ProfileLibraryReference{
435453
Library: &rpc.ProfileLibraryReference_IndexLibrary_{
436454
IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{
437-
Name: l.Library,
438-
Version: l.Version.String(),
455+
Name: l.Library,
456+
Version: l.Version.String(),
457+
IsDependency: l.IsDependency,
439458
},
440459
},
441460
}
@@ -459,7 +478,11 @@ func FromRpcProfileLibraryReference(l *rpc.ProfileLibraryReference) (*ProfileLib
459478
}
460479
version = v
461480
}
462-
return &ProfileLibraryReference{Library: indexLib.GetName(), Version: version}, nil
481+
return &ProfileLibraryReference{
482+
Library: indexLib.GetName(),
483+
Version: version,
484+
IsDependency: indexLib.GetIsDependency(),
485+
}, nil
463486
}
464487
return nil, &cmderrors.InvalidArgumentError{Message: "library not specified"}
465488
}
@@ -468,7 +491,8 @@ func FromRpcProfileLibraryReference(l *rpc.ProfileLibraryReference) (*ProfileLib
468491
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
469492
f.Assert(l.InstallDir == nil,
470493
"InternalUniqueIdentifier should not be called for library references with an install directory")
471-
id := l.String()
494+
495+
id := l.Library + "@" + l.Version.String()
472496
h := sha256.Sum256([]byte(id))
473497
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])
474498
return utils.SanitizeName(res)

internal/arduino/sketch/profiles_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,24 @@ func TestProjectFileLoading(t *testing.T) {
5353
require.Error(t, err)
5454
}
5555
}
56+
57+
func TestProjectFileLibraries(t *testing.T) {
58+
sketchProj := paths.New("testdata", "profiles", "profile_with_libraries.yml")
59+
proj, err := LoadProjectFile(sketchProj)
60+
require.NoError(t, err)
61+
require.Len(t, proj.Profiles, 1)
62+
prof := proj.Profiles[0]
63+
require.Len(t, prof.Libraries, 4)
64+
require.Equal(t, "FlashStorage@1.2.3", prof.Libraries[0].String())
65+
require.Equal(t, "@dir:/path/to/system/lib", prof.Libraries[1].String())
66+
require.Equal(t, "@dir:path/to/sketch/lib", prof.Libraries[2].String())
67+
require.Equal(t, "DependencyLib@2.3.4 (dep)", prof.Libraries[3].String())
68+
require.Equal(t, "FlashStorage_1.2.3_e525d7c96b27788f", prof.Libraries[0].InternalUniqueIdentifier())
69+
require.Panics(t, func() { prof.Libraries[1].InternalUniqueIdentifier() })
70+
require.Panics(t, func() { prof.Libraries[2].InternalUniqueIdentifier() })
71+
require.Equal(t, "DependencyLib_2.3.4_ecde631facb47ae5", prof.Libraries[3].InternalUniqueIdentifier())
72+
73+
orig, err := sketchProj.ReadFile()
74+
require.NoError(t, err)
75+
require.Equal(t, string(orig), proj.AsYaml())
76+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
profiles:
2+
giga:
3+
fqbn: arduino:mbed_giga:giga
4+
platforms:
5+
- platform: arduino:mbed_giga (4.3.1)
6+
libraries:
7+
- FlashStorage (1.2.3)
8+
- dir: /path/to/system/lib
9+
- dir: path/to/sketch/lib
10+
- dependency: DependencyLib (2.3.4)
11+
12+
default_profile: giga_any

internal/cli/feedback/result/rpc.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,13 +1137,15 @@ func NewProfileLibraryReference_LocalLibraryResult(resp *rpc.ProfileLibraryRefer
11371137
}
11381138

11391139
type ProfileLibraryReference_IndexLibraryResult struct {
1140-
Name string `json:"name,omitempty"`
1141-
Version string `json:"version,omitempty"`
1140+
Name string `json:"name,omitempty"`
1141+
Version string `json:"version,omitempty"`
1142+
IsDependency bool `json:"is_dependency,omitempty"`
11421143
}
11431144

11441145
func NewProfileLibraryReference_IndexLibraryResult(resp *rpc.ProfileLibraryReference_IndexLibrary) *ProfileLibraryReference_IndexLibraryResult {
11451146
return &ProfileLibraryReference_IndexLibraryResult{
1146-
Name: resp.GetName(),
1147-
Version: resp.GetVersion(),
1147+
Name: resp.GetName(),
1148+
Version: resp.GetVersion(),
1149+
IsDependency: resp.GetIsDependency(),
11481150
}
11491151
}

rpc/cc/arduino/cli/commands/v1/common.pb.go

Lines changed: 22 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/common.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ message ProfileLibraryReference {
244244
string name = 1;
245245
// Version of the library if taken from the Library Index.
246246
string version = 2;
247+
// If true, this library is marked as a dependency.
248+
bool is_dependency = 3;
247249
}
248250
message LocalLibrary {
249251
// Absolute path to the library.

0 commit comments

Comments
 (0)