-
Notifications
You must be signed in to change notification settings - Fork 0
/
yazi-package
executable file
·226 lines (176 loc) · 6.61 KB
/
yazi-package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
# Author: Jan Larres <jan@majutsushi.net>
# License: MIT/X11
#
# This tool manages Yazi packages similarly to 'ya pack',
# with the difference that it will automatically create a
# .gitignore file to ignore installed packages, and it doesn't
# write hashes to the package config. This means that updating
# a package will not lead to a dirty repo if the config
# directory is under version control.
#
# It also stores the repositories in $XDG_CACHE_HOME instead of
# $XDG_STATE_HOME since they are effectively just caches, and this
# way they get automatically excluded from things like backups.
#
# Create a config file ~/.config/yazi/packages.yaml with content like this:
#
# - url: https://github.com/KKV9/compress.yazi
# - url: https://github.com/yazi-rs/plugins
# path: chmod.yazi
# - url: https://github.com/yazi-rs/plugins
# path: git.yazi
# commit: '0b9f325fe9d1edbc6d7893344cd308533ebd827a'
#
# Supported keys:
# url (required): URL of the repository
# path (optional): Path to the plugin inside the repository
# commit (optional): The commit to pin the package to
#
# See https://github.com/sxyazi/yazi/blob/main/yazi-cli/src/package
import argparse
import dataclasses
import hashlib
import logging
import os
import shutil
import subprocess
import sys
from pathlib import Path
import platformdirs
import yaml
CONFIG_PATH = Path(
os.getenv("YAZI_CONFIG_HOME", platformdirs.user_config_path("yazi"))
).expanduser()
PACKAGE_CONFIG = CONFIG_PATH / "packages.yaml"
PLUGINS_PATH = CONFIG_PATH / "plugins"
FLAVORS_PATH = CONFIG_PATH / "flavors"
REPOS_PATH = platformdirs.user_cache_path("yazi-package")
MARKER_FILE = ".yazi-package"
FLAVOR_FILES = [
"flavor.toml",
"tmtheme.xml",
"README.md",
"preview.png",
"LICENSE",
"LICENSE-tmtheme",
]
PLUGIN_FILES = ["init.lua", "README.md", "LICENSE"]
logging.basicConfig(format="%(message)s", level=logging.INFO)
log = logging.getLogger(__name__)
@dataclasses.dataclass(frozen=True)
class Package:
url: str
path: str = ""
commit: str | None = None
def name(self) -> str:
name = self.path if self.path else self.url.split("/")[-1]
if not name.endswith(".yazi"):
name += ".yazi"
return name
def repo_cache(self) -> Path:
return REPOS_PATH / hashlib.sha256(self.url.encode()).hexdigest()
def main(args: argparse.Namespace) -> int:
if args.verbose:
log.setLevel(logging.DEBUG)
args.func()
return 0
def install() -> None:
packages = read_package_config()
for package in packages:
install_package(package, skip_installed=True)
def update() -> None:
packages = read_package_config()
for package in packages:
install_package(package, skip_installed=False)
def uninstall() -> None:
configured_packages = read_package_config()
installed_packages = get_managed_packages()
to_delete = installed_packages.keys() - set(configured_packages)
for package in to_delete:
log.info("Uninstalling package %s", package.name())
shutil.rmtree(installed_packages[package])
package_repo = package.repo_cache()
if package_repo.exists():
shutil.rmtree(package_repo)
def clean() -> None:
packages = get_managed_packages()
for package in packages.values():
shutil.rmtree(package)
if REPOS_PATH.exists():
shutil.rmtree(REPOS_PATH)
def install_package(package: Package, skip_installed: bool) -> None:
source_path = package.repo_cache() / package.path
is_flavor = (source_path / "flavor.toml").exists()
target_path = (
FLAVORS_PATH / package.name() if is_flavor else PLUGINS_PATH / package.name()
)
marker = target_path / MARKER_FILE
if target_path.exists() and not marker.exists():
log.warning("Non-managed package %s already exists; skipping", package.name())
return
if target_path.exists() and skip_installed:
return
if package.repo_cache().exists():
log.info("Updating repository %s", package.url)
git(["fetch", "-q"], cwd=package.repo_cache())
git(["checkout", "-q", "origin/HEAD"], cwd=package.repo_cache())
else:
log.info("Cloning repository %s", package.url)
git(["clone", "-q", package.url, str(package.repo_cache())])
# Check out pinned commit
if package.commit:
git(["checkout", "-q", package.commit], cwd=package.repo_cache())
# Copy package files to config directory
verb = "Updating" if target_path.exists() else "Installing"
log.info("%s package %s", verb, package.name())
target_path.mkdir(parents=True, exist_ok=True)
files = FLAVOR_FILES if is_flavor else PLUGIN_FILES
for file in files:
shutil.copy(source_path / file, target_path)
git_ignore = target_path / ".gitignore"
git_ignore.write_text("*\n")
marker.write_text(yaml.dump(dataclasses.asdict(package)))
def get_managed_packages() -> dict[Package, Path]:
packages: dict[Package, Path] = {}
for path in (PLUGINS_PATH, FLAVORS_PATH):
for package_path in (
package_path.parent for package_path in path.glob("**/" + MARKER_FILE)
):
with (package_path / MARKER_FILE).open() as f:
package = Package(**yaml.safe_load(f))
packages[package] = package_path
return packages
def read_package_config() -> list[Package]:
with PACKAGE_CONFIG.open() as f:
package_config: list[dict[str, str]] = yaml.safe_load(f)
return [Package(**item) for item in package_config]
def git(args: list[str], cwd: Path | None = None) -> None:
subprocess.run(["git", *args], check=True, cwd=cwd) # noqa: S603, S607
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="manage yazi packages")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="increase output verbosity",
)
subparsers = parser.add_subparsers(required=True)
install_parser = subparsers.add_parser(
"install", help="install configured packages"
)
install_parser.set_defaults(func=install)
update_parser = subparsers.add_parser("update", help="update all packages")
update_parser.set_defaults(func=update)
uninstall_parser = subparsers.add_parser(
"uninstall", help="delete packages that are no longer configured"
)
uninstall_parser.set_defaults(func=uninstall)
clean_parser = subparsers.add_parser(
"clean", help="delete all managed packages and caches"
)
clean_parser.set_defaults(func=clean)
return parser.parse_args()
if __name__ == "__main__":
sys.exit(main(parse_args()))