-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnoxfile.py
More file actions
186 lines (148 loc) · 5.55 KB
/
noxfile.py
File metadata and controls
186 lines (148 loc) · 5.55 KB
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
"""All the action we need during build."""
import json
import urllib.request as url_lib
from pathlib import Path
import nox
nox.options.default_venv_backend = "uv"
nox.options.reuse_venv = True
USE_PYTHON_VERSION = Path(".python-version").read_text()
def _dependency_group(group: str) -> list[str]:
"""Returns dependencies from `pyproject.toml` for the specified dependency group."""
return nox.project.load_toml("pyproject.toml")["dependency-groups"][group]
def _get_python_versions(start_version: str, end_version: str) -> list[str]:
"""Generates a list of Python versions within a given minor version range."""
start_major, start_minor, *_ = map(int, start_version.split("."))
end_major, end_minor, *_ = map(int, end_version.split("."))
if start_major != end_major:
raise ValueError(
f"Major python versions do not match ({start_version}, {end_version})."
)
versions = []
for minor in range(start_minor, end_minor + 1):
version = f"{start_major}.{minor}"
if minor > end_minor:
break
versions.append(version)
return versions
def _install_language_server_libs(session: nox.Session) -> None:
tmp_dir = Path(session.create_tmp())
requirements = tmp_dir.joinpath("requirements.txt")
session.run(
"uv",
"export",
"--locked",
"--only-group",
"language-server",
"-o",
requirements,
silent=True,
)
session.install("pip")
session.run(
"pip",
"install",
"-t",
"./language_server/libs",
"--python-version",
USE_PYTHON_VERSION,
"--only-binary",
":all:",
"--implementation",
"py",
"--abi",
"none",
"--platform",
"any",
"--no-cache-dir",
"--no-deps",
"--no-compile",
"--upgrade",
"-r",
requirements,
)
def _update_py_packages(session: nox.Session) -> None:
session.run("uv", "lock", "--upgrade")
_install_language_server_libs(session)
def _update_nodejs_packages(session: nox.Session) -> None:
def get_package_data(package: str) -> dict:
json_uri = f"https://registry.npmjs.org/{package}"
with url_lib.urlopen(json_uri) as response:
return json.loads(response.read())
pinned = {
"vscode-languageclient",
"@types/vscode",
"@types/node",
}
package_json_path = 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"] # noqa
):
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)
@nox.session(python=USE_PYTHON_VERSION)
def lint(session: nox.Session) -> None:
"""Runs linter and formatter checks."""
session.install(*_dependency_group("linting"))
session.run("flake8")
session.run("black", "--check", ".")
session.run("npm", "run", "lint", external=True)
@nox.session(python=_get_python_versions(USE_PYTHON_VERSION, "3.13"))
def tests(session: nox.Session) -> None:
"""Runs tests for the extension."""
session.install(*_dependency_group("testing"))
session.run("pytest")
@nox.session(python=USE_PYTHON_VERSION)
def update_packages(session: nox.Session) -> None:
"""Updates Python and/or Node.js packages.
Example:
To update both Python and Node.js packages:
nox -s update_packages
To update only Python packages:
nox -s update_packages -- only-python
To update only Node.js packages:
nox -s update_packages -- only-nodejs
"""
if "only-nodejs" not in session.posargs:
_update_py_packages(session)
if "only-python" not in session.posargs:
_update_nodejs_packages(session)
@nox.session(python=USE_PYTHON_VERSION)
def install_packages(session: nox.Session) -> None:
"""Installs required Python and/or Node.js packages.
Example:
To install both Python and Node.js packages:
nox -s install_packages
To install only Python packages:
nox -s install_packages -- only-python
To install only Node.js packages:
nox -s install_packages -- only-nodejs
"""
if "only-nodejs" not in session.posargs:
_install_language_server_libs(session)
if "only-python" not in session.posargs:
session.run("npm", "install", external=True)
@nox.session(python=USE_PYTHON_VERSION)
def build_package(session: nox.Session) -> None:
"""Builds VSIX package for publishing."""
session.run("npm", "run", "vsce-package", external=True)