Skip to content

Commit 3b1449f

Browse files
committed
Use RealName instead of (dir) Name as key in librarymanager.Library map
1 parent ea93eec commit 3b1449f

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

arduino/libraries/librariesmanager/install.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ var (
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.
5151
func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesindex.Release, installLocation libraries.LibraryLocation) (*paths.Path, *libraries.Library, error) {
52-
saneName := utils.SanitizeName(indexLibrary.Library.Name)
53-
5452
var replaced *libraries.Library
55-
if installedLibs, have := lm.Libraries[saneName]; have {
53+
name := indexLibrary.Library.Name
54+
if installedLibs, have := lm.Libraries[name]; have {
5655
for _, installedLib := range installedLibs {
5756
if installedLib.Location != installLocation {
5857
continue
@@ -72,7 +71,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
7271
return nil, nil, fmt.Errorf(tr("Builtin libraries directory not set"))
7372
}
7473

75-
libPath := libsDir.Join(saneName)
74+
libPath := libsDir.Join(utils.SanitizeName(indexLibrary.Library.Name))
7675
if replaced != nil && replaced.InstallDir.EquivalentTo(libPath) {
7776

7877
} else if libPath.IsDir() {
@@ -99,9 +98,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
9998
return fmt.Errorf(tr("removing lib directory: %s"), err)
10099
}
101100

102-
alternatives := lm.Libraries[lib.Name]
101+
alternatives := lm.Libraries[lib.RealName]
103102
alternatives.Remove(lib)
104-
lm.Libraries[lib.Name] = alternatives
103+
lm.Libraries[lib.RealName] = alternatives
105104
return nil
106105
}
107106

@@ -191,8 +190,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
191190

192191
// InstallGitLib installs a library hosted on a git repository on the specified path.
193192
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
194-
libsDir := lm.getLibrariesDir(libraries.User)
195-
if libsDir == nil {
193+
installDir := lm.getLibrariesDir(libraries.User)
194+
if installDir == nil {
196195
return fmt.Errorf(tr("User directory not set"))
197196
}
198197

@@ -204,10 +203,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
204203
return err
205204
}
206205

207-
installPath := libsDir.Join(libraryName)
208-
209206
// Deletes libraries folder if already installed
210-
if _, ok := lm.Libraries[libraryName]; ok {
207+
installPath := installDir.Join(libraryName)
208+
if installPath.IsDir() {
211209
if !overwrite {
212210
return fmt.Errorf(tr("library %s already installed"), libraryName)
213211
}
@@ -217,6 +215,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
217215
Trace("Deleting library")
218216
installPath.RemoveAll()
219217
}
218+
if installPath.Exist() {
219+
return fmt.Errorf(tr("could not create directory %s: a file with the same name exists!", installPath))
220+
}
220221

221222
logrus.
222223
WithField("library name", libraryName).

arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/arduino/arduino-cli/arduino/cores"
2323
"github.com/arduino/arduino-cli/arduino/libraries"
2424
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
25-
"github.com/arduino/arduino-cli/arduino/utils"
2625
"github.com/arduino/arduino-cli/i18n"
2726
paths "github.com/arduino/go-paths-helper"
2827
"github.com/pmylund/sortutil"
@@ -179,9 +178,9 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*
179178
continue
180179
}
181180
library.ContainerPlatform = librariesDir.PlatformRelease
182-
alternatives := lm.Libraries[library.Name]
181+
alternatives := lm.Libraries[library.RealName]
183182
alternatives.Add(library)
184-
lm.Libraries[library.Name] = alternatives
183+
lm.Libraries[library.RealName] = alternatives
185184
}
186185

187186
return statuses
@@ -200,18 +199,17 @@ func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location
200199
return fmt.Errorf(tr("loading library from %[1]s: %[2]s"), libRootDir, err)
201200
}
202201

203-
alternatives := lm.Libraries[library.Name]
202+
alternatives := lm.Libraries[library.RealName]
204203
alternatives.Add(library)
205-
lm.Libraries[library.Name] = alternatives
204+
lm.Libraries[library.RealName] = alternatives
206205
return nil
207206
}
208207

209208
// FindByReference return the installed library matching the Reference
210209
// name and version or, if the version is nil, the library installed
211210
// in the User folder.
212211
func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, installLocation libraries.LibraryLocation) *libraries.Library {
213-
saneName := utils.SanitizeName(libRef.Name)
214-
alternatives, have := lm.Libraries[saneName]
212+
alternatives, have := lm.Libraries[libRef.Name]
215213
if !have {
216214
return nil
217215
}

commands/lib/install.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
144144
if err := lm.Install(libRelease, libPath, installLocation); err != nil {
145145
return &arduino.FailedLibraryInstallError{Cause: err}
146146
}
147-
147+
if libReplaced != nil && !libReplaced.InstallDir.EquivalentTo(libPath) {
148+
if err := lm.Uninstall(libReplaced); err != nil {
149+
return fmt.Errorf("%s: %s", tr("could not remove old library"), err)
150+
}
151+
}
148152
taskCB(&rpc.TaskProgress{Message: tr("Installed %s", libRelease), Completed: true})
149153
return nil
150154
}

0 commit comments

Comments
 (0)