11#!/usr/bin/env python3
22
3+ '''
4+ Auto compile or execute java project by setting root mark file and given java
5+ file path.
6+
7+ Usage:
8+ `python3 jpb.py [-c|-e] target_file_name`
9+ `-c` means compiling mode.
10+ `-e` means executing mode.
11+ '''
12+
313import os
414import sys
515
616def find_project_root (project_file , filepath ):
17+ '''
18+ Find project root dir accroding to mark-file name and given java file path.
19+
20+ Args:
21+ project_file [str] project root mark file `java.project`.You need to create
22+ it in root dir.
23+
24+ filepth [str] target compile or execute java file. The script will start
25+ to find mark-file from its parent dir.
26+
27+ return: [str] project root dir.
28+ '''
729 def forward_walk (filepath ):
830 dirname = os .path .dirname (filepath )
931 if dirname != os .path .join (os .path .splitdrive (dirname )[0 ], os .path .sep ):
@@ -17,18 +39,48 @@ def forward_walk(filepath):
1739 return project_root
1840
1941def compile_class_file (project_root , classpath ):
42+ '''
43+ Compile java file associated with given classpath to class file.
44+
45+ Args:
46+ project_root [str] jvm's workspace also project root dir.
47+
48+ classpath [str] target classpath to compile like `com.hello.Hello`.
49+
50+ return: [None]
51+ '''
2052 cwd_tmp = os .getcwd ()
2153 os .chdir (project_root )
2254 os .system ('javac -encoding UTF-8 -d . ' + classpath .replace ('.' , os .path .sep )+ '.java' )
2355 os .chdir (cwd_tmp )
2456
2557def execute_main_class (project_root , classpath ):
58+ '''
59+ Execute compiled main class of project.
60+
61+ Args:
62+ project_root [str] jvm's workspace.
63+
64+ classpath [str] main class path like `com.hello.Hello`.
65+
66+ return: [None]
67+ '''
2668 cwd_tmp = os .getcwd ()
2769 os .chdir (project_root )
2870 os .system ('java ' + classpath )
2971 os .chdir (cwd_tmp )
3072
3173def get_classpath (project_root , filepath ):
74+ '''
75+ Get standerd classpath from project dir and java file path.
76+
77+ Args:
78+ project_root [str] project root dir.
79+
80+ filepath [str] target java file path.
81+
82+ return: [str] standerd classpath of java file like `com.hello.Hello`.
83+ '''
3284 class_file = filepath .split (project_root + os .sep )[1 ]
3385 class_path = os .path .splitext (class_file )[0 ].replace (os .path .sep , '.' )
3486 return class_path
0 commit comments