Skip to content

Commit b35a0d2

Browse files
committed
Allow directories in profile libraries
1 parent cf62bee commit b35a0d2

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
)
@@ -271,12 +272,26 @@ func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) err
271272

272273
// ProfileLibraryReference is a reference to a library
273274
type ProfileLibraryReference struct {
274-
Library string
275-
Version *semver.Version
275+
Library string
276+
InstallDir *paths.Path
277+
Version *semver.Version
276278
}
277279

278280
// UnmarshalYAML decodes a ProfileLibraryReference from YAML source.
279281
func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
282+
var dataMap map[string]any
283+
if err := unmarshal(&dataMap); err == nil {
284+
if installDir, ok := dataMap["dir"]; !ok {
285+
return errors.New(i18n.Tr("invalid library reference: %s", dataMap))
286+
} else if installDir, ok := installDir.(string); !ok {
287+
return fmt.Errorf("%s: %s", i18n.Tr("invalid library reference: %s"), dataMap)
288+
} else {
289+
l.InstallDir = paths.New(installDir)
290+
l.Library = l.InstallDir.Base()
291+
return nil
292+
}
293+
}
294+
280295
var data string
281296
if err := unmarshal(&data); err != nil {
282297
return err
@@ -294,16 +309,23 @@ func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) erro
294309

295310
// AsYaml outputs the required library as Yaml
296311
func (l *ProfileLibraryReference) AsYaml() string {
297-
res := fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
298-
return res
312+
if l.InstallDir != nil {
313+
return fmt.Sprintf(" - dir: %s\n", l.InstallDir)
314+
}
315+
return fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
299316
}
300317

301318
func (l *ProfileLibraryReference) String() string {
319+
if l.InstallDir != nil {
320+
return fmt.Sprintf("%s@dir:%s", l.Library, l.InstallDir)
321+
}
302322
return fmt.Sprintf("%s@%s", l.Library, l.Version)
303323
}
304324

305325
// InternalUniqueIdentifier returns the unique identifier for this object
306326
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
327+
f.Assert(l.InstallDir == nil,
328+
"InternalUniqueIdentifier should not be called for library references with an install directory")
307329
id := l.String()
308330
h := sha256.Sum256([]byte(id))
309331
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])

0 commit comments

Comments
 (0)