Skip to content

Commit e4c0b9d

Browse files
committed
Allow directories in profile libraries
1 parent 55f86b5 commit e4c0b9d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

commands/instances.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,24 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
363363
} else {
364364
// Load libraries required for profile
365365
for _, libraryRef := range profile.Libraries {
366+
if libraryRef.InstallDir != nil {
367+
libDir := libraryRef.InstallDir
368+
if !libDir.IsAbs() {
369+
libDir = paths.New(req.GetSketchPath()).JoinPath(libraryRef.InstallDir)
370+
}
371+
if !libDir.IsDir() {
372+
return &cmderrors.InvalidArgumentError{
373+
Message: i18n.Tr("Invalid library directory in sketch project: %s", libraryRef.InstallDir),
374+
}
375+
}
376+
lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
377+
Path: libDir,
378+
Location: libraries.User,
379+
IsSingleLibrary: true,
380+
})
381+
continue
382+
}
383+
366384
uid := libraryRef.InternalUniqueIdentifier()
367385
libRoot := s.settings.ProfilesCacheDir().Join(uid)
368386
libDir := libRoot.Join(libraryRef.Library)

internal/arduino/sketch/profiles.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/arduino/arduino-cli/internal/i18n"
2929
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3030
"github.com/arduino/go-paths-helper"
31+
"go.bug.st/f"
3132
semver "go.bug.st/relaxed-semver"
3233
"gopkg.in/yaml.v3"
3334
)
@@ -268,8 +269,9 @@ func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) err
268269

269270
// ProfileLibraryReference is a reference to a library
270271
type ProfileLibraryReference struct {
271-
Library string
272-
Version *semver.Version
272+
Library string
273+
InstallDir *paths.Path
274+
Version *semver.Version
273275
}
274276

275277
// UnmarshalYAML decodes a ProfileLibraryReference from YAML source.
@@ -278,6 +280,11 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
278280
if err := unmarshal(&data); err != nil {
279281
return err
280282
}
283+
if strings.HasPrefix(data, "file:") {
284+
l.InstallDir = paths.New(strings.TrimPrefix(data, "file:"))
285+
l.Library = l.InstallDir.Base()
286+
return nil
287+
}
281288
if libName, libVersion, ok := parseNameAndVersion(data); !ok {
282289
return fmt.Errorf("%s %s", i18n.Tr("invalid library directive:"), data)
283290
} else if v, err := semver.Parse(libVersion); err != nil {
@@ -291,16 +298,23 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
291298

292299
// AsYaml outputs the required library as Yaml
293300
func (l *ProfileLibraryReference) AsYaml() string {
294-
res := fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
295-
return res
301+
if l.InstallDir != nil {
302+
return fmt.Sprintf(" - file:%s\n", l.InstallDir)
303+
}
304+
return fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
296305
}
297306

298307
func (l *ProfileLibraryReference) String() string {
308+
if l.InstallDir != nil {
309+
return fmt.Sprintf("%s@file:%s", l.Library, l.InstallDir)
310+
}
299311
return fmt.Sprintf("%s@%s", l.Library, l.Version)
300312
}
301313

302314
// InternalUniqueIdentifier returns the unique identifier for this object
303315
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
316+
f.Assert(l.InstallDir == nil,
317+
"InternalUniqueIdentifier should not be called for library references with an install directory")
304318
id := l.String()
305319
h := sha256.Sum256([]byte(id))
306320
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])

0 commit comments

Comments
 (0)