Run Kotlin formatter in fmt-diff.py
Change-Id: I7faa8a58c227263bd6bd0469f293190967c4624e
diff --git a/tools/fmt-diff.py b/tools/fmt-diff.py
index 3a63fcf..963eaa7 100755
--- a/tools/fmt-diff.py
+++ b/tools/fmt-diff.py
@@ -17,13 +17,23 @@
'google-java-format-1.24.0', 'scripts',
'google-java-format-diff.py')
+GOOGLE_KOTLIN_FORMAT_DIFF = os.path.join(
+ utils.THIRD_PARTY, 'google', 'google-kotlin-format', '0.54',
+ 'ktfmt-0.54-jar-with-dependencies.jar')
+
GOOGLE_YAPF = os.path.join(utils.THIRD_PARTY, 'google/yapf/20231013')
+JDK21_JAVA = os.path.join(utils.THIRD_PARTY, 'openjdk/jdk-21/linux/bin/java')
+
def ParseOptions():
result = argparse.ArgumentParser()
result.add_argument('--no-java',
- help='Run google-java-format.',
+ help='Do not run google-java-format.',
+ action='store_true',
+ default=False)
+ result.add_argument('--no-kotlin',
+ help='Do not run google-kotlin-format.',
action='store_true',
default=False)
result.add_argument('--python',
@@ -41,6 +51,19 @@
fmt_process.communicate()
+def FormatKotlin(upstream):
+ changed_files_cmd = ['git', 'diff', '--name-only', upstream, '*.kt']
+ changed_files = subprocess.check_output(changed_files_cmd).decode(
+ 'utf-8').splitlines()
+ if not changed_files:
+ return
+ format_cmd = [
+ JDK21_JAVA, '-jar', GOOGLE_KOTLIN_FORMAT_DIFF, '--google-style'
+ ]
+ format_cmd.extend(changed_files)
+ subprocess.check_call(format_cmd)
+
+
def FormatPython(upstream):
changed_files_cmd = ['git', 'diff', '--name-only', upstream, '*.py']
changed_files = subprocess.check_output(changed_files_cmd).decode(
@@ -63,6 +86,8 @@
'upstream']).decode('utf-8').strip()
if not options.no_java:
FormatJava(upstream)
+ if not options.no_kotlin:
+ FormatKotlin(upstream)
if options.python:
FormatPython(upstream)