vscode-extension-update: remove unused script and fix some ruff warning (#426267)
This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
#! /usr/bin/env nix-shell
|
||||
#! nix-shell -i python3 -p nix python3 python3Packages.loguru nix-search-tv vscode-extension-update gitMinimal
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class VSCodeExtensionBatchUpdater:
|
||||
# Extensions to be skipped
|
||||
_excluded_extensions = [
|
||||
# wrong upstream constraint: 0.10.x
|
||||
"vscode-extensions.ms-vscode.theme-tomorrowkit",
|
||||
"vscode-extensions.richie5um2.snake-trail",
|
||||
# not supported
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-cs",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-de",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-es",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-fr",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-it",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-ja",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-ko",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-pl",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-pt-br",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-qps-ploc",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-ru",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-tr",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-zh-hans",
|
||||
"vscode-extensions.ms-ceintl.vscode-language-pack-zh-hant",
|
||||
]
|
||||
|
||||
# Unable to determine the correct file location
|
||||
_extension_file_map = {
|
||||
"vscode-extensions.hashicorp.terraform": "pkgs/applications/editors/vscode/extensions/hashicorp.terraform/default.nix",
|
||||
"vscode-extensions.betterthantomorrow.calva": "pkgs/applications/editors/vscode/extensions/betterthantomorrow.calva/default.nix",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.parser = argparse.ArgumentParser(
|
||||
description="Batch update VSCode extensions"
|
||||
)
|
||||
|
||||
def execute_command(
|
||||
self, command, env: dict[str, str] = None, shell: bool = False
|
||||
) -> str:
|
||||
logger.debug("Executing command: {} (shell={})", command, shell)
|
||||
return subprocess.run(
|
||||
command,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
shell=shell,
|
||||
).stdout.strip()
|
||||
|
||||
def _get_extension_list(self) -> list[str]:
|
||||
# Get extension list from nix-search-tv output
|
||||
command = "nix-search-tv print | grep '^nixpkgs/ vscode-extensions\\.' | cut -d' ' -f2-"
|
||||
output = self.execute_command(command, shell=True)
|
||||
extension_list = output.splitlines()
|
||||
logger.info("Found {} extensions: {}", len(extension_list), extension_list)
|
||||
return extension_list
|
||||
|
||||
def _has_update_script(self, extension: str) -> bool:
|
||||
try:
|
||||
result = self._get_nix_attribute(f"{extension}.updateScript")
|
||||
return "not found" not in result
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
def _get_nix_attribute(self, attribute: str) -> str:
|
||||
return self.execute_command(["nix", "eval", "--raw", "-f", ".", attribute])
|
||||
|
||||
def _get_extension_filename(self, extension: str) -> str | None:
|
||||
return self._extension_file_map.get(extension)
|
||||
|
||||
def _update_extension(self, extension: str) -> None:
|
||||
logger.info("Updating extension: {}", extension)
|
||||
if extension in self._excluded_extensions:
|
||||
return
|
||||
try:
|
||||
if self._has_update_script(extension):
|
||||
return
|
||||
update_command = ["vscode-extension-update", extension, "--commit"]
|
||||
filename = self._get_extension_filename(extension)
|
||||
if filename:
|
||||
update_command.extend(["--override-filename", filename])
|
||||
self.execute_command(update_command)
|
||||
logger.info("Updated extension: {}", extension)
|
||||
except subprocess.CalledProcessError:
|
||||
logger.error("Failed to update extension: {}", extension)
|
||||
self.execute_command(["git", "restore", "."])
|
||||
|
||||
def run(self) -> None:
|
||||
for extension in self._get_extension_list():
|
||||
self._update_extension(extension)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
updater = VSCodeExtensionBatchUpdater()
|
||||
updater.run()
|
||||
@@ -7,6 +7,8 @@ import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from loguru import logger
|
||||
|
||||
@@ -69,7 +71,7 @@ class VSCodeExtensionUpdater:
|
||||
logger.info(f"Extension Current Version: {self.current_version}")
|
||||
|
||||
def execute_command(
|
||||
self, commandline: list[str], env: dict[str, str] = None
|
||||
self, commandline: list[str], env: Optional[dict[str, str]] = None
|
||||
) -> str:
|
||||
"""
|
||||
Executes a shell command and returns its output.
|
||||
@@ -116,33 +118,27 @@ class VSCodeExtensionUpdater:
|
||||
return "targetPlatform=" in source_url
|
||||
|
||||
def _get_nix_vscode_extension_src_hash(self, system: str) -> str:
|
||||
url = self.execute_command(
|
||||
[
|
||||
"nix",
|
||||
"eval",
|
||||
"--raw",
|
||||
"-f",
|
||||
".",
|
||||
f"{self.attribute_path}.src.url",
|
||||
"--system",
|
||||
system,
|
||||
]
|
||||
)
|
||||
if "warning" not in url:
|
||||
sha256 = self.execute_command(["nix-prefetch-url", url])
|
||||
hash = self.execute_command(
|
||||
[
|
||||
"nix",
|
||||
"hash",
|
||||
"convert",
|
||||
"--to",
|
||||
"sri",
|
||||
"--hash-algo",
|
||||
"sha256",
|
||||
sha256,
|
||||
]
|
||||
)
|
||||
return hash
|
||||
url = self.execute_command([
|
||||
"nix",
|
||||
"eval",
|
||||
"--raw",
|
||||
"-f",
|
||||
".",
|
||||
f"{self.attribute_path}.src.url",
|
||||
"--system",
|
||||
system,
|
||||
])
|
||||
sha256 = self.execute_command(["nix-prefetch-url", url])
|
||||
return self.execute_command([
|
||||
"nix",
|
||||
"hash",
|
||||
"convert",
|
||||
"--to",
|
||||
"sri",
|
||||
"--hash-algo",
|
||||
"sha256",
|
||||
sha256,
|
||||
])
|
||||
|
||||
def get_target_platform(self, nix_system: str) -> str:
|
||||
"""
|
||||
@@ -185,16 +181,14 @@ class VSCodeExtensionUpdater:
|
||||
"""
|
||||
try:
|
||||
return json.loads(
|
||||
self.execute_command(
|
||||
[
|
||||
"nix",
|
||||
"eval",
|
||||
"--json",
|
||||
"-f",
|
||||
".",
|
||||
f"{self.attribute_path}.meta.platforms",
|
||||
]
|
||||
)
|
||||
self.execute_command([
|
||||
"nix",
|
||||
"eval",
|
||||
"--json",
|
||||
"-f",
|
||||
".",
|
||||
f"{self.attribute_path}.meta.platforms",
|
||||
])
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
return []
|
||||
@@ -261,14 +255,12 @@ class VSCodeExtensionUpdater:
|
||||
engine_version_constraint
|
||||
)
|
||||
try:
|
||||
self.execute_command(
|
||||
[
|
||||
"semver",
|
||||
self.target_vscode_version,
|
||||
"-r",
|
||||
engine_version_constraint,
|
||||
]
|
||||
)
|
||||
self.execute_command([
|
||||
"semver",
|
||||
self.target_vscode_version,
|
||||
"-r",
|
||||
engine_version_constraint,
|
||||
])
|
||||
logger.info(f"Compatible version found: {candidate_version}")
|
||||
return candidate_version
|
||||
except (ValueError, subprocess.CalledProcessError):
|
||||
@@ -277,13 +269,11 @@ class VSCodeExtensionUpdater:
|
||||
)
|
||||
continue
|
||||
return candidate_version
|
||||
else:
|
||||
logger.error("Error: not found compatible version.")
|
||||
sys.exit(1)
|
||||
logger.error("Error: not found compatible version.")
|
||||
sys.exit(1)
|
||||
|
||||
def replace_version_symbol(self, version: str) -> str:
|
||||
version = re.sub(r"^\^", ">=", version)
|
||||
return version
|
||||
return re.sub(r"^\^", ">=", version)
|
||||
|
||||
def update_version_for_default_nix(self, content: str, new_version: str):
|
||||
target_name = self.attribute_path.removeprefix("vscode-extensions.")
|
||||
@@ -326,12 +316,7 @@ class VSCodeExtensionUpdater:
|
||||
updated_content = (
|
||||
content[:brace_start] + new_block_text + content[block_end + 1 :]
|
||||
)
|
||||
with open(
|
||||
self.override_filename,
|
||||
"w",
|
||||
encoding="utf-8",
|
||||
) as f:
|
||||
f.write(updated_content)
|
||||
Path(self.override_filename).write_text(updated_content, encoding="utf-8")
|
||||
|
||||
def run_nix_update(self, new_version: str, system: str) -> None:
|
||||
"""
|
||||
@@ -353,9 +338,9 @@ class VSCodeExtensionUpdater:
|
||||
if (
|
||||
"pkgs/applications/editors/vscode/extensions/vscode-utils.nix"
|
||||
in self.override_filename
|
||||
) and os.path.exists(
|
||||
) and Path(
|
||||
"pkgs/applications/editors/vscode/extensions/default.nix"
|
||||
):
|
||||
).exists():
|
||||
self.override_filename = (
|
||||
"pkgs/applications/editors/vscode/extensions/default.nix"
|
||||
)
|
||||
@@ -365,22 +350,17 @@ class VSCodeExtensionUpdater:
|
||||
in self.override_filename
|
||||
):
|
||||
with logger.catch(exception=(IOError, ValueError)):
|
||||
with open(
|
||||
self.override_filename,
|
||||
"r",
|
||||
encoding="utf-8",
|
||||
) as f:
|
||||
content = f.read()
|
||||
content = Path(self.override_filename).read_text(encoding="utf-8")
|
||||
if content.count(self.current_version) > 1:
|
||||
self.update_version_for_default_nix(content, new_version)
|
||||
new_version = "skip"
|
||||
if system not in self.supported_nix_systems:
|
||||
hash = self._get_nix_vscode_extension_src_hash(system)
|
||||
src_hash = self._get_nix_vscode_extension_src_hash(system)
|
||||
update_command = [
|
||||
"update-source-version",
|
||||
self.attribute_path,
|
||||
self.new_version,
|
||||
hash,
|
||||
src_hash,
|
||||
f"--system={system}",
|
||||
"--ignore-same-version",
|
||||
"--ignore-same-hash",
|
||||
@@ -412,9 +392,12 @@ class VSCodeExtensionUpdater:
|
||||
self.get_target_platform(self.nix_vscode_extension_platforms[0]),
|
||||
)
|
||||
try:
|
||||
self.execute_command(
|
||||
["semver", self.current_version, "-r", f"<{self.new_version}"]
|
||||
)
|
||||
self.execute_command([
|
||||
"semver",
|
||||
self.current_version,
|
||||
"-r",
|
||||
f"<{self.new_version}",
|
||||
])
|
||||
except subprocess.CalledProcessError:
|
||||
logger.info("Already up to date or new version is older!")
|
||||
sys.exit(0)
|
||||
@@ -423,14 +406,12 @@ class VSCodeExtensionUpdater:
|
||||
self.run_nix_update(version, system)
|
||||
if self.commit:
|
||||
self.execute_command(["git", "add", self.override_filename])
|
||||
self.execute_command(
|
||||
[
|
||||
"git",
|
||||
"commit",
|
||||
"-m",
|
||||
f"{self.attribute_path}: {self.current_version} -> {self.new_version}",
|
||||
]
|
||||
)
|
||||
self.execute_command([
|
||||
"git",
|
||||
"commit",
|
||||
"-m",
|
||||
f"{self.attribute_path}: {self.current_version} -> {self.new_version}",
|
||||
])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user