Skip to content

Commit 592fd37

Browse files
authored
Merge pull request #23 from tupleblog/tpb-script
add script to install and run the project
2 parents 72c9ade + 1eafe3d commit 592fd37

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
Repository for [tupleblog](http://tupleblog.github.io) (in Thai) based on [Jekyll](http://jekyllrb.com/) [HPSTR](https://github.com/mmistakes/hpstr-jekyll-theme) theme.
44

5+
### Write on Gitpod
6+
7+
[Gitpod](https://gitpod.io) is an amazing online IDE especially for GitHub. You can start writing the blog on Gitpod by clicking [here](https://gitpod.io/#https://github.com/tupleblog/tupleblog.github.io) and logging in with your GitHub Authentication.
8+
9+
10+
### `tpb` script
11+
12+
`tpb` script is now live! `tpb.sh` will help you install and serve the site easily. All you need to have is Ruby version >= `2.5.3`.
13+
14+
Here is how the script looks like:
15+
16+
```sh
17+
$ ./tpb.sh
18+
Usage: ./tpb.sh [-i] [-d]
19+
20+
Options:
21+
-i, --install install dependencies with gem (gem install bundler) and bundler (bundle install).
22+
-d, --debug serve jekyll locally using bundler.
23+
```
24+
25+
If you have already installed Ruby, you can go ahead with the command `./tpb.sh -i`. This will install `bundler` if it is not installed yet and then run `bundle install` for you. Another command is `./tpb.sh -d` for debugging locally. This command starts with commenting specified `url` in the `_config.yml` of the project then serving Jekyll with `JEKYLL_ENV=production` prefix. It also uncomments the line for you when you stop the server by hitting `ctrl`+`c`.
26+
27+
528
### Running the blog locally
629

730
First, go to `_config.yml` then change line 9 for running locally. Then, change
@@ -11,7 +34,7 @@ directory to the repository and then do the following
1134
2. Download `ruby` using [Homebrew](https://brew.sh/) for Mac. For Windows, download **RubyInstaller** and
1235
**DEVELOPMENT KIT** regarding your OS architecture from [here](https://rubyinstaller.org/downloads/).
1336
For **RubyInstaller**, just double click and done. For **DEVELOPMENT KIT**, please follows [its instruction](https://github.com/oneclick/rubyinstaller/wiki/Development-Kit).
14-
3. Update your installed gems to latest version `gem update`. If updating failed try running `gem update --system` to update its core (see [this post](http://guides.rubygems.org/ssl-certificate-update/#installing-using-update-packages)).
37+
3. Update your installed gems to latest version `gem update`. If updating failed try running `gem update --system` to update its core (see [this post](http://guides.rubygems.org/ssl-certificate-update/#installing-using-update-packages)).
1538
4. `gem install bundler`
1639
5. `bundle install` (For Windows, run `cmd` as Administrator)
1740
6. `bundle exec jekyll serve` to serve the site in port 4000. You can also run

tpb.sh

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
3+
# ---------------------------
4+
# helper functions
5+
# ---------------------------
6+
## detect if it is on which system, because inplace sed on MacOS requires backup extension
7+
isMac() {
8+
if [[ "$OSTYPE" == "darwin"* ]]; then
9+
return 0
10+
fi
11+
12+
return 1
13+
}
14+
15+
## sed inplace for cross platform
16+
xsed() {
17+
# $1 : pattern
18+
# $2 : filename
19+
if isMac; then
20+
sed -Ei '' "$1" "$2"
21+
else
22+
sed -Ei "$1" "$2"
23+
fi
24+
}
25+
26+
## check if imageMagick is installed
27+
checkRequirement() {
28+
if ! type ruby > /dev/null; then
29+
echo "ruby not found, please install.. (rbenv recommended)"
30+
return 1
31+
fi
32+
if ! type gem > /dev/null; then
33+
echo "gem not found, please install.. (rbenv recommended)"
34+
return 1
35+
fi
36+
return 0
37+
}
38+
39+
## display help
40+
displayHelp(){
41+
echo "Usage: $0 [-i] [-d]"
42+
echo
43+
echo " Options:"
44+
echo " -i, --install install dependencies with gem (gem install bundler) and bundler (bundle install)."
45+
echo " -d, --debug serve jekyll locally using bundler."
46+
echo
47+
48+
exit 1
49+
}
50+
51+
# ---------------------------
52+
# functions
53+
# ---------------------------
54+
## install dependencies
55+
installDeps() {
56+
if ! type bundle > /dev/null; then
57+
echo -e ">> bundle not found, installing..\n"
58+
gem install bundler
59+
fi
60+
61+
echo -e ">> installing bundles..\n"
62+
bundle install
63+
}
64+
65+
## debug locally
66+
debugLocally() {
67+
local config_file
68+
local fwp_pid
69+
config_file="./_config.yml"
70+
71+
if type gp > /dev/null; then
72+
echo -e ">> Running on Gitpod.. Forwarding port 4000 to 4001 to expose publicly..\n"
73+
74+
gp forward-port 4000 4001 &
75+
76+
# collect pid of forwarding port to kill later
77+
fwp_pid="$!"
78+
fi
79+
80+
# comment url line on start
81+
xsed 's/^(url:.*)/\# \1/' $config_file
82+
83+
# serve jekyll locally
84+
JEKYLL_ENV=production bundle exec jekyll serve
85+
86+
# uncomment url line on stop
87+
xsed 's/^#\s*(url: https\:\/\/tupleblog.*)/\1/' $config_file
88+
89+
# if there is forward port, kill it
90+
if [ ! -z "$fwp_pid" ]; then
91+
echo -e "\n>> Killing pid: $fwp_pid.."
92+
kill -9 "$fwp_pid"
93+
fi
94+
}
95+
96+
# ---------------------------
97+
# main
98+
# ---------------------------
99+
100+
main() {
101+
# check requirement first
102+
if ! checkRequirement; then exit 1; fi
103+
104+
case $1 in
105+
'-i' | '--install' )
106+
installDeps
107+
exit
108+
;;
109+
'-d' | '--debug' )
110+
debugLocally
111+
exit
112+
;;
113+
'-t' | '--test' )
114+
debugLocally
115+
# displayHelp
116+
;;
117+
* )
118+
displayHelp
119+
;;
120+
esac
121+
}
122+
123+
main $@

0 commit comments

Comments
 (0)