Friday, July 12, 2013

Terminal IDE - builder

There's an app for Android devices called Terminal IDE by Spartacus Rex. It's a command line development environment for Java and Android and appears to now be hosted in Google Code.

I've been using it for a while off and on, when I'm working on something that won't wait until I have a computer but doesn't warrant carrying a machine with me (like on a backpacking trip). Its power and simplicity make it perfect for a little on-the-go coding as ideas come to mind.

One part that wasn't so simple when I first got started was the builder. I found it to be a little clunky to use. There is another builder script here, along with a few other utilities. That one has lots of options.

Below, I'm posing a builder that I wrote some time ago that is very simple and only has a couple options. It has served me well so perhaps you'll also find it useful.



The simplest way to build and execute your code is to place the script in your home directory (~). Then, cd into the directory containing your .java file, and run this line:
. ~/builder.sh -l

For the help file, you can always run: . ~/builder.sh -h




Here is the entire script:

#!~/system/bin/bash

#Simple Build Script

#For some reason the getopts index needs to be reset manually...
OPTIND=0

launch=0
compile=1
javaFilename=""

while getopts "f:hlo" opt
do
case $opt in
f)
       javaFilename=${OPTARG%.*}
       ;;
     l)
       launch=1
       ;;
     o)
       compile=0
       launch=1
       ;;
     \? | h)
       usage
       return
       ;;
     esac
done

usage()
{
scriptname=builder.sh
cat << EOF
usage: $scriptname [ hlo ] [ f filename.java ]
-h    Show this message
-l    Launch file after building
-o    Only launch, no build
-f    Specify a file instead of finding the java file in the current directory

You may specify class locations by listing them in a file named "includes" in the same directory as the java file to be launched.

Execution examples:
To build the java file in the current directory: $scriptname
To build and launch:                             $scriptname -l
To launch only:                                  $scriptname -o
To specify a file to build and launch:           $scriptname -lf filename.java

Example includes file:
# Bring in my utilities classes
../utilities

EOF
}

#Find java javaFilename if none given
if [[ $javaFilename == "" ]]; then
if [ `ls -l |grep -c .java` != 1 ]; then
       echo  Because there is not a solitary java file in the directory, you must specify a filename using the -f switch.
return
fi
javaFilename=`find . -name *.java`
javaFilename=${javaFilename:2:-5}
else
if [ ! -f ${javaFilename}.java ]; then
echo File ${javaFilename}.java not found.
return
fi
fi

compileList=""
includeList=""
delimiter=""
#Prepare to include other classes, if specified.
if [ -f includes ]; then
while read line; do
if [ "${line}" != "" -a "${line:0:1}" != "#" ]; then
includeList=${includeList}" "${line}"/*.class"
compileList=${compileList}${delimiter}${line}
delimiter=":"
fi
done < includes
else
echo No \"includes\" file.
fi

if [ $compile -eq 1 ]; then
#Clean up. This does not clean up inner class classfiles but does allow for multiple class files in the same directory to be included in the generated jar.
if [ -f ${javaFilename}.class ]; then
rm ${javaFilename}.class
fi
if [ -f ${javaFilename}.jar ];then
rm ${javaFilename}.jar
fi

#Compile java file into classfile(s).
if [ -z $compileList ]; then
javac -verbose ${javaFilename}.java
else
javac -verbose ${javaFilename}.java -classpath $compileList
fi

#Now convert to dex file. The assumption is you want to include all class files in the directory.
dx --dex --verbose --output=${javaFilename}.jar ./*.class $includeList
fi

#Finally, launch if specified.
if [ $launch -eq 1 ]; then
java -jar ${javaFilename}.jar $javaFilename
fi


No comments: