Skip to content

Commit 4a4d1fd

Browse files
cmaglieper1234
andauthored
[breaking] Remove auto detection of Arduino IDE built-in libraries and tools / Allow gRPC install of built-in libraries (#1817)
* Removed IDE-bundle autodetection * Do not return bundled tools folder with HardwarePaths * Created configuration functions to obtain data dir and downloads dir * Bundled libraries configuration is now considere a single directory * Allow library installation on bundled libs dir via gRPC * Added integration test for library install in bundled directory * Fixed nil pointer exception in build options extraction * Updated docs * Removed useless custom env bindings * Applied code review changes Co-authored-by: per1234 <[email protected]> * Set builtin libraries dir by default when running as a daemon * Added test for regression See #1817 (review) * Fixed regression: 'lib uninstall' must operate only on user dir Co-authored-by: per1234 <[email protected]>
1 parent 5036697 commit 4a4d1fd

37 files changed

+963
-624
lines changed

arduino/cores/packagemanager/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (pm *Builder) LoadHardware() []error {
3838
hardwareDirs := configuration.HardwareDirectories(configuration.Settings)
3939
merr := pm.LoadHardwareFromDirectories(hardwareDirs)
4040

41-
bundleToolDirs := configuration.BundleToolsDirectories(configuration.Settings)
41+
bundleToolDirs := configuration.BuiltinToolsDirectories(configuration.Settings)
4242
merr = append(merr, pm.LoadToolsFromBundleDirectories(bundleToolDirs)...)
4343

4444
return merr

arduino/cores/packagemanager/package_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
224224
pmb := packagemanager.NewBuilder(
225225
dataDir1,
226226
configuration.PackagesDir(configuration.Settings),
227-
paths.New(configuration.Settings.GetString("directories.Downloads")),
227+
configuration.DownloadsDir(configuration.Settings),
228228
dataDir1,
229229
"test",
230230
)

arduino/libraries/libraries_location.go

Lines changed: 14 additions & 2 deletions
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+
}

arduino/libraries/librariesmanager/install.go

Lines changed: 13 additions & 10 deletions
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,12 @@ 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+
}
72+
return nil, nil, fmt.Errorf(tr("Builtin libraries directory not set"))
7073
}
7174

7275
libPath := libsDir.Join(saneName)
@@ -79,8 +82,8 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
7982
}
8083

8184
// Install installs a library on the specified path.
82-
func (lm *LibrariesManager) Install(indexLibrary *librariesindex.Release, libPath *paths.Path) error {
83-
libsDir := lm.getUserLibrariesDir()
85+
func (lm *LibrariesManager) Install(indexLibrary *librariesindex.Release, libPath *paths.Path, installLocation libraries.LibraryLocation) error {
86+
libsDir := lm.getLibrariesDir(installLocation)
8487
if libsDir == nil {
8588
return fmt.Errorf(tr("User directory not set"))
8689
}
@@ -100,9 +103,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
100103
return nil
101104
}
102105

103-
//InstallZipLib installs a Zip library on the specified path.
106+
// InstallZipLib installs a Zip library on the specified path.
104107
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string, overwrite bool) error {
105-
libsDir := lm.getUserLibrariesDir()
108+
libsDir := lm.getLibrariesDir(libraries.User)
106109
if libsDir == nil {
107110
return fmt.Errorf(tr("User directory not set"))
108111
}
@@ -184,9 +187,9 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
184187
return nil
185188
}
186189

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

arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func (alts *LibraryAlternatives) Remove(library *libraries.Library) {
7878
}
7979

8080
// FindVersion returns the library mathching the provided version or nil if not found
81-
func (alts *LibraryAlternatives) FindVersion(version *semver.Version) *libraries.Library {
81+
func (alts *LibraryAlternatives) FindVersion(version *semver.Version, installLocation libraries.LibraryLocation) *libraries.Library {
8282
for _, lib := range alts.Alternatives {
83-
if lib.Version.Equal(version) {
83+
if lib.Version.Equal(version) && lib.Location == installLocation {
8484
return lib
8585
}
8686
}
@@ -118,6 +118,7 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
118118
// LoadIndex reads a library_index.json from a file and returns
119119
// the corresponding Index structure.
120120
func (lm *LibrariesManager) LoadIndex() error {
121+
logrus.WithField("index", lm.IndexFile).Info("Loading libraries index file")
121122
index, err := librariesindex.LoadIndex(lm.IndexFile)
122123
if err != nil {
123124
lm.Index = librariesindex.EmptyIndex
@@ -175,9 +176,9 @@ func (lm *LibrariesManager) RescanLibraries() []*status.Status {
175176
return statuses
176177
}
177178

178-
func (lm *LibrariesManager) getUserLibrariesDir() *paths.Path {
179+
func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLocation) *paths.Path {
179180
for _, dir := range lm.LibrariesDir {
180-
if dir.Location == libraries.User {
181+
if dir.Location == installLocation {
181182
return dir.Path
182183
}
183184
}
@@ -244,7 +245,7 @@ func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location
244245
// FindByReference return the installed library matching the Reference
245246
// name and version or, if the version is nil, the library installed
246247
// in the User folder.
247-
func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference) *libraries.Library {
248+
func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, installLocation libraries.LibraryLocation) *libraries.Library {
248249
saneName := utils.SanitizeName(libRef.Name)
249250
alternatives, have := lm.Libraries[saneName]
250251
if !have {
@@ -253,11 +254,11 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference) *l
253254
// TODO: Move "search into user" into another method...
254255
if libRef.Version == nil {
255256
for _, candidate := range alternatives.Alternatives {
256-
if candidate.Location == libraries.User {
257+
if candidate.Location == installLocation {
257258
return candidate
258259
}
259260
}
260261
return nil
261262
}
262-
return alternatives.FindVersion(libRef.Version)
263+
return alternatives.FindVersion(libRef.Version, installLocation)
263264
}

cli/cache/clean.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func initCleanCommand() *cobra.Command {
4040
func runCleanCommand(cmd *cobra.Command, args []string) {
4141
logrus.Info("Executing `arduino-cli cache clean`")
4242

43-
cachePath := configuration.Settings.GetString("directories.Downloads")
44-
err := os.RemoveAll(cachePath)
43+
cachePath := configuration.DownloadsDir(configuration.Settings)
44+
err := cachePath.RemoveAll()
4545
if err != nil {
4646
feedback.Errorf(tr("Error cleaning caches: %v"), err)
4747
os.Exit(errorcodes.ErrGeneric)

cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func preRun(cmd *cobra.Command, args []string) {
158158
configFile := configuration.Settings.ConfigFileUsed()
159159

160160
// initialize inventory
161-
err := inventory.Init(configuration.Settings.GetString("directories.Data"))
161+
err := inventory.Init(configuration.DataDir(configuration.Settings).String())
162162
if err != nil {
163163
feedback.Errorf("Error: %v", err)
164164
os.Exit(errorcodes.ErrBadArgument)

cli/config/validate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var validMap = map[string]reflect.Kind{
3030
"directories.data": reflect.String,
3131
"directories.downloads": reflect.String,
3232
"directories.user": reflect.String,
33+
"directories.builtin.tools": reflect.String,
34+
"directories.builtin.libraries": reflect.String,
3335
"library.enable_unsafe_install": reflect.Bool,
3436
"logging.file": reflect.String,
3537
"logging.format": reflect.String,

cli/core/search.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/arduino/arduino-cli/configuration"
3535
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3636
"github.com/arduino/arduino-cli/table"
37-
"github.com/arduino/go-paths-helper"
3837
"github.com/sirupsen/logrus"
3938
"github.com/spf13/cobra"
4039
)
@@ -130,7 +129,7 @@ func (sr searchResults) String() string {
130129
// of 24 hours is used.
131130
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
132131
func indexesNeedUpdating(duration string) bool {
133-
indexpath := paths.New(configuration.Settings.GetString("directories.Data"))
132+
indexpath := configuration.DataDir(configuration.Settings)
134133

135134
now := time.Now()
136135
modTimeThreshold, err := time.ParseDuration(duration)

cli/daemon/daemon.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func NewCommand() *cobra.Command {
7272
func runDaemonCommand(cmd *cobra.Command, args []string) {
7373
logrus.Info("Executing `arduino-cli daemon`")
7474

75+
// Bundled libraries support is enabled by default when running as a daemon
76+
configuration.Settings.SetDefault("directories.builtin.Libraries", configuration.GetDefaultBuiltinLibrariesDir())
77+
7578
port := configuration.Settings.GetString("daemon.port")
7679
gRPCOptions := []grpc.ServerOption{}
7780
if debugFile != "" {

0 commit comments

Comments
 (0)