Skip to content

Commit 22fb998

Browse files
committed
Factored all duplicated code for importing a library from a directory
1 parent 36dc6d6 commit 22fb998

File tree

1 file changed

+43
-65
lines changed

1 file changed

+43
-65
lines changed

Diff for: arduino/libraries/librariesmanager/install.go

+43-65
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ import (
3939
// that is going to be replaced by the new one.
4040
// This is the result of a call to InstallPrerequisiteCheck.
4141
type LibraryInstallPlan struct {
42+
// Name of the library to install
43+
Name string
44+
45+
// Version of the library to install
46+
Version *semver.Version
47+
4248
// TargetPath is the path where the library should be installed.
4349
TargetPath *paths.Path
4450

@@ -88,6 +94,8 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(name string, version *semve
8894
}
8995

9096
return &LibraryInstallPlan{
97+
Name: name,
98+
Version: version,
9199
TargetPath: libPath,
92100
ReplacedLib: replaced,
93101
UpToDate: upToDate,
@@ -99,12 +107,40 @@ func (lm *LibrariesManager) Install(indexLibrary *librariesindex.Release, instal
99107
return indexLibrary.Resource.Install(lm.DownloadsDir, installPath.Parent(), installPath)
100108
}
101109

102-
// InstallLibraryFromFolder installs a library by copying it from the given folder.
103-
func (lm *LibrariesManager) InstallLibraryFromFolder(libPath *paths.Path, installPath *paths.Path) error {
104-
if installPath.Exist() {
105-
return fmt.Errorf("%s: %s", tr("destination directory already exists"), installPath)
110+
// importLibraryFromDirectory installs a library by copying it from the given directory.
111+
func (lm *LibrariesManager) importLibraryFromDirectory(libPath *paths.Path, overwrite bool) error {
112+
// Check if the library is valid and load metatada
113+
if err := validateLibrary(libPath); err != nil {
114+
return err
115+
}
116+
library, err := libraries.Load(libPath, libraries.User)
117+
if err != nil {
118+
return err
106119
}
107-
if err := libPath.CopyDirTo(installPath); err != nil {
120+
121+
// Check if the library is already installed and determine install path
122+
installPlan, err := lm.InstallPrerequisiteCheck(library.Name, library.Version, libraries.User)
123+
if err != nil {
124+
return err
125+
}
126+
127+
if installPlan.UpToDate {
128+
if !overwrite {
129+
return fmt.Errorf(tr("library %s already installed"), installPlan.Name)
130+
}
131+
}
132+
if installPlan.ReplacedLib != nil {
133+
if !overwrite {
134+
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", installPlan.Name, installPlan.ReplacedLib))
135+
}
136+
if err := lm.Uninstall(installPlan.ReplacedLib); err != nil {
137+
return err
138+
}
139+
}
140+
if installPlan.TargetPath.Exist() {
141+
return fmt.Errorf("%s: %s", tr("destination directory already exists"), installPlan.TargetPath)
142+
}
143+
if err := libPath.CopyDirTo(installPlan.TargetPath); err != nil {
108144
return fmt.Errorf("%s: %w", tr("copying library to destination directory:"), err)
109145
}
110146
return nil
@@ -159,38 +195,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath *path
159195
}
160196
tmpInstallPath := libRootFiles[0]
161197

162-
// Check if the library is valid and load metatada
163-
if err := validateLibrary(tmpInstallPath); err != nil {
164-
return err
165-
}
166-
library, err := libraries.Load(tmpInstallPath, libraries.User)
167-
if err != nil {
168-
return err
169-
}
170-
171-
// Check if the library is already installed
172-
installPlan, err := lm.InstallPrerequisiteCheck(library.Name, library.Version, libraries.User)
173-
if err != nil {
174-
return err
175-
}
176-
if installPlan.UpToDate {
177-
if !overwrite {
178-
return fmt.Errorf(tr("library %s already installed"), library.Name)
179-
}
180-
}
181-
182-
// Remove the old library if present
183-
if installPlan.ReplacedLib != nil {
184-
if !overwrite {
185-
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", library.Name, installPlan.ReplacedLib))
186-
}
187-
if err := lm.Uninstall(installPlan.ReplacedLib); err != nil {
188-
return err
189-
}
190-
}
191-
192198
// Install extracted library in the destination directory
193-
if err := lm.InstallLibraryFromFolder(tmpInstallPath, installPlan.TargetPath); err != nil {
199+
if err := lm.importLibraryFromDirectory(tmpInstallPath, overwrite); err != nil {
194200
return fmt.Errorf(tr("moving extracted archive to destination dir: %s"), err)
195201
}
196202

@@ -238,36 +244,8 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
238244
// We don't want the installed library to be a git repository thus we delete this folder
239245
tmpInstallPath.Join(".git").RemoveAll()
240246

241-
// Check if the library is valid and load metatada
242-
if err := validateLibrary(tmpInstallPath); err != nil {
243-
return err
244-
}
245-
library, err := libraries.Load(tmpInstallPath, libraries.User)
246-
if err != nil {
247-
return err
248-
}
249-
250-
// Check if the library is already installed and determine install path
251-
installPlan, err := lm.InstallPrerequisiteCheck(library.Name, library.Version, libraries.User)
252-
if err != nil {
253-
return err
254-
}
255-
if installPlan.UpToDate {
256-
if !overwrite {
257-
return fmt.Errorf(tr("library %s already installed"), library.Name)
258-
}
259-
}
260-
if installPlan.ReplacedLib != nil {
261-
if !overwrite {
262-
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", library.Name, installPlan.ReplacedLib))
263-
}
264-
if err := lm.Uninstall(installPlan.ReplacedLib); err != nil {
265-
return err
266-
}
267-
}
268-
269247
// Install extracted library in the destination directory
270-
if err := lm.InstallLibraryFromFolder(tmpInstallPath, installPlan.TargetPath); err != nil {
248+
if err := lm.importLibraryFromDirectory(tmpInstallPath, overwrite); err != nil {
271249
return fmt.Errorf(tr("moving extracted archive to destination dir: %s"), err)
272250
}
273251

0 commit comments

Comments
 (0)