make_release cleanup and options
[working/Evergreen.git] / build / tools / make_release
1 #!/bin/bash
2
3 GIT_ABS=`git rev-parse --show-toplevel`
4 GIT_BRANCH=`git rev-parse --abbrev-ref HEAD | sed 's|.*/||'`
5 HEADURLBASE="http://git.evergreen-ils.org/Evergreen.git?h=refs/heads/"
6 HEADURL="$HEADURLBASE$GIT_BRANCH"
7
8 # Drop to the root of the checkout
9 cd $GIT_ABS
10
11 VERSION=AUTO # -v (version)
12 PREV_BRANCH=AUTO # -f (from)
13 PREV_VERSION=AUTO # -F (from version)
14 NO_UPGRADE=AUTO # -n
15 IS_PREVIEW=AUTO # -p
16 TAG_ONLY=NO # -t
17 BUILD_ONLY=NO # -b
18 UPGRADE_PREVIEW=NO # -r
19 SKIP_I18N=NO # -i
20
21 while getopts ":hv:f:F:nptbri" opt; do
22     case $opt in
23         v)
24             VERSION=$OPTARG
25         ;;
26         f)
27             PREV_BRANCH=$OPTARG
28         ;;
29         F)
30             PREV_VERSION=$OPTARG
31         ;;
32         n)
33             NO_UPGRADE=YES
34         ;;
35         r)
36             UPGRADE_PREVIEW=YES
37         ;;
38         i)
39             SKIP_I18N=YES
40         ;;
41         p)
42             IS_PREVIEW=YES
43         ;;
44         t)
45             TAG_ONLY=YES
46         ;;
47         b)
48             BUILD_ONLY=YES
49         ;;
50         \?)
51             echo "Invalid Option: -$OPTARG"
52             exit 1
53         ;;
54         :)
55             echo "-$OPTARG requires an argument."
56             exit 1
57         ;;
58         h)
59             echo "$0 [-v VERSION] [-f PREV_BRANCH | -t | -b] [-F PREV_VERSION] [-n] [-p]"
60             echo "   VERSION is auto-detected by default and is based on the currently checked out branch."
61             echo "   PREV_BRANCH is auto-detected by default but that isn't reliable. Include remote name!"
62             echo "   PREV_VERSION Is auto-detected by default and is based on the PREV_BRANCH's name."
63             echo "   -n specifies that you don't want an upgrade script to be auto-generated."
64             echo "   -p specifies that this is a preview build."
65             echo "   -t turns on tag only mode."
66             echo "   -b turns on build only mode."
67             echo "   -r prompt to preview upgrade SQL in editor before committing."
68             echo "   -i skip i18n; primarily useful for (quickly) testing this script."
69             echo "   NOTE: -t and -b override PREV_BRANCH/PREV_VERSION, but -b overrides -t."
70             exit -1
71         ;;
72     esac
73 done
74
75 if [ $TAG_ONLY = "YES" ]; then
76     PREV_BRANCH="TAG"
77 fi
78
79 if [ $BUILD_ONLY = "YES" ]; then
80     PREV_BRANCH="PACKAGE"
81 fi
82
83 if [ $VERSION = "AUTO" ]; then
84     # Auto-pick version based on branch name
85     echo AUTO VERSION
86     VERSION=`echo $GIT_BRANCH | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
87     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
88     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
89     VERSION=`echo $VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
90     if [ "$VERSION" = "$GIT_BRANCH" ]; then
91         echo "AUTO VERSION FAILED."
92         exit 1
93     fi
94 fi
95 SHORT_VERSION=`echo $VERSION | grep -o '^[0-9]\+\.[0-9]\+'`
96 echo "Version: $VERSION Short: $SHORT_VERSION"
97
98 # prep a couple alternate represenations
99 DASH_VERSION=`echo $VERSION | sed 's/\./-/g'`
100 SHORT_DASH_VERSION=`echo $SHORT_VERSION | sed 's/\./-/g'`
101 UNDER_VERSION=`echo $VERSION | sed -e 's/\./_/g;s/-/_/g'`
102
103 PREVIEW_TEXT=""
104
105 if [ "$IS_PREVIEW" == "YES" ]; then
106     PREVIEW_TEXT="previews/"
107 fi
108
109 # Release Preamble
110 # For adding into README for release builds
111 # The head is used to check if we have it already (no need to add it twice)
112 RELEASE_PREAMBLE_HEAD="Preamble: Getting an Evergreen official release tarball"
113 RELEASE_PREAMBLE=$( cat <<PREAMBLE
114 $RELEASE_PREAMBLE_HEAD
115 -------------------------------------------------------
116
117 To download and extract the source for the current release of Evergreen, issue
118 the following commands as the *user* Linux account:
119
120 [source, bash]
121 ------------------------------------------------------------------------------
122 wget -c http://evergreen-ils.org/downloads/${PREVIEW_TEXT}Evergreen-ILS-$VERSION.tar.gz
123 tar xzf Evergreen-ILS-$VERSION.tar.gz
124 ------------------------------------------------------------------------------
125
126 PREAMBLE
127 )
128
129 # This defines what the preamble comes before
130 RELEASE_PREAMBLE_BEFORE="Preamble: Developer instructions"
131
132 if [ $PREV_BRANCH != "TAG" -a $PREV_BRANCH != "PACKAGE" ]; then
133     if [ $PREV_BRANCH = "AUTO" ]; then
134         echo "AUTO PREVIOUS BRANCH"
135         PREV_BRANCH=`echo ${UNDER_VERSION%_*}`
136         PREV_BRANCH=`git branch -r | grep "rel_${PREV_BRANCH}_[^_]\+$" | sort -rV | head -n1`
137         PREV_BRANCH=`echo $PREV_BRANCH`
138         read -p "Does branch $PREV_BRANCH look like the previous version (y/n)?"
139         if [ "$REPLY" != "y" -a "$REPLY" != 'Y' ]; then
140             echo "Please specify the previous branch as second parameter. To continue auto-version, use AUTO as first parameter."
141             exit 1
142         fi
143     fi
144     git show $PREV_BRANCH &>/dev/null
145     if [ $? -ne 0 -o -z "$PREV_BRANCH" ]; then
146         echo "PREVIOUS VERSION COMMIT NOT FOUND";
147         exit 1
148     fi
149     if [ $PREV_VERSION = "AUTO" ]; then
150         echo "AUTO PREVIOUS VERSION"
151         PREV_BRANCH_END=`echo $PREV_BRANCH | sed 's|.*/||'`
152         PREV_VERSION=`echo $PREV_BRANCH_END | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2.\3-\4/'`
153         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)$/\1.\2.\3/'`
154         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)_\(.\+\)$/\1.\2-\3/'`
155         PREV_VERSION=`echo $PREV_VERSION | sed 's/^rel_\([0-9]\+\)_\([0-9]\+\)$/\1.\2/'`
156         if [ "$PREV_VERSION" = "$PREV_BRANCH_END" ]; then
157             echo "AUTO PREVIOUS VERSION FAILED."
158             exit 1
159         fi
160     fi
161     echo "Previous Version: $PREV_VERSION"
162 else
163     echo "Tagging or packaging, no need for previous version"
164 fi
165
166 if [ $PREV_BRANCH != "PACKAGE" ]; then
167
168     echo "Applying to Application.pm - HEAD -> $DASH_VERSION"
169     echo "Alt: $SHORT_DASH_VERSION -> $DASH_VERSION"
170     sed -i -e "s/\"$SHORT_DASH_VERSION[^\"]*\"/\"$DASH_VERSION\"/" -e "s/\"HEAD\"/\"$DASH_VERSION\"/" $GIT_ABS/Open-ILS/src/perlmods/lib/OpenILS/Application.pm
171
172     if [ -f "$GIT_ABS/Open-ILS/xul/staff_client/windowssetup.nsi" ]; then
173         echo "Applying to windowssetup.nsi - Master -> $SHORT_VERSION"
174         sed -i "s/Master/$SHORT_VERSION/" $GIT_ABS/Open-ILS/xul/staff_client/windowssetup.nsi
175     fi
176
177     echo "Applying to README:"
178     echo "STAMP_ID with $UNDER_VERSION"
179     sed -i -e "s/STAMP_ID=rel_[^ ]*/STAMP_ID=rel_$UNDER_VERSION/" $GIT_ABS/README
180
181     if [ $PREV_BRANCH != "TAG" ]; then
182         if [ "$(grep "$RELEASE_PREAMBLE_HEAD" $GIT_ABS/README )" ]; then
183             echo "Updating old download links"
184             sed -i -e "s|\(previews/\)\?Evergreen-ILS-.*\.tar\.gz|${PREVIEW_TEXT}Evergreen-ILS-$VERSION.tar.gz|" $GIT_ABS/README
185             sed -i -e "s| Evergreen-ILS-.*\.tar\.gz| Evergreen-ILS-$VERSION.tar.gz|" $GIT_ABS/README
186         else
187             echo "Adding Download Preamble"
188             perl -pi -e "s|^|$RELEASE_PREAMBLE\n\n| if /$RELEASE_PREAMBLE_BEFORE/" $GIT_ABS/README
189         fi
190     fi
191
192     echo "Applying to configure.ac:"
193     echo "AC_INIT and AM_INIT_AUTOMAKE entries"
194     sed -i -e "s/^\(AC_INIT(Open-ILS, \)[^,]*,/\1$VERSION,/" -e "s/^\(AM_INIT_AUTOMAKE(\[[^]]*], \[\)[^]]*]/\1$VERSION]/" configure.ac
195
196     echo "Finding/updating old \$HeadURL\$ entries"
197     HEADURLBASE=`echo ${HEADURLBASE} | sed 's/\?/\\?/'`
198     for file in `grep -Rl --exclude=make_release "$HEADURLBASE" $GIT_ABS`
199     do
200         echo $file
201         sed -i "s|${HEADURLBASE}[A-Za-z0-9_]*|$HEADURL|" $file
202     done
203
204     echo "Applying \$HeadURL\$ - $HEADURL"
205     for file in `grep -Rl --exclude=make_release '\\$HeadURL\\$' $GIT_ABS`
206     do
207         echo $file
208         sed -i "s|\\\$HeadURL\\\$|$HEADURL|" $file
209     done
210
211     if [ "$PREV_BRANCH" = "TAG" ]; then
212         echo "Committing (but not pushing) the lot of it"
213         git commit -asm "Tagging version"
214         exit 0;
215     fi
216
217     echo "Applying version to 002.schema.config.sql:"
218     echo "config.upgrade_log entry for $VERSION"
219     sed -i -e "s/\(INSERT INTO config.upgrade_log (version[^)]*) VALUES ('\)[0-9]*\('.*;\).*/&\n\1$VERSION\2/" $GIT_ABS/Open-ILS/src/sql/Pg/002.schema.config.sql;
220
221     if [ "$NO_UPGRADE" = "AUTO" ]; then
222         echo "Checking for DB upgrade potential...."
223         git ls-tree --name-only $PREV_BRANCH -- Open-ILS/src/sql/Pg/upgrade/ | cut -d/ -f6 | cut -d. -f1 | sort > old_upgrades.txt
224         git ls-tree --name-only HEAD -- Open-ILS/src/sql/Pg/upgrade/ | cut -d/ -f6 | cut -d. -f1 | sort > new_upgrades.txt
225         UPGRADE_CHECK=`diff old_upgrades.txt new_upgrades.txt | grep '^>' | cut -d' ' -f2`
226         UPGRADE_FILE=Open-ILS/src/sql/Pg/version-upgrade/$PREV_VERSION-$VERSION-upgrade-db.sql
227         if [ -f "$UPGRADE_FILE" ]; then
228             echo "Upgrade script for $PREV_VERSION-$VERSION already exists. Skipping."
229             UPGRADE_FILE=""
230         elif [ -n "$UPGRADE_CHECK" ]; then
231             echo "Found Upgrade Files! Building Upgrade Script."
232             echo "--Upgrade Script for $PREV_VERSION to $VERSION" > $UPGRADE_FILE
233             echo "\set eg_version '''$VERSION'''" >> $UPGRADE_FILE
234             echo "BEGIN;" >> $UPGRADE_FILE
235             grep "config.upgrade_log.*$VERSION" Open-ILS/src/sql/Pg/002.schema.config.sql >> $UPGRADE_FILE
236             for NUM in $UPGRADE_CHECK; do
237                 cat Open-ILS/src/sql/Pg/upgrade/$NUM.* 2>/dev/null | grep -v '^\s*\(BEGIN\|COMMIT\);\s*$' >> $UPGRADE_FILE
238             done;
239             echo "COMMIT;" >> $UPGRADE_FILE
240             MAYBE_DUPES=`grep -oP 'CREATE (OR REPLACE )?FUNCTION +\K[^ ]*(?= *\()' $UPGRADE_FILE | sort | grep -P '^(.*)\n\1$' | sort -u`
241             if [ -n "$MAYBE_DUPES" ]; then
242                 echo ""
243                 echo "The following functions may be needlessly duplicated in the upgrade script:"
244                 echo "$MAYBE_DUPES"
245                 echo ""
246                 echo "For reference, I am writing the list to maybe_dupes.txt"
247                 echo "$MAYBE_DUPES" > maybe_dupes.txt
248             fi
249             echo ""
250             if [ "$UPGRADE_PREVIEW" = "YES" ]; then
251                 read -p "Please manually check the upgrade file."
252                 ${EDITOR:-vi} $UPGRADE_FILE
253             fi;
254             git add $UPGRADE_FILE
255         fi
256     fi
257
258     grep -i -m 1 Signed-off-by ChangeLog &> /dev/null
259     if [ $? -ne 0 ]; then
260         echo "Building ChangeLog"
261         git log --cherry-pick --right-only --no-merges --pretty --summary --numstat $PREV_BRANCH..HEAD > $GIT_ABS/ChangeLog
262     else
263         echo "Not overwriting existing ChangeLog!"
264     fi
265
266     echo "Committing (but not pushing) the lot of it"
267     COMMIT_MESSAGE="Bumping version numbers and adding Changelog"
268     if [ -n "$UPGRADE_FILE" ]; then
269         COMMIT_MESSAGE="Bumping version numbers, adding Upgrade Script and Changelog"
270     fi
271     git commit -asm "$COMMIT_MESSAGE"
272 fi
273
274 echo "Building release dump"
275 cd $GIT_ABS
276 mkdir -p ../release
277 git archive --prefix=Evergreen-ILS-$VERSION/ HEAD | (cd ../release && tar xf -)
278 cd ../release/Evergreen-ILS-$VERSION
279 if [ -f ./autogen.sh ]; then
280     echo "Running autogen"
281     ./autogen.sh
282 else
283     echo "Running autoreconf"
284     autoreconf -i
285 fi
286
287 if [ "$SKIP_I18N" = "NO" ]; then
288     cd build/i18n
289     echo "Building i18n"
290     make install_all_locales
291     echo "i18n Cleanup"
292     cd ..
293     rm -rf i18n
294     cd ..
295 fi;
296
297 echo "Installing Dojo"
298 if [ ! -f "../dojo.tgz" ]; then
299     wget http://evergreen-ils.org/downloads/dojo.tgz -O ../dojo.tgz
300 fi
301 tar xzf ../dojo.tgz -C Open-ILS/web/js/dojo/
302
303 # set version for later
304 cd Open-ILS/xul/staff_client
305 XULRUNNER_VERSION=`grep '^XULRUNNER_VERSION' Makefile.am`
306 XULRUNNER_VERSION=${XULRUNNER_VERSION##XULRUNNER_VERSION=}
307
308 echo "Prepping server download files"
309 cd ../../../../
310 tar czf Evergreen-ILS-$VERSION.tar.gz Evergreen-ILS-$VERSION/
311 md5sum Evergreen-ILS-$VERSION.tar.gz > Evergreen-ILS-$VERSION.tar.gz.md5
312 if [ $PREV_BRANCH != "PACKAGE" ]; then # We need to have tagged to do this ;)
313     cp Evergreen-ILS-$VERSION/ChangeLog ChangeLog-$PREV_VERSION-$VERSION
314 fi
315
316 echo "Running enough of configure to build staff client"
317 cd Evergreen-ILS-$VERSION/
318 ./configure --disable-core --disable-web --disable-updates --disable-apache-modules --disable-reporter
319
320 echo "Building Release Staff Clients"
321 cd Open-ILS/xul/staff_client
322
323 echo "Grabbing XULRunner (to avoid issues with version changes)"
324 wget http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/$XULRUNNER_VERSION/runtimes/xulrunner-$XULRUNNER_VERSION.en-US.win32.zip
325 wget http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/$XULRUNNER_VERSION/runtimes/xulrunner-$XULRUNNER_VERSION.en-US.linux-i686.tar.bz2
326 wget http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/$XULRUNNER_VERSION/runtimes/xulrunner-$XULRUNNER_VERSION.en-US.linux-x86_64.tar.bz2
327
328 make rigrelease
329 make STAFF_CLIENT_STAMP_ID=rel_$UNDER_VERSION build
330 make win-client
331 mv evergreen_staff_client_setup.exe ../../../../evergreen-setup-$VERSION.exe
332 make linux32-client
333 mv evergreen_staff_client_i686.tar.bz2 ../../../../evergreen-client-${VERSION}_i686.tar.bz2
334 make linux64-client
335 mv evergreen_staff_client_x86_64.tar.bz2 ../../../../evergreen-client-${VERSION}_x86_64.tar.bz2
336 cd ../../../../
337 md5sum evergreen-setup-$VERSION.exe > evergreen-setup-$VERSION.exe.md5
338 md5sum evergreen-client-${VERSION}_i686.tar.bz2 > evergreen-client-${VERSION}_i686.tar.bz2.md5
339 md5sum evergreen-client-${VERSION}_x86_64.tar.bz2 > evergreen-client-${VERSION}_x86_64.tar.bz2.md5
340
341
342 echo "Removing build directory"
343 rm -rf Evergreen-ILS-$VERSION/
344
345 echo ""
346 echo "FOR YOU TODO:"
347 echo "* TEST the release"
348 if [ $PREV_BRANCH != "PACKAGE" ]; then
349     echo "* Push release branch"
350 fi
351 echo "* Upload files"
352 echo "* Send emails"