11package com .github .kayjamlang .executor .cli ;
22
33import com .github .kayjamlang .executor .Executor ;
4- import com .github .kayjamlang .executor .MainLibrary ;
4+ import com .github .kayjamlang .executor .libs .main .MainLibrary ;
5+ import com .github .kayjamlang .executor .libs .Library ;
56import org .apache .commons .cli .*;
67import org .mozilla .universalchardet .UniversalDetector ;
78
89import java .io .File ;
910import java .io .IOException ;
11+ import java .net .URL ;
12+ import java .net .URLClassLoader ;
1013import java .nio .file .Paths ;
14+ import java .util .Enumeration ;
1115import java .util .Scanner ;
16+ import java .util .jar .JarEntry ;
17+ import java .util .jar .JarFile ;
1218
1319public class Main {
1420
@@ -27,20 +33,51 @@ public static void main(String[] args) throws Exception {
2733 cmd = parser .parse (options , args );
2834 } catch (ParseException e ) {
2935 System .out .println (e .getMessage ());
30- formatter .printHelp ("kayjam" , options );
36+ formatter .printHelp ("kayjam-cli " , options );
3137
3238 System .exit (1 );
3339 return ;
3440 }
3541
36- File executeFile = new File (Paths .get (System .getProperty ("user.dir" ),
37- cmd .getOptionValue ("file" )).normalize ().toString ());
38-
3942 Executor executor = new Executor ();
4043 executor .addLibrary (new MainLibrary ());
44+ File libDir = new File (Paths .get (System .getProperty ("user.dir" ),
45+ "./libs/" ).normalize ().toString ());
46+ if (libDir .exists ()){
47+ File [] files = libDir .listFiles ();
48+ if (files !=null )
49+ for (File libJar : files ){
50+ if (libDir .getName ().endsWith (".jar" ))
51+ loadJarLibrary (executor , libJar );
52+ }
53+ }
54+
55+ File executeFile = new File (Paths .get (System .getProperty ("pathFile" ),
56+ cmd .getOptionValue ("file" )).normalize ().toString ());
4157 executor .execute ("{" +read (executeFile )+"}" );
4258 }
4359
60+ private static void loadJarLibrary (Executor executor , File file ) throws IOException , ClassNotFoundException , IllegalAccessException , InstantiationException {
61+ JarFile jarFile = new JarFile (file );
62+ Enumeration <JarEntry > e = jarFile .entries ();
63+
64+ URL [] urls = { new URL ("jar:file:" + file .getPath ()+"!/" ) };
65+ URLClassLoader cl = URLClassLoader .newInstance (urls );
66+
67+ while (e .hasMoreElements ()) {
68+ JarEntry je = e .nextElement ();
69+ if (je .isDirectory () || !je .getName ().endsWith (".class" )){
70+ continue ;
71+ }
72+
73+ String className = je .getName ().substring (0 ,je .getName ().length ()-6 );
74+ className = className .replace ('/' , '.' );
75+ Class <?> c = cl .loadClass (className );
76+ if (c .isAssignableFrom (Library .class ))
77+ executor .addLibrary ((Library ) c .newInstance ());
78+ }
79+ }
80+
4481 private static String read (File file ) throws IOException {
4582 Scanner reader = new Scanner (file , UniversalDetector .detectCharset (file ));
4683
0 commit comments