|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | + Auto compile or execute java project by setting root mark file and given java |
| 5 | + file path. |
| 6 | +
|
| 7 | + usage: jpb [-h] [-c] [-e] [-p PROFILE] [-r PROROOT] mainfile |
| 8 | +
|
| 9 | + positional arguments: |
| 10 | + mainfile main file to build. |
| 11 | +
|
| 12 | + optional arguments: |
| 13 | + -h, --help show this help message and exit |
| 14 | + -c, --compile choose compile mode. |
| 15 | + -e, --execute choose execute mode. |
| 16 | + -p PROFILE, --profile PROFILE |
| 17 | + project file to mark workspace. default is |
| 18 | + `java.project`. |
| 19 | + -r PROROOT, --proroot PROROOT |
| 20 | + point at default project root `pwd`、`cwd` or given |
| 21 | + realpath. default is `cwd`. |
| 22 | +''' |
| 23 | + |
| 24 | +import os |
| 25 | +import argparse |
| 26 | + |
| 27 | +def find_project_root(project_file, filepath, proroot='cwd'): |
| 28 | + ''' |
| 29 | + Find project root dir accroding to mark-file name and given java file path. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_file [str] project root mark file `java.project`.You need to create |
| 33 | + it in root dir. |
| 34 | +
|
| 35 | + filepth [str] target compile or execute java file. The script will start |
| 36 | + to find mark-file from its parent dir. |
| 37 | +
|
| 38 | + return: [str] project root dir. |
| 39 | + ''' |
| 40 | + def forward_walk(filepath): |
| 41 | + dirname = os.path.dirname(filepath) |
| 42 | + if dirname != os.path.join(os.path.splitdrive(dirname)[0], os.path.sep): |
| 43 | + yield dirname |
| 44 | + yield from forward_walk(os.path.dirname(filepath)) |
| 45 | + filepath = os.path.abspath(filepath) |
| 46 | + if proroot == 'cwd': |
| 47 | + # set default project root to cwd. |
| 48 | + project_root = os.getcwd() |
| 49 | + elif proroot == 'pwd': |
| 50 | + # set default project root to file directory. |
| 51 | + project_root = os.path.dirname(filepath) |
| 52 | + else: |
| 53 | + # set default project root to pointed dir. |
| 54 | + project_root = os.path.abspath(proroot) |
| 55 | + for dirname in forward_walk(filepath): |
| 56 | + if os.path.isfile(os.path.join(dirname, project_file)): |
| 57 | + project_root = dirname |
| 58 | + break |
| 59 | + return project_root |
| 60 | + |
| 61 | +def compile_class_file(project_root, classpath): |
| 62 | + ''' |
| 63 | + Compile java file associated with given classpath to class file. |
| 64 | + |
| 65 | + Args: |
| 66 | + project_root [str] jvm's workspace also project root dir. |
| 67 | +
|
| 68 | + classpath [str] target classpath to compile like `com.hello.Hello`. |
| 69 | + |
| 70 | + return: [None] |
| 71 | + ''' |
| 72 | + cwd_tmp = os.getcwd() |
| 73 | + os.chdir(project_root) |
| 74 | + os.system('javac -encoding UTF-8 -d . '+classpath.replace('.', os.path.sep)+'.java') |
| 75 | + os.chdir(cwd_tmp) |
| 76 | + |
| 77 | +def execute_main_class(project_root, classpath): |
| 78 | + ''' |
| 79 | + Execute compiled main class of project. |
| 80 | +
|
| 81 | + Args: |
| 82 | + project_root [str] jvm's workspace. |
| 83 | +
|
| 84 | + classpath [str] main class path like `com.hello.Hello`. |
| 85 | +
|
| 86 | + return: [None] |
| 87 | + ''' |
| 88 | + cwd_tmp = os.getcwd() |
| 89 | + os.chdir(project_root) |
| 90 | + os.system('java '+classpath) |
| 91 | + os.chdir(cwd_tmp) |
| 92 | + |
| 93 | +def get_classpath(project_root, filepath, ): |
| 94 | + ''' |
| 95 | + Get standerd classpath from project dir and java file path. |
| 96 | +
|
| 97 | + Args: |
| 98 | + project_root [str] project root dir. |
| 99 | +
|
| 100 | + filepath [str] target java file path. |
| 101 | +
|
| 102 | + return: [str] standerd classpath of java file like `com.hello.Hello`. |
| 103 | + ''' |
| 104 | + filepath = os.path.abspath(filepath) |
| 105 | + class_file = filepath.split(os.path.abspath(os.path.join(project_root,'%{mark}%')).split('%{mark}%')[0])[1] |
| 106 | + class_path = os.path.splitext(class_file)[0].replace(os.path.sep, '.') |
| 107 | + return class_path |
| 108 | + |
| 109 | +def main(): |
| 110 | + parser = argparse.ArgumentParser() |
| 111 | + parser.add_argument('-c', '--compile', help='choose compile mode.', action='store_true') |
| 112 | + parser.add_argument('-e', '--execute', help='choose execute mode.', action='store_true') |
| 113 | + parser.add_argument('-p', '--profile', help='project file to mark workspace. default is\ |
| 114 | + `java.project`.', default='java.project', type=str) |
| 115 | + parser.add_argument('-r', '--proroot', help='point at default project root `pwd`、`cwd`\ |
| 116 | + or given realpath. default is `cwd`.', default='cwd', type=str) |
| 117 | + parser.add_argument('mainfile', help='main file to build.', type=str) |
| 118 | + args = parser.parse_args() |
| 119 | + if os.path.isfile(args.mainfile): |
| 120 | + project_root = find_project_root(args.profile, args.mainfile, args.proroot) |
| 121 | + classpath = get_classpath(project_root, args.mainfile) |
| 122 | + if args.compile: |
| 123 | + print('----------java project builder (compile mode)----------') |
| 124 | + compile_class_file(project_root, classpath) |
| 125 | + if args.execute: |
| 126 | + print('----------java project builder (execute mode)----------') |
| 127 | + execute_main_class(project_root, classpath) |
| 128 | + else: |
| 129 | + print('File "%s" not found !' % args.mainfile) |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + main() |
0 commit comments