diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-01-11 23:23:57 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-01-11 23:23:57 +0100 |
| commit | cf3a343bf06e66e2bd7ff165693d7e8db3f11034 (patch) | |
| tree | 74a363e3cc4a86f9240e0dae2b85240b6b06d8b0 | |
| parent | b873c503b645419b93c44e9bca803f56d79f393c (diff) | |
Handle completely new files better
First, a file that only exists as a buffer, not even saved yet:
* Pick the first Java target found, it will probably work
* Complain if not found instead of silently pretend the file is valid
Second, a file that was added after the last gradle run so
cache doesn't contain it:
* If no fitting target was found, and the file exist and we used
cached data, force a new gradle run
| -rw-r--r-- | bin/flycheck-android-java.py | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/bin/flycheck-android-java.py b/bin/flycheck-android-java.py index badf1e5..111ff3a 100644 --- a/bin/flycheck-android-java.py +++ b/bin/flycheck-android-java.py @@ -95,16 +95,19 @@ def run_javac(encoding, source, target, bootcp, cp, files, output, args, def file_in_list(needle, files): """Find needle in files and if so, return the list without needle. Otherwise return None.""" - for i in range(0, len(files) - 1): - if os.path.samefile(needle, files[i]): - return files[0:i] + files[i + 1:] + if os.path.exists(needle): + for i in range(0, len(files) - 1): + if os.path.samefile(needle, files[i]): + return files[0:i] + files[i + 1:] return None -def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle): +def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle, + force=False): """Get options for Java compilation from gradle project and run javac.""" (cmd, mtime, projectdir) = get_gradle_command_and_project_mtime( os.path.dirname(sourcefile)) output = None + cached = False if sessiondir != None: outdir = os.path.join(sessiondir, 'java_output') try: @@ -115,9 +118,10 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle): pass cachefile = os.path.join(sessiondir, 'gradle_output') try: - if os.path.getmtime(cachefile) >= mtime: + if not force and os.path.getmtime(cachefile) >= mtime: with open(cachefile, 'r') as f: output = f.read() + cached = True except (OSError, IOError): pass else: @@ -135,17 +139,21 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle): output = output.split('\n') data = None + first = None ret = 0 + compiled = False for line in output: if line == '***': if data: + if not first: + first = data files = file_in_list(sourcefile, data['files']) if files: ret = run_javac(data['encoding'], data['source'], data['target'], data['bootcp'], data['cp'], files, data['output'], data['args'], tempfile, outdir) - data = None + compiled = True break data = {} elif data != None: @@ -166,12 +174,28 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle): elif line.startswith('output='): data['output'] = line[7:] - if data: + if not compiled and data: + if not first: + first = data files = file_in_list(sourcefile, data['files']) if files: ret = run_javac(data['encoding'], data['source'], data['target'], data['bootcp'], data['cp'], files, data['output'], data['args'], tempfile, outdir) + compiled = True + + if not compiled: + # Probably need to rerun gradle to find group for sourcefile + if cached and os.path.exists(sourcefile): + return figure_out_java_compilation(sessiondir, sourcefile, + tempfile, checkstyle, force=True) + # OK, perhaps file doesn't exist yet or not yet added to gradle, + # whatever, assume the first group is good enough + if first: + ret = run_javac(first['encoding'], first['source'], first['target'], + first['bootcp'], first['cp'], first['files'], + first['output'], first['args'], tempfile, outdir) + compiled = True if not ret and checkstyle: cmd = ['java', '-jar', @@ -190,9 +214,13 @@ def figure_out_java_compilation(sessiondir, sourcefile, tempfile, checkstyle): cwd = projectdir else: cwd = None - return subprocess.call(cmd, cwd=cwd) + ret = subprocess.call(cmd, cwd=cwd) + + if not ret and not compiled: + print("Source file not in project and project seems empty!") + ret = -1 - return 0 + return ret def main(argv): parser = argparse.ArgumentParser() |
