@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"strings"
24
24
25
+ "github.com/arduino/arduino-cli/arduino"
25
26
"github.com/arduino/arduino-cli/arduino/globals"
26
27
"github.com/arduino/arduino-cli/arduino/libraries"
27
28
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -49,45 +50,51 @@ var (
49
50
// install path, where the library should be installed and the possible library that is already
50
51
// installed on the same folder and it's going to be replaced by the new one.
51
52
func (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
+ }
53
60
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
64
66
}
65
67
}
66
68
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." ),
71
78
}
72
- return nil , nil , fmt .Errorf (tr ("Builtin libraries directory not set" ))
73
79
}
74
80
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
+ }
77
85
86
+ libPath := installDir .Join (utils .SanitizeName (indexLibrary .Library .Name ))
87
+ if replaced != nil && replaced .InstallDir .EquivalentTo (libPath ) {
88
+ return libPath , replaced , nil
78
89
} else if libPath .IsDir () {
79
90
return nil , nil , fmt .Errorf (tr ("destination dir %s already exists, cannot install" ), libPath )
80
91
}
81
92
return libPath , replaced , nil
82
93
}
83
94
84
95
// 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 )
91
98
}
92
99
93
100
// Uninstall removes a Library
@@ -99,7 +106,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
99
106
return fmt .Errorf (tr ("removing lib directory: %s" ), err )
100
107
}
101
108
102
- lm .Libraries [lib .Name ].Remove (lib )
109
+ alternatives := lm .Libraries [lib .Name ]
110
+ alternatives .Remove (lib )
111
+ lm .Libraries [lib .Name ] = alternatives
103
112
return nil
104
113
}
105
114
@@ -189,8 +198,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
189
198
190
199
// InstallGitLib installs a library hosted on a git repository on the specified path.
191
200
func (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 {
194
203
return fmt .Errorf (tr ("User directory not set" ))
195
204
}
196
205
@@ -202,10 +211,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
202
211
return err
203
212
}
204
213
205
- installPath := libsDir .Join (libraryName )
206
-
207
214
// Deletes libraries folder if already installed
208
- if _ , ok := lm .Libraries [libraryName ]; ok {
215
+ installPath := installDir .Join (libraryName )
216
+ if installPath .IsDir () {
209
217
if ! overwrite {
210
218
return fmt .Errorf (tr ("library %s already installed" ), libraryName )
211
219
}
@@ -215,6 +223,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
215
223
Trace ("Deleting library" )
216
224
installPath .RemoveAll ()
217
225
}
226
+ if installPath .Exist () {
227
+ return fmt .Errorf (tr ("could not create directory %s: a file with the same name exists!" , installPath ))
228
+ }
218
229
219
230
logrus .
220
231
WithField ("library name" , libraryName ).
0 commit comments