-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmeka
More file actions
executable file
·351 lines (265 loc) · 7.14 KB
/
smeka
File metadata and controls
executable file
·351 lines (265 loc) · 7.14 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env bash
# smeka setup script for installing
# smeka in a project.
# Implementation by:
# Nick R. Papior, 2016
# Print out the help information for
# this file.
function print_help() {
echo "Usage: $0 [options]"
echo ""
echo "Required options:"
echo " --project (if performed on fresh project)"
echo ""
echo "Options:"
echo " -D|--directory <path>"
echo " the project directory for the main Makefile"
echo " -P|--project <name>"
echo " project name (hard coded in the makefiles)"
echo " -v|--version <version>"
echo " project version"
echo " --smeka-dir <relative path>"
echo " the name of the path for the sub-makefiles"
echo " created by smeka [$_smeka_dir]"
echo " --project-dir-smeka"
echo " install the Makefile.project in the smeka directory"
echo " (instead of in the same folder as the Makefile)"
echo " -h|--help"
echo " this help message"
}
# Convert a path to a full path with prefixed
# /
# Hence
# ../this/dir
# would be converted to (roughly)
# $(dirname $(pwd)/../this/dir)/dir
function full_path() {
local path=$1
shift
# Convert path to a full path (/.../) path
printf "%s" "$(cd $(dirname $path) && pwd -P)/$(basename $path)"
}
# Common error exit codes
_ERR_DIR=1
_ERR_MAKEFILE=2
_ERR_UNSET_PROJECT=3
# This script directory
# This may not work in all OS's.
# For now this is fine.
_cur_dir=$(full_path $(dirname $0))
# Create a temporary directory ($$ == PID of current shell)
_tmp_dir=$(mktemp -dt "$(basename $0).$$.XXXXXXXX")
# Default clean-up utility
function clean_up() {
rm -rf $_tmp_dir
}
# Create default trap function
function def_trap() {
clean_up
echo "Exiting $0 before completion!!!"
}
# This is a no-op function
function no_op() {
echo no-op > /dev/null
}
# Default options
_smeka_dir=smeka
_version=DONOTUSE
_project_dir_smeka=0 # 0 = false, 1 = true
# Options that are REQUIRED to be
# set by the user.
_project=
_directory=.
while [ $# -gt 0 ]
do
# get the current option
opt=$1
shift
case $opt in
# Specify the installation directory
# This should be the root of the project
# files (where the Makefile should reside)
-D|--directory)
_directory=$1
shift
;;
# The name of the project.
# This will be hard-coded into the Makefiles
# as the project name.
-P|--project)
_project=$1
shift
;;
# Denote the version of the project
-v|--version)
_version=$1
shift
;;
# Change the (relative) directory of the smeka files
--smeka-dir)
_smeka_dir=$1
shift
;;
# Decide the placement of the Makefile.project
# file (default to same folder as Makefile)
--project-dir-smeka)
_project_dir_smeka=1
;;
-h|--help)
print_help
exit 0
;;
esac
done
# Begin actual installation of smeka
err=0
# 1. Check parameters
if [ -z "$_directory" ]; then
echo "ERROR"
echo "Installing smeka REQUIRES the directory of the"
echo "parent project directory."
echo "Please add -D|--directory flag"
err=$_ERR_DIR
fi
if [ ! -d "$_directory" ]; then
echo "ERROR"
echo "Project directory:"
echo " $_directory"
echo "does not exist."
echo "Please correct your input."
err=$_ERR_DIR
fi
# If there is an error the directory is erroneous
[ $err -ne 0 ] && exit $err
# Convert to full path
_directory=$(full_path $_directory)
# 2. Check whether the project directory
# has an old installation of smeka.
pushd $_directory > /dev/null
trap "def_trap ; popd" SIGINT SIGTERM
# Check if there is a Makefile
previous_smeka=0
if [ -e Makefile ]; then
previous_smeka=1
fi
if [ $previous_smeka -eq 1 ]; then
# Check that it is a smeka Makefile
# If it isn't we do not allow installing
# smeka.
# The project should be:
# 1) a clean (no Makefile) directory
# 2) Having a previous installed smeka installation
grep "SMEKASETTINGS" Makefile > /dev/null
rval=$?
if [ $rval -ne 0 ]; then
echo "ERROR!"
echo "An existing Makefile exists which is not a"
echo "smeka Makefile."
echo "Please rename your original Makefile and re-run."
err=$_ERR_MAKEFILE
fi
fi
popd > /dev/null
[ $err -ne 0 ] && exit $err
######################
## Proceed with the ##
## installation! ##
######################
# Move to head of this CMD
# Also assign a trap to popd
pushd $_directory > /dev/null
trap "def_trap ; popd" SIGINT SIGTERM
if [ $previous_smeka -eq 1 ]; then
# Re-create the makefile with content of smeka
tmp_make=$_tmp_dir/Makefile
in_smeka=0
while IFS='' read -r line
do
case $line in
# If detecting SMEKASETTINGS
# We overwrite the content with the
# smeka Makefile and continue reading
*SMEKASETTINGS*)
sed -e "s:%%SMEKA_DIR%%:$_smeka_dir:" \
$_cur_dir/make/Makefile >> $tmp_make
in_smeka=1
;;
# We are done with reading the
# smeka files
*SMEKAENDSETTINGS*)
in_smeka=0
;;
# Lines not part of smeka,
# or if in smeka, we skip them.
*)
if [ $in_smeka -eq 0 ]; then
echo "$line" >> $tmp_make
fi
;;
esac
done < "Makefile"
# Remove the old Makefile and re-install the
# currently created makefile
# Now the makefile has been created
# and we wish to move it
mv -f $tmp_make Makefile
elif [ -z "_project" ]; then
# Error, the project name hasn't been set.
echo "ERROR"
echo "For a fresh smeka installation the project name"
echo "is REQUIRED information."
echo "Please see:"
echo " $0 --help"
echo "on how to set the project name."
exit $_ERR_UNSET_PROJECT
else
# The Makefile does not exist, copy it
sed -e "s:%%SMEKA_DIR%%:$_smeka_dir:" \
$_cur_dir/make/Makefile >> Makefile
fi
# Ensure the smeka Makefile directory exists...
# This command will create both the smeka-directory
# AND the vendor folder... :)
mkdir -p $_smeka_dir/vendor
# Copy the files (except Makefile.project)
# to the smeka directory
for f in $_cur_dir/make/smeka/*.*
do
# Skip the Makefile.project
# It needs special attention
[ "$f" == Makefile.project ] && continue
# Make sure we overwrite old files
cp -f $f $_smeka_dir/
done
# Copy the vendor files
# WE ALWAYS DO THIS
# We issue a warning if a file is overwritten!
for f in $_cur_dir/make/smeka/vendor/*
do
bf=$(basename $f)
# Issue warning if already existing
if [ -e $_smeka_dir/vendor/$bf ]; then
echo "Vendor file '$bf' is being overwritten (this may be neglected when updating smeka)"
fi
# Make sure we overwrite old files
cp -f $f $_smeka_dir/vendor/
done
# If the project file does not exists, perform sed and create
if [ -e Makefile.project ]; then
# We need not do anything.
# Everything has been set previously by the user
# ASSUMEDLY
no_op
else
# The Makefile.project file does not exist.
# Here the file is created.
sed "s:%%VERSION%%:$_version:;s:%%PROJECT%%:$_project:" \
$_cur_dir/make/Makefile.project > Makefile.project
fi
popd > /dev/null
# Clean-up script
clean_up
exit 0
# Local Variables:
# mode: bash
# End: