Skip to content

Commit

Permalink
cli: Specify variant and optionally port with port/board/variant.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleech committed Oct 31, 2024
1 parent f5c10f0 commit 3832763
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/mpbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def build_board(
build_container_override: Optional[str] = None,
idf: Optional[str] = IDF_DEFAULT,
mpy_dir: str|Path|None = None,
port: str|None = None,
) -> None:
# mpy_dir = mpy_dir or Path.cwd()
# mpy_dir = Path(mpy_dir)
Expand All @@ -46,7 +47,7 @@ def build_board(
raise SystemExit()

_board = db.boards[board]
port = _board.port.name
port = port or _board.port.name

if variant and variant not in [v.name for v in _board.variants]:
print("Invalid variant")
Expand Down Expand Up @@ -144,12 +145,14 @@ def clean_board(
board: str,
variant: Optional[str] = None,
idf: Optional[str] = IDF_DEFAULT,
mpy_dir: Optional[str] = None,
mpy_dir: str|Path|None = None,
port: str|None = None,
) -> None:
build_board(
board=board,
variant=variant,
mpy_dir=mpy_dir,
port=port,
idf=idf,
extra_args=["clean"],
)
38 changes: 29 additions & 9 deletions src/mpbuild/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .list_boards import print_boards
from .check_images import check_images
from .completions import list_ports, list_boards, list_variants_for_board
from .find_boards import parse_board_spec

app = typer.Typer(chain=True)

Expand All @@ -20,6 +21,21 @@ def _complete(words: list[str], incomplete: str):
return completion


def _complete_spec(incomplete: str):
parts = incomplete.split("/")
if len(parts) == 1:
if incomplete.lower() == incomplete:
return _complete_port(incomplete)
return _complete_board(incomplete)
elif len(parts) == 2:
if parts[0].lower() == parts[0]:
port = parts[0]
#todo
else:
board = parts[0]
#todo


def _complete_board(incomplete: str):
return _complete(list_boards(), incomplete)

Expand All @@ -35,13 +51,13 @@ def _complete_port(incomplete: str):

@app.command()
def build(
board: Annotated[
str, typer.Argument(help="Board name", autocompletion=_complete_board)
board_spec: Annotated[
str, typer.Argument(help="Board name, optionally with /variant", autocompletion=_complete_spec)
],
variant: Annotated[
Optional[str],
typer.Argument(help="Board variant", autocompletion=_complete_variant),
] = None,
#variant: Annotated[
# Optional[str],
# typer.Argument(help="Board variant", autocompletion=_complete_variant),
#] = None,
idf: Annotated[
Optional[str],
typer.Option(help="esp32 port only: select IDF version to build with"),
Expand All @@ -57,17 +73,21 @@ def build(
"""
Build a MicroPython board.
"""
build_board(board, variant, extra_args or [], build_container, idf)
port, board, variant = parse_board_spec(board_spec)
build_board(board, variant, extra_args or [], build_container, idf, port=port)


@app.command()
def clean(
board: str, variant: Annotated[Optional[str], typer.Argument()] = None
board_spec: Annotated[
str, typer.Argument(help="Board name, optionally with /variant")
],
) -> None:
"""
Clean a MicroPython board.
"""
clean_board(board, variant)
port, board, variant = parse_board_spec(board_spec)
clean_board(board, variant, port=port)


@app.command("list")
Expand Down
15 changes: 15 additions & 0 deletions src/mpbuild/find_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ def find_mpy_root(root: str| Path | None = None):
"Please run from MicroPython source tree or specify with env: MICROPY_DIR"
)
root = root.parent


def parse_board_spec(spec):
"""
board spec format is <port/>board</variant>
ie both port and variant are optional, with just the board as mandatory.
"""
port = None
specs = spec.split("/")
if specs[0].lower() == specs[0]:
port = specs.pop(0)
board = specs.pop(0)
variant = specs[0] if specs else ""
return port, board, variant

0 comments on commit 3832763

Please sign in to comment.