Skip to content

Commit 8a4489f

Browse files
committed
Fixed lib install --git-url pre-install checks
Now it performs all the needed checks to avoid multiple installations.
1 parent 66ed9c8 commit 8a4489f

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

arduino/libraries/librariesmanager/install.go

+52-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package librariesmanager
1717

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"net/url"
2223
"os"
@@ -192,28 +193,24 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
192193
return fmt.Errorf(tr("User directory not set"))
193194
}
194195

195-
libraryName, ref, err := parseGitURL(gitURL)
196+
gitLibraryName, ref, err := parseGitURL(gitURL)
196197
if err != nil {
197198
return err
198199
}
199200

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
210205
}
206+
defer tmp.RemoveAll()
207+
tmpInstallPath := tmp.Join(gitLibraryName)
211208

212209
depth := 1
213210
if ref != "" {
214211
depth = 0
215212
}
216-
repo, err := git.PlainClone(installPath.String(), false, &git.CloneOptions{
213+
repo, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
217214
URL: gitURL,
218215
Depth: depth,
219216
Progress: os.Stdout,
@@ -232,14 +229,52 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
232229
}
233230
}
234231

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 {
238241
return err
239242
}
240243

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+
}
243278
return nil
244279
}
245280

0 commit comments

Comments
 (0)