Files
nx_post_support/server/noxfile.py
T
Christoph Brandau 53ebc5d055 chore(lsprotocol): migrate to 2025.0.0 and cleanup artifacts
The changes align the project with the 2025.0.0 lsprotocol
release, removing the old backport and updating type hints
in the protocol hooks to use Sequence where appropriate. The
dist-info and packaging metadata for older lsprotocol
versions are replaced with the new 2025.0.0 artifacts.

- Remove exceptiongroup backport used on Python <3.11
- Use Sequence instead of List in LS protocol hooks
- Replace old dist-info with 2025.0.0 metadata
2026-09-03 08:39:12 +02:00

194 lines
5.6 KiB
Python

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""All the action we need during build"""
import json
import os
import pathlib
import sys
import tomllib
import urllib.request as url_lib
import nox # pylint: disable=import-error
# Use uv to create session environments. The uv-managed standalone Python builds
# don't ship pythonw.exe, which makes the default virtualenv backend fail on Windows.
nox.options.default_venv_backend = "uv"
def _read_dependencies() -> list[str]:
"""Read project dependencies from pyproject.toml."""
pyproject = tomllib.loads(pathlib.Path("pyproject.toml").read_text())
return list(pyproject.get("project", {}).get("dependencies", []))
def _install_bundle(session: nox.Session) -> None:
deps = _read_dependencies()
session.run(
"uv",
"pip",
"install",
"--target",
"./libs",
"--no-cache-dir",
"--upgrade",
"--overrides",
"./uv-overrides.txt",
*deps,
external=True,
)
def _check_files(names: list[str]) -> None:
root_dir = pathlib.Path(__file__).parent
for name in names:
file_path = root_dir / name
lines: list[str] = file_path.read_text().splitlines()
if any(line for line in lines if line.startswith("# TODO:")):
raise Exception(f"Please update {os.fspath(file_path)}.")
def _get_package_data(package):
json_uri = f"https://registry.npmjs.org/{package}"
with url_lib.urlopen(json_uri) as response:
return json.loads(response.read())
def _update_npm_packages(session: nox.Session) -> None:
pinned = {
"vscode-languageclient",
"@types/vscode",
"@types/node",
}
package_json_path = pathlib.Path(__file__).parent / "package.json"
package_json = json.loads(package_json_path.read_text(encoding="utf-8"))
for package in package_json["dependencies"]:
if package not in pinned:
data = _get_package_data(package)
latest = "^" + data["dist-tags"]["latest"]
package_json["dependencies"][package] = latest
for package in package_json["devDependencies"]:
if package not in pinned:
data = _get_package_data(package)
latest = "^" + data["dist-tags"]["latest"]
package_json["devDependencies"][package] = latest
# Ensure engine matches the package
if package_json["engines"]["vscode"] != package_json["devDependencies"]["@types/vscode"]:
print("Please check VS Code engine version and @types/vscode version in package.json.")
new_package_json = json.dumps(package_json, indent=4)
# JSON dumps uses \n for line ending on all platforms by default
if not new_package_json.endswith("\n"):
new_package_json += "\n"
package_json_path.write_text(new_package_json, encoding="utf-8")
session.run("npm", "install", external=True)
def _setup_template_environment(session: nox.Session) -> None:
"""Install project dependencies into the bundled libs directory."""
_install_bundle(session)
@nox.session()
def setup(session: nox.Session) -> None:
"""Sets up the template for development."""
_setup_template_environment(session)
@nox.session()
def tests(session: nox.Session) -> None:
"""Runs all the tests for the extension."""
deps = _read_dependencies()
session.run(
"uv",
"pip",
"install",
"--python",
sys.executable,
"--overrides",
"./uv-overrides.txt",
*deps,
external=True,
)
session.run("uv", "pip", "install", "--python", sys.executable, "pytest", external=True)
session.run("pytest", "tests/python_tests")
@nox.session()
def lint(session: nox.Session) -> None:
"""Runs linter and formatter checks on python files."""
deps = _read_dependencies()
session.run(
"uv",
"pip",
"install",
"--python",
sys.executable,
"--overrides",
"./uv-overrides.txt",
*deps,
external=True,
)
session.run(
"uv",
"pip",
"install",
"--python",
sys.executable,
"pytest",
"pylint",
"black",
"isort",
external=True,
)
session.run("pylint", "-d", "W0511", "./bundled/tool")
session.run(
"pylint",
"-d",
"W0511",
"--ignore=./tests/python_tests/test_data",
"./tests/python_tests",
)
session.run("pylint", "-d", "W0511", "noxfile.py")
# check formatting using black
session.run("black", "--check", "./bundled/tool")
session.run("black", "--check", "./tests/python_tests")
session.run("black", "--check", "noxfile.py")
# check import sorting using isort
session.run("isort", "--check", "./bundled/tool")
session.run("isort", "--check", "./tests/python_tests")
session.run("isort", "--check", "noxfile.py")
# check typescript code
session.run("npm", "run", "lint", external=True)
@nox.session()
def build_package(session: nox.Session) -> None:
"""Builds VSIX package for publishing."""
_check_files(["README.md", "LICENSE", "SECURITY.md", "SUPPORT.md"])
_setup_template_environment(session)
session.run("npm", "install", external=True)
session.run("npm", "run", "vsce-package", external=True)
def _update_uv_lock(session: nox.Session) -> None:
session.run("uv", "lock", "--upgrade", external=True)
@nox.session()
def update_python_packages(session: nox.Session) -> None:
"""Update Python packages."""
_update_uv_lock(session)
@nox.session()
def update_npm_packages(session: nox.Session) -> None:
"""Update npm packages."""
_update_npm_packages(session)