-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidsl.sh
180 lines (153 loc) · 4.22 KB
/
apidsl.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
#!/bin/bash
# https://github.com/apidsl/bash
## USAGE:
# apidsl example2.txt
# apidsl example/example3.txt
# apidsl "http("https://www.rezydent.de/").xpath("title")"
# CONTRIBUTION
## Author: Tom Sapletta
## Created Date: 01.08.2023
## CONFIG
INPUT_FILE=$1
INPUT_FOLDER=".apidsl"
COMMAND_LANGUAGE="bash"
COMMAND_FOLDER="apidsl/$COMMAND_LANGUAGE"
CACHE_FOLDER=".apidsl"
mkdir -p "$CACHE_FOLDER"
#
INPUT_FTIME="$(date +%s).txt"
INPUT_FILE_PATH="$INPUT_FOLDER/$INPUT_FTIME"
if [ ! -f "$INPUT_FILE" ]; then
# IF file not exist then create file with the input text
echo "$1" >$INPUT_FILE_PATH
else
# if file exist copy to folder
cp $INPUT_FILE $INPUT_FILE_PATH
fi
CACHE_FILE="$INPUT_FILE_PATH.cache.txt"
BASH_FILE="$INPUT_FILE_PATH.sh"
BASH_LOOP_FILE="$CACHE_FOLDER/$INPUT_FTIME.loop.sh"
#Create temporary file with new line in place
#cat $INPUT_FILE | sed -e "s/)/\n/" > $CACHE_FILE
DSL_HASH="#"
DSL_SLASHSLASH='//'
DSL_DOT="."
DSL_SEMICOLON=";"
DSL_LEFT_BRACE="("
DSL_RIGHT_BRACE=")"
DSL_RIGHT_BRACE_SEMICOLON=");"
DSL_RIGHT_BRACE_DOT=")."
DSL_NEW="\n"
DSL_EMPTY=""
DSL_LOOP="forEachLine"
## START
[ -z "$INPUT_FILE_PATH" ] && echo "INPUT_FILE is empty" && exit
echo "#!/bin/bash" >$BASH_FILE
# REMOVE COMMENTS
echo "" >$CACHE_FILE
while IFS= read -r line; do
[ -z "$line" ] && continue
#echo "${line:0:1}"
# Remove Comments
[ "${line:0:1}" == "${DSL_HASH}" ] && continue
[ "${line:0:1}" == "${DSL_SLASHSLASH}" ] && continue
echo "${line}" >>$CACHE_FILE
done <"$INPUT_FILE_PATH"
sed -i "s/${DSL_RIGHT_BRACE_DOT}/${DSL_NEW}/g" $CACHE_FILE
sed -i "s/${DSL_RIGHT_BRACE}/${DSL_NEW}/g" $CACHE_FILE
# array to hold all lines read
functions=()
values=()
# PRE processing
while IFS= read -r line; do
[ -z "$line" ] && continue
IFS="$DSL_LEFT_BRACE"
read -ra line <<<"$line"
index=0
key=""
value=""
for i in "${line[@]}"; do
index=$((index + 1))
i="$(echo -e "${i}" | tr -d '[:space:]')"
if [ $index -gt 2 ]; then
echo $index "break"
# break
fi
if [ $index == 1 ]; then
key=$i
fi
done
[ "$key" = "$i" ] && functions+=("$key") && values+=("")
[ "$key" != "$i" ] && functions+=("$key") && values+=("$i")
done <"$CACHE_FILE"
CURRENT_FOLDER=$(pwd)
length=${#functions[@]}
loop=
loop_functions=()
loop_values=()
for ((i = 0; i < ${length}; i++)); do
#echo "$i"
#echo "${functions[$i]}"
#echo "${values[$i]}"
# Replace dot to slash for path at installed packages
#key="${functions[$i]/./\/}"
key="${functions[$i]}"
IFS='.' read -a keys <<< "$key"
value="${values[$i]}"
CMD_FILE_NAME=$key
CMD_FOLDER_NAME=
[ ! -z "${keys[1]}" ] && CMD_FILE_NAME=${keys[1]} && CMD_FOLDER_NAME=/${keys[0]}
[ "$key" == "split" ] && loop="1"
#[ "$key" == "filesRecursive" ] && loop="1"
if [ -z "$loop" ]; then
echo -n "cd ${COMMAND_FOLDER}${CMD_FOLDER_NAME} && " >>$BASH_FILE
echo -n "./${CMD_FILE_NAME}.sh $value" >>$BASH_FILE
echo -n " && cd $CURRENT_FOLDER " >>$BASH_FILE
echo -n " | " >>$BASH_FILE
else
loop_functions+=("$key")
loop_values+=("$value")
fi
done
## LOOP with split function
## TODO: more loop options
## TODO: many loop in one sentence
if [ ! -z "$loop" ]; then
echo $BASH_LOOP_FILE
echo -n "./$BASH_LOOP_FILE " >>$BASH_FILE
echo "#!/bin/bash" >$BASH_LOOP_FILE
echo "IFS='' read -d '' -r list" >>$BASH_LOOP_FILE
echo 'while IFS= read -r ITEM; do' >>$BASH_LOOP_FILE
#echo ' echo "$ITEM"' >>$BASH_LOOP_FILE
length=${#loop_functions[@]}
first=1
first_val=1
for ((i = 0; i < ${length}; i++)); do
#echo "${loop_functions[$i]}"
#echo "${loop_values[$i]}"
key="${loop_functions[$i]}"
value="${loop_values[$i]}"
if [ -z "$first" ]; then
echo -n "./$COMMAND_FOLDER/$key.sh $value" >>$BASH_LOOP_FILE
echo -n ' | ' >>$BASH_LOOP_FILE
first_val=
else
#value='$ITEM'
echo -n ' ' >>$BASH_LOOP_FILE
echo -n 'echo "$ITEM" | ' >>$BASH_LOOP_FILE
#echo -n "./$COMMAND_FOLDER/$key.sh $value" >>$BASH_LOOP_FILE
#echo -n " | " >>$BASH_LOOP_FILE
fi
first=
done
truncate -s -3 $BASH_LOOP_FILE
echo "" >>$BASH_LOOP_FILE
#echo "done" >>$BASH_LOOP_FILE
echo 'done <<< "$list"' >>$BASH_LOOP_FILE
else
truncate -s -3 $BASH_FILE
fi
#cat $CACHE_FILE
#cat $BASH_FILE
#cat $BASH_LOOP_FILE
./$BASH_FILE