-
Notifications
You must be signed in to change notification settings - Fork 7
/
release.sh
executable file
·325 lines (286 loc) · 7.35 KB
/
release.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
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
#!/usr/bin/env bash
#
# This file is a WordPress.org plugin release script.
# For more about how it works, see the documentation at
# https://github.com/INN/docs/blob/master/projects/wordpress-plugins/release.sh.md
#
RELEASE_DIR=release;
SVN_PATH=$RELEASE_DIR/svn;
SVN_REPO="https://plugins.svn.wordpress.org/pym-shortcode/";
BLACKLIST=(
.\*
release.sh
mkdocs.yml
phpunit.xml
requirements.txt
composer.lock
composer.json
package.json
bower.json
Gruntfile.js
release/\*
tests/\*
node_modules/\*
./\*\*/.\*
);
function ensure_release_dir() {
local release_dir="$1";
if [[ ! -d $release_dir ]]
then
echo "Creating $release_dir directory";
mkdir -p $release_dir;
fi
}
function is_master_branch() {
local remotes=`git ls-remote --quiet`;
local current=`get_current_git_commit`;
local is_master=`echo "$remotes" | grep "refs/heads/master" | grep $current | awk '{print $2;}'`;
echo "$is_master";
}
function is_git_tag() {
local remotes=`git ls-remote --quiet`;
local current=`get_current_git_commit`;
local is_tag=`echo "$remotes" | grep "refs/tags" | grep $current | awk '{print $2;}' | sed -e 's/^refs\/tags\///;s/\^{}$//'`;
echo "$is_tag";
}
function get_current_git_commit() {
echo `git rev-parse HEAD`;
}
function check_repo_state() {
#
# Check the state of this git repo. If no tag is checked out
# and we're not on the master branch, bail.
#
local is_master=`is_master_branch`;
local is_tag=`is_git_tag`;
if [[ "$is_master" == "" && "$is_tag" == "" ]]
then
echo "Bad release state for git repo!";
echo "Make sure you've checked out a tag or the master branch before releasing.";
exit 1;
else
echo "Repository is ready for deployment...";
fi
}
function confirm_deployment() {
# make sure we know what we're doing
local is_master=`is_master_branch`;
local is_tag=`is_git_tag`;
local which_text="[master] and [$is_tag]";
if [[ $is_master == "" ]]
then
which_text="[$is_tag]";
fi
if [[ $is_tag == "" ]]
then
which_text="[master]";
fi
read -p "Really release plugin from $which_text? [y/N] " -r;
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "No changes made. Exiting...";
exit 0;
fi
}
function init_update_svn_repo() {
local svn_path="$1";
local svn_repo="$2";
if [[ "$svn_path" == "" || "$svn_repo" == "" ]]
then
echo "The svn_path and svn_repo arguments are required.";
exit 1;
fi
# init and update svn repo
if [[ ! -d $svn_path ]]
then
echo " - checking out svn repo";
OUT=`mkdir -p $svn_path && svn checkout $svn_repo $svn_path`
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
else
echo " - updating svn repo";
OUT=`cd $svn_path && svn update`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
fi
}
function create_release_zip() {
# Build a release zip file
echo "Creating release/wp-release.zip";
OUT=`rm -f release/wp-release.zip`;
OUT=`zip -x "${BLACKLIST[@]}" -q -r release/wp-release.zip .`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
}
function install_update_dependencies() {
echo "Checking for third-party/vendor dependencies...";
# If composer.json exists, run composer install
if [[ -f composer.json ]]
then
echo " - installing composer dependencies";
composer install --no-dev;
fi
# If package.json exists, run npm install
if [[ -f package.json ]]
then
echo " - installing npm dependencies";
npm install;
fi
# If bower.json exists, run bower install
if [[ -f bower.json ]]
then
echo " - installing bower dependencies";
bower install;
fi
}
function write_trunk() {
local svn_path="$1";
local is_master=`is_master_branch`;
if [[ $is_master != "" ]]
then
trunk_path=$svn_path/trunk;
echo "Writing to $trunk_path";
# overwrite with unzip
OUT=`rm -rf $trunk_path && unzip -o release/wp-release.zip -d $trunk_path`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi;
# stage all changes (adds and removes)
OUT=`cd $trunk_path && svn st | grep '^\?' | awk '{print \$2}' | xargs svn add` # add all
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
OUT=`cd $trunk_path && svn st | grep '^\!' | awk '{print \$2}' | xargs svn rm` # remove all
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
# make sure something has changed besides the autoloader hashes
CHANGES=`cd $trunk_path && svn st | grep -v 'autoload\(_real\)\?\.php'`
if [[ $CHANGES == "" ]]
then
echo "There are no changes to commit for trunk.";
OUT=`cd $trunk_path && svn revert --recursive .`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
else
echo "Committing $trunk_path (slow) ...";
CURRENT=`get_current_git_commit`;
OUT=`cd $trunk_path && svn commit -m "update trunk to git $CURRENT"`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
fi
fi
}
function write_tag() {
local svn_path="$1";
local is_tag=`is_git_tag`;
if [[ $is_tag != "" ]]
then
WP_TAG=`echo $is_tag | sed -e 's/^v//'`;
TAG_PATH=$svn_path/tags/$WP_TAG;
echo "Writing to $TAG_PATH";
# overwrite with unzip
OUT=`rm -rf $TAG_PATH && unzip -o release/wp-release.zip -d $TAG_PATH`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
# TODO: set version numbers and/or ensure version numbers in plugin files are correct
# stage all changes (adds and removes)
OUT=`cd $svn_path/tags && svn st | grep '^\?' | awk '{print \$2}' | xargs svn add`; # add all
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
OUT=`cd $svn_path/tags && svn st | grep '^\!' | awk '{print \$2}' | xargs svn rm`; # remove all
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
# make sure something has changed besides the autoloader hashes
CHANGES=`cd $svn_path/tags && svn st | grep -v 'autoload\(_real\)\?\.php'`;
if [[ $CHANGES == "" ]]
then
echo "There are no changes to commit for $TAG_PATH";
OUT=`cd $svn_path/tags && svn revert --recursive .`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
else
echo "Committing $TAG_PATH (slow) ...";
CURRENT=`get_current_git_commit`;
OUT=`cd $svn_path/tags && svn commit -m "update $WP_TAG to git $CURRENT"`;
if [[ $? -ne 0 ]]
then
echo "$OUT";
exit 1;
fi
fi
fi
}
function help_text() {
echo "Usage: ./release.sh [--dry_run, --help]";
echo "";
echo "--dry_run: Create the release directory and release zip,
but bon't actually commit to the SVN repository."
echo "";
echo "--help: Display this help screen and exit.";
echo "";
echo "For more information about this script, see:
https://github.com/INN/docs/blob/master/projects/wordpress-plugins/release.sh.md";
echo "";
exit 0;
}
# Parse args
if [[ $@ =~ "help" || $@ =~ "--help" ]]
then
help_text;
fi
if [[ $@ =~ "dry_run" || $@ =~ "--dry_run" ]]
then
dry_run=1;
else
dry_run=0;
fi
# Start the release process
ensure_release_dir "$RELEASE_DIR";
check_repo_state;
confirm_deployment;
if [[ $dry_run == 0 ]]
then
init_update_svn_repo "$SVN_PATH" "$SVN_REPO";
fi
install_update_dependencies;
create_release_zip;
if [[ $dry_run == 0 ]]
then
write_trunk "$SVN_PATH";
write_tag "$SVN_PATH";
fi
echo "Release process finished."