Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # |
| 4 | # Copyright 2017, 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 | |
| 19 | """Script that is used by developers to run style checks on Kotlin files.""" |
| 20 | |
Luca Stefani | 67e7d24 | 2020-02-09 13:28:26 +0100 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 23 | import argparse |
| 24 | import errno |
| 25 | import os |
| 26 | import subprocess |
| 27 | import sys |
| 28 | |
| 29 | MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__)) |
Matvei Malkov | 8d1c18d | 2019-02-26 15:08:19 +0000 | [diff] [blame] | 30 | KTLINT_JAR = os.path.join(MAIN_DIRECTORY, 'ktlint-android-all.jar') |
Aurimas Liutikas | 68e5195 | 2018-04-04 14:55:27 -0700 | [diff] [blame] | 31 | FORMAT_MESSAGE = ''' |
| 32 | ********************************************************************** |
| 33 | To format run: |
| 34 | {}/ktlint.py --format --file {} |
| 35 | ********************************************************************** |
| 36 | ''' |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def main(args=None): |
| 40 | parser = argparse.ArgumentParser() |
Aurimas Liutikas | a610028 | 2017-12-12 09:47:00 -0800 | [diff] [blame] | 41 | parser.add_argument('--file', '-f', nargs='*') |
Tyson Henning | e4158b3 | 2018-01-19 16:15:55 -0800 | [diff] [blame] | 42 | parser.add_argument('--format', '-F', dest='format', action='store_true') |
| 43 | parser.add_argument('--noformat', dest='format', action='store_false') |
| 44 | parser.set_defaults(format=False) |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 45 | args = parser.parse_args() |
Aurimas Liutikas | 68e5195 | 2018-04-04 14:55:27 -0700 | [diff] [blame] | 46 | kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')] |
| 47 | ktlint_args = kt_files[:] |
Tyson Henning | e4158b3 | 2018-01-19 16:15:55 -0800 | [diff] [blame] | 48 | if args.format: |
| 49 | ktlint_args += ['-F'] |
| 50 | if not ktlint_args: |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 51 | sys.exit(0) |
| 52 | |
Aurimas Liutikas | 91d5c55 | 2018-05-30 13:55:44 -0700 | [diff] [blame] | 53 | ktlint_args += ['--android'] |
| 54 | |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 55 | ktlint_env = os.environ.copy() |
| 56 | ktlint_env['JAVA_CMD'] = 'java' |
| 57 | try: |
Tyson Henning | e4158b3 | 2018-01-19 16:15:55 -0800 | [diff] [blame] | 58 | check = subprocess.Popen(['java', '-jar', KTLINT_JAR] + ktlint_args, |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 59 | stdout=subprocess.PIPE, env=ktlint_env) |
| 60 | stdout, _ = check.communicate() |
| 61 | if stdout: |
Luca Stefani | 67e7d24 | 2020-02-09 13:28:26 +0100 | [diff] [blame] | 62 | print('prebuilts/ktlint found errors in files you changed:') |
| 63 | print(stdout) |
| 64 | print(FORMAT_MESSAGE.format(MAIN_DIRECTORY, ' '.join(kt_files))) |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 65 | sys.exit(1) |
| 66 | else: |
| 67 | sys.exit(0) |
| 68 | except OSError as e: |
| 69 | if e.errno == errno.ENOENT: |
Luca Stefani | 67e7d24 | 2020-02-09 13:28:26 +0100 | [diff] [blame] | 70 | print('Error running ktlint!') |
Aurimas Liutikas | 30ef8f6 | 2017-11-16 17:42:50 -0800 | [diff] [blame] | 71 | sys.exit(1) |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | main() |