Jordan Demeulenaere | 75418db | 2022-08-29 17:31:50 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # |
| 4 | # Copyright 2022, The Android Open Source Project |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | """Script to generate an include file that can be passed to ktfmt.py. |
| 19 | |
| 20 | The include file is generated from one or more folders containing one or more |
| 21 | Kotlin files. The generated include file will exclude all Kotlin files that |
| 22 | currently don't follow ktfmt format. This way, we can easily start formatting |
| 23 | all new files in a project/directory. |
| 24 | """ |
| 25 | |
| 26 | import argparse |
| 27 | import os |
| 28 | import subprocess |
| 29 | import sys |
| 30 | |
| 31 | |
| 32 | def main(): |
| 33 | parser = argparse.ArgumentParser( |
| 34 | 'Generate an include file that can be passed to ktfmt.py') |
| 35 | parser.add_argument( |
| 36 | '--output', '-o', required=True, help='The output include file.') |
| 37 | parser.add_argument( |
| 38 | 'files', |
| 39 | nargs='*', |
| 40 | help='The files or directories that should be included.') |
| 41 | args = parser.parse_args() |
| 42 | |
| 43 | # Add a line preprended with '+' for included files/folders. |
| 44 | with open(args.output, 'w+') as out: |
| 45 | includes = args.files |
| 46 | includes.sort() |
| 47 | for include in includes: |
| 48 | out.write('+' + include + '\n') |
| 49 | |
| 50 | # Retrieve all Kotlin files. |
| 51 | kt_files = [] |
| 52 | for file in args.files: |
| 53 | if os.path.isdir(file): |
| 54 | for root, dirs, files in os.walk(file): |
| 55 | for f in files: |
| 56 | if is_kotlin_file(f): |
| 57 | kt_files += [os.path.join(root, f)] |
| 58 | |
| 59 | if is_kotlin_file(file): |
| 60 | kt_files += [file] |
| 61 | |
| 62 | # Check all files with ktfmt. |
| 63 | ktfmt_args = ['--kotlinlang-style', '--set-exit-if-changed', '--dry-run' |
| 64 | ] + kt_files |
| 65 | dir = os.path.dirname(__file__) |
| 66 | ktfmt_jar = os.path.normpath( |
| 67 | os.path.join(dir, |
| 68 | '../../prebuilts/build-tools/common/framework/ktfmt.jar')) |
| 69 | |
| 70 | ktlint_env = os.environ.copy() |
| 71 | ktlint_env['JAVA_CMD'] = 'java' |
| 72 | try: |
| 73 | process = subprocess.Popen( |
| 74 | ['java', '-jar', ktfmt_jar] + ktfmt_args, |
| 75 | stdout=subprocess.PIPE, |
| 76 | env=ktlint_env) |
| 77 | |
| 78 | # Add a line prepended with '-' for all files that are not correctly. |
| 79 | # formatted. |
| 80 | stdout, _ = process.communicate() |
| 81 | incorrect_files = stdout.decode('utf-8').splitlines() |
| 82 | incorrect_files.sort() |
| 83 | for file in incorrect_files: |
| 84 | out.write('-' + file + '\n') |
| 85 | except OSError as e: |
| 86 | print('Error running ktfmt') |
| 87 | sys.exit(1) |
| 88 | |
| 89 | |
| 90 | def is_kotlin_file(name): |
| 91 | return name.endswith('.kt') or name.endswith('.kts') |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | main() |