-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathafn-app.sh
executable file
·302 lines (239 loc) · 7.57 KB
/
afn-app.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
#!/bin/bash
set -o nounset -o pipefail -o errexit
WEBDEV="${1:-}"
function is_webdev() {
if [ "$WEBDEV" == "webdev" ]; then
return 0
else
return 1
fi
}
function emptyfile() {
truncate -s 0 $1
}
function add_template() {
add_to_file "$1" "$2" ""
}
function add_js_asset() {
add_to_file "$1" "$2" ";"
}
function add_css_asset() {
add_to_file "$1" "$2" ""
}
function add_to_file() {
to_add="$1"
add_to="$2"
separator="$3"
echo "$separator" >> $add_to
cat $to_add >> $add_to
echo "$separator" >> $add_to
}
export -f add_js_asset
export -f add_css_asset
export -f add_template
export -f add_to_file
function add_min_prefix() {
file=$1
ext="${file##*.}"
filename="${file%.*}"
echo -n "${filename}.min.${ext}"
}
export -f add_min_prefix
RUNNING_DIR=$(pwd)
if ! [ -z $AFN_BUILD_DIR ]; then
echo "Building app into $AFN_BUILD_DIR"
COMPILED_APP="$AFN_BUILD_DIR"
else
COMPILED_APP="$RUNNING_DIR/tmp/app-compiled"
fi
SHOULD_RESET_FILE="$RUNNING_DIR/tmp/should_reset"
WEB_PID_FILE="$RUNNING_DIR/tmp/web.pid"
APP_VERSION_ID=`date | sha1sum -t | awk '{print $1}'`
APP_VERSION="dyn"
APP_COMMIT_ID=${APP_COMMIT_ID:-"N/A"}
APPLICATION_CSS="$COMPILED_APP/assets/application-$APP_VERSION_ID.css"
APPLICATION_JS="$COMPILED_APP/assets/application-$APP_VERSION_ID.js"
# Delete the cache directory
rm -fr tmp/cache/
function should_reset() {
#### NOTE - this uses process returns code so 0 means it should reset while 1 means it doesn't so it fits in the logic of a bash if
if [ -e $SHOULD_RESET_FILE ]; then
return 0
else
return 1
fi
}
function ace_js() {
cd bower_components/ace-anyfile-notepad
if ! find -maxdepth 1 -name 'afn-dist' -type d | egrep '.*' > /dev/null ; then
npm install
make afn-dist
fi
cd -
mkdir $COMPILED_APP/ace.js
cp -a bower_components/ace-anyfile-notepad/afn-dist/* $COMPILED_APP/ace.js/
}
function pages_css() {
# pages.css
echo "Building pages.css"
if should_reset; then
echo "Resetting pages.css"
rm -f assets/css/libs/bootstrap.min.css.scss
rm -f $COMPILED_APP/assets/pages-*.css
fi
cp bower_components/bootstrap/dist/css/bootstrap.min.css assets/css/libs/bootstrap.min.css.scss
./node_modules/.bin/node-sass --include-path assets/css/ assets/css/pages.css.scss >> $COMPILED_APP/assets/pages-$APP_VERSION_ID.css
rm -f assets/css/libs/bootstrap.min.css.scss
}
function pages() {
# Build website
echo "Building site pages"
if should_reset; then
echo "Resetting pages"
rm -fr $COMPILED_APP/site
rm -f $COMPILED_APP/index.html
fi
mkdir $COMPILED_APP/site
for page in home faq news help_translate privacy_policy payment-success payment-failure terms-of-service email-cancel contact blocked_user; do
echo "-Building page $page"
if [ "$page" == "home" ]; then
COLUMNS=3
else
COLUMNS=1
fi
perl -MTemplate -e "\$tt = Template->new({INCLUDE_PATH => ['$COMPILED_APP', '.']}) ; \$tt->process('site/layout.tt', {WEBDEV => '$WEBDEV', APP_VERSION_ID => '$APP_VERSION_ID', APP_VERSION => '$APP_VERSION', APP_COMMIT_ID => '$APP_COMMIT_ID', PAGE_KEY => '$page', COLUMNS => $COLUMNS}, '$COMPILED_APP/site/$page.html') || die \$tt->error()"
done
cp $COMPILED_APP/site/home.html $COMPILED_APP/index.html
if is_webdev; then
cp -a pages $COMPILED_APP/
fi
}
function application_css() {
# application.css
echo "Building application.css"
if should_reset; then
echo "Resetting application.css"
rm -f $COMPILED_APP/assets/application-*.css
fi
add_css_asset bower_components/bootstrap/dist/css/bootstrap.min.css $APPLICATION_CSS
add_css_asset node_modules/tether-shepherd/dist/css/shepherd-theme-default.css $APPLICATION_CSS
add_css_asset public/jqueryFileTree/jqueryFileTree.css $APPLICATION_CSS
./node_modules/.bin/node-sass --include-path assets/css/ assets/css/editor.css.scss >> $APPLICATION_CSS
if ! is_webdev; then
./node_modules/.bin/minify $APPLICATION_CSS > `add_min_prefix $APPLICATION_CSS`
else
cp $APPLICATION_CSS `add_min_prefix $APPLICATION_CSS`
fi
}
function application_js() {
# application.js
echo "Building application.js"
if should_reset; then
echo "Resetting application.js"
rm -f $COMPILED_APP/assets/application-*.js
fi
add_js_asset node_modules/dropbox/dist/Dropbox-sdk.min.js $APPLICATION_JS
add_js_asset bower_components/jquery/dist/jquery.min.js $APPLICATION_JS
add_js_asset bower_components/jquery-ui/jquery-ui.min.js $APPLICATION_JS
add_js_asset bower_components/bootstrap/dist/js/bootstrap.min.js $APPLICATION_JS
add_js_asset node_modules/tether-shepherd/dist/js/tether.js $APPLICATION_JS
add_js_asset node_modules/tether-shepherd/dist/js/shepherd.min.js $APPLICATION_JS
add_js_asset public/jqueryFileTree/jqueryFileTree.js $APPLICATION_JS
add_js_asset public/jquery.cookie.min.js $APPLICATION_JS
add_js_asset assets/js/libs/rsvp.min.js $APPLICATION_JS
add_js_asset assets/js/libs/route-recognizer.js $APPLICATION_JS
add_js_asset assets/js/DataBinder.js $APPLICATION_JS
add_js_asset assets/js/Model.js $APPLICATION_JS
add_js_asset assets/js/Model/Preference.js $APPLICATION_JS
add_js_asset assets/js/Model/CloudFile.js $APPLICATION_JS
add_js_asset assets/js/Widget/Preference.js $APPLICATION_JS
add_js_asset assets/js/helpers.js $APPLICATION_JS
# todo - exclude the files above
find assets/js/ -name '*.js' | while read file; do add_js_asset "$file" $APPLICATION_JS ; done
if ! is_webdev; then
./node_modules/.bin/minify $APPLICATION_JS > `add_min_prefix $APPLICATION_JS`
else
cp $APPLICATION_JS `add_min_prefix $APPLICATION_JS`
fi
}
function editor_part() {
# editor.part
echo "Building app.partials"
if should_reset; then
echo "Resetting app.partials"
rm -f $COMPILED_APP/app.partials
fi
find . -name '_*.html' | while read file ; do add_template "$file" $COMPILED_APP/app.partials ; done
}
function app() {
# Build single page app
echo "Building app"
if should_reset; then
echo "Resetting app"
rm -f $COMPILED_APP/app.html
fi
cp editor-layout.tt $COMPILED_APP/editor-layout.tt
perl render.pl --COMPILED_APP_DIR=$COMPILED_APP --APP_VERSION_ID=$APP_VERSION_ID --APP_VERSION=$APP_VERSION --APP_COMMIT_ID=$APP_COMMIT_ID --SYNTAX_DB=$COMPILED_APP/syntaxes.json
}
function public_assets() {
# Adding public assets
cp -frp public/* $COMPILED_APP/
}
function download_if_necessary() {
DST="$1"
URL="$2"
mkdir -p tmp/cache/
if ! is_webdev || ! [ -f tmp/cache/$DST ]; then
echo "Fetching $DST"
curl "$URL" --fail --silent --show-error > tmp/cache/$DST
fi
cp tmp/cache/$DST $COMPILED_APP/$DST
}
function json_resources() {
# Adding JSON resources (from prod for now...)
download_if_necessary extensions.json https://api.anyfile-notepad.semaan.ca/extensions
download_if_necessary syntaxes.json https://api.anyfile-notepad.semaan.ca/syntaxes
download_if_necessary mime_types.json https://api.anyfile-notepad.semaan.ca/mime_types
}
function build_all() {
mkdir -p $COMPILED_APP
cd /tmp/
rm -fr $COMPILED_APP/*
cd -
mkdir -p $COMPILED_APP
mkdir -p $COMPILED_APP/assets
ace_js
pages_css
pages
application_css
application_js
json_resources
editor_part
app
public_assets
}
build_all
if ! is_webdev; then
exit
fi
function watch_dir() {
DIR=$1
ACTION=$2
OPTIONS=${3:-}
while true; do
inotifywait $OPTIONS -r -e create,modify,delete $RUNNING_DIR/$DIR
for action in $2; do
eval $action
done
done
}
function exit_cleanup() {
echo "Exiting..."
rm -f $SHOULD_RESET_FILE
}
trap exit_cleanup EXIT
watch_dir . build_all &
echo "1" > $SHOULD_RESET_FILE
while true; do
sleep 1
done