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
diff --git a/git_cl.py b/git_cl.py
index 8eda0dc..5b5872b 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -3534,6 +3534,7 @@
def CMDformat(parser, args):
"""Runs auto-formatting tools (clang-format etc.) on the diff."""
CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java']
+ GN_EXTS = ['.gn', '.gni']
parser.add_option('--full', action='store_true',
help='Reformat the full content of all touched files')
parser.add_option('--dry-run', action='store_true',
@@ -3574,6 +3575,7 @@
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'])]
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(
RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n'))
@@ -3655,6 +3657,16 @@
'found in this checkout. Files in other languages are still ' +
'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