Skip to content

Instantly share code, notes, and snippets.

@beporter
Last active August 29, 2015 14:03
Show Gist options
  • Save beporter/eedb0c72662e12cdaecd to your computer and use it in GitHub Desktop.
Save beporter/eedb0c72662e12cdaecd to your computer and use it in GitHub Desktop.
Provides a pre-composer mechanism for setting up a copy of the CakePHP core project in a local directory and then symlinking it into a target Cake project path.

CakePHP Core Setup

These scripts automate the process of obtaining a copy of the CakePHP core at a specific point release, the latest point release for a minor version, or a specific commit. They also provide a way to symlink the Cake core into an existing Cake project, which is handy for keeping all of the core files out of your project repo.

Usage

  • Place add-cakephp-version.sh in a folder of its own.

  • Run ./add-cakephp-version.sh x.y[.z] (where x is a major version number, y is a minor version number and z is an optional point release) to clone the CakePHP official git repository and set up a folder named cake_x.y.z in the same folder as the script.

--

  • cake-core-setup.sh was originally written for use with vagrant. When defined as a provisioning script in your Vagrantfile as in the following example, the script will automatically download a Cake core inside the VM and create a symlink to it at /var/www/cake (inside the VM).

      config.vm.provision :shell, :path => "/vagrant/path/to/care-core-setup.sh"
    
  • (You should add /cake to your project's .gitignore file so the symlink isn't accidentally committed.)

  • You may have to edit the SOURCE_DIR, DEST_DIR and CAKE_RELEASE variables to suit your needs.

#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
This script will download and create a CakePHP "core" folder for
the version number specified as the command line argument in
whatever folder it lives in. You must relocate this script to its
own directory where it can spawn new folders for each Cake version
without interfering with a git repo. This should be somewhere
central on your system where all Cake projects can symlink to the
individual Cake version required.
Usage:
cakes/${0##*/} x.y[.z] [-f]
Pass a CakePHP release number (or major.minor for latest) as
the first argument.
Specify '-f' as the second argument to forcibly replace the
cakephp repo.
EOT
exit 0
}
if [ "$1" = '-h' ]; then
usage
fi
umask a+rw
DIR="$( cd -P "$( dirname "$0" )" >/dev/null 2>&1 && pwd )";
TMPDIR="${DIR}/tmp"
REPO=git://github.com/cakephp/cakephp.git
REUSE_REPO=1 # Change this to 0 to force the script to clone the whole cakephp repo every time.
# Force the cakephp repo to be deleted and recreated. Safe, but sloooowwww.
if [ "$2" = '-f' ]; then
REUSE_REPO=0
fi
# Just a little precaution since this script comes bundled in the
# CakePHP-Skeleton's bin/ folder, but should't be used from there.
if [ "${DIR##*/}" = 'bin' ]; then
echo "!!! The script has detected you are trying to run it from the bin/ folder."
echo "!!! You must move this script to its own folder outside of the git repo."
exit 2
fi
if [ -n "$1" ]; then
TAG=$1
else
usage
fi
# If configured, reuse the existing temp folder (if present).
if [ $REUSE_REPO -gt 0 ] && [ -d "${TMPDIR}" ]; then
echo "## Reusing existing tmp folder: ${TMPDIR}" >&2
else
# Delete a leftover temp folder if present.
if [ -d "${TMPDIR}" ]; then
echo "## Purging old tmp folder: ${TMPDIR}" >&2
rm -rf ${TMPDIR}
fi
echo "## Cloning cake repository from ${REPO}" >&2
git clone ${REPO} ${TMPDIR}
fi
echo "## Fetching updates from remote." >&2
cd ${TMPDIR}
git fetch
# "Fill in" the full version number if we were only given a major and minor.
NUM_DOTS=$( tr -dc '.' <<<"$TAG" | awk '{ print length; }' )
if [ $NUM_DOTS -lt 2 ]; then
echo "## Determining latest point release." >&2
POINTRELEASE=$( git tag -l | grep "^${TAG}[0-9\.]*$" | sed "s/${TAG}\.//" | sort -n | tail -1 )
TAG="${TAG}.${POINTRELEASE}"
fi
echo "## Verifying requested tag: ${TAG}" >&2
git show-ref --tags --quiet --verify -- "refs/tags/${TAG}"
if [ $? -ne 0 ]; then
echo "!! Tag '$TAG' does not exist in the repo. Please check your input and try again."
exit 1
fi
DESTDIR="${DIR}/cake_${TAG}"
# Automatically replace an existing copy of the version.
if [ -d "${DESTDIR}" ]; then
echo "## Recreating Cake core v${TAG}" >&2
rm -rf ${DESTDIR}
else
echo "## Creating Cake core v${TAG}" >&2
fi
echo "## Staging snapshot." >&2
mkdir ${DESTDIR}
git archive $TAG --format=zip > archive.zip
mv archive.zip ${DESTDIR}
echo "## Installing snapshot into destination." >&2
cd ${DESTDIR}
unzip -q archive.zip
echo "## Cleaning up." >&2
rm -f archive.zip
if [ $REUSE_REPO -eq 0 ]; then
rm -rf ${TMPDIR}
fi
echo "## Done: ${DESTDIR}" >&2
#!/usr/bin/env bash
# Copies the bundled `add-cakephp-version`script into a home inside the
# VM where it can (hopefully) be used to create working symlinks.
SOURCE_DIR=/vagrant/bin
DEST_DIR=/home/vagrant/cakes/
CAKE_RELEASE=1.3
# These next two are custom to cheat and create a tag for a specific commit. -bp
# CAKE_COMMIT=67c91aa009
# CAKE_TAG=1.2.0.6311-beta
echo "## Copying add-cake-version script into the VM."
ADD_CAKE_SCRIPT=add-cakephp-version.sh
mkdir -p "$DEST_DIR/"
cp "$SOURCE_DIR/$ADD_CAKE_SCRIPT" "$DEST_DIR"
echo "## Cloning (latest) CakePHP v${CAKE_RELEASE} to seed the local repo."
echo " (This make take a while the first time...)"
ADD_VERSION_OUTPUT=$( "$DEST_DIR/$ADD_CAKE_SCRIPT" $CAKE_RELEASE 2>&1 >/dev/null)
# This bit adds a local tag for the specific commit we're concerned about if CAKE_TAG and CAKE_COMMIT are set.
if [ -n "$CAKE_COMMIT" ] && [ -n "$CAKE_TAG" ]; then
JUNK=$( cd "$DEST_DIR/tmp"; git show-ref --tags --quiet --verify -- "refs/tags/${CAKE_TAG}")
if [ $? -ne 0 ]; then
echo "## Setting up special CakePHP v${CAKE_TAG} (${CAKE_COMMIT})."
cd "$DEST_DIR/tmp"
git checkout -q $CAKE_COMMIT
git tag $CAKE_TAG
fi
ADD_VERSION_OUTPUT=$( "$DEST_DIR/$ADD_CAKE_SCRIPT" $CAKE_TAG 2>&1 >/dev/null)
fi
echo "$ADD_VERSION_OUTPUT" # For the console.
CORE_DIR=$( echo "$ADD_VERSION_OUTPUT" | tail -1 | sed -r 's/^## Done: //') # To extract the path.
echo "## Symlinking CakePHP core into shared folder '$CORE_DIR/cake'."
$SOURCE_DIR/symlink-cake-core "$CORE_DIR/cake"
chown -R vagrant:www-data "$DEST_DIR"
chmod -R a+rx "$DEST_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment