-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·260 lines (210 loc) · 5.49 KB
/
build.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/sh
#################################################
## ##
## build script for garvel (UNIX-like systems) ##
## ##
#################################################
# setup paths correctly so that the script
# can be invoked from any directory
curr_dir=$(dirname -- "$0")
cd -- ${curr_dir}
cd ../
PROJECT_ROOT=`pwd`
echo ${PROJECT_ROOT}
## base vars
JAVAC=
JAVAC_FLAGS="-Xlint -source 1.7 -target 1.7"
CLASSPATH="."
JAVA=
## project vars
PROJECT_NAME=garvel
BUILD_DIR=${PROJECT_ROOT}/build
SRC_ROOT=${PROJECT_ROOT}/src
GARVEL_TEMPLATE_DIR=${BUILD_DIR}/com/tzj/garvel/common/templates
GARVEL_TEMPLATE_FILE=${SRC_ROOT}/com/tzj/garvel/common/templates/GarvelTemplate.gl
MAIN_CLASS=Main
## JAR vars
TARGET_DIR=${PROJECT_ROOT}/target
TARGET_NAME=garvel.jar
TARGET_ENTRY_POINT=com/tzj/garvel/cli/CLI
GARVEL_WRAPPER=garvel.sh
## check for the java compiler
function check_java()
{
if [[ ! -z "${JAVA_HOME}" ]]
then
JAVAC=${JAVA_HOME}/bin/javac
JAVA=${JAVA_HOME}/bin/java
else
which javac > /dev/null
if [[ $? -ne "0" ]]
then
echo "No java compiler found; please set JAVA_HOME explicitly or make javac available on the PATH"
exit 1
else
JAVAC=javac
JAVA=java
fi
fi
}
## delete the target dir if present
function delete_target_dir()
{
if [[ -e ${TARGET_DIR} && -d ${TARGET_DIR} ]]
then
rm -rf ${TARGET_DIR}
fi
echo
}
## create the main build directory
function create_build_dir()
{
echo "[ Creating build directory ]"
echo
mkdir -p ${BUILD_DIR}
if [[ $? -ne "0" ]]
then
echo "Failed to create directory ${BUILD_DIR}. Please check that you have sufficient permissions."
exit 2
fi
# make the templates directory explicitly
mkdir -p ${GARVEL_TEMPLATE_DIR}
if [[ $? -ne "0" ]]
then
echo "Failed to create directory ${BUILD_DIR} (unabled to create ${GARVEL_TEMPLATE_DIR}. Please check that you have sufficient permissions."
exit 2
fi
echo "[ Created build directory ]"
}
## create the project JAR file
function create_project_jars()
{
echo
echo "[ Creating project JAR ${TARGET_NAME} ]"
if [[ ! -d ${TARGET_DIR} ]]
then
mkdir ${TARGET_DIR}
if [[ "$?" -ne "0" ]]
then
echo "Unable to create ${TARGET_DIR}"
delete_build_dir 5
fi
fi
pushd ${BUILD_DIR} > /dev/null
CLASS_FILES=`find ./ -name *.class`
TEMPLATE_FILES=`find ./ -name *.gl`
jar cfe ${TARGET_NAME} ${TARGET_ENTRY_POINT} ${CLASS_FILES} ${TEMPLATE_FILES}
if [[ "$?" -eq "0" ]]
then
mv ${TARGET_NAME} ${TARGET_DIR}
if [[ "$?" -ne "0" ]]
then
echo "Failed to move ${TARGET_NAME} to ${TARGET_DIR}"
delete_build_dir 6
fi
echo
echo "[ Created project JAR ${TARGET_NAME} in ${TARGET_DIR} ]"
else
echo "[ Unable to create ${TARGET_NAME} ]"
delete_build_dir 4
fi
popd > /dev/null
}
## compile the sources
function compile_sources()
{
echo
pushd ${SRC_ROOT} > /dev/null
files=`find ./ -name *.java`
echo "[ Compiling source files "
echo
echo "${files}"
echo
echo "to ${BUILD_DIR} ]"
echo
${JAVAC} ${JAVAC_FLAGS} -d ${BUILD_DIR} ${files} > /dev/null
compile_code=$?
if [[ ${compile_code} -ne "0" ]]
then
delete_build_dir 3
fi
echo "[ Finished compiling source files ]"
cp ${GARVEL_TEMPLATE_FILE} ${GARVEL_TEMPLATE_DIR}/GarvelTemplate.gl
create_project_jars
popd > /dev/null
echo
}
## check if test was successful
function check_success
{
if [[ "$2" -ne "0" ]]
then
echo "test $1 failed"
exit 7
fi
}
## run all the tests
function run_tests()
{
echo
echo "[ Running tests ]"
echo
`garvel version` >/dev/null
check_success "test_version" $?
echo "test_version... success"
`garvel new foo` >/dev/null
check_success "test_new" $?
echo "test_new... success"
echo "[ All tests passed successfully ]"
}
## create a wrapper for the garvel jar file
function create_garvel_script()
{
echo "[ Creating wrapper script for ${PROJECT_NAME} ]"
echo
if [[ -z ${TARGET_DIR} ]]
then
create_target_dir
fi
script_path="${TARGET_DIR}/${GARVEL_WRAPPER}"
touch ${script_path}
echo '#!/bin/sh' >> ${script_path}
echo >> ${script_path}
echo "java -jar ${TARGET_DIR}/${TARGET_NAME} \$@" >> ${script_path}
chmod +ux ${script_path}
echo "[ Finished creating wrapper script ${GARVEL_WRAPPER} for ${PROJECT_NAME} in ${TARGET_DIR} ]"
echo
echo "Add the following lines to your .bashrc or .bash_profile file:"
echo " export PATH=\"${script_path}\":\$PATH"
echo " alias ${PROJECT_NAME}=\"${script_path}\""
echo
}
## delete build data (takes exit code as input)
function delete_build_dir()
{
echo "[ Deleting build directory ]"
echo
if [[ -e ${BUILD_DIR} ]]
then
rm -rf ${BUILD_DIR}
if [[ "$1" -ne "0" ]]
then
echo "[ Finished deleting build directory. Exiting with error code $1 ]"
echo
else
echo "[ Finished deleting build directory. Build was successful. ]"
echo
fi
exit $1
fi
}
#
# steps
#
check_java
delete_target_dir
create_build_dir
compile_sources
#run_tests
create_garvel_script
delete_build_dir 0