Skip to content

Commit 1e26fdf

Browse files
committed
Allow library installation on bundled libs dir via gRPC
1 parent 8050841 commit 1e26fdf

File tree

7 files changed

+523
-423
lines changed

7 files changed

+523
-423
lines changed

Diff for: arduino/libraries/libraries_location.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
9393
func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
9494
switch *d {
9595
case IDEBuiltIn:
96-
return rpc.LibraryLocation_LIBRARY_LOCATION_IDE_BUILTIN
96+
return rpc.LibraryLocation_LIBRARY_LOCATION_BUILTIN
9797
case PlatformBuiltIn:
9898
return rpc.LibraryLocation_LIBRARY_LOCATION_PLATFORM_BUILTIN
9999
case ReferencedPlatformBuiltIn:
@@ -110,7 +110,7 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
110110
// FromRPCLibraryLocation converts a rpc.LibraryLocation to a LibraryLocation
111111
func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
112112
switch l {
113-
case rpc.LibraryLocation_LIBRARY_LOCATION_IDE_BUILTIN:
113+
case rpc.LibraryLocation_LIBRARY_LOCATION_BUILTIN:
114114
return IDEBuiltIn
115115
case rpc.LibraryLocation_LIBRARY_LOCATION_PLATFORM_BUILTIN:
116116
return PlatformBuiltIn
@@ -124,3 +124,15 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
124124
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
125125
}
126126
}
127+
128+
// FromRPCLibraryInstallLocation converts a rpc.LibraryInstallLocation to a LibraryLocation
129+
func FromRPCLibraryInstallLocation(l rpc.LibraryInstallLocation) LibraryLocation {
130+
switch l {
131+
case rpc.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_BUILTIN:
132+
return IDEBuiltIn
133+
case rpc.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_USER:
134+
return User
135+
default:
136+
panic(fmt.Sprintf("invalid rpc.LibraryInstallLocation value %d", l))
137+
}
138+
}

Diff for: arduino/libraries/librariesmanager/install.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ var (
4848
// InstallPrerequisiteCheck performs prequisite checks to install a library. It returns the
4949
// install path, where the library should be installed and the possible library that is already
5050
// installed on the same folder and it's going to be replaced by the new one.
51-
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release) (*paths.Path, *libraries.Library, error) {
51+
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release, installLocation libraries.LibraryLocation) (*paths.Path, *libraries.Library, error) {
5252
saneName := utils.SanitizeName(indexLibrary.Library.Name)
5353

5454
var replaced *libraries.Library
5555
if installedLibs, have := lm.Libraries[saneName]; have {
5656
for _, installedLib := range installedLibs.Alternatives {
57-
if installedLib.Location != libraries.User {
57+
if installedLib.Location != installLocation {
5858
continue
5959
}
6060
if installedLib.Version != nil && installedLib.Version.Equal(indexLibrary.Version) {
@@ -64,9 +64,13 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
6464
}
6565
}
6666

67-
libsDir := lm.getUserLibrariesDir()
67+
libsDir := lm.getLibrariesDir(installLocation)
6868
if libsDir == nil {
69-
return nil, nil, fmt.Errorf(tr("User directory not set"))
69+
if installLocation == libraries.User {
70+
return nil, nil, fmt.Errorf(tr("User directory not set"))
71+
} else {
72+
return nil, nil, fmt.Errorf(tr("Builtin libraries directory not set"))
73+
}
7074
}
7175

7276
libPath := libsDir.Join(saneName)
@@ -79,8 +83,8 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
7983
}
8084

8185
// Install installs a library on the specified path.
82-
func (lm *LibrariesManager) Install(indexLibrary *librariesindex.Release, libPath *paths.Path) error {
83-
libsDir := lm.getUserLibrariesDir()
86+
func (lm *LibrariesManager) Install(indexLibrary *librariesindex.Release, libPath *paths.Path, installLocation libraries.LibraryLocation) error {
87+
libsDir := lm.getLibrariesDir(installLocation)
8488
if libsDir == nil {
8589
return fmt.Errorf(tr("User directory not set"))
8690
}
@@ -100,9 +104,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
100104
return nil
101105
}
102106

103-
//InstallZipLib installs a Zip library on the specified path.
107+
// InstallZipLib installs a Zip library on the specified path.
104108
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string, overwrite bool) error {
105-
libsDir := lm.getUserLibrariesDir()
109+
libsDir := lm.getLibrariesDir(libraries.User)
106110
if libsDir == nil {
107111
return fmt.Errorf(tr("User directory not set"))
108112
}
@@ -184,9 +188,9 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
184188
return nil
185189
}
186190

187-
//InstallGitLib installs a library hosted on a git repository on the specified path.
191+
// InstallGitLib installs a library hosted on a git repository on the specified path.
188192
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
189-
libsDir := lm.getUserLibrariesDir()
193+
libsDir := lm.getLibrariesDir(libraries.User)
190194
if libsDir == nil {
191195
return fmt.Errorf(tr("User directory not set"))
192196
}

Diff for: arduino/libraries/librariesmanager/librariesmanager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ func (lm *LibrariesManager) RescanLibraries() []*status.Status {
175175
return statuses
176176
}
177177

178-
func (lm *LibrariesManager) getUserLibrariesDir() *paths.Path {
178+
func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLocation) *paths.Path {
179179
for _, dir := range lm.LibrariesDir {
180-
if dir.Location == libraries.User {
180+
if dir.Location == installLocation {
181181
return dir.Path
182182
}
183183
}

Diff for: commands/lib/install.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/arduino/arduino-cli/arduino"
24+
"github.com/arduino/arduino-cli/arduino/libraries"
2425
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2526
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2627
"github.com/arduino/arduino-cli/commands"
@@ -30,13 +31,13 @@ import (
3031

3132
// LibraryInstall FIXMEDOC
3233
func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
33-
3434
lm := commands.GetLibraryManager(req)
3535
if lm == nil {
3636
return &arduino.InvalidInstanceError{}
3737
}
3838

3939
toInstall := map[string]*rpc.LibraryDependencyStatus{}
40+
installLocation := libraries.FromRPCLibraryInstallLocation(req.GetInstallLocation())
4041
if req.NoDeps {
4142
toInstall[req.Name] = &rpc.LibraryDependencyStatus{
4243
Name: req.Name,
@@ -81,7 +82,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
8182
// Check if any of the libraries to install is already installed and remove it from the list
8283
j := 0
8384
for i, libRelease := range libReleasesToInstall {
84-
_, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease)
85+
_, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease, installLocation)
8586
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
8687
taskCB(&rpc.TaskProgress{Message: tr("Already installed %s", libRelease), Completed: true})
8788
} else if err != nil {
@@ -104,7 +105,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
104105
return err
105106
}
106107

107-
if err := installLibrary(lm, libRelease, taskCB); err != nil {
108+
if err := installLibrary(lm, libRelease, installLocation, taskCB); err != nil {
108109
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
109110
continue
110111
} else {
@@ -123,10 +124,10 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
123124
return nil
124125
}
125126

126-
func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release, taskCB rpc.TaskProgressCB) error {
127+
func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release, installLocation libraries.LibraryLocation, taskCB rpc.TaskProgressCB) error {
127128
taskCB(&rpc.TaskProgress{Name: tr("Installing %s", libRelease)})
128129
logrus.WithField("library", libRelease).Info("Installing library")
129-
libPath, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease)
130+
libPath, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease, installLocation)
130131
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
131132
taskCB(&rpc.TaskProgress{Message: tr("Already installed %s", libRelease), Completed: true})
132133
return err
@@ -140,7 +141,7 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
140141
taskCB(&rpc.TaskProgress{Message: tr("Replacing %[1]s with %[2]s", libReplaced, libRelease)})
141142
}
142143

143-
if err := lm.Install(libRelease, libPath); err != nil {
144+
if err := lm.Install(libRelease, libPath, installLocation); err != nil {
144145
return &arduino.FailedLibraryInstallError{Cause: err}
145146
}
146147

Diff for: commands/lib/upgrade.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121

2222
"github.com/arduino/arduino-cli/arduino"
23+
"github.com/arduino/arduino-cli/arduino/libraries"
2324
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2425
"github.com/arduino/arduino-cli/commands"
2526
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -64,7 +65,7 @@ func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downlo
6465

6566
// Go through the list and install them
6667
for _, lib := range libs {
67-
if err := installLibrary(lm, lib.Available, taskCB); err != nil {
68+
if err := installLibrary(lm, lib.Available, libraries.User, taskCB); err != nil {
6869
if !errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
6970
return err
7071
}

0 commit comments

Comments
 (0)