Skip to content

Commit

Permalink
fix: backup installed plugins before updating resources
Browse files Browse the repository at this point in the history
  • Loading branch information
raymond-u committed Mar 15, 2024
1 parent 28eb8e8 commit 0f75fd4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/lungo_cli/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,16 @@ def update_app_data(self) -> None:
or self.file_utils.read_text(self.storage.init_file) != config_hash
):
self.console.print_info("Updating bundled resources...")

# Backup the installed plugins directory if it exists
if self.storage.installed_plugins_dir.is_dir():
self.file_utils.move(self.storage.installed_plugins_dir, self.storage.cache_plugins_dir)

self.file_utils.copy_package_resources(f"{PACKAGE_NAME}.resources", ".", self.storage.bundled_dir)

if self.storage.cache_plugins_dir.is_dir():
self.file_utils.move(self.storage.cache_plugins_dir, self.storage.installed_plugins_dir)

self.file_utils.change_mode(self.storage.bundled_dir, 0o700)
self.file_utils.remove(self.storage.init_file)

Expand Down
15 changes: 15 additions & 0 deletions src/lungo_cli/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def copy(self, src: str | PathLike[str], dst: str | PathLike[str], merge: bool =
self.console.print_error(f"Failed to copy {format_path(src.name)} to {format_path(dst.name)} ({e}).")
raise Exit(code=1)

def move(self, src: str | PathLike[str], dst: str | PathLike[str], merge: bool = False) -> None:
src = Path(src)
dst = Path(dst)

try:
self.create_dir(dst.parent)

if not merge:
self.remove(dst)

shutil.move(src, dst)
except Exception as e:
self.console.print_error(f"Failed to move {format_path(src.name)} to {format_path(dst.name)} ({e}).")
raise Exit(code=1)

def remove(self, path: str | PathLike[str]) -> None:
path = Path(path)

Expand Down
12 changes: 8 additions & 4 deletions src/lungo_cli/core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def data_dir(self, value: str | PathLike[str]):
def data_latest_dir(self) -> Path:
return self._data_dir / self._storage_version

@property
def custom_plugins_dir(self) -> Path:
return self.config_dir / "plugins"

@property
def bundled_dir(self) -> Path:
return self.data_latest_dir / self.bundled_rel
Expand Down Expand Up @@ -96,6 +92,14 @@ def managed_rel(self) -> Path:
def excluded_rel(self) -> Path:
return Path("excluded")

@property
def cache_plugins_dir(self) -> Path:
return self.cache_latest_dir / "plugins"

@property
def custom_plugins_dir(self) -> Path:
return self.config_dir / "plugins"

@property
def installed_plugins_dir(self) -> Path:
return self.bundled_dir / "plugins"
Expand Down

0 comments on commit 0f75fd4

Please sign in to comment.