Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4a657f4

Browse files
committedJan 19, 2021
Fix lib install with git url
1 parent c6be6fa commit 4a657f4

File tree

5 files changed

+297
-136
lines changed

5 files changed

+297
-136
lines changed
 

‎arduino/libraries/librariesmanager/install.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22+
"net/url"
2223
"os"
2324
"strings"
2425

@@ -111,21 +112,42 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
111112
}
112113

113114
//InstallGitLib installs a library hosted on a git repository on the specified path.
114-
func (lm *LibrariesManager) InstallGitLib(url string) error {
115+
func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
115116
libsDir := lm.getUserLibrariesDir()
116117
if libsDir == nil {
117118
return fmt.Errorf("User directory not set")
118119
}
119-
i := strings.LastIndex(url, "/")
120-
folder := strings.TrimRight(url[i+1:], ".git")
121-
path := libsDir.Join(folder)
122120

123-
_, err := git.PlainClone(path.String(), false, &git.CloneOptions{
124-
URL: url,
121+
libraryName, err := parseGitURL(gitURL)
122+
if err != nil {
123+
return err
124+
}
125+
126+
installPath := libsDir.Join(libraryName)
127+
128+
_, err = git.PlainClone(installPath.String(), false, &git.CloneOptions{
129+
URL: gitURL,
125130
Progress: os.Stdout,
126131
})
127132
if err != nil {
128133
return err
129134
}
130135
return nil
131136
}
137+
138+
func parseGitURL(gitURL string) (string, error) {
139+
var res string
140+
if strings.HasPrefix(gitURL, "git") || strings.HasPrefix(gitURL, "ssh") {
141+
// We can't parse these as URLs
142+
i := strings.LastIndex(gitURL, "/")
143+
res = strings.TrimRight(gitURL[i+1:], ".git")
144+
} else if path := paths.New(gitURL); path.Exist() {
145+
res = path.Base()
146+
} else if parsed, err := url.Parse(gitURL); err == nil {
147+
i := strings.LastIndex(parsed.Path, "/")
148+
res = strings.TrimRight(parsed.Path[i+1:], ".git")
149+
} else {
150+
return "", fmt.Errorf("invalid git url")
151+
}
152+
return res, nil
153+
}

‎cli/lib/install.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/arduino/arduino-cli/commands/lib"
3030
"github.com/arduino/arduino-cli/configuration"
3131
rpc "github.com/arduino/arduino-cli/rpc/commands"
32+
"github.com/arduino/go-paths-helper"
3233
"github.com/spf13/cobra"
3334
)
3435

@@ -85,9 +86,18 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
8586
}
8687

8788
if installFlags.gitURL {
89+
url := args[0]
90+
if url == "." {
91+
wd, err := paths.Getwd()
92+
if err != nil {
93+
feedback.Errorf("Couldn't get current working directory: %v", err)
94+
os.Exit(errorcodes.ErrGeneric)
95+
}
96+
url = wd.String()
97+
}
8898
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
8999
Instance: instance,
90-
Url: args[0],
100+
Url: url,
91101
}
92102
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
93103
if err != nil {

‎poetry.lock

Lines changed: 179 additions & 129 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ black = { version = "^19.10b0", allow-prereleases = true }
2020
filelock = "^3.0.12"
2121
pytest-xdist = "^2.1.0"
2222
pytest_httpserver = "^0.3.5"
23+
GitPython = "^3.1.12"
2324

2425
[tool.black]
2526
line-length = 120

‎test/test_lib.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# otherwise use the software for commercial activities involving the Arduino
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to license@arduino.cc.
15+
import platform
16+
1517
import simplejson as json
18+
import pytest
19+
from git import Repo
1620
from pathlib import Path
1721

1822

@@ -383,3 +387,77 @@ def test_lib_list_with_updatable_flag(run_command):
383387
assert "6.11.0" == data[0]["library"]["version"]
384388
assert "6.11.0" != data[0]["release"]["version"]
385389
assert "" != data[0]["release"]["version"]
390+
391+
392+
def test_install_with_git_url_from_current_directory(run_command, downloads_dir, data_dir):
393+
assert run_command("update")
394+
395+
env = {
396+
"ARDUINO_DATA_DIR": data_dir,
397+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
398+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
399+
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
400+
}
401+
402+
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
403+
# Verifies library is not installed
404+
assert not lib_install_dir.exists()
405+
406+
# Clone repository locally
407+
git_url = "https://github.com/arduino-libraries/WiFi101.git"
408+
repo_dir = Path(data_dir, "WiFi101")
409+
assert Repo.clone_from(git_url, repo_dir)
410+
411+
assert run_command("lib install --git-url .", custom_working_dir=repo_dir, custom_env=env)
412+
413+
# Verifies library is installed to correct folder
414+
assert lib_install_dir.exists()
415+
416+
417+
@pytest.mark.skipif(
418+
platform.system() == "Windows",
419+
reason="Using a file uri as git url doesn't work on Windows, "
420+
+ "this must be removed when this issue is fixed: https://github.com/go-git/go-git/issues/247",
421+
)
422+
def test_install_with_git_url_local_file_uri(run_command, downloads_dir, data_dir):
423+
assert run_command("update")
424+
425+
env = {
426+
"ARDUINO_DATA_DIR": data_dir,
427+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
428+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
429+
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
430+
}
431+
432+
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
433+
# Verifies library is not installed
434+
assert not lib_install_dir.exists()
435+
436+
# Clone repository locally
437+
git_url = "https://github.com/arduino-libraries/WiFi101.git"
438+
repo_dir = Path(data_dir, "WiFi101")
439+
assert Repo.clone_from(git_url, repo_dir)
440+
441+
assert run_command(f"lib install --git-url {repo_dir.as_uri()}", custom_working_dir=repo_dir, custom_env=env)
442+
443+
444+
def test_install_with_git_local_url(run_command, downloads_dir, data_dir):
445+
assert run_command("update")
446+
447+
env = {
448+
"ARDUINO_DATA_DIR": data_dir,
449+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
450+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
451+
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
452+
}
453+
454+
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
455+
# Verifies library is not installed
456+
assert not lib_install_dir.exists()
457+
458+
# Clone repository locally
459+
git_url = "https://github.com/arduino-libraries/WiFi101.git"
460+
repo_dir = Path(data_dir, "WiFi101")
461+
assert Repo.clone_from(git_url, repo_dir)
462+
463+
assert run_command(f"lib install --git-url {repo_dir}", custom_working_dir=repo_dir, custom_env=env)

0 commit comments

Comments
 (0)
Please sign in to comment.