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