Skip to content

Commit ed2fc63

Browse files
authored
Merge pull request #152 from robotpy/manual-init
Provide 'version' argument for init command
2 parents 02e5eb2 + 818d712 commit ed2fc63

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

robotpy_installer/cli_init.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import argparse
2-
import inspect
32
import logging
43
import pathlib
4+
import typing as T
55

6+
from packaging.version import Version
7+
8+
from . import installer
69
from . import pyproject
10+
from .errors import Error
711
from .utils import handle_cli_error
812

913

@@ -16,10 +20,29 @@ class Init:
1620
"""
1721

1822
def __init__(self, parser: argparse.ArgumentParser):
19-
pass
23+
parser.add_argument(
24+
"version",
25+
nargs="?",
26+
help="Manually specify the RobotPy package version to use instead of autodetecting it",
27+
type=Version,
28+
)
2029

2130
@handle_cli_error
22-
def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
31+
def run(
32+
self,
33+
main_file: pathlib.Path,
34+
project_path: pathlib.Path,
35+
version: T.Optional[Version],
36+
):
37+
38+
supported_year = int(installer._WPILIB_YEAR)
39+
if version is not None and version.major != int(installer._WPILIB_YEAR):
40+
msg = (
41+
f"Only RobotPy {supported_year}.x is supported by this version "
42+
f"of robotpy-installer (specified {version})"
43+
)
44+
raise Error(msg)
45+
2346
project_path.mkdir(parents=True, exist_ok=True)
2447

2548
# Create robot.py if it doesn't already exist
@@ -33,7 +56,9 @@ def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
3356
# Create pyproject.toml if it doesn't already exist
3457
pyproject_path = pyproject.toml_path(project_path)
3558
if not pyproject_path.exists():
36-
pyproject.write_default_pyproject(project_path)
59+
pyproject.write_default_pyproject(
60+
project_path, str(version) if version is not None else None
61+
)
3762

3863
logger.info("Created %s", pyproject_path)
3964

robotpy_installer/pyproject.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,28 @@ def write_default_gitignore(project_path: pathlib.Path):
373373

374374
def write_default_pyproject(
375375
project_path: pathlib.Path,
376+
robotpy_version: typing.Optional[str] = None,
376377
):
377378
"""
378379
Using the current environment, write a minimal pyproject.toml
379380
380381
:param project_path: Path to robot project
381382
"""
382383

383-
robotpy_version = robotpy_installed_version()
384+
if robotpy_version is None:
385+
robotpy_version = robotpy_installed_version()
386+
387+
try:
388+
provides_extra = metadata("robotpy").get_all("Provides-Extra")
389+
except PackageNotFoundError:
390+
provides_extra = []
384391

385-
provides_extra = metadata("robotpy").get_all("Provides-Extra")
386392
if not provides_extra:
387393
extras = ""
388394
else:
389-
extras = "\n # ".join(f'"{extra}",' for extra in sorted(provides_extra))
395+
extras = " #" + "\n # ".join(
396+
f'"{extra}",' for extra in sorted(provides_extra)
397+
)
390398

391399
content = inspect.cleandoc(
392400
f"""
@@ -404,7 +412,7 @@ def write_default_pyproject(
404412
# Which optional robotpy components should be installed?
405413
# -> equivalent to `pip install robotpy[extra1, ...]
406414
components = [
407-
# ##EXTRAS##
415+
##EXTRAS##
408416
]
409417
410418
# Other pip packages to install, such as vendor packages (each element

0 commit comments

Comments
 (0)