Skip to content

Commit 6d83692

Browse files
committed
Added support for 'dependency:' field in profiles libraries
1 parent 5128cc2 commit 6d83692

File tree

5 files changed

+68
-29
lines changed

5 files changed

+68
-29
lines changed

internal/arduino/sketch/profiles.go

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

350350
// ProfileLibraryReference is a reference to a library
351351
type ProfileLibraryReference struct {
352-
Library string
353-
Version *semver.Version
354-
InstallDir *paths.Path
355-
GitURL *url.URL
352+
Library string
353+
Version *semver.Version
354+
IsDependency bool
355+
InstallDir *paths.Path
356+
GitURL *url.URL
356357
}
357358

358359
// UnmarshalYAML decodes a ProfileLibraryReference from YAML source.
359360
func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
360361
var dataMap map[string]any
361-
var data string
362+
var libReference string
362363
if err := unmarshal(&dataMap); err == nil {
363364
if installDir, ok := dataMap["dir"]; ok {
364365
if installDir, ok := installDir.(string); !ok {
@@ -381,15 +382,24 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
381382
l.Library = strings.TrimSuffix(l.Library, ".git")
382383
return nil
383384
}
385+
} else if depLib, ok := dataMap["dependency"]; ok {
386+
if libReference, ok = depLib.(string); !ok {
387+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), dataMap)
388+
}
389+
l.IsDependency = true
390+
// Fallback
384391
} else {
385392
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), dataMap)
386393
}
387-
} else if err := unmarshal(&data); err != nil {
394+
} else if err := unmarshal(&libReference); err != nil {
388395
return err
396+
} else {
397+
l.IsDependency = false
389398
}
390399

391-
if libName, libVersion, ok := parseNameAndVersion(data); !ok {
392-
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), data)
400+
// Parse reference in the format "LIBRARY_NAME (VERSION)"
401+
if libName, libVersion, ok := parseNameAndVersion(libReference); !ok {
402+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference"), libReference)
393403
} else if v, err := semver.Parse(libVersion); err != nil {
394404
return fmt.Errorf("%s: %w", i18n.Tr("invalid version"), err)
395405
} else {
@@ -407,7 +417,11 @@ func (l *ProfileLibraryReference) AsYaml() string {
407417
if l.GitURL != nil {
408418
return fmt.Sprintf(" - git: %s\n", l.GitURL)
409419
}
410-
return fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
420+
dep := ""
421+
if l.IsDependency {
422+
dep = "dependency: "
423+
}
424+
return fmt.Sprintf(" - %s%s (%s)\n", dep, l.Library, l.Version)
411425
}
412426

413427
func (l *ProfileLibraryReference) String() string {
@@ -417,10 +431,14 @@ func (l *ProfileLibraryReference) String() string {
417431
if l.GitURL != nil {
418432
return "@git:" + l.GitURL.String()
419433
}
434+
dep := ""
435+
if l.IsDependency {
436+
dep = " (dep)"
437+
}
420438
if l.Version == nil {
421-
return l.Library
439+
return l.Library + dep
422440
}
423-
return fmt.Sprintf("%s@%s", l.Library, l.Version)
441+
return fmt.Sprintf("%s@%s%s", l.Library, l.Version, dep)
424442
}
425443

426444
// Match checks if this library reference matches another one.
@@ -467,8 +485,9 @@ func (l *ProfileLibraryReference) ToRpc() *rpc.ProfileLibraryReference {
467485
return &rpc.ProfileLibraryReference{
468486
Library: &rpc.ProfileLibraryReference_IndexLibrary_{
469487
IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{
470-
Name: l.Library,
471-
Version: l.Version.String(),
488+
Name: l.Library,
489+
Version: l.Version.String(),
490+
IsDependency: l.IsDependency,
472491
},
473492
},
474493
}
@@ -499,7 +518,11 @@ func FromRpcProfileLibraryReference(l *rpc.ProfileLibraryReference) (*ProfileLib
499518
}
500519
version = v
501520
}
502-
return &ProfileLibraryReference{Library: indexLib.GetName(), Version: version}, nil
521+
return &ProfileLibraryReference{
522+
Library: indexLib.GetName(),
523+
Version: version,
524+
IsDependency: indexLib.GetIsDependency(),
525+
}, nil
503526
}
504527
return nil, &cmderrors.InvalidArgumentError{Message: "library not specified"}
505528
}
@@ -518,7 +541,7 @@ func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
518541
return id + "-" + hex.EncodeToString(h[:])[:8]
519542
}
520543

521-
id := l.String()
544+
id := l.Library + "@" + l.Version.String()
522545
h := sha256.Sum256([]byte(id))
523546
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])
524547
return utils.SanitizeName(res)

internal/arduino/sketch/profiles_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ func TestProjectFileLibraries(t *testing.T) {
6060
require.NoError(t, err)
6161
require.Len(t, proj.Profiles, 1)
6262
prof := proj.Profiles[0]
63-
require.Len(t, prof.Libraries, 5)
63+
require.Len(t, prof.Libraries, 6)
6464
require.Equal(t, "FlashStorage@1.2.3", prof.Libraries[0].String())
6565
require.Equal(t, "@dir:/path/to/system/lib", prof.Libraries[1].String())
6666
require.Equal(t, "@dir:path/to/sketch/lib", prof.Libraries[2].String())
6767
require.Equal(t, "@git:https://github.com/username/HelloWorld.git#v2.13", prof.Libraries[3].String())
6868
require.Equal(t, "@git:https://github.com/username/HelloWorld.git#v2.14", prof.Libraries[4].String())
69+
require.Equal(t, "DependencyLib@2.3.4 (dep)", prof.Libraries[5].String())
6970
require.Equal(t, "FlashStorage_1.2.3_e525d7c96b27788f", prof.Libraries[0].InternalUniqueIdentifier())
7071
require.Panics(t, func() { prof.Libraries[1].InternalUniqueIdentifier() })
7172
require.Panics(t, func() { prof.Libraries[2].InternalUniqueIdentifier() })
7273
require.Equal(t, "git-github.com_username_HelloWorld.git_v2.13-0c146203", prof.Libraries[3].InternalUniqueIdentifier())
7374
require.Equal(t, "git-github.com_username_HelloWorld.git_v2.14-49f5df7f", prof.Libraries[4].InternalUniqueIdentifier())
75+
require.Equal(t, "DependencyLib_2.3.4_ecde631facb47ae5", prof.Libraries[5].InternalUniqueIdentifier())
7476

7577
orig, err := sketchProj.ReadFile()
7678
require.NoError(t, err)

internal/arduino/sketch/testdata/profiles/profile_with_libraries.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ profiles:
99
- dir: path/to/sketch/lib
1010
- git: https://github.com/username/HelloWorld.git#v2.13
1111
- git: https://github.com/username/HelloWorld.git#v2.14
12+
- dependency: DependencyLib (2.3.4)
1213

1314
default_profile: giga_any

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

Lines changed: 24 additions & 13 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)