-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.sh
More file actions
executable file
·141 lines (121 loc) · 3.67 KB
/
compile.sh
File metadata and controls
executable file
·141 lines (121 loc) · 3.67 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# compiles and runs the Java code
# @author Michel Balamou ([email protected])
# export PS1="\\[\e[91m\]\W \$ \\[\e[0m\]"; clear;
# Formating tags
RESET="$(tput sgr0)"
BLUE="$(tput setaf 32)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 178)"
GREY="$(tput setaf 234)"
CYANE="$(tput setaf 26)"
BRED="$(tput setaf 160)"
# Output variables
source "$HOME/compiler/config"
function run()
{
classname=$1
isWarning=false
offset=2
folder="class"
warning="Note: Recompile with -Xlint:unchecked for details."
classname="${1%.java}" # remove .java extension
# Check if the file name exists
# if it does, save it in a .tmp_data file
# if it doesn't, then load the name form the previous .tmp_data file
if [ -e "./${classname}.java" ]; then
echo "${classname}" > .tmp_data
else
if [ ! -z "$1" ]; then
echo "${GREY}The file ${RESET}${BRED}${classname}.java${RESET}${GREY} doesn't exist${RESET}"
fi
if [ ! -e "./.tmp_data" ]; then # no tmp data file
until [ -f "$classname" ]; do
read -p "${GREY}Please specify a class name to compile:${RESET} " classname
classname="${classname%.java}" # remove .java extension
classname+=".java"
done
classname="${classname%.java}"
echo "${classname}" > .tmp_data
echo ""
else
classname=$(<".tmp_data")
echo "${GREY}Running previous class${RESET} ${CYANE}${classname}${RESET}"
echo ""
fi
offset=1
fi
#++++++++++++++++++++
createfolder $folder
echo "$JAVA"
echo "$COMPILING"
# check if other dependencies are specified
if [ -e "./.dependencies" ]; then
dep=$(<".dependencies")
compile_text="$(javac -classpath $dep $classname.java -d $folder 2>&1)" # compiles the file named $1.java into the directory class/$1.class
else
compile_text="$(javac $classname.java -d $folder 2>&1)" # compiles the file named $1.java into the directory class/$1.class
fi
if [ -z "$compile_text" ]; then # there is no errors
# NO ERRORS
echo "${BLUE}0 errors${RESET}"
echo $RUN
# ${classname##*/} removes everything up to the last /, so that we can extract the classname without
# the directory it is stored in, as we will be runnning it from another folder
java -cp $folder "${classname##*/}" "${@:$offset}" # runs class $classname from the 'class' folder
else
# ERRORS
if [[ $compile_text == *"$warning"* ]]; then # if the error file contains the string 'warning'
isWarning=true
echo -e "${GREEN}\c"
else
echo -e "${BLUE}\c"
fi
echo -e "${compile_text}\c" # output the file otherwise
echo "${RESET}"
fi
# XLINT / GENERICS CAUSE THIS PROBLEM +++++++++++++++
if $isWarning; then
# SOME ERRORS
echo "${GREEN}You forgot to put the generic type of a variable (i.e you put Node next instead of Node<E> next)${RESET}"
echo "${RUN}"
# ${classname##*/} removes everything up to the last /, so that we can extract the classname without
# the directory it is stored in, as we will be runnning it from another folder
java -cp $folder "${classname##*/}" "${@:$offset}" # runs class $1 from the compiled directory
fi
}
function removefile()
{
if [ -f $1 ]; then
rm $1
fi
}
function createfolder()
{
if [ ! -d $1 ]; then
mkdir -p $1
fi
}
################
# MAIN SCOPE #
################
case $1 in
"--uninstall")
rm "/usr/local/bin/compile"
rm -r "$HOME/compiler/"
;;
# CLEAR ~~~~~~~~~~~~~~~~~~~~~~~
"--clear")
rm -r "class" 2> /dev/null
[ -e ".tmp_data" ] && rm ".tmp_data"
echo "Folder 'class' and the file .tmp_data have been removed"
;;
# OTHERWISE ~~~~~~~~~~~~~~~~~~
*)
# send all the arguments to the run function. Quotes are needed so
# that parameters like these "hello world" are interpreted as one word.
clear
run "${@}"
echo $END
;;
esac