Skip to content

Commit

Permalink
format: allow input from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 authored and dcbaker committed Dec 6, 2024
1 parent d8ea5c4 commit 38051a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/markdown/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ When `--recursive` option is specified, `meson.build` files from
`subdir` are also analyzed (must be used in conjunction with `--inplace`
or `--check-only` option).

*Since 1.7.0* You can use `-` as source file name to read source from standard
input instead of reading it from a file. This cannot be used with `--recursive`
or `--inline` arguments.


#### Differences with `muon fmt`

Expand Down
4 changes: 4 additions & 0 deletions docs/markdown/snippets/format_from_stdin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## format command now accept stdin argument

You can now use `-` argument for `meson format` to read input from stdin
instead of reading it from a file.
15 changes: 13 additions & 2 deletions mesonbuild/mformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from copy import deepcopy
from dataclasses import dataclass, field, fields, asdict
from pathlib import Path
import sys

from . import mparser
from .mesonlib import MesonException
Expand Down Expand Up @@ -966,6 +967,12 @@ def run(options: argparse.Namespace) -> int:
if options.recursive and not (options.inplace or options.check_only):
raise MesonException('--recursive argument requires either --inplace or --check-only option')

from_stdin = len(options.sources) == 1 and options.sources[0].name == '-' and options.sources[0].parent == Path()
if options.recursive and from_stdin:
raise MesonException('--recursive argument is not compatible with stdin input')
if options.inplace and from_stdin:
raise MesonException('--inplace argument is not compatible with stdin input')

sources: T.List[Path] = options.sources.copy() or [Path(build_filename)]
if not options.configuration:
default_config_path = sources[0].parent / 'meson.format'
Expand All @@ -979,7 +986,11 @@ def run(options: argparse.Namespace) -> int:
src_file = src_file / build_filename

try:
code = src_file.read_text(encoding='utf-8')
if from_stdin:
src_file = Path('STDIN') # used for error messages and introspection
code = sys.stdin.read()
else:
code = src_file.read_text(encoding='utf-8')
except IOError as e:
raise MesonException(f'Unable to read from {src_file}') from e

Expand All @@ -1002,7 +1013,7 @@ def run(options: argparse.Namespace) -> int:
with options.output.open('w', encoding='utf-8', newline=formatter.current_config.newline) as of:
of.write(formatted)
except IOError as e:
raise MesonException(f'Unable to write to {src_file}') from e
raise MesonException(f'Unable to write to {options.output}') from e
else:
print(formatted, end='')

Expand Down

0 comments on commit 38051a5

Please sign in to comment.