-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.sh
executable file
·265 lines (237 loc) · 4.61 KB
/
helper.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
261
262
263
264
265
#!/bin/bash
# Init checking
function print_options {
printf "Use:\n -m or --make: runs cmake from build folder.\n -b or --build: builds program.\n -e or --execute: executes target.\n -c or --clean: cleans build directory. If added as a second option the it will also clean the build directory before executing the action.\n"
}
if [[ $# > 2 ]]; then
printf "You cannot have that many options."
exit 1
elif [[ $# == 0 ]]; then
print_options
exit 1
fi
# Variables
BUILD_DIR="build"
TARGET_DIR="nikte"
TARGET="nikte"
OPT1=$1
OPT2=$2
# Functions
# Runs CMake
function cmake_func {
if [ ! -d "$BUILD_DIR" ]; then
mkdir "$BUILD_DIR"
fi
cd $BUILD_DIR
cmake ..
}
# Runs cmake and make
function make_func {
if [ ! -d "$BUILD_DIR" ]; then
mkdir $BUILD_DIR
cd $BUILD_DIR
cmake ..
elif [ ! -f "$BUILD_DIR"/Makefile ]; then
cd $BUILD_DIR
cmake ..
else
cd $BUILD_DIR
fi
make -j 4
}
# Execute target
function exec_func {
if [ ! -d "$BUILD_DIR" ]; then
mkdir $BUILD_DIR
cd $BUILD_DIR
cmake ..
make -j 4
elif [ ! -f "$BUILD_DIR"/Makefile ]; then
cd $BUILD_DIR
cmake ..
make -j 4
elif [ ! -f "$BUILD_DIR"/"$TARGET_DIR"/"$TARGET" ]; then
cd $BUILD_DIR
make -j 4
else
cd $BUILD_DIR
fi
cp -r ../nikte/assets example
cd $TARGET_DIR
./"$TARGET"
}
# Cleans build dir partially or completely
function clean_func {
case $1 in
-c|--clean)
if [ -d "$BUILD_DIR/$TARGET_DIR" ]; then
rm -rf "$BUILD_DIR/$TARGET_DIR"
rm -rf "$BUILD_DIR/assets"
fi
shift
;;
-ca|--clean-all)
if [ -d "$BUILD_DIR" ]; then
rm -rf $BUILD_DIR/example
rm -rf $BUILD_DIR/nikte
rm -rf $BUILD_DIR/src
rm -rf $BUILD_DIR/test
fi
shift
;;
*)
printf "Invalid clean option.\n"
print_options
exit 1
;;
esac
}
# Creates the boilerplate for a cpp class
function create_class {
if [[ -n $OPT2 ]]; then
class_name=$OPT2
else
echo "Type the name of your class with_underscores"
read class_name
fi
class_camel_name=""
names=$(echo $class_name | tr "_" " ")
for name in $names
do
name="$(tr '[:lower:]' '[:upper:]' <<< ${name:0:1})${name:1}"
class_camel_name="$class_camel_name$name"
done
echo "#pragma once
class ${class_camel_name} {
public:
${class_camel_name}();
~${class_camel_name}();
};
" > "$class_name.h"
echo "#include \"${class_name}.h\"
${class_camel_name}::${class_camel_name}() {
}
${class_camel_name}::~${class_camel_name}() {
}
" > "$class_name.cpp"
}
# Formats cpp code using clang-format
function format_code {
find include/ -iname "*.hpp" -o -iname "*.cpp" | xargs clang-format --verbose -i -style=file
find src/ -type f \( -iname "*.hpp" -or -iname "*.cpp" ! -iname "lemmatizer.cpp" \) | xargs clang-format --verbose -i -style=file
find nikte/ -iname "*.hpp" -o -iname "*.cpp" | xargs clang-format --verbose -i -style=file
find test/ -type f \( -iname "*.h" -o -iname "*.cpp" ! -iname "catch.cpp" \) | xargs clang-format --verbose -i -style=file
}
function copy_assets {
cp -r nikte/assets $BUILD_DIR/nikte
}
function copy_scripts {
cp -r nikte/scripts $BUILD_DIR/nikte
}
# Args evalutation
case $OPT1 in
-m|--make)
MODE=CMAKE # Invoke cmake
shift
;;
-b|--build)
MODE=MAKE # Invoke make
shift
;;
-e|--execute)
MODE=EXECUTE # Execute program
shift
;;
-be)
MODE=BE # Make and Exec
shift
;;
-c|--clean|-ca|--clean-all)
MODE=CLEAN # Clean build directory
shift
;;
--create-class)
MODE=CREATE_CLASS
shift
;;
-f|--format-code)
MODE=FORMAT_CODE # Formats cpp code using clang-format
shift
;;
-cp|--copy-assets)
MODE=COPY_ASSETS
shift
;;
-bt|--build-tests)
TARGET_DIR="test"
TARGET="tests"
MODE=MAKE # Invoke make
shift
;;
-rt|--run-tests)
TARGET_DIR="test"
TARGET="tests"
MODE=EXECUTE # Execute program
shift
;;
-t|--test)
TARGET_DIR="test"
TARGET="tests"
MODE=BE # Make and Exec
shift
;;
*)
printf "Invalid option.\n"
print_options
exit 1
;;
esac
if [[ -n $OPT2 ]] && [[ $OPT1 != $OPT2 ]] && [[ $MODE != CREATE_CLASS ]]; then
case $OPT2 in
-c|--clean)
clean_func
shift
;;
*)
printf "Invalid second option. Ignoring...\n"
;;
esac
fi
case $MODE in
CMAKE)
cmake_func
shift
;;
MAKE)
make_func
shift
;;
EXECUTE)
copy_scripts
exec_func
shift
;;
BE)
make_func
cd ..
exec_func
shift
;;
CLEAN)
clean_func $OPT1
shift
;;
CREATE_CLASS)
create_class
shift
;;
FORMAT_CODE)
format_code
shift
;;
COPY_ASSETS)
copy_assets
copy_scripts
shift
;;
esac