@@ -17,6 +17,7 @@ package librariesmanager
17
17
18
18
import (
19
19
"context"
20
+ "errors"
20
21
"fmt"
21
22
"net/url"
22
23
"os"
@@ -192,28 +193,24 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
192
193
return fmt .Errorf (tr ("User directory not set" ))
193
194
}
194
195
195
- libraryName , ref , err := parseGitURL (gitURL )
196
+ gitLibraryName , ref , err := parseGitURL (gitURL )
196
197
if err != nil {
197
198
return err
198
199
}
199
200
200
- // Deletes libraries folder if already installed
201
- installPath := installDir .Join (libraryName )
202
- if installPath .IsDir () {
203
- if ! overwrite {
204
- return fmt .Errorf (tr ("library %s already installed" ), libraryName )
205
- }
206
- installPath .RemoveAll ()
207
- }
208
- if installPath .Exist () {
209
- return fmt .Errorf (tr ("could not create directory %s: a file with the same name exists!" , installPath ))
201
+ // Clone library in a temporary directory
202
+ tmp , err := paths .MkTempDir ("" , "" )
203
+ if err != nil {
204
+ return err
210
205
}
206
+ defer tmp .RemoveAll ()
207
+ tmpInstallPath := tmp .Join (gitLibraryName )
211
208
212
209
depth := 1
213
210
if ref != "" {
214
211
depth = 0
215
212
}
216
- repo , err := git .PlainClone (installPath .String (), false , & git.CloneOptions {
213
+ repo , err := git .PlainClone (tmpInstallPath .String (), false , & git.CloneOptions {
217
214
URL : gitURL ,
218
215
Depth : depth ,
219
216
Progress : os .Stdout ,
@@ -232,14 +229,52 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
232
229
}
233
230
}
234
231
235
- if err := validateLibrary (installPath ); err != nil {
236
- // Clean up installation directory since this is not a valid library
237
- installPath .RemoveAll ()
232
+ // We don't want the installed library to be a git repository thus we delete this folder
233
+ tmpInstallPath .Join (".git" ).RemoveAll ()
234
+
235
+ // Check if the library is valid and load metatada
236
+ if err := validateLibrary (tmpInstallPath ); err != nil {
237
+ return err
238
+ }
239
+ library , err := libraries .Load (tmpInstallPath , libraries .User )
240
+ if err != nil {
238
241
return err
239
242
}
240
243
241
- // We don't want the installed library to be a git repository thus we delete this folder
242
- installPath .Join (".git" ).RemoveAll ()
244
+ // Check if the library is already installed and determine install path
245
+ var installPath * paths.Path
246
+ libInstalled , libReplaced , err := lm .InstallPrerequisiteCheck (library .Name , library .Version , libraries .User )
247
+ if errors .Is (err , ErrAlreadyInstalled ) {
248
+ if ! overwrite {
249
+ return fmt .Errorf (tr ("library %s already installed" ), library .Name )
250
+ }
251
+ installPath = libInstalled
252
+ } else if err != nil {
253
+ return err
254
+ } else if libReplaced != nil {
255
+ if ! overwrite {
256
+ return fmt .Errorf (tr ("Library %[1]s is already installed, but with a different version: %[2]s" , library .Name , libReplaced ))
257
+ }
258
+ installPath = libReplaced .InstallDir
259
+ } else {
260
+ installPath = installDir .Join (library .Name )
261
+ if ! overwrite && installPath .IsDir () {
262
+ return fmt .Errorf (tr ("library %s already installed" , library .Name ))
263
+ }
264
+ }
265
+
266
+ // Deletes libraries folder if already installed
267
+ if installPath .IsDir () {
268
+ installPath .RemoveAll ()
269
+ }
270
+ if installPath .Exist () {
271
+ return fmt .Errorf (tr ("could not create directory %s: a file with the same name exists!" , installPath ))
272
+ }
273
+
274
+ // Copy extracted library in the destination directory
275
+ if err := tmpInstallPath .CopyDirTo (installPath ); err != nil {
276
+ return fmt .Errorf (tr ("moving extracted archive to destination dir: %s" ), err )
277
+ }
243
278
return nil
244
279
}
245
280
0 commit comments