Skip to content

Commit ebafc23

Browse files
committed
Allow directories in profile libraries
1 parent 2947cfb commit ebafc23

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-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.Unmanaged,
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: 26 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,12 +269,26 @@ 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.
276278
func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
279+
var dataMap map[string]any
280+
if err := unmarshal(&dataMap); err == nil {
281+
if installDir, ok := dataMap["dir"]; !ok {
282+
return errors.New(i18n.Tr("invalid library reference: %s", dataMap))
283+
} else if installDir, ok := installDir.(string); !ok {
284+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference: %s"), dataMap)
285+
} else {
286+
l.InstallDir = paths.New(installDir)
287+
l.Library = l.InstallDir.Base()
288+
return nil
289+
}
290+
}
291+
277292
var data string
278293
if err := unmarshal(&data); err != nil {
279294
return err
@@ -291,16 +306,23 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
291306

292307
// AsYaml outputs the required library as Yaml
293308
func (l *ProfileLibraryReference) AsYaml() string {
294-
res := fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
295-
return res
309+
if l.InstallDir != nil {
310+
return fmt.Sprintf(" - dir: %s\n", l.InstallDir)
311+
}
312+
return fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
296313
}
297314

298315
func (l *ProfileLibraryReference) String() string {
316+
if l.InstallDir != nil {
317+
return fmt.Sprintf("%s@dir:%s", l.Library, l.InstallDir)
318+
}
299319
return fmt.Sprintf("%s@%s", l.Library, l.Version)
300320
}
301321

302322
// InternalUniqueIdentifier returns the unique identifier for this object
303323
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
324+
f.Assert(l.InstallDir == nil,
325+
"InternalUniqueIdentifier should not be called for library references with an install directory")
304326
id := l.String()
305327
h := sha256.Sum256([]byte(id))
306328
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])

0 commit comments

Comments
 (0)