summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-01-11 00:48:06 +0100
committerJoel Klinghed <the_jk@yahoo.com>2017-01-11 00:48:06 +0100
commit16810b083ae6589a26ee0d0b60563447a92fc588 (patch)
tree3c9adaaecd2aa1e3865bd78dd584760e00fa611d /bin
parentf933d0d3efcd441eecde54bcd5891c1d8cac1eec (diff)
Add support for running checkstyle after compilation
Diffstat (limited to 'bin')
-rw-r--r--bin/flycheck-android-java.py83
1 files changed, 58 insertions, 25 deletions
diff --git a/bin/flycheck-android-java.py b/bin/flycheck-android-java.py
index 4c9c82f..badf1e5 100644
--- a/bin/flycheck-android-java.py
+++ b/bin/flycheck-android-java.py
@@ -1,6 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
+import argparse
import errno
import os
import subprocess
@@ -41,10 +42,12 @@ While iterating the paths, also check mtime on build.gradle files"""
if gradlew:
cmd = [gradlew, '-p', topdir]
elif build_gradle:
- cmd = ['gradle', '-p', os.path.dirname(build_gradle)]
+ topdir = os.path.dirname(build_gradle)
+ cmd = ['gradle', '-p', topdir]
else:
+ topdir = ''
cmd = ['gradle']
- return (cmd, mtime)
+ return (cmd, mtime, topdir)
def filter_source_files(files):
"""Remove any item not looking like a Java source file."""
@@ -97,9 +100,9 @@ Otherwise return None."""
return files[0:i] + files[i + 1:]
return None
-def figure_out_java_compilation(sessiondir, sourcefile, tempfile):
+def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle):
"""Get options for Java compilation from gradle project and run javac."""
- (cmd, mtime) = get_gradle_command_and_project_mtime(
+ (cmd, mtime, projectdir) = get_gradle_command_and_project_mtime(
os.path.dirname(sourcefile))
output = None
if sessiondir != None:
@@ -132,15 +135,18 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile):
output = output.split('\n')
data = None
+ ret = 0
for line in output:
if line == '***':
if data:
files = file_in_list(sourcefile, data['files'])
if files:
- return run_javac(data['encoding'], data['source'],
- data['target'], data['bootcp'],
- data['cp'], files, data['output'],
- data['args'], tempfile, outdir)
+ ret = run_javac(data['encoding'], data['source'],
+ data['target'], data['bootcp'],
+ data['cp'], files, data['output'],
+ data['args'], tempfile, outdir)
+ data = None
+ break
data = {}
elif data != None:
if line.startswith('args='):
@@ -159,31 +165,58 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile):
data['files'] = line[6:].split(':')
elif line.startswith('output='):
data['output'] = line[7:]
+
if data:
files = file_in_list(sourcefile, data['files'])
if files:
- return run_javac(data['encoding'], data['source'], data['target'],
- data['bootcp'], data['cp'], files, data['output'],
- data['args'], tempfile, outdir)
+ ret = run_javac(data['encoding'], data['source'], data['target'],
+ data['bootcp'], data['cp'], files, data['output'],
+ data['args'], tempfile, outdir)
+
+ if not ret and checkstyle:
+ cmd = ['java', '-jar',
+ os.path.abspath(os.path.join(projectdir, checkstyle['jar']))]
+
+ if 'config' in checkstyle:
+ cmd.extend(['-c', checkstyle['config']])
+
+ if 'properties' in checkstyle:
+ cmd.extend(['-p', checkstyle['properties']])
+
+ cmd.append(tempfile)
+ if checkstyle['path']:
+ cwd = os.path.join(projectdir, checkstyle['path'])
+ elif len(projectdir):
+ cwd = projectdir
+ else:
+ cwd = None
+ return subprocess.call(cmd, cwd=cwd)
+
return 0
def main(argv):
- sessiondir = None
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--checkstyle-jar')
+ parser.add_argument('--checkstyle-path')
+ parser.add_argument('--checkstyle-config')
+ parser.add_argument('--checkstyle-properties')
+ parser.add_argument('sessiondir', nargs='?')
+ parser.add_argument('sourcefile')
+ parser.add_argument('tempfile', nargs='?')
+ args = parser.parse_args(argv[1:])
- if len(sys.argv) == 4:
- sessiondir = sys.argv[1]
- sourcefile = sys.argv[2]
- tempfile = sys.argv[3]
- elif len(sys.argv) == 3:
- sourcefile = sys.argv[1]
- tempfile = sys.argv[2]
- elif len(sys.argv) == 2:
- sourcefile = sys.argv[1]
- tempfile = sourcefile
- else:
- return -1
+ sourcefile = args.sourcefile
+ tempfile = args.tempfile or sourcefile
+
+ checkstyle = None
+ if args.checkstyle_jar:
+ checkstyle = {'jar': args.checkstyle_jar,
+ 'path': args.checkstyle_path,
+ 'config': args.checkstyle_config,
+ 'properties': args.checkstyle_properties}
sourcefile = os.path.abspath(sourcefile)
- return figure_out_java_compilation(sessiondir, sourcefile, tempfile)
+ return figure_out_java_compilation(args.sessiondir, sourcefile, tempfile,
+ checkstyle)
sys.exit(main(sys.argv))