-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboiler.sh
executable file
Β·542 lines (515 loc) Β· 16.1 KB
/
boiler.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/bin/bash
includeFiles=()
languageOfIncludeFiles=()
addToFiles=()
languageOfAddToFiles=()
forFlagInput=()
includeFlagInput=()
languages=( php css html c js java )
commandInput=($*)
[[ ${#commandInput[@]} -eq 0 ]] && exit 1
sizeOfCommand=${#commandInput[@]}
language="${commandInput[0]}"
nameOfFile="${commandInput[-1]}"
include=()
#launch to be used by both excercise feature and boiler creation feater
#last minute change
#making editor visible to the networking functions
editor=
function launch(){
editor=$1
case "$editor" in
"vim")
vim "$2"
;;
"vi")
vi "$2"
;;
"pico")
pico "$2"
;;
"nano")
nano "$2"
;;
"kate")
kate "$2"
;;
esac
printf "FILE:\n %s \nHAS BEEN CREATED\n" "$2"
}
#
# BOILER PLATE CREATOR FUNCTIONS
#
function printC(){
index=0
for l in ${languageOfIncludeFiles[@]};do
if [[ $l == "c" ]];then
printf "#include<%s>\n" "${includeFiles[$index]}"
fi
let index+=1
done
}
function createC(){
printf "#include<stdio.h>\n#include<stdlib.h>\n#include<string.h>\n"
[[ $# -eq 1 ]] && printf "/*%s*/\n" "$1"
printC
printf "int main(int argc, char *argv[]){\n}"
}
function createPython(){
[[ $# -eq 1 ]] && printf "\"\"\"%s\"\"\"\n" "$1"
}
function createJava(){
[[ $# -eq 1 ]] && printf "/*%s*/" "$1"
printf "class $nameOfFIile\n{\n\tpublic static void main(String[] args)\n\t{\n\t}\n}"
}
function printPHP(){
index=0
for l in ${languageOfIncludeFiles[@]};do
if [[ $l == "php" ]];then
printf "\n include '%s'" "${includeFiles[$index]}"
fi
let index+=1
done
}
function createPhp(){
printf "<?php\n"
[[ $# -eq 1 ]] && printf "/*%s*/\n" "$1"
printPHP
printf "\n//php CODE HERE\n?>"
}
function createJavascript(){
question="//$1"
printf "$question"
}
#NOTE: printCSS & printJS function are for html file linking
function printCSS(){
index=0
for l in ${languageOfIncludeFiles[@]};do
if [[ $l == "css" ]];then
printf "\n <link rel='stylesheet' href='%s'>" "${includeFiles[$index]}"
fi
let index+=1
done
}
function printJS(){
index=0
for l in ${languageOfIncludeFiles[@]};do
if [[ $l == "js" ]];then
printf "\n<script src='%s'></script>" "${includeFiles[$index]}"
fi
let index+=1
done
}
function createHtml(){
printf "<!DOCTYPE html>\n<html>\n<head>"
printCSS
printf "\n <title>Page Title</title>\n</head>\n<body>\n <!--Code -->"
printJS
printf "\n</body>\n</html>"
}
function createCss(){
printf ""
}
#
# END OF BOILER PLATE CREATOR FUNCTIONS
#
#
# TODOS
# IMPLEMENT INOTIFY ON THE CREATED FILE EXCERCISE QUESTION
#
# FUTURE
# add multiple languages!
#
#
# NETWORKING EXCERICISE FUNCTIONS
#
links=()
excerciseTopics=()
indexOfChoosenExcecise=
usersEditor=
function getTopicFrom(){
ln=$1
ln=$(echo "$ln" | cut -d / -f 3)
ln=$(echo "$ln" | cut -d . -f 1)
echo "$ln"
}
function createLinks(){
betaLinks=$1
webRoot="https://www.w3resource.com"
for betaLink in ${betaLinks[@]};do
excerciseTopics+=($(getTopicFrom "$betaLink"))
betaLink=$(echo "$betaLink"|tr -d \")
links+=($webRoot$betaLink)
done
}
function getExcerciseTopics(){
if [[ $language == "js" ]];then
webData="$(wget https://www.w3resource.com/javascript-exercises/ -q -O -)"
betaLinks="$(echo "$webData" | grep '.*<li><a href="/javascript-exercises.*' )"
betaLinks="$(echo "$betaLinks" | sed 's|.*href=||')"
betaLinks=($(echo "$betaLinks" | sed 's|>.*||'))
createLinks $betaLinks
elif [[ $language == "c" ]];then
webData="$(wget https://www.w3resource.com/c-programming-exercises/ -q -O -)"
betaLinks="$(echo "$webData" | grep '<li><a href=".*/c-programming-exercises.*' )"
betaLinks=${betaLinks//https:\/\/www.w3resource.com/}
betaLinks="$(echo "$betaLinks" | sed 's|.*href=||')"
betaLinks=($(echo "$betaLinks" | sed 's|>.*||'))
createLinks $betaLinks
elif [[ $language == "php" ]];then
webData="$(wget https://www.w3resource.com/php-exercises/ -q -O -)"
betaLinks="$(echo "$webData" | grep '<li><a href=".*/php-exercises.*' )"
betaLinks=${betaLinks//https:\/\/www.w3resource.com/}
betaLinks="$(echo "$betaLinks" | sed 's|.*href=||')"
betaLinks=($(echo "$betaLinks" | sed 's|>.*||'))
createLinks $betaLinks
elif [[ $language == "python" ]];then
webData="$(wget https://www.w3resource.com/python-exercises/ -q -O -)"
betaLinks="$(echo "$webData" | grep '<li><a href=".*/python-exercises.*' )"
betaLinks=${betaLinks//https:\/\/www.w3resource.com/}
betaLinks="$(echo "$betaLinks" | sed 's|.*href=||')"
betaLinks=($(echo "$betaLinks" | sed 's|>.*||'))
createLinks $betaLinks
#TO DO FIGURE OUT PYTHON IMPLEMENTATION
elif [[ $language == "java" ]];then
webData="$(wget https://www.w3resource.com/java-exercises/ -q -O -)"
betaLinks="$(echo "$webData" | grep '<li><a href=".*/java-exercises.*' )"
betaLinks=${betaLinks//https:\/\/www.w3resource.com/}
betaLinks="$(echo "$betaLinks" | sed 's|.*href=||')"
betaLinks=($(echo "$betaLinks" | sed 's|>.*||'))
createLinks $betaLinks
fi
count=0
}
function promptExcercises(){
count=0
for ln in "${excerciseTopics[@]}";do
printf "%d->%s\n" "$count" "$ln"
let count++
done
}
function getExcerciseQuestion(){
oldIFS="$IFS"
IFS=$'\n'
let questionNumber="$2"-1
excerciseLink="$1"
excerciseWebData="$(wget $excerciseLink -q -O -)"
excerciseWebData=$(echo "$excerciseWebData" | grep -E '<p.*' | grep -E '<strong>[0-9]' | sed -e 's/<[^>]*>//g' | sed 's|Go to the editor||g')
excerciseWebData=($(echo "$excerciseWebData"))
echo "${excerciseWebData[$questionNumber]}"
IFS="$oldIFS"
}
function collectUnansweredQuestionNumber(){
topic="$1"
statisticFile="~/\.boilerExcerciseStatistics"
if grep "$topic" ~/\.boilerExcerciseStatistics &> /dev/null;then
statsForTopic=$(grep "$topic" ~/\.boilerExcerciseStatistics)
oldIFS=$IFS
IFS="@"
questions=($(grep "$1" ~/\.boilerExcerciseStatistics))
sed -i "s|$statsForTopic|DELETE|g" ~/\.boilerExcerciseStatistics
sed -i '/DELETE/d' ~/\.boilerExcerciseStatistics
statsForTopic="$statsForTopic""@""${#questions[@]}"
echo "$statsForTopic" >> ~/\.boilerExcerciseStatistics
let question="${#questions[@]}"
echo "$question"
IFS=$oldIFS
else
echo "$topic""@1" >> ~/\.boilerExcerciseStatistics
echo "1"
fi
}
function createQuestionFile(){
topic="$1"
question="$2"
questionNumber=$(echo "$question" | head -c 1)
qFileName="$questionNumber""${excerciseTopics[$indexOfChoosenExcecise]}"
if [[ "$language" == "js" ]];then
qFileName="$qFileName"".js"
createJavascript "$question" >> "$qFileName"
elif [[ "$language" == "c" ]];then
createC "$question" >> "$questionNumber""${excerciseTopics[$indexOfChoosenExcecise]}"".c"
qFileName="$qFileName"".c"
elif [[ "$language" == "php" ]];then
createPhp "$question" >> "$questionNumber""${excerciseTopics[$indexOfChoosenExcecise]}"".php"
qFileName="$qFileName"".php"
elif [[ "$language" == "python" ]];then
createPython "$question" >> "$questionNumber""${excerciseTopics[$indexOfChoosenExcecise]}"".py"
qFileName="$qFileName"".py"
elif [[ "$language" == "java" ]];then
createJava "$question" >> "$questionNumber""${excerciseTopics[$indexOfChoosenExcecise]}"".java"
qFileName="$qFileName"".java"
fi
topic="$1"
launch "$editor" "$qFileName"
}
ChoosenTopicUrl=""
questionNumber=
function createExcerciseFile(){
ChoosenTopicUrl=$1
questionNumber=$(collectUnansweredQuestionNumber "$ChoosenTopicUrl")
question=$(getExcerciseQuestion "$ChoosenTopicUrl" "$questionNumber")
createQuestionFile "$ChoosenTopicUrl" "$question"
}
function handleChoosenExcercise(){
choosen=$1
numberOfExcercises=${#links[@]}
if [[ $choosen -gt $numberOfExcercises ]] || [[ $choosen -lt 0 ]] || [[ $choosen -eq $numberOfExcercises ]];then
echo "Sorry Choice Invalid File Excercise Not Created"
exit 1
fi
#Passing Actual URL
createExcerciseFile "${links[$choosen]}"
}
function initExcercise(){
getExcerciseTopics
if [[ $language == "js" ]];then
printf "you have choosen JS EXERCISES please Choose By Number\n------------------\n"
elif [[ $language == "c" ]];then
printf "You Have Choosen C Exercises please Choose By Number\n-------------------\n"
elif [[ $language == "php" ]];then
printf " You HaveChoosen PHP Exercises Please Choose By Number\n-----------------\n"
elif [[ $language == "python" ]];then
printf " You HaveChoosen PYTHON Exercises Please Choose By Number\n-----------------\n"
printf "NOTE: python practice might have bugs\n"
fi
promptExcercises
read -p 'CHOICE > ' choosenExcercise
indexOfChoosenExcecise=$choosenExcercise
handleChoosenExcercise "$choosenExcercise"
}
#
# END OF NETWORKING EXCERCISE FEATURE
#
function printError(){
printf "INVALID COMMAND USAGE run boiler -help for guidance\n"
exit 1
}
function indexFileByLanguage(){
whatArray=$2
if [[ ! $1 =~ .*\..* ]];then
printf "FILE:%s HAS NO EXTENSION NAME\n" $1
exit
fi
lang=$(printf "%s" $1 | cut -d '.' -f 2)
if [[ $whatArray == "include" ]];then
languageOfIncludeFiles+=($lang)
elif [[ $whatArray == "addto" ]];then
languageOfAddToFiles+=($lang)
fi
}
function getFlagInputAtIndex(){
indexOffset=$1
flag=$2
[[ "$flag" == "-include" ]] && endPoint="-addto"
[[ "$flag" == "-addto" ]] && endPoint="-include"
for (( i=$indexOffset ; i < $sizeOfCommand - 1; i++ ));do
[[ "${commandInput[$i]}" == "$endPoint" ]] && break
if [[ $flag == "-include" ]];then
includeFiles+=(${commandInput[$i]})
indexFileByLanguage ${commandInput[$i]} "include"
elif [[ $flag == "-addto" ]];then
addToFiles+=(${commandInput[$i]})
indexFileByLanguage ${commandInput[$i]} "addto"
fi
done
if [[ $flag == "-include" ]];then
[[ ${#includeFiles[@]} -eq 0 ]] && printError
elif [[ $flag == "-addto" ]];then
[[ ${#addToFiles[@]} -eq 0 ]] && printError
fi
}
function pursueOption(){
flag=$1
readyForArgs="false"
currentIndex=0
for com in ${commandInput[@]};do
[[ $currentIndex -eq $sizeOfCommand ]] && break
if [[ "$readyForArgs" == "true" ]];then
let readyForArgs="done"
getFlagInputAtIndex $currentIndex $flag
fi
if [[ "$com" == "$flag" ]];then
readyForArgs="true"
fi
let currentIndex+=1
done
}
function printHelpMessage(){
#sorry for the long printf
printf "Create Boiler Files\nUSAGE: boiler \e[4mlanguageOfBoiler\e[0m [option] [FileName]\n\t-ex\t CREATES EXERCISE FILE W/ QUESTION \e[4mNO FILE NAME NEEDED\e[0m\n\t-addTo <files>\tadd Boiler To <files>\n\t-include <files>\tinclude <files> in boiler file\n\t-help\t help on boiler command usage\nNOTE: -addto & -include file(s) must have an extension (i.e. somefile.js somefile.php)\nEXAMPLE:\n\t>boiler html -include *.js *.css app\n\t-Creates an html file called app.html that has all \n\tthe .js files and .css files linked onto it\n\t>boiler js -ex\n\t-Creates a javascript exercise file\n\t>boiler c example1\n\t-Creates a c file named example1.c with all the \n\tdefault libraries and a main function\n"
exit 0
}
function initCommandArguments(){
if [[ $sizeOfCommand -eq 0 ]];then
printError
exit 1
elif [[ $sizeOfCommand -eq 1 ]];then
[[ ${commandInput[0]} == "-help" ]] || printError
printHelpMessage
elif [[ $sizeOfCommand -eq 2 ]];then
if [[ ${commandInput[-1]} == "-ex" ]];then
initExcercise
exit 0
fi
elif [[ $sizeOfCommand -gt 2 ]];then
if [[ "${commandInput[1]}" == "-include" ]];then
pursueOption "-addto"
getFlagInputAtIndex 2 "-include"
elif [[ "${commandInput[1]}" == "-addto" ]];then
pursueOption "-include"
getFlagInputAtIndex 2 "-addto"
else
printError
fi
fi
}
function validLanguage(){
#Minor Fix
[[ ${commandInput[-1]} == "-help" ]] && return 0
for l in ${languages[@]};do
if [[ "$language" == "$l" ]];then
return 0
fi
done
return 1
}
function checkDuplicate(){
for file in *;do
if [[ $file == "$nameOfFile.$language" ]];then
printf "File Name: %s.%s Already Exists\n" $nameOfFIile $language
exit 1
fi
done
}
function checkCommandForValidFileName(){
if [[ ${commandInput[-1]} =~ .*\..* ]];then
printf "INVALID COMMAND USAGE\n%s can not contain a extension\nor no file name was given\n\nboiler -help for more info\n" $nameOfFile
exit 1
fi
}
function initialErrorChecking(){
checkCommandForValidFileName
if ! validLanguage;then
printf "boiler: Language %s is not found\nuse boiler -help for info on boiler command\n" $language
exit 1
fi
checkDuplicate
}
function checkAllAddToFilesExist(){
for aFile in ${addToFiles[@]};do
if [[ ! -f $aFile ]];then
printf "add to file %s doesnt exist\n" $aFile
exit 1
fi
done
}
function addLinkTo(){
fileToAddLink=$1
languageOfTheFile=$2
boilerLanguage=$3
#languageofthefile where links will be added
if [[ $languageOfTheFile == "html" ]];then
if [[ $boilerLanguage == "js" ]];then
sed -i 's|</body>|\ninsert\n</body>|g' $fileToAddLink
link="<script src='$nameOfFile'></script>"
commd="s|insert|"$link"|g"
sed -i "$commd" $fileToAddLink
elif [[ $boilerLanguage == "css" ]];then
sed -i 's|<head>|<head>\ninsert|g' $fileToAddLink
link="<link rel='stylesheet' href='"$nameOfFile"'></link>"
commd="s|insert|"$link"|g"
sed -i "$commd" $fileToAddLink
fi
elif [[ $languageOfTheFile == "php" ]];then
if [[ $boilerLanguage == "php" ]];then
firstLine=$(head -n 1 $fileToAddLink)
commd="s|"$firstLine"|"$firstLine"INSERTME|g"
sed -i "$commd" $fileToAddLink
link="include '"$nameOfTheFile"'\n"
commd="s|INSERTME|"$link"|g"
sed -i "$commd" $fileToAddLink
else
printf "PHP FILE CAN NOT BE LINKED TO OTHER SOURCES"
exit 1
fi
fi
}
function implementAddToFlag(){
index=0
for f in ${addToFiles[@]};do
addLinkTo $f ${languageOfAddToFiles[$index]} $language
let index+=1
done
}
function addToFilesFor(){
if [[ ${#addToFiles[@]} -gt 0 ]];then
checkAllAddToFilesExist
implementAddToFlag
fi
}
editors=( vim vi pico nano kate )
function configureBoilerSettings(){
if [[ $1 -lt 0 || $1 -ge ${#editors[@]} ]];then
printf "YOU MUST CHOOSE BETWEEN LIST GIVEN\n"
exit 1
else
printf "autoEditor:true\neditor:%s" "${editors[$1]}" >> ~/.boilersettings
fi
}
function launchEditor(){
autoEditorSetting=`grep "autoEditor" $HOME/\.boilersettings`
if [ -z $autoEditorSetting ];then
printf "Would you Like To Auto Launch a specified Editor On Boiler Creation?(y/n)\n"
read input
if [[ $input == "y" ]];then
printf "What Editor Would You Like To Launch Automatically?\n"
printf "CHOOSE BY NUMBER\n0->vim\n1->vi\n2->pico\n3->nano\n4->kate\n"
read editorIndexChoosen
printf "Are you sure?\n"
read checker
if [[ $checker == "y" || $checker == "yes" ]];then
configureBoilerSettings $editorIndexChoosen
elif [[ $checker == "n" || $checker == "no" ]];then
launchEditor
else
printf "INPROPER INPUT $checker\n"
exit 0
fi
elif [[ $input == "n" ]];then
printf "Auto Editor Setting Disable, you may edit the .boilersetting file in your home directory if you wish to enable\n"
fi
else
if [[ $autoEditorSetting = *"true"* ]];then
usersEditor=`grep 'editor' $HOME/.boilersettings | cut -d':' -f2`
launch "$usersEditor" "$1"
fi
fi
}
function handleLanguage(){
boilerLanguage=$1
nameOfFile="$nameOfFile.$boilerLanguage"
if [[ "$boilerLanguage" == "php" ]];then
createPhp >> "$nameOfFile"
elif [[ "$boilerLanguage" == "c" ]];then
createC >> "$nameOfFile"
elif [[ "$boilerLanguage" == "js" ]];then
createJavascript >> "$nameOfFile"
elif [[ "$boilerLanguage" == "html" ]];then
createHtml >> "$nameOfFile"
elif [[ "$boilerLanguage" == "node" ]];then
createNode >> "$nameOfFile"
elif [[ "$boilerLanguage" == "css" ]];then
createCss >> "$nameOfFile"
elif [[ "$boilerLanguage" == "java" ]];then
createJava >> "$nameOfFile"
fi
addToFilesFor $boilerLanguage
launchEditor "$nameOfFile"
}
function main(){
initialErrorChecking
initCommandArguments
handleLanguage "$language"
}
main