@@ 3,6 3,7 @@
# Generates imports for Java stuff
from glob import iglob
+import json
import os
import sys
import zipfile
@@ 10,7 11,6 @@ import zipfile
class_map = {}
def list_classes(jar):
- global class_map
zfile = zipfile.ZipFile(jar)
for member in zfile.namelist():
# Don't care about nested classes or non-classes
@@ 28,19 28,32 @@ _, class_name = sys.argv
home = os.getenv('HOME')
root = os.getcwd()
-if root == home:
- print("Do not run this in the home directory", file=sys.stderr)
- exit(1)
while not os.access(os.path.join(root, ".gradle"), os.F_OK | os.R_OK):
if root == '/' or root == home:
print("Could not find '.gradle'", file=sys.stderr)
exit(1)
root, _ = os.path.split(root)
+if root == home:
+ print("Could not find '.gradle'", file=sys.stderr)
+ exit(1)
-for jar in iglob(os.path.join(root, ".gradle/**/*.jar"), recursive=True):
- list_classes(jar)
+cache_path = os.path.join(root, ".gradle", "jimport-cache.json")
-if class_name not in class_map:
- print(f"No such class '{class_name}'", file=sys.stderr)
- exit(1)
-print('\n'.join(class_map[class_name]))
+def prepare_cache():
+ for jar in iglob(os.path.join(root, ".gradle/**/*.jar"), recursive=True):
+ list_classes(jar)
+ with open(cache_path, "w+") as cache:
+ json.dump(class_map, cache)
+
+if class_name == "-r":
+ prepare_cache()
+ exit()
+
+if os.access(cache_path, os.F_OK | os.R_OK):
+ with open(cache_path) as cache:
+ class_map = json.load(cache)
+else:
+ prepare_cache()
+
+if class_name in class_map:
+ print('\n'.join(class_map[class_name]))