setup.sh - Make each step optional (but enabled by default)
authorTim Otten <totten@civicrm.org>
Sat, 3 Jan 2015 03:05:48 +0000 (19:05 -0800)
committerTim Otten <totten@civicrm.org>
Sat, 3 Jan 2015 20:25:14 +0000 (12:25 -0800)
The question of running "composer install", "npm install", etc., as part of
setup.sh is a bit curious:

 * For most Civi developers and automated tasks tasks, they run
   "git pull && setup.sh" once a day to get up-to-date. This
   implies that automatic downloading ("composer install") is good.
 * For a developer who's explicitly testing/changing dependencies,
   it may be useful to reset the DB without re-downloading (because
   re-downloading would trample their changes).

This patch takes each major step in setup.sh ("download dependencies", "run
gencode", "init DB") and allows them to be run separately.

bin/setup.sh

index 4e47fbb88f3d4f57431860437a5b211d8a35930b..9696cd5c2f529c3bd11b15c377e357f6899056bf 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/env bash
 set -e
-set -x
 
 CALLEDPATH=`dirname $0`
 
@@ -21,10 +20,86 @@ source "$CALLEDPATH/setup.conf"
 source "$CALLEDPATH/setup.lib.sh"
 
 if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
-  echo; echo Usage: setup.sh [schema file] [database data file] [database name] [database user] [database password] [database host] [database port] [additional args]; echo
+  echo
+  echo "Usage: setup.sh [options] [schema file] [database data file] [database name] [database user] [database password] [database host] [database port] [additional args]"
+  echo "[options] is a mix of zero or more of following:"
+  echo "  -a     (All)       Implies -Dgsdf (default)"
+  echo "  -D     (Download)  Download depdencies"
+  echo "  -g     (GenCode)   Generate DAO files, SQL files, etc"
+  echo "  -s     (Schema)    Load new schema in DB"
+  echo "  -d     (Data)      Load default data in DB"
+  echo "  -f     (Flush)     Flush caches and settings"
+  echo
+  echo "Example: Perform a full setup"
+  echo "   setup.sh -a"
+  echo
+  echo "Example: Setup all the code but leave the DB alone"
+  echo "   setup.sh -Dg"
+  echo
+  echo "Example: Keep the existing code but reset the DB"
+  echo "   setup.sh -sdf"
   exit 0
 fi
 
+###############################################################################
+## Parse command line options
+
+FOUND_ACTION=
+DO_DOWNLOAD=
+DO_GENCODE=
+DO_SCHEMA=
+DO_DATA=
+DO_FLUSH=
+
+while getopts "aDgsdf" opt; do
+  case $opt in
+    a)
+      DO_DOWNLOAD=1
+      DO_GENCODE=1
+      DO_SCHEMA=1
+      DO_DATA=1
+      DO_FLUSH=1
+      FOUND_ACTION=1
+      ;;
+    D)
+      DO_DOWNLOAD=1
+      FOUND_ACTION=1
+      ;;
+    g)
+      DO_GENCODE=1
+      FOUND_ACTION=1
+      ;;
+    s)
+      DO_SCHEMA=1
+      FOUND_ACTION=1
+      ;;
+    d)
+      DO_DATA=1
+      FOUND_ACTION=1
+      ;;
+    f)
+      DO_FLUSH=1
+      FOUND_ACTION=1
+      ;;
+    \?)
+      echo "Invalid option: -$OPTARG" >&2
+      exit 1
+      ;;
+    :)
+      echo "Option -$OPTARG requires an argument." >&2
+      exit 1
+      ;;
+  esac
+done
+if [ -z "$FOUND_ACTION" ]; then
+  DO_DOWNLOAD=1
+  DO_GENCODE=1
+  DO_SCHEMA=1
+  DO_DATA=1
+  DO_FLUSH=1
+fi
+
+shift $((OPTIND-1))
 
 # fetch command line arguments if available
 if [ ! -z "$1" ] ; then SCHEMA="$1"; fi
@@ -49,68 +124,82 @@ if [ -z $DBPASS ] ; then
   DBPASS=$REPLY
 fi
 
-# download any PHP dependencies
-pushd "$CALLEDPATH/.."
-  composer install
-  # Install npm and bower modules
-  npm install
-popd
+MYSQLCMD=$(mysql_cmd)
+###############################################################################
+## Execute tasks
+set -x
+
+if [ -n "$DO_DOWNLOAD" ]; then
+  pushd "$CALLEDPATH/.."
+    composer install
+    # Install npm and bower modules
+    npm install
+  popd
+fi
 
 # run code generator if it's there - which means it's
 # checkout, not packaged code
-if [ -d "$CALLEDPATH/../xml" ]; then
-  cd "$CALLEDPATH/../xml"
-  if [ -z "$DBPORT" ]; then
-    PHP_MYSQL_HOSTPORT="$DBHOST"
-  else
-    PHP_MYSQL_HOSTPORT="$DBHOST:$DBPORT"
-  fi
-  "$PHP5PATH"php -d mysql.default_host="$PHP_MYSQL_HOSTPORT" -d mysql.default_user=$DBUSER -d mysql.default_password=$DBPASS GenCode.php $SCHEMA '' ${GENCODE_CMS}
+if [ -n "$DO_GENCODE" -a -d "$CALLEDPATH/../xml" ]; then
+  pushd "$CALLEDPATH/../xml"
+    if [ -z "$DBPORT" ]; then
+      PHP_MYSQL_HOSTPORT="$DBHOST"
+    else
+      PHP_MYSQL_HOSTPORT="$DBHOST:$DBPORT"
+    fi
+    "$PHP5PATH"php -d mysql.default_host="$PHP_MYSQL_HOSTPORT" -d mysql.default_user=$DBUSER -d mysql.default_password=$DBPASS GenCode.php $SCHEMA '' ${GENCODE_CMS}
+  popd
 fi
 
-cd "$CALLEDPATH/../sql"
-echo; echo "Dropping civicrm_* tables from database $DBNAME"
-# mysqladmin -f -u $DBUSER $PASSWDSECTION $DBARGS drop $DBNAME
-MYSQLCMD=$(mysql_cmd)
-echo "SELECT table_name FROM information_schema.TABLES  WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'VIEW'" \
-  | $MYSQLCMD \
-  | grep '^\(civicrm_\|log_civicrm_\)' \
-  | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop view " $1 ";"}' \
-  | $MYSQLCMD
-echo "SELECT table_name FROM information_schema.TABLES  WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'BASE TABLE'" \
-  | $MYSQLCMD \
-  | grep '^\(civicrm_\|log_civicrm_\)' \
-  | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop table " $1 ";"}' \
-  | $MYSQLCMD
-
-
-echo; echo Creating database structure
-$MYSQLCMD < civicrm.mysql
-
-# load civicrm_generated.mysql sample data unless special DBLOAD is passed
-if [ -z $DBLOAD ]; then
-    echo; echo Populating database with example data - civicrm_generated.mysql
-    $MYSQLCMD < civicrm_generated.mysql
-else
-    echo; echo Populating database with required data - civicrm_data.mysql
-    $MYSQLCMD < civicrm_data.mysql
-    echo; echo Populating database with $DBLOAD data
-    $MYSQLCMD < $DBLOAD
+if [ -n "$DO_SCHEMA" ]; then
+  pushd "$CALLEDPATH/../sql"
+    echo; echo "Dropping civicrm_* tables from database $DBNAME"
+    echo "SELECT table_name FROM information_schema.TABLES  WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'VIEW'" \
+      | $MYSQLCMD \
+      | grep '^\(civicrm_\|log_civicrm_\)' \
+      | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop view " $1 ";"}' \
+      | $MYSQLCMD
+    echo "SELECT table_name FROM information_schema.TABLES  WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'BASE TABLE'" \
+      | $MYSQLCMD \
+      | grep '^\(civicrm_\|log_civicrm_\)' \
+      | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop table " $1 ";"}' \
+      | $MYSQLCMD
+
+    echo; echo Creating database structure
+    $MYSQLCMD < civicrm.mysql
+  popd
 fi
 
-# load additional script if DBADD defined
-if [ ! -z $DBADD ]; then
-    echo; echo Loading $DBADD
-    $MYSQLCMD < $DBADD
+if [ -n "$DO_DATA" ]; then
+  pushd "$CALLEDPATH/../sql"
+    # load civicrm_generated.mysql sample data unless special DBLOAD is passed
+    if [ -z $DBLOAD ]; then
+        echo; echo Populating database with example data - civicrm_generated.mysql
+        $MYSQLCMD < civicrm_generated.mysql
+    else
+        echo; echo Populating database with required data - civicrm_data.mysql
+        $MYSQLCMD < civicrm_data.mysql
+        echo; echo Populating database with $DBLOAD data
+        $MYSQLCMD < $DBLOAD
+    fi
+
+    # load additional script if DBADD defined
+    if [ ! -z $DBADD ]; then
+        echo; echo Loading $DBADD
+        $MYSQLCMD < $DBADD
+    fi
+  popd
 fi
 
-# run the cli script to build the menu and the triggers
-cd "$CALLEDPATH/.."
-"$PHP5PATH"php bin/cli.php -e System -a flush --triggers 1 --session 1
+if [ -n "$DO_FLUSH" ]; then
+  pushd "$CALLEDPATH/.."
+    # run the cli script to build the menu and the triggers
+    "$PHP5PATH"php bin/cli.php -e System -a flush --triggers 1 --session 1
 
-# reset config_backend and userFrameworkResourceURL which gets set
-# when config object is initialized
-$MYSQLCMD -e "UPDATE civicrm_domain SET config_backend = NULL; UPDATE civicrm_setting SET value = NULL WHERE name = 'userFrameworkResourceURL';"
+    # reset config_backend and userFrameworkResourceURL which gets set
+    # when config object is initialized
+    $MYSQLCMD -e "UPDATE civicrm_domain SET config_backend = NULL; UPDATE civicrm_setting SET value = NULL WHERE name = 'userFrameworkResourceURL';"
+  popd
+fi
 
-echo; echo "Setup Complete. Logout from your CMS to avoid session conflicts."
+echo; echo "NOTE: Logout from your CMS to avoid session conflicts."