blob: 72adbab6f34cbf9c9988524f8082c878e0830105 [file] [log] [blame]
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -08001#!/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 Stefani67e7d242020-02-09 13:28:26 +010021from __future__ import print_function
22
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080023import argparse
24import errno
25import os
26import subprocess
27import sys
28
29MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__))
Matvei Malkov8d1c18d2019-02-26 15:08:19 +000030KTLINT_JAR = os.path.join(MAIN_DIRECTORY, 'ktlint-android-all.jar')
Aurimas Liutikas68e51952018-04-04 14:55:27 -070031FORMAT_MESSAGE = '''
32**********************************************************************
33To format run:
34{}/ktlint.py --format --file {}
35**********************************************************************
36'''
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080037
38
39def main(args=None):
40 parser = argparse.ArgumentParser()
Aurimas Liutikasa6100282017-12-12 09:47:00 -080041 parser.add_argument('--file', '-f', nargs='*')
Tyson Henninge4158b32018-01-19 16:15:55 -080042 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 Liutikas30ef8f62017-11-16 17:42:50 -080045 args = parser.parse_args()
Aurimas Liutikas68e51952018-04-04 14:55:27 -070046 kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')]
47 ktlint_args = kt_files[:]
Tyson Henninge4158b32018-01-19 16:15:55 -080048 if args.format:
49 ktlint_args += ['-F']
50 if not ktlint_args:
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080051 sys.exit(0)
52
Aurimas Liutikas91d5c552018-05-30 13:55:44 -070053 ktlint_args += ['--android']
54
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080055 ktlint_env = os.environ.copy()
56 ktlint_env['JAVA_CMD'] = 'java'
57 try:
Tyson Henninge4158b32018-01-19 16:15:55 -080058 check = subprocess.Popen(['java', '-jar', KTLINT_JAR] + ktlint_args,
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080059 stdout=subprocess.PIPE, env=ktlint_env)
60 stdout, _ = check.communicate()
61 if stdout:
Luca Stefani67e7d242020-02-09 13:28:26 +010062 print('prebuilts/ktlint found errors in files you changed:')
63 print(stdout)
64 print(FORMAT_MESSAGE.format(MAIN_DIRECTORY, ' '.join(kt_files)))
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080065 sys.exit(1)
66 else:
67 sys.exit(0)
68 except OSError as e:
69 if e.errno == errno.ENOENT:
Luca Stefani67e7d242020-02-09 13:28:26 +010070 print('Error running ktlint!')
Aurimas Liutikas30ef8f62017-11-16 17:42:50 -080071 sys.exit(1)
72
73if __name__ == '__main__':
74 main()