-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·121 lines (109 loc) · 3.17 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
#!/bin/bash
src="/usr/local/src/run/src"
importFunctions(){
local filename="$1";
if [[ ! -f "${src}/${filename}" ]];then
echo "something went wrong, please try again";
exit 9;
fi
source "${src}/${filename}"
func=$2
if [[ -n "${func}" ]];then
shift 2;
"${func}" "$@";
fi
}
checkfunction(){
if [ "$(type -t "$1")" = "function" ]; then
echo "true"
else
echo "false"
fi
}
deleteFile() {
local file
if [[ "${deleteExecutable}" == "true" ]];then
for file in "$@"; do
rm -f "$file"
done
fi
}
declare -A my_dict #caches
main() {
local file="$1"
local extension="${file##*.}"
local fwe="${file%.*}"
if [[ ! -f ${file} ]];then
importFunctions "functions.sh" "recommd_command" "run ${file}"
echo "file not found: ${file}"
return
fi
if [[ ! -f "${src}/languages/run_${extension}.sh" ]];then
echo "Error: Unsupported file type: $file"
return
fi
check=$(checkfunction "run_${extension}");
if [[ ! -v my_dict["${extension}"] ]];then
source "${src}/languages/run_${extension}.sh";
my_dict["${extension}"]=$(declare -f run_lang)
else
eval "${my_dict["${extension}"]}"
fi
run_lang "${file}" "${fwe}" "${arguments}"
}
if [[ "${1:0:2}" == "--" ]]; then
importFunctions "functions.sh" "double_dash_functions" "$@"
fi
deleteExecutable="false"
multipleFiles="false" #run multiple files
comment="main" #comments while executing multiple files
arguments=""
while getopts ":dmtcb:i:ha:" opt; do
case ${opt} in
d ) deleteExecutable="true" ;;
a ) arguments="$OPTARG" ;;
i )importFunctions "install.sh" "install_packages" "$OPTARG";
exit ;;
m ) multipleFiles="true" ;;
t ) importFunctions "functions.sh" "latest_file_run" "$@";;
b) importFunctions "functions.sh" "run_buddle" "$@"; exit ;;
h) importFunctions "functions.sh" "double_dash_functions" "--help";exit ;;
c ) comment="comment";;
\? ) importFunctions "functions.sh" "recommd_command" "run ${@}";echo "Invalid option: $OPTARG please refer run --help" 1>&2; exit 3 ;;
: ) echo "Option -$OPTARG requires an argument." 1>&2; exit 3 ;;
esac
done
if [[ -z ${@} ]];then
echo "Please ,provide the filename/filepath";
exit 4;
fi
shift $((OPTIND -1)) ;
terminal_width=""
comment(){
terminal_width=${terminal_width:-$(tput cols)};
echo -e "\e[1;4;31m${1}\e[0m\n";
main "$1";
printf '%*s\n' "${terminal_width}" | tr ' ' '-'
}
if [ "${multipleFiles}" == "true" ]; then
if [ "$#" != 1 ];then
echo "too many argumetns only one needed that is pattren.
Once try again removing -m option or use cotation for pattern";
exit 3
fi
cd "$(dirname "${1}")" || exit
OLD_IFS=$IFS
IFS=$'\n'
for f in $(ls -p | grep -v / |
grep -E "$(basename "${1}")" | grep -v -E '\.out$|\.o$');do
${comment} "$f";
done
IFS=$OLD_IFS
else
current_path="$(pwd)"
for f in "$@";do
cd "$(dirname "${f}")" || exit
${comment} "$(basename "${f}")";
cd "${current_path}";
done
fi