Kaynağa Gözat

[depot_tools] add google_java_format.py

google_java_format.py is a simple wrapper script that finds and
executes the google-java-format binary from Chromium tree.

This CL moves the corresponding function from git_cl.py so that
the logic can be executed without git_cl.py. This is the same strcture
used in git_cl.py for running other language formatters.
- clang_format.py
- swift_format.py
- rustfmt.py
- gn.py

With this patch, google-java-format can be used to run to formatter
a java file in a chromium tree that is located in a non git checkout.

Change-Id: I5f1b845040b28c0a8f15dd2e7c48c83ce2d9df97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5525851
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Scott Lee <ddoman@chromium.org>
Scott Lee 1 yıl önce
ebeveyn
işleme
a76c50e296
4 değiştirilmiş dosya ile 83 ekleme ve 21 silme
  1. 4 21
      git_cl.py
  2. 8 0
      google-java-format
  3. 12 0
      google-java-format.bat
  4. 59 0
      google_java_format.py

+ 4 - 21
git_cl.py

@@ -46,6 +46,7 @@ import gerrit_util
 import git_common
 import git_footers
 import git_new_branch
+import google_java_format
 import metrics
 import metrics_utils
 import owners_client
@@ -6142,34 +6143,16 @@ def _RunClangFormatDiff(opts, clang_diff_files, top_dir, upstream_commit):
     return return_value
 
 
-def _FindGoogleJavaFormat():
-    # Allow non-chromium projects to use a custom location.
-    primary_solution_path = gclient_paths.GetPrimarySolutionPath()
-    if primary_solution_path:
-        override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH')
-        if override:
-            # Make relative to solution root if not an absolute path.
-            return os.path.join(primary_solution_path, override)
-
-        path = os.path.join(primary_solution_path, 'third_party',
-                            'google-java-format', 'google-java-format')
-        # Check that the .jar exists, since it is conditionally downloaded via
-        # DEPS conditions.
-        if os.path.exists(path) and os.path.exists(path + '.jar'):
-            return path
-    return None
-
-
 def _RunGoogleJavaFormat(opts, paths, top_dir, upstream_commit):
     """Runs google-java-format and sets a return value if necessary."""
-    google_java_format = _FindGoogleJavaFormat()
-    if google_java_format is None:
+    tool = google_java_format.FindGoogleJavaFormat()
+    if tool is None:
         # Fail silently. It could be we are on an old chromium revision, or that
         # it is a non-chromium project. https://crbug.com/1491627
         print('google-java-format not found, skipping java formatting.')
         return 0
 
-    base_cmd = [google_java_format, '--aosp']
+    base_cmd = [tool, '--aosp']
     if not opts.diff:
         if opts.dry_run:
             base_cmd += ['--dry-run']

+ 8 - 0
google-java-format

@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+base_dir=$(dirname "$0")
+
+PYTHONDONTWRITEBYTECODE=1 exec python3 "$base_dir/google_java_format.py" "$@"

+ 12 - 0
google-java-format.bat

@@ -0,0 +1,12 @@
+@echo off
+:: Copyright 2014 The Chromium Authors. All rights reserved.
+:: Use of this source code is governed by a BSD-style license that can be
+:: found in the LICENSE file.
+setlocal
+
+:: Ensure that "depot_tools" is somewhere in PATH so this tool can be used
+:: standalone, but allow other PATH manipulations to take priority.
+set PATH=%PATH%;%~dp0
+
+:: Defer control.
+python3 "%~dp0\google_java_format.py" %*

+ 59 - 0
google_java_format.py

@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+"""Redirects to the version of google-java-format checked into the Chrome tree.
+
+google-java-format executable is pulled down from the cipd storage whenever
+you sync Chrome. This script finds and runs the executable.
+"""
+
+import gclient_paths
+import os
+import subprocess
+import sys
+
+
+def FindGoogleJavaFormat():
+    """Returns the path to the google-java-format executable."""
+    # Allow non-chromium projects to use a custom location.
+    primary_solution_path = gclient_paths.GetPrimarySolutionPath()
+    if primary_solution_path:
+        override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH')
+        if override:
+            # Make relative to solution root if not an absolute path.
+            return os.path.join(primary_solution_path, override)
+
+        path = os.path.join(primary_solution_path, 'third_party',
+                            'google-java-format', 'google-java-format')
+        # Check that the .jar exists, since it is conditionally downloaded via
+        # DEPS conditions.
+        if os.path.exists(path) and os.path.exists(path + '.jar'):
+            return path
+    return None
+
+
+def main(args):
+    google_java_format = FindGoogleJavaFormat()
+    if google_java_format is None:
+        # Fail silently. It could be we are on an old chromium revision,
+        # or that it is a non-chromium project. https://crbug.com/1491627.
+        print('google-java-format not found, skipping java formatting.')
+        return 0
+
+    # Add some visibility to --help showing where the tool lives, since this
+    # redirection can be a little opaque.
+    help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
+    if any(match in args for match in help_syntax):
+        print('\nDepot tools redirects you to the google-java-format at:\n' +
+              '    %s\n' % google_java_format)
+
+    return subprocess.call([google_java_format] + args)
+
+
+if __name__ == '__main__':
+    try:
+        sys.exit(main(sys.argv[1:]))
+    except KeyboardInterrupt:
+        sys.stderr.write('interrupted\n')
+        sys.exit(1)