Commit 1ef2616e authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

[ios] Ensure bots can pretty print $root_build_dir/args.gn

The downstream iOS bots import a gn file from $root_build_dir/args.gn
that contains nested scopes. Support to parse the file has been added
to //build/gn_helpers.py with https://crrev.com/c/2513217.

The next step on the bot is pretty printing the data parsed, which
fails as nested scopes are converted to nested dictionaries. This
CL adds support to pretty print those.

Bug: 1144711
Change-Id: I3130d493c6385f2016d91245e8a5f12e710495cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517679
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823627}
parent 53f9bf7d
......@@ -93,7 +93,7 @@ def ToGNString(value, pretty=False):
elif isinstance(v, dict):
if level > 0:
raise GNError('Attempting to recursively print a dictionary.')
yield '{'
for key in sorted(v):
if not isinstance(key, basestring_compat):
raise GNError('Dictionary key is not a string.')
......@@ -101,14 +101,16 @@ def ToGNString(value, pretty=False):
raise GNError('Dictionary key is not a valid GN identifier.')
yield key # No quotations.
yield '='
for tok in GenerateTokens(value[key], level + 1):
for tok in GenerateTokens(v[key], level + 1):
yield tok
if level > 0:
yield '}'
else: # Not supporting float: Add only when needed.
raise GNError('Unsupported type when printing to GN.')
can_start = lambda tok: tok and tok not in ',]='
can_end = lambda tok: tok and tok not in ',[='
can_start = lambda tok: tok and tok not in ',}]='
can_end = lambda tok: tok and tok not in ',{[='
# Adds whitespaces, trying to keep everything (except dicts) in 1 line.
def PlainGlue(gen):
......@@ -134,12 +136,14 @@ def ToGNString(value, pretty=False):
yield '\n' + ' ' * level # New dict item.
elif tok == '=' or prev_tok in '=':
yield ' ' # Separator before and after '=', on same line.
if tok == ']':
if tok in ']}':
level -= 1
if int(prev_tok == '[') + int(tok == ']') == 1: # Exclude '[]' case.
# Exclude '[]' and '{}' cases.
if int(prev_tok == '[') + int(tok == ']') == 1 or \
int(prev_tok == '{') + int(tok == '}') == 1:
yield '\n' + ' ' * level
yield tok
if tok == '[':
if tok in '[{':
level += 1
if tok == ',':
yield '\n' + ' ' * level
......
......@@ -55,6 +55,18 @@ class UnitTest(unittest.TestCase):
[[[], [[]]], []],
'[ [ [ ], [ [ ] ] ], [ ] ]',
'[\n [\n [],\n [\n []\n ]\n ],\n []\n]\n',
),
(
[{
'a': 1,
'c': {
'z': 8
},
'b': []
}],
'[ { a = 1\nb = [ ]\nc = { z = 8 } } ]\n',
'[\n {\n a = 1\n b = []\n c = {\n' +
' z = 8\n }\n }\n]\n',
)
]
for obj, exp_ugly, exp_pretty in test_cases:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment