This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
forked from lvivier/step-gh-pages
-
Notifications
You must be signed in to change notification settings - Fork 11
/
functions.sh
208 lines (179 loc) · 4.13 KB
/
functions.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
#!/bin/bash
function getAllStepVars {
( set -o posix ; set ) | grep WERCKER_GIT_PUSH | sed -E 's/=.+//g' | xargs
}
function sanitizeOutput {
echo "$@" | sed -E 's_(.+://).+@_\1oauth-token@_g'
}
function s_info {
info "$(sanitizeOutput $@)"
}
function s_success {
success "$(sanitizeOutput $@)"
}
function s_debug {
if [ "$WERCKER_GIT_PUSH_DEBUG" == "true" ]; then
info "DEBUG: $(sanitizeOutput $@)"
fi
}
function s_warning {
info "WARNING: $(sanitizeOutput $@)"
}
function s_fail {
fail "$(sanitizeOutput $@)"
}
function s_setMessage {
setMessage "$(sanitizeOutput $@)"
}
# RETURNS REPO_PATH SET in GIT_PUSH or current WERCKER
function getRepoPath {
if [ -n "$WERCKER_GIT_PUSH_REPO" ]; then
echo "$WERCKER_GIT_PUSH_REPO"
else
echo "$WERCKER_GIT_OWNER/$WERCKER_GIT_REPOSITORY"
fi
}
#RETURNS FULL REMOTE PATH OF THE REPO
function getRemoteURL {
repo=$(getRepoPath)
if [ -n "$WERCKER_GIT_PUSH_GH_OAUTH" ]; then
echo "https://$WERCKER_GIT_PUSH_GH_OAUTH@github.com/$repo.git"
elif [ -n "$WERCKER_GIT_PUSH_HOST" ]; then
git_user=$(getGitSSHUser)
echo "$git_user@$WERCKER_GIT_PUSH_HOST:$repo.git"
else
echo ""
fi
}
#RETURNS GIT SSH USER
function getGitSSHUser {
if [ -n "$WERCKER_GIT_PUSH_USER" ]; then
echo "$WERCKER_GIT_PUSH_USER"
else
echo "git"
fi
}
#RETURNS BRANCH WE WANT TO PUSH TO
function getBranch {
if [ -n "$WERCKER_GIT_PUSH_GH_PAGES" ]; then
if [[ $(getRepoPath) =~ $WERCKER_GIT_OWNER\/$WERCKER_GIT_OWNER\.github\.(io|com)$ ]]; then
echo "master"
else
echo "gh-pages"
fi
elif [ -n "$WERCKER_GIT_PUSH_BRANCH" ]; then
echo "$WERCKER_GIT_PUSH_BRANCH"
else
echo "$WERCKER_GIT_BRANCH"
fi
}
#RETURNS BASE DIR WE WANT TO PUSH FROM
function getBaseDir {
# if directory provided, cd to it
if [ -n "$WERCKER_GIT_PUSH_BASEDIR" ]; then
echo $(pwd)/$WERCKER_GIT_PUSH_BASEDIR/
else
echo $(pwd)/
fi
}
function initEmptyRepoAt {
cd
rm -rf $1
mkdir -p $1
cd $1
git init -q
}
function cloneRepo {
result=$(git clone $1 $2 -q 2>&1)
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "failed to clone repo"
fi
}
function checkBranchExistence {
cd $1
git ls-remote -q --exit-code . origin/$2 > /dev/null
}
function checkoutBranch {
cd $1
result=$(git checkout $2 -q 2>&1)
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "failed to checkout existing branch $2"
fi
}
function getTagWithPython {
result=$(cat $1 | python -c 'import sys, json; print json.load(sys.stdin)["version"]' 2>&1);
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "Could not load version from $1"
else
echo "$result"
fi
}
function getTagWithNode {
result=$(node -p "require(\"$1\").version" 2>&1)
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "Could not load version from $1"
else
echo "$result"
fi
}
function getTagFromJSON {
if [ -f $1$2 ]; then
if [ -n "`which node`" ]; then
getTagWithNode $1$2
else
if [ -n "`which python`" ]; then
getTagWithPython $1$2
else
echo "$3"
fi
fi
else
echo "$3"
fi
}
function getTag {
if [ -n "$1" ]; then
case $1 in
"bower") getTagFromJSON $2 "bower.json" $1 ;;
"node") getTagFromJSON $2 "package.json" $1 ;;
*) echo $1;;
esac
fi
}
function createCNAME {
if [ -n "$WERCKER_GIT_PUSH_GH_PAGES_DOMAIN" ]; then
echo $WERCKER_GIT_PUSH_GH_PAGES_DOMAIN > "$1/CNAME"
s_info "Will create CNAME file: $1/CNAME"
fi
}
function pushBranch {
result="$(git push -q -f $1 $2:$3 2>&1)"
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "failed pushing to $3 on $1"
else
s_success "pushed to $3 on $1"
fi
}
function pushTag {
git tag -a $2 -m "Tagged by $WERCKER_STARTED_BY" -f > /dev/null
result="$(git push --tags $1 2>&1)"
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "failed pushing to tag $1 with $2"
else
s_success "tagged $1 with $2"
fi
}
function deleteTag {
git tag -d $2 > /dev/null
result="$(git push $1 --delete refs/tags/$2 2>&1)"
if [[ $? -ne 0 ]]; then
s_warning "$result"
s_fail "failed delete $2 from $1"
fi
}