@@ -22,6 +22,7 @@ import (
2222 "os"
2323 "strings"
2424
25+ "github.com/arduino/arduino-cli/arduino"
2526 "github.com/arduino/arduino-cli/arduino/globals"
2627 "github.com/arduino/arduino-cli/arduino/libraries"
2728 "github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -49,45 +50,51 @@ var (
4950// install path, where the library should be installed and the possible library that is already
5051// installed on the same folder and it's going to be replaced by the new one.
5152func (lm * LibrariesManager ) InstallPrerequisiteCheck (indexLibrary * librariesindex.Release , installLocation libraries.LibraryLocation ) (* paths.Path , * libraries.Library , error ) {
52- saneName := utils .SanitizeName (indexLibrary .Library .Name )
53+ installDir := lm .getLibrariesDir (installLocation )
54+ if installDir == nil {
55+ if installLocation == libraries .User {
56+ return nil , nil , fmt .Errorf (tr ("User directory not set" ))
57+ }
58+ return nil , nil , fmt .Errorf (tr ("Builtin libraries directory not set" ))
59+ }
5360
54- var replaced * libraries.Library
55- if installedLibs , have := lm .Libraries [saneName ]; have {
56- for _ , installedLib := range installedLibs .Alternatives {
57- if installedLib .Location != installLocation {
58- continue
59- }
60- if installedLib .Version != nil && installedLib .Version .Equal (indexLibrary .Version ) {
61- return installedLib .InstallDir , nil , ErrAlreadyInstalled
62- }
63- replaced = installedLib
61+ name := indexLibrary .Library .Name
62+ libs := lm .FindByReference (& librariesindex.Reference {Name : name }, installLocation )
63+ for _ , lib := range libs {
64+ if lib .Version != nil && lib .Version .Equal (indexLibrary .Version ) {
65+ return lib .InstallDir , nil , ErrAlreadyInstalled
6466 }
6567 }
6668
67- libsDir := lm .getLibrariesDir (installLocation )
68- if libsDir == nil {
69- if installLocation == libraries .User {
70- return nil , nil , fmt .Errorf (tr ("User directory not set" ))
69+ if len (libs ) > 1 {
70+ libsDir := paths .NewPathList ()
71+ for _ , lib := range libs {
72+ libsDir .Add (lib .InstallDir )
73+ }
74+ return nil , nil , & arduino.MultipleLibraryInstallDetected {
75+ LibName : name ,
76+ LibsDir : libsDir ,
77+ Message : tr ("Automatic library install can't be performed in this case, please manually remove all duplicates and retry." ),
7178 }
72- return nil , nil , fmt .Errorf (tr ("Builtin libraries directory not set" ))
7379 }
7480
75- libPath := libsDir .Join (saneName )
76- if replaced != nil && replaced .InstallDir .EquivalentTo (libPath ) {
81+ var replaced * libraries.Library
82+ if len (libs ) == 1 {
83+ replaced = libs [0 ]
84+ }
7785
86+ libPath := installDir .Join (utils .SanitizeName (indexLibrary .Library .Name ))
87+ if replaced != nil && replaced .InstallDir .EquivalentTo (libPath ) {
88+ return libPath , replaced , nil
7889 } else if libPath .IsDir () {
7990 return nil , nil , fmt .Errorf (tr ("destination dir %s already exists, cannot install" ), libPath )
8091 }
8192 return libPath , replaced , nil
8293}
8394
8495// Install installs a library on the specified path.
85- func (lm * LibrariesManager ) Install (indexLibrary * librariesindex.Release , libPath * paths.Path , installLocation libraries.LibraryLocation ) error {
86- libsDir := lm .getLibrariesDir (installLocation )
87- if libsDir == nil {
88- return fmt .Errorf (tr ("User directory not set" ))
89- }
90- return indexLibrary .Resource .Install (lm .DownloadsDir , libsDir , libPath )
96+ func (lm * LibrariesManager ) Install (indexLibrary * librariesindex.Release , libPath * paths.Path ) error {
97+ return indexLibrary .Resource .Install (lm .DownloadsDir , libPath .Parent (), libPath )
9198}
9299
93100// Uninstall removes a Library
@@ -99,7 +106,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
99106 return fmt .Errorf (tr ("removing lib directory: %s" ), err )
100107 }
101108
102- lm .Libraries [lib .Name ].Remove (lib )
109+ alternatives := lm .Libraries [lib .Name ]
110+ alternatives .Remove (lib )
111+ lm .Libraries [lib .Name ] = alternatives
103112 return nil
104113}
105114
@@ -189,8 +198,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
189198
190199// InstallGitLib installs a library hosted on a git repository on the specified path.
191200func (lm * LibrariesManager ) InstallGitLib (gitURL string , overwrite bool ) error {
192- libsDir := lm .getLibrariesDir (libraries .User )
193- if libsDir == nil {
201+ installDir := lm .getLibrariesDir (libraries .User )
202+ if installDir == nil {
194203 return fmt .Errorf (tr ("User directory not set" ))
195204 }
196205
@@ -202,10 +211,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
202211 return err
203212 }
204213
205- installPath := libsDir .Join (libraryName )
206-
207214 // Deletes libraries folder if already installed
208- if _ , ok := lm .Libraries [libraryName ]; ok {
215+ installPath := installDir .Join (libraryName )
216+ if installPath .IsDir () {
209217 if ! overwrite {
210218 return fmt .Errorf (tr ("library %s already installed" ), libraryName )
211219 }
@@ -215,6 +223,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
215223 Trace ("Deleting library" )
216224 installPath .RemoveAll ()
217225 }
226+ if installPath .Exist () {
227+ return fmt .Errorf (tr ("could not create directory %s: a file with the same name exists!" , installPath ))
228+ }
218229
219230 logrus .
220231 WithField ("library name" , libraryName ).
0 commit comments