Skip to content

Instantly share code, notes, and snippets.

@zhangtaihao
Created November 30, 2010 19:21
Show Gist options
  • Save zhangtaihao/722217 to your computer and use it in GitHub Desktop.
Save zhangtaihao/722217 to your computer and use it in GitHub Desktop.
Use this script to archive a specific release of the project. This script exports a git project with a specific object (e.g. tag), adds version information to the module <code>*.info</code> files and packages the project into properly named archive.
To start using the script, download the script and place it in the parent directory of your git project folders. For security reasons, the uploaded script below is named as a text file. If desired, the script can be renamed.
<h3>Usage</h3>
<kbd>export-project [-D &lt;output_dir&gt;] [-C &lt;working_dir&gt;] [-z|-g] [-x &lt;ext&gt;]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-N] [-P] [-A|-F &lt;url&gt;] &lt;project&gt; &lt;tree-ish&gt; [&lt;version&gt;]</kbd>
<h4>Arguments</h4>
<kbd>&lt;project&gt;</kbd>
<p class="marginleft">Project folder name. The project name must not contain directory path other than the name of the folder itself. To refer to another directory, use <kbd>-C</kbd> below instead.</p>
<kbd>&lt;tree-ish&gt;</kbd>
<p class="marginleft">Git object under project folder to export.</p>
<kbd>&lt;version&gt;</kbd>
<p class="marginleft"><em>Optional</em>. Specify this string to override the git object name in producing the version information and resulting archive file name.</p>
<h4>Options</h4>
<kbd>-D &lt;output_dir&gt;</kbd>
<p class="marginleft">Set the project archive output directory. The default is &quot;<kbd>releases</kbd>&quot;.</p>
<kbd>-C &lt;working_dir&gt;</kbd>
<p class="marginleft">Change the project working directory to <kbd>&lt;working_dir&gt;</kbd>. Use this option to specify the parent directory of the project folder.</p>
<kbd>-z&nbsp;-g</kbd>
<p class="marginleft">Archive format to use for storage. To archive as a zip file, use <kbd>-z</kbd>. To archive as a gzipped tarball, use <kbd>-g</kbd>. The default is <kbd>-z</kbd>.</p>
<kbd>-x &lt;ext&gt;</kbd>
<p class="marginleft">Specify output archive file extension. Do not include leading or trailing dots.</p>
<kbd>-N</kbd>
<p class="marginleft">Do not add version to info files.</p>
<kbd>-P</kbd>
<p class="marginleft">Print &quot;<code>project</code>&quot; tag in the info files.</p>
<kbd>-A</kbd>
<p class="marginleft">Use Acton Drupal feature server (i.e. <a href="http://drupal.anu.edu.au/fserver">http://drupal.anu.edu.au/fserver</a>) as the project status server in &quot;<code>project status url</code>&quot;.</p>
<kbd>-F &lt;url&gt;</kbd>
<p class="marginleft">Use <kbd>&lt;url&gt;</kbd> as the project status server in&nbsp;&quot;<code>project status url</code>&quot;.</p>
<h4>Examples</h4>
<kbd>export-project acton 6.x-1.4</kbd>
<kbd>export-project anu_site_manager HEAD 6.x-1.5-dev</kbd>
<kbd>export-project -D /tmp/acton&nbsp;anu_site_manager HEAD 6.x-1.5-dev</kbd>
<kbd>export-project -gPA anu_site_manager HEAD 6.x-1.5-dev</kbd>
<h3>Change log</h3>
<p class="marginleft"><strong>29/09/2010</strong>:&nbsp;Fixed script for Mac <kbd>mktemp</kbd>.</p>
<p class="marginleft"><strong>13/10/2010</strong>: Added &quot;project&quot; info insertion capability.</p>
<p class="marginleft"><strong>29/10/2010</strong>: Script now injects packaging date.</p>
<p class="marginleft"><strong>11/11/2010</strong>: Added options for disabling info file alteration and specifying file extension. Script now also checks for existence of programs to use.</p>
#!/bin/bash
CMD=`basename $0`
CWD=`pwd`
ARGNUM=2
print_usage() {
echo "Usage: $CMD [-D <output_dir>] [-C <working_dir>] [-z|-g] [-x <ext>]"
echo " [-N] [-P] [-A|-F <url>] <project> <tree-ish> [<version>]"
exit 1
}
check_program() {
type -P "$1" &>/dev/null || { echo "$1 not found. Aborting." >&2; exit 1; }
}
EXPORTDIR="$PWD/releases"
COMPRESS="z"
EXT=""
NOVERSION=""
FSERVER=""
PROJECTSTR=""
ACTONDRUPAL="http://drupal.anu.edu.au/fserver"
while getopts "D:C:zgx:NPAF:" OPTION
do
case $OPTION in
D ) EXPORTDIR=$OPTARG;;
C ) CWD=$OPTARG;;
z ) COMPRESS="z";;
g ) COMPRESS="g";;
x ) EXT=$OPTARG;;
N ) NOVERSION="1";;
P ) PROJECTSTR="1";;
F ) FSERVER=$OPTARG;;
A ) FSERVER=$ACTONDRUPAL;;
* ) print_usage;;
esac
done
shift $(($OPTIND - 1))
# Argument count check
if [ $# -lt $ARGNUM ]; then
print_usage
fi
# Check programs
check_program git
if [ $COMPRESS = "z" ]; then
check_program zip
fi
if [ $COMPRESS = "g" ]; then
check_program tar
fi
check_program mktemp
# Argument preprocessing
if [ ! -d "$CWD" ]; then
echo "$CMD: invalid working directory."
exit 2
else
pushd "$CWD" > /dev/null
CWD="$PWD"
popd > /dev/null
fi
if [ ! -d "$EXPORTDIR" ]; then
mkdir -p "$EXPORTDIR"
fi
if [ "$?" != "0" ]; then
echo "$CMD: output directory cannot be created."
exit 2
fi
pushd "$EXPORTDIR" > /dev/null
EXPORTDIR="$PWD"
popd > /dev/null
PROJECT=$1
TREE=$2
VERSION=$3
WRITEINFO=""
cd $CWD
# Argument validation
if [ ! -d "$PROJECT" ] || [ "`dirname "$PROJECT"`" != '.' ]; then
echo "$CMD: invalid project."
exit 2
fi
PROJECTDIR="$PWD/$PROJECT"
cd "$PROJECTDIR"
git ls-tree "$TREE" > /dev/null
if [ "$?" != "0" ]; then
echo "$CMD: invalid tree-ish object."
exit 2
fi
# Archive extension
if [ -z "$EXT" ]; then
if [ $COMPRESS = "z" ]; then
EXT="zip"
else
EXT="tar.gz"
fi
fi
# Default version number
if [ -z "$VERSION" ]; then
VERSION="$TREE"
fi
# Create temporary working directory
TMPDIR="`mktemp -d /tmp/export-project.XXXXXX`"
mkdir -p "$TMPDIR/$PROJECT"
# Export
echo -n "Exporting project..."
cd "$PROJECTDIR"
git archive "$TREE" | tar -xC "$TMPDIR/$PROJECT"
if [ "$?" != "0" ]; then
echo "error exporting tree."
exit 3
fi
echo "done"
INFO="\n\n; Info added by export script on `date +%F`"
# Add version
if [ -z "$NOVERSION" ]; then
INFO="$INFO\nversion = \"$VERSION\""
WRITEINFO="1"
fi
# Prepare project string
if [ -n "$PROJECTSTR" ]; then
INFO="$INFO\nproject = \"$PROJECT\""
WRITEINFO="1"
fi
# Prepare feature server string
if [ -n "$FSERVER" ]; then
INFO="$INFO\nproject status url = \"$FSERVER\""
WRITEINFO="1"
fi
# Write info file
if [ -n "$WRITEINFO" ]; then
echo -n "Writing info..."
find "$TMPDIR/$PROJECT" | grep '\.info$' | while read -r f; do
echo -e "$INFO" >> "$f"
done
echo "done"
fi
# Compress
echo -n "Packaging project..."
cd "$TMPDIR"
ARCHIVE=""
case $COMPRESS in
z )
ARCHIVE="$PROJECT-$VERSION.$EXT"
zip -r "$ARCHIVE" "$PROJECT" > /dev/null
;;
g )
ARCHIVE="$PROJECT-$VERSION.$EXT"
tar -czf "$ARCHIVE" "$PROJECT" > /dev/null
;;
esac
if [ "$?" != "0" ]; then
echo "error archiving project."
rm -fR "$TMPDIR"
exit 4
fi
echo "done"
mv "$ARCHIVE" "$EXPORTDIR"
rm -fR "$TMPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment