maintenance/resources: Enable 'set -e' and 'set -u' in bash scripts
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 29 Apr 2015 01:02:50 +0000 (02:02 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 29 Apr 2015 01:04:22 +0000 (02:04 +0100)
* set -e: Abort on error.
* set -u: Emit error for undefined variables.

This removes the need for endless "&&"-chains and easy-to-forget
"|| exit 1" statements.

Change-Id: I41a1c0d1bd57b5e241b83224096d32876fd913af

maintenance/resources/update-oojs-ui.sh
maintenance/resources/update-oojs.sh

index b5a58c7..d0989e2 100755 (executable)
@@ -1,8 +1,8 @@
-#!/usr/bin/env bash
+#!/bin/bash -eu
 
 # This script generates a commit that updates our copy of OOjs UI
 
-if [ -n "$2" ]
+if [ -n "${2:-}" ]
 then
        # Too many parameters
        echo >&2 "Usage: $0 [<version>]"
@@ -14,18 +14,21 @@ TARGET_DIR="resources/lib/oojs-ui" # Destination relative to the root of the rep
 NPM_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'update-oojs-ui') # e.g. /tmp/update-oojs-ui.rI0I5Vir
 
 # Prepare working tree
-cd "$REPO_DIR" &&
-git reset composer.json && git checkout composer.json &&
-git reset $TARGET_DIR && git checkout $TARGET_DIR && git fetch origin &&
-git checkout -B upstream-oojs-ui origin/master || exit 1
+cd "$REPO_DIR"
+git reset composer.json
+git checkout composer.json
+git reset -- $TARGET_DIR
+git checkout -- $TARGET_DIR
+git fetch origin
+git checkout -B upstream-oojs-ui origin/master
 
 # Fetch upstream version
 cd $NPM_DIR
-if [ -n "$1" ]
+if [ -n "${1:-}" ]
 then
-       npm install "oojs-ui@$1" || exit 1
+       npm install "oojs-ui@$1"
 else
-       npm install oojs-ui || exit 1
+       npm install oojs-ui
 fi
 
 OOJSUI_VERSION=$(node -e 'console.log(require("./node_modules/oojs-ui/package.json").version);')
@@ -36,21 +39,21 @@ then
 fi
 
 # Copy files, picking the necessary ones from source and distribution
-rm -r "$REPO_DIR/$TARGET_DIR" || exit 1
-mkdir -p "$REPO_DIR/$TARGET_DIR/i18n" || exit 1
-mkdir -p "$REPO_DIR/$TARGET_DIR/images" || exit 1
-mkdir -p "$REPO_DIR/$TARGET_DIR/themes/mediawiki/images" || exit 1
-cp ./node_modules/oojs-ui/dist/{oojs-ui-mediawiki-noimages.css,oojs-ui-mediawiki.js,oojs-ui.js} "$REPO_DIR/$TARGET_DIR" || exit 1
-cp -R ./node_modules/oojs-ui/dist/i18n "$REPO_DIR/$TARGET_DIR" || exit 1
-cp -R ./node_modules/oojs-ui/dist/images "$REPO_DIR/$TARGET_DIR" || exit 1
-cp -R ./node_modules/oojs-ui/dist/themes/mediawiki/images "$REPO_DIR/$TARGET_DIR/themes/mediawiki" || exit 1
-cp ./node_modules/oojs-ui/src/themes/mediawiki/*.json "$REPO_DIR/$TARGET_DIR/themes/mediawiki" || exit 1
+rm -r "$REPO_DIR/$TARGET_DIR"
+mkdir -p "$REPO_DIR/$TARGET_DIR/i18n"
+mkdir -p "$REPO_DIR/$TARGET_DIR/images"
+mkdir -p "$REPO_DIR/$TARGET_DIR/themes/mediawiki/images"
+cp ./node_modules/oojs-ui/dist/{oojs-ui-mediawiki-noimages.css,oojs-ui-mediawiki.js,oojs-ui.js} "$REPO_DIR/$TARGET_DIR"
+cp -R ./node_modules/oojs-ui/dist/i18n "$REPO_DIR/$TARGET_DIR"
+cp -R ./node_modules/oojs-ui/dist/images "$REPO_DIR/$TARGET_DIR"
+cp -R ./node_modules/oojs-ui/dist/themes/mediawiki/images "$REPO_DIR/$TARGET_DIR/themes/mediawiki"
+cp ./node_modules/oojs-ui/src/themes/mediawiki/*.json "$REPO_DIR/$TARGET_DIR/themes/mediawiki"
 
 # Clean up temporary area
 rm -rf "$NPM_DIR"
 
 # Generate commit
-cd $REPO_DIR || exit 1
+cd $REPO_DIR
 
 COMMITMSG=$(cat <<END
 Update OOjs UI to v$OOJSUI_VERSION
@@ -64,4 +67,7 @@ END
 composer require oojs/oojs-ui $OOJSUI_VERSION --no-update
 
 # Stage deletion, modification and creation of files. Then commit.
-git add --update $TARGET_DIR && git add $TARGET_DIR && git add composer.json && git commit -m "$COMMITMSG" || exit 1
+git add --update $TARGET_DIR
+git add $TARGET_DIR
+git add composer.json
+git commit -m "$COMMITMSG"
index 1d5c2b1..b91cb28 100755 (executable)
@@ -1,8 +1,8 @@
-#!/usr/bin/env bash
+#!/bin/bash -eu
 
 # This script generates a commit that updates our copy of OOjs
 
-if [ -n "$2" ]
+if [ -n "${2:-}" ]
 then
        # Too many parameters
        echo >&2 "Usage: $0 [<version>]"
@@ -14,17 +14,19 @@ TARGET_DIR="resources/lib/oojs" # Destination relative to the root of the repo
 NPM_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'update-oojs') # e.g. /tmp/update-oojs.rI0I5Vir
 
 # Prepare working tree
-cd "$REPO_DIR" &&
-git reset $TARGET_DIR && git checkout $TARGET_DIR && git fetch origin &&
-git checkout -B upstream-oojs origin/master || exit 1
+cd "$REPO_DIR"
+git reset -- $TARGET_DIR
+git checkout -- $TARGET_DIR
+git fetch origin
+git checkout -B upstream-oojs origin/master
 
 # Fetch upstream version
 cd $NPM_DIR
-if [ -n "$1" ]
+if [ -n "${1:-}" ]
 then
-       npm install "oojs@$1" || exit 1
+       npm install "oojs@$1"
 else
-       npm install oojs || exit 1
+       npm install oojs
 fi
 
 OOJS_VERSION=$(node -e 'console.log(require("./node_modules/oojs/package.json").version);')
@@ -35,13 +37,13 @@ then
 fi
 
 # Copy file(s)
-rsync --force ./node_modules/oojs/dist/oojs.jquery.js "$REPO_DIR/$TARGET_DIR" || exit 1
+rsync --force ./node_modules/oojs/dist/oojs.jquery.js "$REPO_DIR/$TARGET_DIR"
 
 # Clean up temporary area
 rm -rf "$NPM_DIR"
 
 # Generate commit
-cd $REPO_DIR || exit 1
+cd $REPO_DIR
 
 COMMITMSG=$(cat <<END
 Update OOjs to v$OOJS_VERSION
@@ -52,4 +54,6 @@ END
 )
 
 # Stage deletion, modification and creation of files. Then commit.
-git add --update $TARGET_DIR && git add $TARGET_DIR && git commit -m "$COMMITMSG" || exit 1
+git add --update $TARGET_DIR
+git add $TARGET_DIR
+git commit -m "$COMMITMSG"