-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-machine-for-blogging
executable file
·106 lines (91 loc) · 3.87 KB
/
configure-machine-for-blogging
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
#!/bin/sh
# Use ShellCheck, <https://www.shellcheck.net>, to analyze this script and catch common bugs.
# Exit immediately if a command (individually or part of a pipeline) exits with a non-zero exit status.
# FIXME: -o pipefail isn't available in POSIX shell.
set -eo pipefail
# Enable diagnostics when expanding unset variables.
set -u
hasDirectoryInPathLikeVariable()
{
pathDirectories=$(echo "$1" | tr ':' "\n")
for directory in $pathDirectories
do
if [ "$directory" = "$2" ]; then
echo "1"
return
fi
done
echo "0"
}
hasDirectoryInPathEnvironmentVariable()
{
hasDirectoryInPathLikeVariable "$PATH" "$1"
}
# Derived from the steps on <https://jekyllrb.com/docs/installation/macos/>.
# Ensure Ruby and other developer command line tools are installed.
echo "Running xcode-select --install..." >&2
# Will spawn /System/Library/CoreServices/Install Command Line Developer Tools.app/Contents/MacOS/Install Command Line Developer Tools
xcode-select --install >/dev/null 2>&1 || true
toolsInstallerPID=$(pgrep 'Install Command Line Developer Tools') || true
if [ -n "$toolsInstallerPID" ]; then
# Assume that when the installer exits that installation was successfull.
lsof -p "$toolsInstallerPID" +r 1 >/dev/null 2>&1
fi
if ! which ruby >/dev/null 2>&1; then
echo "Couldn't find ruby. Is it installed?" >&2
exit 1
fi
rubyVersion=$(ruby -e 'puts [RbConfig::CONFIG["MAJOR"], RbConfig::CONFIG["MINOR"], 0].join(".")') # Ignore teeny version
pathToRubyGemBinaryDirectory="$HOME/.gem/ruby/$rubyVersion/bin"
hasRubyGemBinaryDirectoryInPath=$(hasDirectoryInPathEnvironmentVariable "$pathToRubyGemBinaryDirectory")
if [ "$hasRubyGemBinaryDirectoryInPath" = "0" ]; then
echo "Ensure the following is in your PATH:" >&2
echo " $pathToRubyGemBinaryDirectory" >&2
fi
# Ensure "gem env gempath" lists the parent directory of $pathToRubyGemBinaryDirectory.
# FIXME: Is this necessary?
pathToRubyGemDirectory=$(dirname "$pathToRubyGemBinaryDirectory")
hasRubyGemBinaryDirectoryInGemEnvironmentPaths=$(hasDirectoryInPathLikeVariable "$(gem env gempath)" "$pathToRubyGemDirectory")
if [ "$hasRubyGemBinaryDirectoryInGemEnvironmentPaths" = "0" ]; then
echo "Couldn't find $pathToRubyGemDirectory in \"gem env gempath\"." >&2
exit 1
fi
# Check if Jekyll is installed. If not, then install latest.
echo "Checking for Jekyll..." >&2
gem list --silent -i jekyll
if ! gem list --silent -i jekyll; then
echo "Installing latest Jekyll..." >&2
SDKROOT="$(xcrun --show-sdk-path)"
export SDKROOT
gem install --user-install bundler jekyll
fi
# Create blog directory
echo "Checking for blog directory..." >&2
programDirectory=$(dirname "$0")
blogDirectory="$programDirectory/blog"
if [ ! -d "$blogDirectory" ]; then
echo "Creating blog directory and installing Jekyll dependencies..." >&2
# I want to install Jekyll's dependencies without sudo access. So, skip bundle install and manually install
# dependencies under blog/vender/bundle.
jekyll new --skip-bundle blog
savedCurrentWorkingDirectory=$(pwd)
cd "$blogDirectory"
bundle config set --local path 'vendor/bundle'
bundle install
cd "$savedCurrentWorkingDirectory"
fi
# Check for Jekyll::Compose
# See <https://github.com/jekyll/jekyll-compose>.
# FIXME: Automate adding the jekyll-compose line (below) to the Gemfile.
echo "Checking for jekyll-compose..." >&2
blogGemFile="$blogDirectory/Gemfile"
if ! BUNDLE_GEMFILE="$blogGemFile" bundle show jekyll-compose >/dev/null 2>&1; then
echo "Couldn't find jekyll-compose." >&2
echo "Add the following line to $blogGemFile:" >&2
echo " gem 'jekyll-compose', group: [:jekyll_plugins]" >&2
echo "Then run:" >&2
echo " BUNDLE_GEMFILE='$blogGemFile' bundle" >&2
fi
echo "" >&2
echo "To run the included web server, do:" >&2
echo " cd $blogDirectory && bundle exec jekyll serve --livereload --open-url" >&2