Skip to content

Commit

Permalink
feat: install from github url
Browse files Browse the repository at this point in the history
  • Loading branch information
lxl66566 committed Apr 8, 2024
1 parent eb4fa2e commit 2581f13
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
4 changes: 3 additions & 1 deletion bpm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def value_in(value, in_list):
install_parser = subparsers.add_parser(
"install", aliases=["i"], help="Install packages."
)
install_parser.add_argument("packages", nargs="+", help="Package name to install")
install_parser.add_argument(
"packages", nargs="+", help="Package name or github url to install"
)
install_parser.add_argument(
"-b",
"--bin-name",
Expand Down
36 changes: 26 additions & 10 deletions bpm/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging as log
from pathlib import Path
from tempfile import TemporaryDirectory
from urllib.parse import urlparse

from simpleufcs import UFCS

Expand All @@ -26,19 +27,34 @@ def cli_install(args):
)
exit(1)
for package in args.packages:
if not args.dry_run and repo_group.find_repo(package)[1]:
log.info(f"{package} is already installed.")
real_name = package
test_parse = urlparse(package)
if test_parse.netloc == "github.com":
real_name = RepoHandler.get_info_by_url(package)[1]
if not args.dry_run and repo_group.find_repo(real_name)[1]:
log.info(f"{real_name} is already installed.")
continue
try:
repo = RepoHandler(
package,
prefer_gnu=args.prefer_gnu,
one_bin=args.one_bin,
).with_bin_name(args.bin_name)
if not args.local:
repo = repo.ask(quiet=args.quiet, sort=args.sort).get_asset(
interactive=args.interactive
if test_parse.netloc == "github.com":
repo = (
RepoHandler(
real_name,
prefer_gnu=args.prefer_gnu,
one_bin=args.one_bin,
)
.set_by_url(package)
.with_bin_name(args.bin_name)
)
else:
repo = RepoHandler(
package,
prefer_gnu=args.prefer_gnu,
one_bin=args.one_bin,
).with_bin_name(args.bin_name)
if not args.local:
repo.ask(quiet=args.quiet, sort=args.sort)
if not args.local:
repo.get_asset(interactive=args.interactive)

except Exception as e:
log.error(f"Failed on searching `{package}`: {e}")
Expand Down
33 changes: 24 additions & 9 deletions bpm/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,30 @@ def file_list(self) -> list[str]:
def add_file_list(self, file):
self.installed_files.append(str(file))

def set_url(self, url: str):
@staticmethod
def get_info_by_fullname(fullname: str):
"""
set repo_owner and repo_name from url
Returns: a tuple of (repo_owner, repo_name)
"""
# why does https://t.me/withabsolutex/1479 happens?
r = urlparse(url).path.strip("/").split("/")
assert len(r) == 2, "parsing invalid URL"
self.repo_owner = r[0]
self.repo_name = r[1]
r = fullname.strip("/").split("/")
assert len(r) == 2, "parsing invalid fullname"
return tuple(r)

@staticmethod
def get_info_by_url(url: str):
"""
set repo_owner and repo_name from url
"""
return RepoHandler.get_info_by_fullname(urlparse(url).path)

def set_by_url(self, url: str):
"""
set repo_owner and repo_name from url
"""
info = self.get_info_by_url(url)
self.repo_owner = info[0]
self.repo_name = info[1]
return self

def search(self, page=1, sort=None) -> Optional[list[str]]:
Expand Down Expand Up @@ -204,7 +219,7 @@ def ask(self, quiet: bool = False, sort=None):
raise RepoNotFoundError
if quiet:
log.info(f"auto select repo: {repo_selections[0]}")
return self.set_url(repo_selections[0])
return self.set_by_url(repo_selections[0])
for i, item in enumerate(repo_selections):
print(f"{i+1}: {item}")
try:
Expand All @@ -219,7 +234,7 @@ def ask(self, quiet: bool = False, sort=None):
elif temp == "p":
page -= 1
continue
return self.set_url(repo_selections[int(temp) - 1])
return self.set_by_url(repo_selections[int(temp) - 1])
except KeyboardInterrupt:
print("Canceled.")
exit(0)
Expand Down Expand Up @@ -331,7 +346,7 @@ def test_get_filelist(self):
self.assertListEqual(test.file_list, ["a", "b", "c"])

def test_parse_url(self):
test = RepoHandler("yazi").set_url("https://github.com/sxyazi/yazi")
test = RepoHandler("yazi").set_by_url("https://github.com/sxyazi/yazi")
self.assertEqual(test.repo_owner, "sxyazi")
self.assertEqual(test.repo_name, "yazi")

Expand Down

0 comments on commit 2581f13

Please sign in to comment.