Skip to content

Commit 0ae5232

Browse files
author
vinay-lanka
committed
add zip library imports
have added a --zip-path flag to lib install command takes absolute path to zip file and extracts it to libraries directory
1 parent 763b4a7 commit 0ae5232

File tree

16 files changed

+608
-209
lines changed

16 files changed

+608
-209
lines changed

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

+76
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
package librariesmanager
1717

1818
import (
19+
"archive/zip"
1920
"errors"
2021
"fmt"
22+
"io"
23+
"log"
24+
"os"
25+
"path/filepath"
26+
"strings"
2127

2228
"github.com/arduino/arduino-cli/arduino/libraries"
2329
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -85,3 +91,73 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
8591
lm.Libraries[lib.Name].Remove(lib)
8692
return nil
8793
}
94+
95+
//InstallZipLib installs a Zip library on the specified path.
96+
func (lm *LibrariesManager) InstallZipLib(libPath string) error {
97+
libsDir := lm.getUserLibrariesDir()
98+
if libsDir == nil {
99+
return fmt.Errorf("User directory not set")
100+
}
101+
err := Unzip(libPath, libsDir.String())
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
return nil
106+
}
107+
108+
//Unzip takes the ZipLibPath and Extracts it to LibsDir
109+
func Unzip(src string, dest string) error {
110+
111+
var filenames []string
112+
113+
r, err := zip.OpenReader(src)
114+
if err != nil {
115+
return err
116+
}
117+
defer r.Close()
118+
119+
for _, f := range r.File {
120+
121+
// Store filename/path for returning and using later on
122+
fpath := filepath.Join(dest, f.Name)
123+
124+
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
125+
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
126+
return fmt.Errorf("%s: illegal file path", fpath)
127+
}
128+
129+
filenames = append(filenames, fpath)
130+
131+
if f.FileInfo().IsDir() {
132+
// Make Folder
133+
os.MkdirAll(fpath, os.ModePerm)
134+
continue
135+
}
136+
137+
// Make File
138+
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
139+
return err
140+
}
141+
142+
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
143+
if err != nil {
144+
return err
145+
}
146+
147+
rc, err := f.Open()
148+
if err != nil {
149+
return err
150+
}
151+
152+
_, err = io.Copy(outFile, rc)
153+
154+
// Close the file without defer to close before next iteration of loop
155+
outFile.Close()
156+
rc.Close()
157+
158+
if err != nil {
159+
return err
160+
}
161+
}
162+
return nil
163+
}

Diff for: cli/lib/install.go

+59-43
Original file line numberDiff line numberDiff line change
@@ -40,64 +40,80 @@ func initInstallCommand() *cobra.Command {
4040
Run: runInstallCommand,
4141
}
4242
installCommand.Flags().BoolVar(&installFlags.noDeps, "no-deps", false, "Do not install dependencies.")
43+
installCommand.Flags().StringVarP(&installFlags.gitURL, "git-url", "g", "", "Enter git url for libraries hosted on repositories")
44+
installCommand.Flags().StringVarP(&installFlags.zipPath, "zip-path", "z", "", "Enter a path to zip file")
4345
return installCommand
4446
}
4547

4648
var installFlags struct {
47-
noDeps bool
49+
noDeps bool
50+
gitURL string
51+
zipPath string
4852
}
4953

5054
func runInstallCommand(cmd *cobra.Command, args []string) {
5155
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
52-
libRefs, err := ParseLibraryReferenceArgsAndAdjustCase(instance, args)
53-
if err != nil {
54-
feedback.Errorf("Arguments error: %v", err)
55-
os.Exit(errorcodes.ErrBadArgument)
56-
}
57-
58-
toInstall := map[string]*rpc.LibraryDependencyStatus{}
59-
if installFlags.noDeps {
60-
for _, libRef := range libRefs {
61-
toInstall[libRef.Name] = &rpc.LibraryDependencyStatus{
62-
Name: libRef.Name,
63-
VersionRequired: libRef.Version,
64-
}
56+
if installFlags.zipPath != "" {
57+
ZiplibraryInstallReq := &rpc.ZipLibraryInstallReq{
58+
Instance: instance,
59+
Path: installFlags.zipPath,
6560
}
66-
} else {
67-
for _, libRef := range libRefs {
68-
depsResp, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{
69-
Instance: instance,
70-
Name: libRef.Name,
71-
Version: libRef.Version,
72-
})
73-
if err != nil {
74-
feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err)
61+
_, err := lib.ZipLibraryInstall(context.Background(), ZiplibraryInstallReq)
62+
if err != nil {
63+
feedback.Errorf("Error installing Zip Library %v", err)
64+
os.Exit(errorcodes.ErrGeneric)
65+
}
66+
}else{
67+
libRefs, err := ParseLibraryReferenceArgsAndAdjustCase(instance, args)
68+
if err != nil {
69+
feedback.Errorf("Arguments error: %v", err)
70+
os.Exit(errorcodes.ErrBadArgument)
71+
}
72+
73+
toInstall := map[string]*rpc.LibraryDependencyStatus{}
74+
if installFlags.noDeps {
75+
for _, libRef := range libRefs {
76+
toInstall[libRef.Name] = &rpc.LibraryDependencyStatus{
77+
Name: libRef.Name,
78+
VersionRequired: libRef.Version,
79+
}
7580
}
76-
for _, dep := range depsResp.GetDependencies() {
77-
feedback.Printf("%s depends on %s@%s", libRef, dep.GetName(), dep.GetVersionRequired())
78-
if existingDep, has := toInstall[dep.GetName()]; has {
79-
if existingDep.GetVersionRequired() != dep.GetVersionRequired() {
80-
// TODO: make a better error
81-
feedback.Errorf("The library %s is required in two different versions: %s and %s",
82-
dep.GetName(), dep.GetVersionRequired(), existingDep.GetVersionRequired())
83-
os.Exit(errorcodes.ErrGeneric)
81+
} else {
82+
for _, libRef := range libRefs {
83+
depsResp, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{
84+
Instance: instance,
85+
Name: libRef.Name,
86+
Version: libRef.Version,
87+
})
88+
if err != nil {
89+
feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err)
90+
}
91+
for _, dep := range depsResp.GetDependencies() {
92+
feedback.Printf("%s depends on %s@%s", libRef, dep.GetName(), dep.GetVersionRequired())
93+
if existingDep, has := toInstall[dep.GetName()]; has {
94+
if existingDep.GetVersionRequired() != dep.GetVersionRequired() {
95+
// TODO: make a better error
96+
feedback.Errorf("The library %s is required in two different versions: %s and %s",
97+
dep.GetName(), dep.GetVersionRequired(), existingDep.GetVersionRequired())
98+
os.Exit(errorcodes.ErrGeneric)
99+
}
84100
}
101+
toInstall[dep.GetName()] = dep
85102
}
86-
toInstall[dep.GetName()] = dep
87103
}
88104
}
89-
}
90105

91-
for _, library := range toInstall {
92-
libraryInstallReq := &rpc.LibraryInstallReq{
93-
Instance: instance,
94-
Name: library.Name,
95-
Version: library.VersionRequired,
96-
}
97-
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
98-
if err != nil {
99-
feedback.Errorf("Error installing %s: %v", library, err)
100-
os.Exit(errorcodes.ErrGeneric)
106+
for _, library := range toInstall {
107+
libraryInstallReq := &rpc.LibraryInstallReq{
108+
Instance: instance,
109+
Name: library.Name,
110+
Version: library.VersionRequired,
111+
}
112+
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
113+
if err != nil {
114+
feedback.Errorf("Error installing %s: %v", library, err)
115+
os.Exit(errorcodes.ErrGeneric)
116+
}
101117
}
102118
}
103119
}

Diff for: commands/daemon/daemon.go

+5
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,8 @@ func (s *ArduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.Libr
278278
func (s *ArduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {
279279
return lib.LibraryList(ctx, req)
280280
}
281+
282+
//ZipLibraryInstall FIXMEDOC
283+
func (s *ArduinoCoreServerImpl) ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq) (*rpc.ZipLibraryInstallResp, error) {
284+
return lib.ZipLibraryInstall(ctx, req)
285+
}

Diff for: commands/lib/install.go

+20
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,23 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
7575
taskCB(&rpc.TaskProgress{Message: "Installed " + libRelease.String(), Completed: true})
7676
return nil
7777
}
78+
79+
//ZipLibraryInstall FIXMEDOC
80+
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq) (*rpc.ZipLibraryInstallResp, error) {
81+
res := &rpc.ZipLibraryInstallResp{}
82+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
83+
Path := req.GetPath()
84+
if err := installZipLibrary(lm, Path); err != nil {
85+
res.Status = "Error installing Zip Library"
86+
return res, err
87+
}
88+
res.Status = "Success! Installed Zip Library"
89+
return res, nil
90+
}
91+
92+
func installZipLibrary(lm *librariesmanager.LibrariesManager, libPath string) error {
93+
if err := lm.InstallZipLib(libPath); err != nil {
94+
return err
95+
}
96+
return nil
97+
}

Diff for: rpc/commands/board.pb.go

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)