-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite
executable file
·381 lines (358 loc) · 10.7 KB
/
site
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
# redirect to bash
if [ -e /proc/$$/ ] ; then
PPROC=$(cat /proc/$$/stat | cut -d " " -f 2)
if [ "(sh)" == "$PPROC" ] ; then
/bin/bash $0 $*
exit $?
fi
fi
# check cli php
check_php () {
PHP=php
for I in php56cli php55cli php54cli php53cli php5cli php71cli php70cli php7cli \
php56 php55 php54 php53 php5 php71 php70 php7 php
do
if [ "" != "$($I -v 2>/dev/null | grep cli)" ] ; then
PHP=$I
break
fi
done
}
# check libxml version
check_libxml () {
$PHP -r 'libxml_get_errors();' >/dev/null 2>&1
if [ 0 -ne $? ] ; then
export LD_PRELOAD=~/usr/libxml/lib/libxml2.so
fi
}
# exec composer
_composer () {
local COMPOSER_PATH=
test -e "$(dirname $0)/composer.phar" && COMPOSER_PATH=$PHP\ $(dirname $0)/composer.phar
composer >/dev/null 2>&1 && COMPOSER_PATH=composer
if [ "$GH_TOKEN" != "" ]; then
COMPOSER_PATH=$PHP\ $(dirname $0)/composer.phar
fi
case "$1" in
"exist" ) test "" = "$COMPOSER_PATH" ;;
"self-install") curl --insecure -L https://getcomposer.org/installer | $PHP ;;
"self-update" ) ;;
* ) $COMPOSER_PATH $* ;;
esac
}
# exec sculpin
_sculpin () {
local SCULPIN_PATH1=$(dirname $0)/sculpin.phar
local SCULPIN_PATH2=$(dirname $0)/.vendor/bin/sculpin
case "$1" in
"exist" ) test -e "$SCULPIN_PATH1" || test -e "$SCULPIN_PATH2" ;;
"self-install") # for I in lock json ; do cp -pf $(dirname $0)/composer.{$I,$I~} ; done
$PHP composer.phar require sculpin/sculpin:^2.1@dev dflydev/embedded-composer:@dev
# for I in lock json ; do mv -f $(dirname $0)/composer.{$I~,$I} ; done
;;
"self-update" ) ;;
* ) if [ -e "$SCULPIN_PATH1" ] ; then
$PHP $SCULPIN_PATH1 $*
else
$PHP $SCULPIN_PATH2 $*
fi ;;
esac
}
# exec bowerphp
_bower () {
if [ "" != "$(whereis bower)" ]; then
bower $* -s
else
$PHP .vendor/beelab/bowerphp/bin/bowerphp $*
fi
}
# get config value
get_ini () {
# $1 : config file
# $2 : key
# $3 : default value
local RESULT
RESULT=$(cat "$1" | grep "$2" | sed -e 's/^[^=]*=\s*\(.*\)/\1/g')
if [ "" == "$RESULT" ] ; then
RESULT=$3
fi
echo $RESULT
}
exec_usage () {
echo 'usage: site <command> [option]'
echo 'commands are:'
echo ' init initialize site'
echo ' clean cleanup site, remove ignore files'
echo ' generate [OUT_PATH] generate site'
echo ' production [CONF_PATH] production site, set webhook.conf path'
echo ' deploy production site for GitHub Pages with Travis CI'
echo ' test launch web server [ http://localhost:8000/ ] for test site'
echo ' spider check broken link'
echo ' new [STUB] create new empty page'
}
core_init () {
#
check_php
check_libxml
# install Component from Composer and Bower
_composer exist || _composer self-install
_composer self-update
_composer install
_bower install
# create default webhook conf
if [ ! -e "$(dirname $0)/webhook.conf" ] ; then
pushd $(dirname $0) >/dev/null 2>&1
echo "log_file=$(pwd)/webhook.log"> $(pwd)/webhook.conf
echo "secret_key=$(cat /dev/urandom | head -n 64 | shasum -a 512 -b | sed -e "s/[\* -]//g")">> $(pwd)/webhook.conf
echo "git_cmd=$(whereis git)">> $(pwd)/webhook.conf
echo "repos_path=$(pwd)">> $(pwd)/webhook.conf
echo "output_path=$(pwd)/public">> $(pwd)/webhook.conf
echo "notify_path=$(pwd)/webhook.notify">> $(pwd)/webhook.conf
popd >/dev/null 2>&1
fi
}
exec_init () {
exec 9< $0
perl -mFcntl=:flock -e "open(LOCK,'<&=9');exit(!flock(LOCK,LOCK_EX|LOCK_NB))" || exit 0
#
core_init
}
exec_clean () {
rm -rf .vendor .sculpin *.lock *.phar
rm -rf output_* app/cache app/logs
find source/assets -maxdepth 1 -mindepth 1 -not -name js -and -not -name css -and -not -name fonts -exec rm -rf {} \;
}
core_generate () {
rm -rf output_prod/* > /dev/null 2>&1
_sculpin generate --env=prod > /dev/null
if [ $? -ne 0 ]; then echo "Could not generate the site"; exit 1; fi
OUT_PATH=$1
if [ "" != "$OUT_PATH" ] ; then
if [ "" != "$(basename $OUT_PATH)" ] ; then
# swap new/old output directory
rm -rf ${OUT_PATH}~ > /dev/null 2>&1
mv $OUT_PATH ${OUT_PATH}~
mv output_prod $OUT_PATH
# Move the other files that were generated by sculpin.
mkdir -p $OUT_PATH/files
yes no | mv $OUT_PATH~/files/* $OUT_PATH/files
mv -f $OUT_PATH~/{log,.fast-cgi-bin,google*.html} $OUT_PATH
mv -f $OUT_PATH~/{hsp-users.jp,*.sharkpp.net} $OUT_PATH
fi
fi
}
exec_generate () {
exec 9< $0
perl -mFcntl=:flock -e "open(LOCK,'<&=9');exit(!flock(LOCK,LOCK_EX|LOCK_NB))" || exit 0
# init
check_php
check_libxml
_sculpin exist || core_init
# generate
core_generate $1
}
exec_production () {
exec 9< $0
perl -mFcntl=:flock -e "open(LOCK,'<&=9');exit(!flock(LOCK,LOCK_EX|LOCK_NB))" || exit 0
# check update
if [ ! -e "$1" ] ; then
echo config \"$1\" not found
exit 1
fi
cd $(dirname $0)
NOTIFY=$(get_ini $1 notify_path notify)
if [ ! -e "$NOTIFY" ] ; then
echo not updated
exit 0
fi
rm -f $NOTIFY
# init
check_php
check_libxml
_sculpin exist || core_init
# repository update
GIT=$(get_ini $1 git_cmd git)
$GIT pull origin master
if [ 0 -ne $? ] ; then
exit 1
fi
# install assets
_bower install
# generate site
core_generate $(get_ini $1 output_path public)
}
exec_deploy0 () {
if [ "" == "$GH_TOKEN" ] ; then
exit 0
fi
PHP=php
set -x
# install Component from Composer and Bower
# _composer exist || _composer self-install
# _composer self-update
_composer install
_bower install
# set git config
git config --global user.email "${GIT_EMAIL}"
git config --global user.name "${GIT_USERNAME}"
# clone output repository
DEPLOY_REPOS="https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git"
DEPLOY_BRANCH=gh-pages
rm -rf output_prod
git clone --quiet -b $DEPLOY_BRANCH $DEPLOY_REPOS output_prod
if [ ! -e "output_prod" ] ; then
git init output_prod
pushd output_prod
git remote add origin $DEPLOY_REPOS
git checkout --orphan $DEPLOY_BRANCH
popd
else
pushd output_prod
git pull
popd
rm -rf output_prod/*
fi
# xx
echo "imports:" > app/config/sculpin_site_prod.yml
echo "- sculpin_site.yml">>app/config/sculpin_site_prod.yml
echo "google_analytics_tracking_id: ${GOOGLE_ANALYTICS_TRACKING_ID}">>app/config/sculpin_site_prod.yml
# production generate
PRIOD_BEGIN0=$(php -r 'echo microtime(true);')
PRIOD_BEGIN=$(php -r 'echo microtime(true);')
_sculpin generate --env=prod
if [ 0 -ne $? ] ; then
return 1
fi
PRIOD_END=$(php -r 'echo microtime(true);')
# check google analytics tracking id
if [ "" == "$(cat output_prod/index.html | grep "${GOOGLE_ANALYTICS_TRACKING_ID}")" ]; then
return 1
fi
# deploy message
DEPLOY_MSG=$(git log --pretty=format:"Deploy from https://github.com/${TRAVIS_REPO_SLUG}/commit/%H : %s" -1)
# create README.md
pushd output_prod
cat ../README-generated-template.md \
| sed -e "s/{DATE}/$(date +'%Y')/g" \
-e "s/{BUILD_DATE}/$(date +'%Y-%m-%d %H:%M:%S')/g" \
-e "s/{BUILD_FILES}/$(find . -type f|wc -l)/g" \
-e "s/{BUILD_SEC}/$(echo $PRIOD_END-$PRIOD_BEGIN*2+$PRIOD_BEGIN0|bc)/g" \
> README.md
# deploy
git add . -A
echo $DEPLOY_MSG | git commit -F -
git push --quiet origin $DEPLOY_BRANCH
popd
# tweet
return 0
}
exec_deploy () {
set -x
#php composer.phpr install
#php .vendor/beelab/bowerphp/bin/bowerphp install
#
rm -rf output_prod
# xx
echo "imports:" > app/config/sculpin_site_prod.yml
echo "- sculpin_site.yml">>app/config/sculpin_site_prod.yml
echo "google_analytics_tracking_id: ${GOOGLE_ANALYTICS_TRACKING_ID}">>app/config/sculpin_site_prod.yml
# production generate
PRIOD_BEGIN0=$(php -r 'echo microtime(true);')
PRIOD_BEGIN=$(php -r 'echo microtime(true);')
ls -l .
ls -l .vendor/bin/
php .vendor/bin/sculpin generate --env=prod
if [ 0 -ne $? ] ; then
return 1
fi
PRIOD_END=$(php -r 'echo microtime(true);')
# check google analytics tracking id
if [ "" == "$(cat output_prod/index.html | grep "${GOOGLE_ANALYTICS_TRACKING_ID}")" ]; then
return 1
fi
# deploy message
DEPLOY_MSG=$(git log --pretty=format:"Deploy from https://github.com/${REPO_SLUG}/commit/%H : %s" -1)
# create README.md
pushd output_prod
cat ../README-generated-template.md \
| sed -e "s/{DATE}/$(date +'%Y')/g" \
-e "s/{BUILD_DATE}/$(date +'%Y-%m-%d %H:%M:%S')/g" \
-e "s/{BUILD_FILES}/$(find . -type f|wc -l)/g" \
-e "s/{BUILD_SEC}/$(echo $PRIOD_END-$PRIOD_BEGIN*2+$PRIOD_BEGIN0|bc)/g" \
> README.md
# deploy
popd
# tweet
return 0
}
exec_test () {
# init
check_php
check_libxml
_sculpin exist || core_init
#
rm -rf output_dev/*
_sculpin generate --watch --server
}
exec_spider () {
# wget install check
wget --help > /dev/null
if [ $? -eq 127 ] ; then
echo wget command not installed
exit
fi
# init
check_php
check_libxml
_sculpin exist || exec_init
RUNNING=$(ps ax|grep "bin/sculpin[ ]"|wc -l|sed -e "s/ //g")
if [ 0 -eq $RUNNING ] ; then
rm -rf output_dev/*
# launch server and waiting for generate
while read f; do
if [ "" != "$(echo "$f" | grep "server is running at")" ] ; then break ; fi
done < <(_sculpin generate --watch --server &)
fi
wget --spider --no-directories --recursive --page-requisites --level=0 --no-verbose http://localhost:8000
if [ 0 -eq $RUNNING ] ; then
killall $PHP
fi
}
exec_new () {
NEW_PAGE=source/_posts/$(date +%Y-%m-%d)-$1.md
echo "---" > $NEW_PAGE
echo "title: \"$1\"" >>$NEW_PAGE
echo "date: $(date +'%Y-%m-%d %H:%M')" >>$NEW_PAGE
echo "tags: [雑記]" >>$NEW_PAGE
echo "categories: [ブログ]" >>$NEW_PAGE
echo "" >>$NEW_PAGE
echo "---" >>$NEW_PAGE
echo create new blog page is $NEW_PAGE
}
exec_update () {
# init
check_php
check_libxml
_composer exist || _composer self-install
_composer self-update
_composer update
_bower update
}
case "$1" in
"init" ) exec_init ;;
"i" ) exec_init ;;
"clean" ) exec_clean ;;
"generate" ) exec_generate $2;;
"g" ) exec_generate $2;;
"production" ) exec_production $2;;
"deploy" ) exec_deploy;;
"test" ) exec_test ;;
"t" ) exec_test ;;
"spider" ) exec_spider ;;
"new" ) exec_new $2;;
"n" ) exec_new $2;;
"update" ) exec_update ;;
"u" ) exec_update ;;
* ) exec_usage ;;
esac