Skip to content

Commit d5d2d6d

Browse files
committed
Added 'new' command to gtool and updated docs accordingly
1 parent c2d3abd commit d5d2d6d

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ The `br` command argument will clean, build, and run the given class all in one
2323

2424
**Example**: `gtool br GrokkingFastSlowPointers` will clean and build all .java files and then run the main method for the `GrokkingFastSlowPointers` class.
2525

26+
### `gtool new` - Easy java class creator
27+
Feed a class name to this method (e.g. `gtool new GrokkingTopSort`) to create a respective .java file with the appropriate package name (set with [`DEFAULT_PACKAGE_NAME`](https://github.com/CunningDJ/grokJava/blob/master/gtool#L8)) and boilerplate code.
28+
2629
### Verbose
2730
If for some reason you want a verbose printout of the stages being performed by `gtool`, simply change the [initialized `VERBOSE` value](https://github.com/CunningDJ/grokJava/blob/master/gtool#L10) to `1`. Maybe I'll add a `-v` flag but I don't feel like it right now.

gtool

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function build() {
3030
}
3131

3232
function run() {
33-
classname=$1
33+
classname="$1"
3434
full_classname="$DEFAULT_PACKAGE_NAME.$classname"
3535
log "Running java class: $full_classname"
3636
cd $CLASSFILES_DIR
@@ -39,7 +39,7 @@ function run() {
3939

4040
function buildrun() {
4141
build;
42-
run $1
42+
run "$1"
4343
}
4444

4545
function log() {
@@ -48,6 +48,27 @@ function log() {
4848
fi
4949
}
5050

51+
function new() {
52+
classname="$1"
53+
javafile_name="$classname.java"
54+
if [[ "$classname" == "" ]]; then
55+
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";
56+
exit 1;
57+
elif [[ -e "$javafile_name" ]]; then
58+
echo "ERROR: $javafile_name already exists!";
59+
exit 1;
60+
else
61+
# Create the file
62+
log "Creating new java file $javafile_name ..."
63+
touch $javafile_name;
64+
printf "package $DEFAULT_PACKAGE_NAME;\n\nclass $classname {\n" >> $javafile_name;
65+
printf "\tpublic static void main(String[] args) {\n\t\tTester tester = new Tester();\n" >> $javafile_name;
66+
printf "\t\tString testTitle=\"\";\n" >> $javafile_name;
67+
printf "\n\t\t// TEST CLASS METHODS HERE USING TESTER CLASS\n" >> $javafile_name;
68+
printf "\t}\n}\n" >> $javafile_name;
69+
fi
70+
}
71+
5172

5273
# MAIN
5374
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
@@ -60,6 +81,8 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
6081
build;
6182
elif [[ "$cmd" == "clean" ]]; then
6283
clean;
84+
elif [[ "$cmd" == "new" ]]; then
85+
new "$2";
6386
else
6487
printf "USAGE: gtool CMD [...ARGS]\n\tCMD build|clean|run|br\n";
6588
fi

0 commit comments

Comments
 (0)