-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun
executable file
·324 lines (290 loc) · 9.19 KB
/
run
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
#!/bin/bash
##########################################################
# code-runner #
# #
# Automate the process for compiling and executing #
# your code in multiple langs using single command. #
# #
# Author: Arpit(proffapt) Bhardwaj #
# Git(hub/lab): proffapt #
# Twitter: @proffapt #
# Telegram: @proffapt #
# Email: [email protected] #
# License: BSD License 2.0 #
# #
# Note: If You find any bugs in code-runner, please #
# REPORT it to me by creating an Issue; #
# Or if you have a fix, create a Pull Request. #
# #
##########################################################
# Can't rewrite without this banner(line 3-21)!
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
USAGE="${blue}USAGE:${white} run ${yellow}[OPTIONS] ${cyan}file ${yellow}:${white} compiles into an executable, executes it and then deletes it ${yellow}-${white} by default
${blue}OPTIONS: ${white}
[${red}--cc${white}/${red}-c${white}]=${cyan}<compiler>${white} ${yellow}:${white} compiles the *.c file with specified compiler
[${red}--cxx${white}]=${cyan}<compiler>${white} ${yellow}:${white} compiles the *.cpp file with specified compiler
[${red}--python${white}/${red}-py${white}]=${cyan}<2/3>${white} ${yellow}:${white} executes the given python file with specified python version
[${red}--debug${white}/${red}d${white}] ${yellow}:${white} compiles the file and pipes it into the default debugger
[${red}--compile${white}/${red}c${white}] ${yellow}:${white} compiles the file to an executable but doesn't execute it ${red}(${white}persistent${red})${white}
[${red}--persistent${white}/${red}p${white}] ${yellow}:${white} keeps the intermediate compiled files and cache directories ${yellow}-${white} which are deleted by default
${blue}Clean Utility:${white}
run ${red}rcexe${white} ${yellow}:${white} deletes all executable files within the folder recursively
run ${red}cexe${white} ${yellow}:${white} deletes all executable files within the folder NON recursively
run ${red}clean${white} ${yellow}:${white} removes debug folder, executables and temp files
${blue}Miscellaneous:${white}
run [${red}--list-langs${white}/${red}ll${white}] ${yellow}:${white} lists all languages supported by current version of code-runner
run [${red}--version${white}/${red}v${white}] ${yellow}:${white} echoes the current version of code-runner"
_EXIT_() {
echo "${blue}Exiting code-runner"
exit 0
}
## choosing either gdb or lldb according to which one is present, if both are then gdb is prioritised
default_debugger() {
if [[ -n $(command -v gdb) ]]; then
: "${debugger:=gdb}"
elif [[ -n $(command -v lldb) ]]; then
: "${debugger:=lldb}"
fi
}
rclean_exe() {
find . -type f -perm +111 -exec rm -rf {} \; 2>/dev/null
}
clean_exe() {
find . -type f -maxdepth 1 -perm +111 -exec rm -rf {} \; 2>/dev/null
}
clean() {
find . -type d -name "*.dSYM" -exec rm -rf {} \; 2>/dev/null
find . -type f -name "*.out" -exec rm -rf {} \; 2>/dev/null
find . -type f -name "*.class" -exec rm -rf {} \; 2>/dev/null
find . -type f -name "tempCodeRunnerFile.*" -exec rm -rf {} \; 2>/dev/null
rm -rf __pycache__ ./*.pyc
}
run() {
set -m
# C[++] fam specifics
default_debugger
CFLAGS="-Wall"
CPPSTD="-std=c++20"
if [[ $DEBUG == true ]]; then
CFLAGS+=" -g -O0"
else
CFLAGS+=" -s -w -O3"
fi
if [[ ${FILE#*.} == c ]]; then
"${CC:=gcc}" ${CFLAGS} "$FILE" -o "${FILE%%.*}.out"
if [[ $DEBUG == true ]]; then
"$debugger" "${FILE%%.*}.out"
elif [[ $COMPILE != true ]]; then
./"${FILE%%.*}.out"
fi
if [[ $PERSISTENT != true ]]; then
rm -rf ./"${FILE%%.*}.out"
rm -rf ./"${FILE%%.*}.out.dSYM"
fi
elif [[ ${FILE#*.} == cpp ]]; then
"${CXX:=g++}" ${CPPSTD} ${CFLAGS} "$FILE" -o "${FILE%%.*}.out"
if [[ $DEBUG == true ]]; then
"$debugger" "${FILE%%.*}.out"
elif [[ $COMPILE != true ]]; then
./"${FILE%%.*}.out"
fi
if [[ $PERSISTENT != true ]]; then
rm -rf ./"${FILE%%.*}.out"
rm -rf ./"${FILE%%.*}.out.dSYM"
fi
elif [[ ${FILE#*.} == py ]]; then
if [[ $pyv == "" ]]; then
## Default will be python2.
COMPILED="${FILE%%.*}.pyc"
python2 -m py_compile "$FILE" 2>/dev/null
if [[ ! -f $COMPILED ]]; then
py="python3"
dpyv=3
else
py="python2"
dpyv=2
fi
rm -rf "${FILE%%.*}.pyc" __pycache__
elif [[ $pyv == 3 ]]; then
py="python3"
dpyv=3
else
py="python2"
dpyv=2
fi
echo "${yellow}>> Using python $dpyv${white}"
if [[ $COMPILE == true ]]; then
"$py" -m py_compile "$FILE" 2>/dev/null
else
"$py" "$FILE"
fi
elif [[ ${FILE#*.} == rs ]]; then
rustc "$FILE" -o "${FILE%%.*}.out"
if [[ $COMPILE != true ]]; then
./"${FILE%%.*}.out"
fi
if [[ $PERSISTENT != true ]]; then
rm -rf ./"${FILE%%.*}.out"
fi
elif [[ ${FILE#*.} == java ]]; then
javac "$FILE"
JCF=$(find . -type f -name "*.class" -mmin -1 | cut -c 3-)
classFileCount=$(find . -type f -name "*.class" -mmin -1 | wc -l)
if [[ $COMPILE != true ]]; then
while [ "$classFileCount" -gt 0 ]
do
classFile=$(echo "$JCF" | awk "NR==$classFileCount{print}")
mainMethodCheck=$(grep -q "main" "$classFile" && echo "mainMethodFound")
if [[ $mainMethodCheck == "mainMethodFound" ]]; then
java "${classFile%%.*}"
break
fi
(( classFileCount-- ))
done
fi
if [[ $PERSISTENT != true ]]; then
find . -type f -name "*.class" -mmin -2 -exec rm -rf {} \; 2>/dev/null
fi
elif [[ ${FILE#*.} == sh ]]; then
./"$FILE"
elif [[ ${FILE#*.} == js ]]; then
if [[ $DEBUG == true ]]; then
node inspect "$FILE"
else
node "$FILE"
fi
elif [[ ${FILE#*.} == go ]]; then
if [[ $COMPILE == true ]]; then
go build -o "${FILE%%.*}.out" "$FILE"
else
go run "$FILE"
fi
if [[ $PERSISTENT != true ]]; then
rm -rf "${FILE%%.*}.out"
fi
elif [[ ${FILE#*.} == pl ]]; then
perl "$FILE"
elif [[ ${FILE#*.} == lua ]]; then
lua "$FILE"
elif [[ ${FILE#*.} == rb ]]; then
ruby "$FILE"
elif [[ ${FILE#*.} == hs ]]; then
if [[ $DEBUG == true ]]; then
ghci "$FILE"
fi
if [[ $COMPILE == true ]]; then
ghc --make "$FILE"
else
runghc "$FILE"
fi
fi
}
if [[ -z $* ]]; then
echo "$USAGE" && exit 0
fi
for arg in "$@"; do
case $arg in
"-h" | "--help")
echo "$USAGE" && exit 0
;;
"--list-langs" | "ll")
echo
echo " ${cyan}Languages supported by the currently installed version of ${red}code-runner${cyan}:"
echo " ${yellow}>>${white} C ${cyan}[${white}with ${green}debug${white} support${cyan}]"
echo " ${yellow}>>${white} C++ ${cyan}[${white}with ${green}debug${white} support${cyan}]"
echo " ${yellow}>>${white} Python"
echo " ${yellow}>>${white} Rust"
echo " ${yellow}>>${white} Java"
echo " ${yellow}>>${white} Bash-Script"
echo " ${yellow}>>${white} Javascript ${cyan}[${white}with ${green}debug${white} support${cyan}]"
echo " ${yellow}>>${white} Golang"
echo " ${yellow}>>${white} Perl"
echo " ${yellow}>>${white} Lua"
echo " ${yellow}>>${white} Ruby"
echo " ${yellow}>>${white} Haskell ${cyan}[${white}with ${green}debug${white} support${cyan}]"
echo
;;
"--version" | "v")
echo "${cyan}code-runner ${white}version : ${yellow}1.1.7${white}"
;;
"--cc="* | "-c="*)
c=${arg#*=}
if [[ -z $c ]]; then
echo "${red}ERROR: ${white}No arguments given to --cc/-c"
echo "${cyan}USAGE: ${white}--cc/-c=gcc/clang"
_EXIT_
else
export CC=$c
shift
fi
;;
"--cxx="*)
cx=${arg#*=}
if [[ -z $cx ]]; then
echo "${red}ERROR: ${white}No arguments given to --cxx"
echo "${cyan}USAGE: ${white}--cxx=g++/clang++"
_EXIT_
else
export CXX=$cx
shift
fi
;;
"--python="* | "-py="*)
pyv=${arg#*=}
if [ "$pyv" != 3 ] && [ "$pyv" != 2 ]; then
echo "${red}ERROR: ${white}Expected versions for python are 2 and 3"
_EXIT_
fi
;;
"--debug" | "d")
DEBUG=true
;;
"--persistent" | "p")
PERSISTENT=true
;;
"--compile" | "c")
COMPILE=true
PERSISTENT=true
;;
"clean")
clean
;;
"cexe")
clean_exe
;;
"rcexe")
rclean_exe
;;
*".c" | *".cpp" | *".py" | *".rs" | *".java" | *".sh" | *".js" | *".go" | *".pl" | *".lua" | *".rb" | *".hs")
FILE_LOCATION=$arg
if [[ ! -f $FILE_LOCATION ]]; then
echo "${red}ERROR: ${white}Specified file doesn't exist."
_EXIT_
fi
if [[ $FILE_LOCATION != */* ]]; then
FILE=$FILE_LOCATION
run
else
LOCATION=${FILE_LOCATION%/*}
FILE=${FILE_LOCATION##*/}
cd "$LOCATION" || exit 1
run
fi
;;
*.*)
echo "${red}ERROR: ${white}Usupported file-type specified.
To get a list of supported file-types use: ${green}run ll${white}"
_EXIT_
;;
*)
echo "${red}ERROR: ${white}Unknown argument specified.
To get a list of supported arguments use: ${green}run --help${white}"
_EXIT_
;;
esac
done