Skip to content

Commit

Permalink
Added --no-overwrite flag in lib install command (#1793)
Browse files Browse the repository at this point in the history
* Added no_overwrite field in LibraryInstall

* Added tests

* Check if libraries needs to be installed prior to download/install

* Update rpc/cc/arduino/cli/commands/v1/lib.proto

Co-authored-by: per1234 <[email protected]>

Co-authored-by: per1234 <[email protected]>
  • Loading branch information
cmaglie and per1234 authored Jul 11, 2022
1 parent 38ee95c commit 2ea3608
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 312 deletions.
21 changes: 12 additions & 9 deletions cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ import (
)

var (
noDeps bool
gitURL bool
zipPath bool
noDeps bool
noOverwrite bool
gitURL bool
zipPath bool
)

func initInstallCommand() *cobra.Command {
Expand All @@ -59,6 +60,7 @@ func initInstallCommand() *cobra.Command {
},
}
installCommand.Flags().BoolVar(&noDeps, "no-deps", false, tr("Do not install dependencies."))
installCommand.Flags().BoolVar(&noOverwrite, "no-overwrite", false, tr("Do not overwrite already installed libraries."))
installCommand.Flags().BoolVar(&gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
installCommand.Flags().BoolVar(&zipPath, "zip-path", false, tr("Enter a path to zip file"))
return installCommand
Expand Down Expand Up @@ -87,7 +89,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallRequest{
Instance: instance,
Path: path,
Overwrite: true,
Overwrite: !noOverwrite,
}, output.TaskProgress())
if err != nil {
feedback.Errorf(tr("Error installing Zip Library: %v"), err)
Expand All @@ -110,7 +112,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
err := lib.GitLibraryInstall(context.Background(), &rpc.GitLibraryInstallRequest{
Instance: instance,
Url: url,
Overwrite: true,
Overwrite: !noOverwrite,
}, output.TaskProgress())
if err != nil {
feedback.Errorf(tr("Error installing Git Library: %v"), err)
Expand All @@ -128,10 +130,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {

for _, libRef := range libRefs {
libraryInstallRequest := &rpc.LibraryInstallRequest{
Instance: instance,
Name: libRef.Name,
Version: libRef.Version,
NoDeps: noDeps,
Instance: instance,
Name: libRef.Name,
Version: libRef.Version,
NoDeps: noDeps,
NoOverwrite: noOverwrite,
}
err := lib.LibraryInstall(context.Background(), libraryInstallRequest, output.ProgressBar(), output.TaskProgress())
if err != nil {
Expand Down
28 changes: 27 additions & 1 deletion commands/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package lib
import (
"context"
"errors"
"fmt"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
Expand Down Expand Up @@ -64,7 +65,8 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
}
}

didInstall := false
// Find the libReleasesToInstall to install
libReleasesToInstall := []*librariesindex.Release{}
for _, lib := range toInstall {
libRelease, err := findLibraryIndexRelease(lm, &rpc.LibraryInstallRequest{
Name: lib.Name,
Expand All @@ -73,7 +75,31 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
if err != nil {
return err
}
libReleasesToInstall = append(libReleasesToInstall, libRelease)
}

// Check if any of the libraries to install is already installed and remove it from the list
j := 0
for i, libRelease := range libReleasesToInstall {
_, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease)
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
taskCB(&rpc.TaskProgress{Message: tr("Already installed %s", libRelease), Completed: true})
} else if err != nil {
return err
} else {
libReleasesToInstall[j] = libReleasesToInstall[i]
j++
}
if req.GetNoOverwrite() {
if libReplaced != nil {
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", libRelease, libReplaced))
}
}
}
libReleasesToInstall = libReleasesToInstall[:j]

didInstall := false
for _, libRelease := range libReleasesToInstall {
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2ea3608

Please sign in to comment.