-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtool
executable file
·89 lines (78 loc) · 2.45 KB
/
gtool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
# GROKKING JAVA TOOL - Home made tool to build & run Grokking The Coding Interview study files
# CONSTANTS
## Change this to whatever value you'd like to package the files under, and make sure to update the all
## of the .java files' "package XX;" header lines accordingly
DEFAULT_PACKAGE_NAME='com.cunningdj.grokJava'
## Change this to 1 if you want a printout of each step the tool is undergoing
VERBOSE=0
CLASSFILES_DIR=classes
## Moving into script directory
sd=`dirname $0`
cd $sd
# COMMANDS
function clean() {
log "Cleaning $CLASSFILES_DIR/ ..."
if [[ -d $CLASSFILES_DIR ]]; then
rm -r $CLASSFILES_DIR
fi
}
function build() {
clean;
log "Building class files in $CLASSFILES_DIR/ ..."
mkdir $CLASSFILES_DIR
javac -d $CLASSFILES_DIR ./*.java
}
function run() {
classname="$1"
full_classname="$DEFAULT_PACKAGE_NAME.$classname"
log "Running java class: $full_classname"
cd $CLASSFILES_DIR
java "$full_classname"
}
function buildrun() {
build;
run "$1"
}
function log() {
if [[ $VERBOSE -eq 1 ]]; then
echo "$@";
fi
}
function new() {
classname="$1"
javafile_name="$classname.java"
if [[ "$classname" == "" ]]; then
printf "USAGE: gtool new CLASSNAME\n\tCLASSNAME Name of the java class. A file [CLASSNAME].java will be made at the top level, with the 'package $DEFAULT_PACKAGE_NAME;' (set in gtool via DEFAULT_PACKAGE_NAME) already added at the top along with the basic class declaration.\n";
exit 1;
elif [[ -e "$javafile_name" ]]; then
echo "ERROR: $javafile_name already exists!";
exit 1;
else
# Create the file
log "Creating new java file $javafile_name ..."
touch $javafile_name;
printf "package $DEFAULT_PACKAGE_NAME;\n\nclass $classname {\n" >> $javafile_name;
printf "\tpublic static void main(String[] args) {\n\t\tTester tester = new Tester();\n" >> $javafile_name;
printf "\t\tString testTitle=\"\";\n" >> $javafile_name;
printf "\n\t\t// TEST CLASS METHODS HERE USING TESTER CLASS\n" >> $javafile_name;
printf "\t}\n}\n" >> $javafile_name;
fi
}
# MAIN
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
cmd=$1
if [[ "$cmd" == "run" ]]; then
run "$2";
elif [[ "$cmd" == "br" || "$cmd" == "buildrun" ]]; then
buildrun "$2";
elif [[ "$cmd" == "build" ]]; then
build;
elif [[ "$cmd" == "clean" ]]; then
clean;
elif [[ "$cmd" == "new" ]]; then
new "$2";
else
printf "USAGE: gtool CMD [...ARGS]\n\tCMD build|clean|run|br|new\n";
fi
fi