-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwyvern
executable file
·305 lines (260 loc) · 7.98 KB
/
wyvern
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
#!/bin/bash
USE_EXE='No' #'Yes' if on WSL and you want to use Windows compatible .exe files
WYVERN_DIR='.wyvern' # Where to put dependencies and other files wyvern needs to run
BUILD_DIR='.'
shopt -s extglob
if [[ $USE_EXE == 'Yes' ]]; then
EXE_SUFFIX=".exe"
else
EXE_SUFFIX=''
fi
mkdir -p "$WYVERN_DIR"
function dep() {
if [[ ! $(ls "$WYVERN_DIR" | grep "$1$EXE_SUFFIX") ]]; then
src="https://github.com/SteveBeeblebrox/$1/releases/download/v$2/$1$EXE_SUFFIX"
echo "Downloading $1$EXE_SUFFIX from $src..."
wget $src -P "$WYVERN_DIR" 2>/dev/null
chmod +x "$WYVERN_DIR/$1$EXE_SUFFIX"
fi
}
dep mless 4.1.2
dep mtsc 4.9.4
dep pt 1.1.0
if [[ ! $(ls "$WYVERN_DIR" | grep generator.py) ]]; then
cat << 'ENDPY' > "$WYVERN_DIR/generator.py"
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import globals as user_globals
import fileinput
import textwrap
import re
def execexpr(script, globals=None, locals=None):
import ast
stmts = list(ast.iter_child_nodes(ast.parse(script)))
if not stmts:
return None
if isinstance(stmts[-1], ast.Expr):
if len(stmts) > 1:
exec(compile(ast.Module(body=stmts[:-1],type_ignores=[]), filename="<ast>", mode="exec"), globals, locals)
return eval(compile(ast.Expression(body=stmts[-1].value), filename="<ast>", mode="eval"), globals, locals)
else:
return exec(script, globals, locals)
with fileinput.input() as file:
print(re.sub( \
r'%{(?P<content>[\s\S]*?)}', \
lambda match: str(execexpr( \
textwrap.dedent(match.group('content')), \
globals().update({ \
key: getattr(user_globals, key) \
for key in dir(user_globals) \
if not key.startswith('_') \
}), \
{} \
) or '').strip(), \
''.join([line for line in file]) \
), end='')
ENDPY
fi
if [[ ! $(ls | grep globals.py) ]]; then
touch globals.py
fi
function isnewer {
[[ $1 -nt $2 || "$FORCE_REBUILD" ]]
}
function mkfile() {
mkdir -p "$(dirname "$1")" && touch "$1"
}
function tsc() {
out="$BUILD_DIR/$("$WYVERN_DIR/pt$EXE_SUFFIX" $1 ts~js '(?<=\.m|\.)tsx?$~~js' '(?=\..?js.?$)~~.min')"
if isnewer "$1" "$out"; then
mkfile "$out"
"$WYVERN_DIR/mtsc$EXE_SUFFIX" --target=es2019 --jsx=JSX.createElement $1 --minify --out=- > $out
fi
}
function less() {
out="$BUILD_DIR/$("$WYVERN_DIR/pt$EXE_SUFFIX" $1 less~css -e css)"
if isnewer "$1" "$out"; then
mkfile "$out"
"$WYVERN_DIR/mless$EXE_SUFFIX" $1 --out= > $out
fi
}
function htmlx() {
out="$BUILD_DIR/$("$WYVERN_DIR/pt$EXE_SUFFIX" $1 -e html)"
if isnewer "$1" "$out"; then
mkfile "$out"
python3 "$WYVERN_DIR/generator.py" $1 | "$WYVERN_DIR/mtsc$EXE_SUFFIX" --verbose --html --target=es2019 --jsx=JSX.createElement --module=es2020 --out | "$WYVERN_DIR/mless$EXE_SUFFIX" --html --verbose --out= > $out
fi
}
function grc() {
out="$BUILD_DIR/$("$WYVERN_DIR/pt$EXE_SUFFIX" $1 --ext=)"
if isnewer "$1" "$out"; then
mkfile "$out"
curl "$(cat $1)?$(date +%s)" 2>/dev/null > $out
fi
}
function compile() {
filename="$(basename "$1")"
echo "Compiling $filename..."
type="${filename##*.}"
if [[ "$type" == 'ts' || "$type" == 'mts' || "$type" == 'tsx' || "$type" == 'js' || "$type" == 'mjs' ]]; then
tsc $1
elif [[ "$type" == 'less' ]]; then
less $1
elif [[ "$type" == 'htmlx' ]]; then
htmlx $1
elif [[ "$type" == 'grc' ]]; then
grc $1
else
echo "Unsupported file type: '$type' for file '$1'"
fi
echo 'Done!'
}
function lsext() {
find | grep -P '\.'$1'$' | grep -Pv '/\.'
}
POSITIONAL_ARGS=()
FORCE_REBUILD=''
if [[ $(ls | grep 'wyvern\.conf') ]]; then
echo "Reading config file..."
config=$(cat wyvern.conf)
if [[ $(echo "$config" | grep '^--out .') ]]; then
BUILD_DIR="$(echo "$config" | grep -Po '(?<=--out )[^\r\n]*')"
fi;
if [[ $(echo "$config" | grep '^--force') ]]; then
FORCE_REBUILD='YES'
fi;
fi;
# https://stackoverflow.com/a/14203146
while [[ $# -gt 0 ]]; do
case $1 in
# -e|--extension)
# EXTENSION="$2"
# shift # past argument
# shift # past value
# ;;
-o|--out)
BUILD_DIR="$2"
shift # past argument
shift # past value
;;
-f|--force)
FORCE_REBUILD='YES'
shift
;;
-*|--*)
echo "Unknown option '$1'"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
ACTION=$1
shift
if [[ "$FORCE_REBUILD" == "YES" && "$BUILD_DIR" != '.' && -n "${BUILD_DIR// /}" && -n "${BUILD_DIR//\//}" ]]; then
rm -rf "$BUILD_DIR/"
fi
mkdir -p "$BUILD_DIR"
if [ "$ACTION" == 'run' ]; then
echo "Running webserver on localhost. Stop with ^C"
(cd "$BUILD_DIR" && python3 -m http.server --bind localhost)
elif [ "$ACTION" == 'help' ]; then
cat << HELPTEXT
=== Wyvern ===
(C) 2022-2023 Trin Wasinger
No information avalible yet
HELPTEXT
elif [ "$ACTION" == 'link' ]; then
mkdir -p l
echo "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><meta http-equiv=\"refresh\" content=\"0; url=$2\"></head><body><p>Redirecting you to $2</p></body></html>" > l/$1.html
elif [ "$ACTION" == 'build' ]; then
echo "Starting..."
if [ $(ls | grep prebuild.sh) ]; then
source prebuild.sh
elif [ $(ls | grep pre-build.sh) ]; then
source pre-build.sh
fi
echo "Updating project dependencies..."
for file in $(lsext grc); do
grc $file
done
echo "Compiling Less..."
for file in $(lsext less); do
less $file
done;
echo "Compiling TypeScript..."
for file in $(lsext ts); do
tsc $file
done;
for file in $(lsext mts); do
tsc $file
done;
for file in $(lsext tsx); do
tsc $file
done;
for file in $(find | grep -P '(?<!\.min)\.m?js$' | grep -Pv '/\.'); do
tsc $file
done;
echo "Generating HTML..."
for file in $(lsext htmlx); do
htmlx $file
done;
if [[ "$BUILD_DIR" != '.' ]]; then
echo "Copying static files to build dir..."
for file in $(find -type f | grep -vP '\.(?:htmlx|py|m?tsx?|less|grc|conf)$|^\.\/\.|^\.\/(?:_site|__pycache__)\/|^\.\/wyvern$'); do
out="$BUILD_DIR/$file"
mkdir -p $(dirname $out)
cp $file "$out"
done;
fi;
echo "Generating Sitemap..."
(cd "$BUILD_DIR" && python3 <(cat << 'ENDPY'
import os
import json
def dirmap(directory_path):
directory_map ={}
for entry_name in os.listdir(directory_path):
if entry_name.startswith('.'):
continue
entry_path = os.path.join(directory_path, entry_name)
if os.path.isfile(entry_path):
directory_map[entry_name] = os.path.getmtime(entry_path)
elif os.path.isdir(entry_path):
directory_map[entry_name] = dirmap(entry_path)
return directory_map
# Sitemap
# javascript:new Date($timestamp*1000)
try:
sitemap = open("sitemap.json", "w")
json.dump(dirmap('.'), sitemap)
except:
print('Error creating sitemap!')
finally:
sitemap.close()
ENDPY
))
echo "Finishing..."
if [ $(ls | grep postbuild.sh) ]; then
source postbuild.sh
elif [ $(ls | grep post-build.sh) ]; then
source post-build.sh
fi
echo "Done!"
elif [ "$ACTION" == '' ]; then
echo "Wyvern is ready"
elif [ "$ACTION" == 'watch' ]; then
echo "Watching $1. Stop with ^C"
while true; do
inotifywait -e modify $1 &>/dev/null && compile $1
done
elif [ "$ACTION" == 'compile' ]; then
compile $1
elif [ "$ACTION" == 'ghpages' ]; then
echo "GitHub Pages support NYI!"
else
echo "Unknown action '$ACTION'"
fi