From 911e2a08932b1fcb959bdeadbad3db58fbc5f9ba Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 2 Feb 2024 20:12:11 +0000 Subject: [PATCH] Add `git cl format --mojom` for .mojom file formatting This is not enabled by default because it is new and should be considered beta. Bug: 780263 Change-Id: I7a4813c7ac4cef7441c5373dff4bb1289e7b99b6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5259981 Reviewed-by: Josip Sokcevic Commit-Queue: Robert Sesek --- git_cl.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/git_cl.py b/git_cl.py index 16b2d9410..0277a1053 100755 --- a/git_cl.py +++ b/git_cl.py @@ -6338,6 +6338,29 @@ def _RunGnFormat(opts, paths, top_dir, upstream_commit): return return_value +def _RunMojomFormat(opts, paths, top_dir, upstream_commit): + primary_solution_path = gclient_paths.GetPrimarySolutionPath() + if not primary_solution_path: + DieWithError('Could not find the primary solution path (e.g. ' + 'the chromium checkout)') + mojom_format_path = os.path.join(primary_solution_path, 'mojo', 'public', + 'tools', 'mojom', 'mojom_format.py') + if not os.path.exists(mojom_format_path): + DieWithError('Could not find mojom formater at ' + f'"{mojom_format_path}"') + + cmd = [mojom_format_path] + if opts.dry_run: + cmd.append('--dry-run') + cmd.extend(paths) + + ret = subprocess2.call(cmd) + if opts.dry_run and ret != 0: + return 2 + + return ret + + def _FormatXml(opts, paths, top_dir, upstream_commit): # Skip the metrics formatting from the global presubmit hook. These files # have a separate presubmit hook that issues an error if the files need @@ -6454,6 +6477,10 @@ def CMDformat(parser, args): action='store_false', help='Disables formatting of Swift file types using swift-format.') + parser.add_option('--mojom', + action='store_true', + help='Enables formatting of .mojom files.') + parser.add_option('--no-java', action='store_true', help='Disable auto-formatting of .java') @@ -6510,6 +6537,8 @@ def CMDformat(parser, args): formatters += [(['.swift'], _RunSwiftFormat)] if opts.python is not False: formatters += [(['.py'], _RunYapf)] + if opts.mojom: + formatters += [(['.mojom'], _RunMojomFormat)] top_dir = settings.GetRoot() return_value = 0