-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jekyll-build
executable file
·154 lines (127 loc) · 4.31 KB
/
jekyll-build
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
#!/usr/bin/env bash
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
__NAME__="${__SOURCE__##*/}"
__AUTHOR__='S0AndS0'
__DESCRIPTION__='Initializes Jekyll under gh-pages branch within named repository'
## Provides 'failure'
source "${__DIR__}/shared_functions/modules/trap-failure/failure.sh"
trap 'failure "LINENO" "BASH_LINENO" "${BASH_COMMAND}" "${?}"' ERR
## Provides: argument_parser <arg-array-reference> <acceptable-arg-reference>
source "${__DIR__}/shared_functions/modules/argument-parser/argument-parser.sh"
## Provides: __license__ <description> <author>
source "${__DIR__}/shared_functions/license"
usage(){
_message="${1}"
_repo_name="${_repo_name:-repository-name}"
cat <<EOF
## Usage
# ssh ${USER}@host-or-ip ${__NAME__} ${_repo_name}
#
# ${__DESCRIPTION__}
#
# ${_repo_name}
# Name of repository to run
# bundle exec jekyll build --destination "${_srv_path//${HOME}/~}"
#
# -s --submodules
# Runs: git submodule update --init --recursive --merge
#
# -l --license
# Shows script or project license then exits
#
# -v --verbose
# Runs build commands verbosely
#
# -h --help help
# Displays this message and exits
EOF
(("${#_message}")) && [[ "${_message}" != '0' ]] && {
printf >&2 'Error - %s\n' "${_message}"
}
}
_args=("${@:?# No arguments provided try: ${__NAME__} help}")
_valid_args=('--help|-h|help:bool'
'--verbose|-v|verbose:bool'
'--submodules|-s|submodules:bool'
'--license|-l|license:bool'
'--repo-name:posix-nil')
argument_parser '_args' '_valid_args'
_exit_status="$?"
_git_path="${HOME}/git/${_repo_name}"
_www="${HOME}/www"
_srv_path="${_www}/${_repo_name}"
_github_api_token_path="${HOME}/.config/jekyll-build/github-api-token"
if ((_help)) || ((_exit_status)); then
usage "${_exit_status}"
exit "${_exit_status}"
elif ((_license)); then
__license__ "${__DESCRIPTION__}" "${__AUTHOR__}"
exit 0
fi
(("${#_repo_name}")) || {
usage 'missing repository name argument!'
exit "1"
}
[[ -d "${_git_path}" ]] || {
printf '# No repository %s at %s\n' "${_repo_name}" "${_git_path}"
exit 1
}
if [ -f "${_git_path}/_config.yml" ]; then
_config_files="_config.yml"
elif [ -f "${_git_path}/_config.yaml" ]; then
_config_files="_config.yaml"
else
printf '%s has no _config.yml or _config.yaml file\n' "${_repo_name}"
exit 1
fi
## Build cama seperated list of _config*.yml files
while read _p; do
_f="${_p##*/}"
[[ "${_f}" == '_config.yml' ]] && continue
[[ "${_f}" == '_config.yaml' ]] && continue
_config_files+=",${_f}"
done < <(ls -1 "${_git_path}"/_config*.y*ml)
[[ -d "${_www}" ]] || {
mkdir -vp "${_www}"
}
_pwd="${PWD}"
cd "${_git_path}"
((_submodules)) && {
git submodule update --init --recursive --merge
}
source "${HOME}/.bash_aliases"
_build_args=(
'--destination' "${_srv_path}"
'--config' "${_config_files}"
)
((_verbose)) && {
_build_args+=('--verbose')
}
## Use API keys if configured for repo and available to user during build process
if grep -q -- 'jekyll-github-metadata' "${_git_path}/Gemfile" && [ -f "${_github_api_token_path}" ]; then
printf '# JEKYLL_GITHUB_TOKEN="$(<github-api-token-path)" bundle exec jekyll build %s\n' "${_build_args[*]}"
JEKYLL_GITHUB_TOKEN="$(<"${_github_api_token_path}")" bundle exec jekyll build ${_build_args[@]}
else
printf '# bundle exec jekyll build %s"\n' "${_build_args[*]}"
bundle exec jekyll build ${_build_args[@]}
fi
[[ "${_pwd}" == "${_git_path}" ]] || {
cd "${_pwd}"
}
## Set permissions for files and directories under repo www directory
[[ -e "${HOME}/git-shell-commands/www-set-permissions" ]] && {
"${HOME}/git-shell-commands/www-set-permissions" "${_repo_name}"
} || {
_current_group="$(groups "${USER}" | awk '{print $3}')"
_www_group="$(groups "${USER}" | grep -oE 'www[-a-z]{0,29}')"
(("${#_current_group}")) && (("${#_www_group}")) && {
find "${_srv_path}" -group "${_current_group}" -exec chgrp -h "${_www_group}" {} \;
find "${_srv_path}" -group "${_www_group}" -type d -exec chmod 750 {} \;
find "${_srv_path}" -group "${_www_group}" -type f -exec chmod 640 {} \;
}
}
printf '# %s finished\n' "${__NAME__}"