Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions robotpy_installer/cli_init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import argparse
import inspect
import logging
import pathlib
import typing as T

from packaging.version import Version

from . import installer
from . import pyproject
from .errors import Error
from .utils import handle_cli_error


Expand All @@ -16,10 +20,29 @@ class Init:
"""

def __init__(self, parser: argparse.ArgumentParser):
pass
parser.add_argument(
"version",
nargs="?",
help="Manually specify the RobotPy package version to use instead of autodetecting it",
type=Version,
)

@handle_cli_error
def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
def run(
self,
main_file: pathlib.Path,
project_path: pathlib.Path,
version: T.Optional[Version],
):

supported_year = int(installer._WPILIB_YEAR)
if version is not None and version.major != int(installer._WPILIB_YEAR):
msg = (
f"Only RobotPy {supported_year}.x is supported by this version "
f"of robotpy-installer (specified {version})"
)
raise Error(msg)

project_path.mkdir(parents=True, exist_ok=True)

# Create robot.py if it doesn't already exist
Expand All @@ -33,7 +56,9 @@ def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
# Create pyproject.toml if it doesn't already exist
pyproject_path = pyproject.toml_path(project_path)
if not pyproject_path.exists():
pyproject.write_default_pyproject(project_path)
pyproject.write_default_pyproject(
project_path, str(version) if version is not None else None
)

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

Expand Down
16 changes: 12 additions & 4 deletions robotpy_installer/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,28 @@ def write_default_gitignore(project_path: pathlib.Path):

def write_default_pyproject(
project_path: pathlib.Path,
robotpy_version: typing.Optional[str] = None,
):
"""
Using the current environment, write a minimal pyproject.toml

:param project_path: Path to robot project
"""

robotpy_version = robotpy_installed_version()
if robotpy_version is None:
robotpy_version = robotpy_installed_version()

try:
provides_extra = metadata("robotpy").get_all("Provides-Extra")
except PackageNotFoundError:
provides_extra = []

provides_extra = metadata("robotpy").get_all("Provides-Extra")
if not provides_extra:
extras = ""
else:
extras = "\n # ".join(f'"{extra}",' for extra in sorted(provides_extra))
extras = " #" + "\n # ".join(
f'"{extra}",' for extra in sorted(provides_extra)
)

content = inspect.cleandoc(
f"""
Expand All @@ -404,7 +412,7 @@ def write_default_pyproject(
# Which optional robotpy components should be installed?
# -> equivalent to `pip install robotpy[extra1, ...]
components = [
# ##EXTRAS##
##EXTRAS##
]

# Other pip packages to install, such as vendor packages (each element
Expand Down