with_kapt_args.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import sys
  2. import os
  3. import subprocess
  4. import platform
  5. import argparse
  6. import re
  7. def parse_args(args):
  8. parser = argparse.ArgumentParser()
  9. parser.add_argument('--ap-classpath', nargs='*', type=str, dest='classpath')
  10. cmd_start = args.index('--')
  11. return parser.parse_args(args[:cmd_start]), args[cmd_start+1:]
  12. def get_ap_classpath(directory):
  13. jar_re = re.compile(r'.*(?<!-sources)\.jar')
  14. found_jars = [os.path.join(address, name) for address, dirs, files in os.walk(directory) for name in files if jar_re.match(name)]
  15. if len(found_jars) != 1:
  16. raise Exception("found %d JAR files in directory %s" % (len(found_jars), directory))
  17. arg = 'plugin:org.jetbrains.kotlin.kapt3:apclasspath=' + found_jars[0]
  18. return '-P', arg
  19. def create_extra_args(args):
  20. cp_opts = [arg for d in args.classpath for arg in get_ap_classpath(d)]
  21. return cp_opts
  22. if __name__ == '__main__':
  23. args, cmd = parse_args(sys.argv[1:])
  24. res = cmd + create_extra_args(args)
  25. if platform.system() == 'Windows':
  26. sys.exit(subprocess.Popen(res).wait())
  27. else:
  28. os.execv(res[0], res)