-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss
More file actions
executable file
·353 lines (326 loc) · 8.08 KB
/
oss
File metadata and controls
executable file
·353 lines (326 loc) · 8.08 KB
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
#!/bin/bash
OLDPWD=$PWD
THISDIR=$PWD
if [[ ! -f "$THISDIR/oss" ]]; then
THISDIR=${0%/oss}
cd $THISDIR
fi
SRC=$THISDIR/src
BASHDIR=$THISDIR/bash-stuff
TEMPLATE=$BASHDIR/template.bash
COLORIZE=$BASHDIR/colorize.bash
THREAD=$BASHDIR/thread.bash
STATIC=$SRC/static-listing.txt
EDITOR="emacsclient -t" # Let's face it, you should be using this anyways :P
BROWSER=${BROWSER:-firefox}
ROOTDIR=${ROOTDIR:-.}
OUTPUTZIP=$THISDIR/Game-to-Code.zip
ZIP_LOOP_SLEEP=${ZIP_LOOP_SLEEP:-30m}
if [[ -z $GITUSER ]]; then
case ${USER,,} in
tanner|thobson|azunyan) GITUSER=player1537;;
nascar) GITUSER=Nascar;;
andy) GITUSER=arw12625;;
esac
fi
source $COLORIZE || function color-dir() { echo "$@"; }
source $THREAD && init
## lsr
# lsr directory
# prints the results of ls -R in a nicer way
# See also: https://github.com/player1537/bash-stuff/blob/master/ls-R.bash
function lsr() {
ls -R "$@" | sed -e '
# Delete empty lines
/^[ \t]*$/d;
# If the line is a directory, store to the hold buffer
/^[^ \t].*:$/ {
s/^\.\([a-z]*\):$/\1/;
s/^\.\///; s/:$/\//;
h;
d;
};
# Grab the hold buffer (a path)
G;
# Join it with the current filename
s/^\(.*\)\n\(.*\)/\2\1/
'
}
## cleandirs
# cleandirs directory
# runs clean in every directory and subdirectory in directory
function cleandirs() {
local dir
dir=$1
(
cd $dir
clean
for dir in *; do
if [[ -d $dir ]]; then
cleandirs "$dir"
fi
done
)
}
## clean
# clean
# removes all backup files /*~$/
function clean() {
local file
for file in *~; do
if [[ -a $file ]]; then
rm *~
return
fi
done
}
## install
# source oss install
# adds oss to your path
function install() {
export PATH=$PATH:$THISDIR
}
## compile-parallel
# oss compile-parallel
# Compiles everything in parallel, basically does the same
#+ thing as just compile (but hopefully faster).
function compile-parallel() {
local outdir pool file
outdir=$THISDIR/output
pool=compile-parallel
compile-clean-output-dir
cd $SRC
for file in $(lsr $SRC | egrep "\.tpl$"); do
spawn-thread $pool compile-one $file
done
join-pool $pool
compile-copy-static
}
## compile
# oss compile
# compiles all the code in the source directory
function compile() {
local file line outputfile outdir
outdir=$THISDIR/output
compile-clean-output-dir
cd $SRC
for file in $(lsr $SRC | egrep "\.tpl$"); do
compile-one "$file"
done
compile-copy-static
}
## compile-save-blockly
# compile-save-blockly
# Just... saves blockly to the current directory, so that
#+ compiling is faster. Uses $outdir in scope.
function compile-save-blockly() {
if [[ -e $outdir/blockly ]]; then
echo saving blockly
mv $outdir/blockly ./blockly
fi
}
function compile-restore-blockly() {
if [[ -e ./blockly ]]; then
echo restoring blockly
mv ./blockly $outdir/blockly
fi
}
## compile-clean-output-dir
# compile-clean-output-dir
# This just clears out output/ so that we have a normalized
#+ compiling sequence, and it's not different each time.
function compile-clean-output-dir() {
compile-save-blockly
rm -rf $outdir
mkdir -p $outdir
compile-restore-blockly
}
## compile-copy-static
# compile-copy-static
# Reads through the static-listing.txt file and copies each
#+ directory/file over to output/. $outdir passed in scope
function compile-copy-static() {
local file
while true; do
read file || break
if [[ -n $file ]]; then
if [[ $file =~ ^blockly$ ]] && [[ -e $outdir/blockly ]]; then
echo Skipping blockly-like file, $file
continue
fi
echo Copying $SRC/$file to $outdir/$file
cp $SRC/$file $outdir/$file -r
fi
done < $STATIC
}
## compile-one
# compile-one /full/path/to/file.tpl
# compile() uses this to only compile each file. It's moved to a
#+ sepearate function to be able to generate a single file without
#+ having to do the rest. A clean compile should be done every so
#+ often to make sure it all is correct.
function compile-one() {
local file line outdir outputfile
outdir=$THISDIR/output
for file; do
file=${file:?File must be set}
read line <$file
if [[ $line =~ ^\#!template ]]; then
outputfile=${line#\#!template }
echo Compiling "$(color-dir ${file#$THISDIR/})" to "$(color-dir $outputfile)"
ROOTDIR=$ROOTDIR OUTPUTDIR=$outdir $TEMPLATE $outputfile "$file"
fi
done
}
## compile-one-wrapper
# compile-one-wrapper file.tpl
# compile-one needs its arg t have a full path, so this
#+ ensures that. It also makes sure it's in the right
#+ directory
function compile-one-wrapper() {
local file
file=${1:?File must be set}
file=$PWD/$file
cd $SRC
compile-one "$file"
}
## template
# oss template <template.bash's args>
# proxies to template
function template() {
cd $SRC
exec $TEMPLATE "$@"
}
## edit
# oss edit {t=>template,o=>oss,l=>output}
# opens one of these files with $EDITOR
function edit() {
local whatdo file
whatdo=$1
if [[ -n $2 ]]; then
#file=$(find $THISDIR/output/ -name "$2*")
#echo "$file"
file=$2*
fi
if [[ -a $whatdo ]]; then
exec $EDITOR $whatdo
fi
case $whatdo in
t*) edit $TEMPLATE;;
o*) edit $THISDIR/oss;;
l*) edit $THISDIR/output/$file;;
*) echo "No such file or shortcut, '$whatdo'";;
esac
}
## ls-tpl
# ls-tpl {args for ls}
# does the equivalent of `ls *.tpl` so it's easier to
#+ find template files instead of having lots of extra
#+ HTML files
function ls-tpl() {
if [[ -z "$1" ]]; then
(
cd $OLDPWD
ls *.tpl
)
else
ls "$@"
fi
}
## view-output
# view-output
# Opens output/index.html in $BROWSER
function view-output() {
local outputdir
outputdir=$THISDIR/output
$BROWSER $outputdir/index.html
}
## list
# list
# grabs all the different commands from this file and
#+ prints them out, separated by commas
function list() {
sed $THISDIR/oss -ne '/case x$whatdo/,/esac/ { s/[ \t]*x\([a-z][a-z]*\).*/\1/p; }' | tr "\n" ","
}
## print-help
# oss help
# prints the documentation from this project
function print-help() {
sed $THISDIR/oss -ne '/^#/ { /^#!/ !p; }'
}
function create-zip() (
echo Creating zip file
cd $THISDIR/output
zip -v $OUTPUTZIP -r ./*
echo Finished creating zip file
)
function zip-loop() {
while true; do
echo Pulling
git pull || break
echo Compiling
oss c || break
oss z || break
echo Copying zip to /www/
cp $OUTPUTZIP /www/download.zip
echo sleeping $ZIP_LOOP_SLEEP
sleep $ZIP_LOOP_SLEEP || break
done
}
## never-fail-zip-loop
# never-fail-zip-loop
# This will ensure that, even if the normal
#+ zip loop fails, it will continue
#+ updating. This way we don't run into an
#+ issue where the server stops updating
#+ the zip file.
function never-fail-zip-loop() {
while true; do
zip-loop
echo zip-loop died for some reason... Sleeping 5m
sleep 5m
done
}
## add-git-user-to-git-config
# add-git-user-to-git-config
# Very hackily adds your username to your
#+ .git/config file. Probably shouldn't
#+ rely on it too much.
function add-git-user-to-git-config() {
local gituser_plus_at
gituser_plus_at=$GITUSER${GITUSER:+@}
cp $THISDIR/.git/{config,config.backup}
sed $THISDIR/.git/config -i -e '/\[remote "origin"\]/,/\[/ s/\(url = https:\/\/\).*\(github.com*\)/\1'$gituser_plus_at'\2/'
}
## remove-git-user-from-git-config
# remove-git-user-from-git-config
# Very hackily removes your username from
#+ .git/config.
function remove-git-user-from-git-config() (
export GITUSER=
add-git-user-to-git-config
)
function main() {
local whatdo
whatdo=$1; shift
case x$whatdo in
xinstall|xi) install "$@";;
xcompile|xc) compile "$@";;
xone-compile|xcc) compile-one-wrapper "$@";;
xcompile-parallel|xcp) compile-parallel "$@";;
xtemplate|xt) template "$@";;
xedit|xe) edit "$@";;
xrunthis|xr) eval "$@";;
xclean|xcl) cleandirs $SRC;;
xls|xls) ls-tpl "$@";;
xview|xv) view-output "$@";;
xzip|xz) create-zip "$@";;
xadd-git|xag) add-git-user-to-git-config "$@";;
xrm-git|xrg) remove-git-user-from-git-config "$@";;
xzip-loop|xzl) zip-loop "$@";;
xzip-loop-loop|xzL) never-fail-zip-loop "$@";;
xhelp|xh) print-help "$@";;
x) echo "Pass a command: {$(list)}";;
esac
}
main "$@"