Skip to content

Instantly share code, notes, and snippets.

@beporter
Last active January 9, 2020 20:28
Show Gist options
  • Save beporter/31e7d1f5beeffda0da94 to your computer and use it in GitHub Desktop.
Save beporter/31e7d1f5beeffda0da94 to your computer and use it in GitHub Desktop.
Make the process of testing a `composer create-project` command easier by operating against your local working copy.
#!/usr/bin/env bash
# Allows you to test the create-project process using your local
# checked-out copy of the skeleton as the source. You MUST commit the
# changes you want to test to a git branch! You MUST name that branch
# as the first argument and the destination path to set up the fresh
# copy into as the second.
#
# Usage:
# - Place this script in your package's root directory and make it executable.
# - Set the PACKAGE_NAME variable below to match your composer.json's `name`.
# - Make changes to your package and commit them to a git branch.
# - Run `./composer-create-project-test.sh your-branch-name /target/dir`
# - The new copy of your project will be in /target/dir.
# Capture command line args, or set defaults.
BRANCH_NAME=${1:-master}
DEST_DIR=${2:-~/Desktop/newapp}
PACKAGE_NAME=vendor/package
# Warn the dev if they have uncommitted changes since they won't
# be included in the spawned project.
if ! git diff-index --quiet --cached HEAD; then
echo "## There are local uncommitted changes to the repo."
echo "## Running create-project now will NOT include these changes."
while true; do
read -p "## Are you sure you want to continue? [Y/n]: " yn
case $yn in
[Nn]* ) exit;;
[Yy]* ) break;;
* ) break;;
esac
done
fi
# Warn if the destination dir is non-empty, and offer to delete it.
# Composer will fail if the directory is not empty.
if [ -d "${DEST_DIR}" ] && [ "$(ls -A "${DEST_DIR}")" ]; then
echo "## The destination directory \`${DEST_DIR}\` is not empty."
while true; do
read -p "## Do you want to delete it and continue? [Y/n]: " yn
case $yn in
[Nn]* ) exit;;
[Yy]* ) rm -rf "${DEST_DIR}"; break;;
* ) rm -rf "${DEST_DIR}"; break;;
esac
done
fi
# Set up a packages.json file to use with create-project.
cat <<EOD > packages.json
{
"packages": {
"${PACKAGE_NAME}": {
"dev-master": {
"name": "${PACKAGE_NAME}",
"version": "dev-$BRANCH_NAME",
"source": {
"url": "./",
"type": "git",
"reference": "${BRANCH_NAME}"
}
}
}
}
}
EOD
# Execute the proper command.
composer create-project --repository-url=./packages.json ${PACKAGE_NAME} "${DEST_DIR}" dev-${BRANCH_NAME}
# Clean up after ourselves.
rm -f packages.json
@philwolstenholme
Copy link

philwolstenholme commented Sep 16, 2019

@beporter when I try this I get the following error:

[InvalidArgumentException]
  Package drupal-composer/drupal-project is missing reference information

This is the contents of the packages.json that gets generated:

{
  "packages": {
    "drupal-composer/drupal-project": {
      "dev-develop": {
        "name": "drupal-composer/drupal-project",
        "version": "dev-develop",
        "source": {
          "url": "./",
          "type": "git",
          "reference": ""
        }
      }
    }
  }
}


I don't suppose you know what I might have done wrong? Or maybe a Composer update means your script doesn't work anymore?

@philwolstenholme
Copy link

Ah! I had to change "reference": "" to "reference": "${BRANCH_NAME}" in the part of the script that generates the packages.json file.

@beporter
Copy link
Author

beporter commented Jan 9, 2020

Thanks for that catch. I've updated the script. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment