with_kapt_args.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 = [
  15. os.path.join(address, name)
  16. for address, dirs, files in os.walk(directory)
  17. for name in files
  18. if jar_re.match(name)
  19. ]
  20. if len(found_jars) != 1:
  21. raise Exception("found %d JAR files in directory %s" % (len(found_jars), directory))
  22. arg = 'plugin:org.jetbrains.kotlin.kapt3:apclasspath=' + found_jars[0]
  23. return '-P', arg
  24. def create_extra_args(args):
  25. cp_opts = [arg for d in args.classpath for arg in get_ap_classpath(d)]
  26. return cp_opts
  27. if __name__ == '__main__':
  28. args, cmd = parse_args(sys.argv[1:])
  29. res = cmd + create_extra_args(args)
  30. if platform.system() == 'Windows':
  31. sys.exit(subprocess.Popen(res).wait())
  32. else:
  33. os.execv(res[0], res)