Make "git cl format" format GN files.

Always formats full GN files that are modified since the format should
be canonical.

I tried to make it consistent in style with the existing formatters. I've
tested running it locally. For "git cl format" it formats GN files. For
"git cl format --diff" it outputs the full formatted files like clang but
doesn't modify. For "git cl format --dry-run" it doesn't modify or output.

TEST=Works running locally.
BUG=

Review URL: https://codereview.chromium.org/1666403002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@298617 0039d316-1c4b-4281-b951-d872f2087c98
changes/01/332501/1
kylechar@chromium.org 10 years ago
parent edf62ce445
commit 8b61f11e22

@ -3534,6 +3534,7 @@ def MatchingFileType(file_name, extensions):
def CMDformat(parser, args): def CMDformat(parser, args):
"""Runs auto-formatting tools (clang-format etc.) on the diff.""" """Runs auto-formatting tools (clang-format etc.) on the diff."""
CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java'] CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java']
GN_EXTS = ['.gn', '.gni']
parser.add_option('--full', action='store_true', parser.add_option('--full', action='store_true',
help='Reformat the full content of all touched files') help='Reformat the full content of all touched files')
parser.add_option('--dry-run', action='store_true', parser.add_option('--dry-run', action='store_true',
@ -3574,6 +3575,7 @@ def CMDformat(parser, args):
clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)] clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)]
python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])] python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])]
dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])] dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])]
gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)]
top_dir = os.path.normpath( top_dir = os.path.normpath(
RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n')) RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n'))
@ -3655,6 +3657,16 @@ def CMDformat(parser, args):
'found in this checkout. Files in other languages are still ' + 'found in this checkout. Files in other languages are still ' +
'formatted.') 'formatted.')
# Format GN build files. Always run on full build files for canonical form.
if gn_diff_files:
cmd = ['gn', 'format']
if not opts.dry_run and not opts.diff:
cmd.append('--in-place')
for gn_diff_file in gn_diff_files:
stdout = RunCommand(cmd + [gn_diff_file], cwd=top_dir)
if opts.diff:
sys.stdout.write(stdout)
return return_value return return_value

Loading…
Cancel
Save