-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathcommands.py
More file actions
115 lines (100 loc) · 3.66 KB
/
commands.py
File metadata and controls
115 lines (100 loc) · 3.66 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
import sys
from pathlib import Path
from typing import List, Optional
import protobuf_parser
import rich
import typer
from rich.syntax import Syntax
from ... import __version__
from ..models import monkey_patch_oneof_index
from . import DEFAULT_LINE_LENGTH, VERBOSE, utils
from .runner import compile_protobufs
monkey_patch_oneof_index()
app = typer.Typer()
@app.callback(context_settings={"help_option_names": ["-h", "--help"]})
def callback(ctx: typer.Context) -> None:
"""The callback for all things betterproto"""
if ctx.invoked_subcommand is None:
rich.print(ctx.get_help())
@app.command()
def version(ctx: typer.Context) -> None:
rich.print("betterproto version:", __version__)
@app.command(context_settings={"help_option_names": ["-h", "--help"]})
@utils.run_sync
async def compile(
verbose: bool = typer.Option(
VERBOSE, "-v", "--verbose", help="Whether or not to be verbose"
),
line_length: int = typer.Option(
DEFAULT_LINE_LENGTH,
"-l",
"--line-length",
help="The line length to format with",
),
generate_services: bool = typer.Option(
True, help="Whether or not to generate servicer stubs"
),
output: Optional[Path] = typer.Option(
None,
help="The name of the output directory",
file_okay=False,
allow_dash=True,
),
paths: List[Path] = typer.Argument(
...,
help="The protobuf files to compile",
exists=True,
allow_dash=True,
readable=False,
),
) -> None:
"""The recommended way to compile your protobuf files."""
files = utils.get_files(paths)
if not files:
return rich.print("[bold]No files found to compile")
for output_path, protos in files.items():
output = output or (Path(output_path.parent.name) / output_path.name).resolve()
output.mkdir(exist_ok=True, parents=True)
results = await compile_protobufs(
*protos,
output=output,
verbose=verbose,
generate_services=generate_services,
line_length=line_length,
from_cli=True,
)
for result in results:
for error in result.errors:
if error.message.startswith("Syntax error"):
rich.print(
f"[red]File {str(result.file)}:\n",
Syntax.from_path(
str(result.file),
line_numbers=True,
line_range=(max(error.line - 5, 0), error.line),
),
f"{' ' * (error.column + 3)}^\nSyntaxError: {error.message}[red]",
file=sys.stderr,
)
elif isinstance(error, protobuf_parser.Warning):
rich.print(f"Warning: {error}", file=sys.stderr)
else:
failed_files = "\n".join(f" - {file}" for file in protos)
rich.print(
f"[red]Protoc failed to generate outputs for:\n\n"
f"{failed_files}\n\nSee the output for the issue:\n{error}[red]",
file=sys.stderr,
)
# has_warnings = all(isinstance(e, Warning) for e in errors)
# if not errors or has_warnings:
if True:
rich.print(
f"[bold green]Finished generating output for "
f"{len(protos)} file{'s' if len(protos) != 1 else ''}, "
f"output is in {output.as_posix()}"
)
# if errors:
# if not has_warnings:
# exit(2)
# exit(1)
exit(0)