Skip to content

Instantly share code, notes, and snippets.

@gedex
Last active October 23, 2016 23:26
Show Gist options
  • Save gedex/f7441bb14fbb04058c24014d8d8db8c9 to your computer and use it in GitHub Desktop.
Save gedex/f7441bb14fbb04058c24014d8d8db8c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
CUR_DIR=$(pwd)
PRE_COMMIT_URL="https://gist.githubusercontent.com/gedex/f7441bb14fbb04058c24014d8d8db8c9/raw/5aadc9e6e42c8ddc1f79bfc57e15ef2b5f75b604/pre-commit"
PRE_COMMIT="/tmp/the-extendables-pre-commit"
echo "Your WP_PLUGIN_Dir. No trailing slash."
printf "($CUR_DIR): "
read -e input
input="${input%/}"
PLUGINS_DIR=${input:-$CUR_DIR}
printf "Overwrite pre-commit hook if exists in extension repo (Y|n)? "
read -e input
OVERWRITE="${input:-y}"
echo
function install_eslint {
echo "## Installing eslint"
if ! npm install -g eslint 2>/dev/null; then
echo "Failed to install eslint (npm install -g eslint)"
fi
}
function install_phpcs {
echo "## Installing phpcs"
if [ "$( type -t phpcs )" == '' ]; then
wget -nv -O "/usr/local/bin/phpcs" "https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar"
chmod +x "/usr/local/bin/phpcs"
else
echo "## Command phpcs already available. Skip installing."
fi
echo "## Installing WordPress-Coding-Standards"
if ! phpcs -i | grep -q 'WordPress'; then
GIT_WORK_TREE_BACKUP="$GIT_WORK_TREE"
unset GIT_WORK_TREE
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $HOME/WordPress-Coding-Standards
GIT_WORK_TREE="$GIT_WORK_TREE_BACKUP"
phpcs --config-set installed_paths $HOME/WordPress-Coding-Standards
else
echo "## WordPress-Coding-Standards already set. Skip installing."
fi
}
function is_cloned_extension {
if [ -d "$PLUGINS_DIR/$1" ]; then
if [ -d "$PLUGINS_DIR/$1/.git" ]; then
return 0
else
return 1
fi
else
return 1
fi
}
function cp_pre_commit {
if [ ! -f "$PLUGINS_DIR/$1/.git/hooks/pre-commit" ]; then
cp "$PRE_COMMIT" "$PLUGINS_DIR/$1/.git/hooks/pre-commit"
else
if [ "$OVERWRITE" != "y" ]; then
echo "## File pre-commit already exists. Skipping."
else
cp "$PRE_COMMIT" "$PLUGINS_DIR/$1/.git/hooks/pre-commit"
fi
fi
}
function install_pre_commit_hook {
wget -nv -O "$PRE_COMMIT" "$PRE_COMMIT_URL"
chmod +x $PRE_COMMIT
extendables_extensions=(
woocommerce-360-image
woocommerce-account-funds
woocommerce-additional-variation-images
woocommerce-anti-fraud
woocommerce-bookings
woocommerce-branding
woocommerce-brands
woocommerce-bulk-download
woocommerce-bulk-stock-management
woocommerce-checkout-field-editor
woocommerce-coupon-campaigns
woocommerce-currency-converter-widget
woocommerce-deposits
woocommerce-disability-vat-exemption
woocommerce-email-customizer
woocommerce-shipping-flat-rate-boxes
woocommerce-force-sells
woocommerce-give-products
woocommerce-jplayer-product-sampler
woocommerce-min-max-quantities
woocommerce-subscribe-to-newsletter
woocommerce-ninjaforms-product-addons
woocommerce-order-barcodes
woocommerce-pdf-watermark
woocommerce-shipping-per-product
woocommerce-photography
woocommerce-points-and-rewards
woocommerce-pre-orders
woocommerce-product-addons
woocommerce-product-csv-import-suite
woocommerce-product-enquiry-form
woocommerce-product-finder
woocommerce-product-gallery-slider
woocommerce-product-vendors
woocommerce-products-compare
woocommerce-quick-view
woocommerce-sale-flash-pro
woocommerce-sales-report-email
woocommerce-software-add-on
woocommerce-splash-popup
woocommerce-store-catalog-pdf-download
woocommerce-subscription-downloads
woocommerce-table-rate-shipping
woocommerce-terms-conditions-popup
wooslider-products-slideshow
woocommerce-box-office
woocommerce-accommodation-bookings
woocommerce-store-credit
woocommerce-advanced-notifications
woocommerce-gateway-2checkout-inline-checkout
woocommerce-gateway-2checkout-api
woocommerce-gateway-allied-wallet
woocommerce-gateway-amazon-payments-advanced
woocommerce-amazon-s3-storage
woocommerce-ambassador
woocommerce-gateway-anz-egate
woocommerce-shipping-aramex
woocommerce-shipping-australia-post
woocommerce-gateway-authipay
woocommerce-aweber-newsletter-subscription
woocommerce-gateway-bluepay
woocommerce-shipping-canada-post
woocommerce-clickatell-sms-notifications
woocommerce-sms-notifications
woocommerce-shipping-colissimo
woocommerce-gateway-datacash
woocommerce-drip
woocommerce-gateway-eway-shared-payments
woocommerce-shipping-fedex
woocommerce-gateway-firstdata-uk
woocommerce-freshdesk
woocommerce-help-scout
woocommerce-hipchat
woocommerce-gateway-sisow-ideal
woocommerce-instagram
woocommerce-interfax-integration
woocommerce-gateway-iveri-lite
woocommerce-gateway-levelup
woocommerce-mad-mimi
woocommerce-gateway-moip
woocommerce-gateway-mygate
woocommerce-shipping-new-zealand-post
woocommerce-gateway-nochex
woocommerce-gateway-pagamento-digital
woocommerce-gateway-pagseguro
woocommerce-gateway-payfast
woocommerce-gateway-paygate
woocommerce-gateway-paypal-adaptive-payments
woocommerce-gateway-paypal-pro
woocommerce-gateway-paypal-pro-hosted
woocommerce-gateway-payu-in
woocommerce-gateway-payu-pl
woocommerce-gateway-payza
woocommerce-shipping-postnl
woocommerce-shipping-purolator
woocommerce-shipping-royalmail
woocommerce-shipping-sapo-dps
woocommerce-shipping-sapo-ips
woocommerce-shipment-tracking
woocommerce-shipstation
woocommerce-slack
woocommerce-shipping-spee-dee
woocommerce-shipping-speed-services-couriers
woocommerce-shipping-stamps
woocommerce-gateway-stripe
woocommerce-taxamo
woocommerce-shipping-ups
woocommerce-uservoice-integration
woocommerce-shipping-usps
woocommerce-gateway-litle
woocommerce-gateway-vcs
woocommerce-gateway-vcs-direct
woocommerce-xero
woocommerce-gateway-affirm
woocommerce-gateway-paypal-powered-by-braintree
woocommerce-gateway-gocardless
box-packer
woocommerce-distance-rate-shipping
woocommerce-eu-vat-number
woocommerce-gateway-eway
woo-stamps-authenticator
woocommerce-shipping-multiple-addresses
woocommerce-follow-up-emails
woocommerce-warranty
woocommerce-review-for-discount
woocommerce-cart-add-ons
)
for ext in "${extendables_extensions[@]}"; do
if is_cloned_extension "${ext}"; then
echo "## Copying pre-hook template to $PLUGINS_DIR/$ext/.git/hooks"
cp_pre_commit $ext
fi
done
}
install_eslint
install_phpcs
install_pre_commit_hook
#!/usr/bin/env bash
pass=true
function lint_php_files {
phpfiles=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.php(:|$)')
if type phpcs 2>/dev/null; then
has_phpcs=true
else
has_phpcs=false
fi
echo "## PHP syntax check"
for phpfile in $phpfiles; do
php -lf "$phpfile"
if [ $? -ne 0 ]; then
printf "\033[31mPHP syntax check failed: %s\033[0m\n" "${phpfile}"
pass=false
else
printf "\033[32mPHP syntax check passed: %s\033[0m\n" "${phpfile}"
fi
if [ "$has_phpcs" == 'true' ]; then
phpcs --standard="WordPress-Core" $phpfile
if [ $? -ne 0 ]; then
printf "\033[31mWordPress Coding Standards check failed: %s\033[0m\n" "${phpfile}""]]"
pass=false
else
printf "\033[32mWordPress Coding Standards check passed: %s\033[0m\n" "${phpfile}""]]"
fi
fi
done
}
function lint_js_files {
jsfiles=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.js(:|$)')
if [ "$( type -f eslint )" == '' ]; then
echo "## Command eslint is not available. Skipping JS lint."
echo
has_phpcs=true
else
echo "## Running eslint on changed files"
has_phpcs=false
fi
for jsfile in $jsfiles; do
eslint --cache "$jsfile"
if [ $? -ne 0 ]; then
printf "\033[31mEslint Failed: %s\033[0m\n" "${jsfile}"
pass=false
else
printf "\033[32mEslint Passed: %s\033[0m\n" "${jsfile}"
fi
done
}
lint_php_files
lint_js_files
if ! $pass; then
printf "\n\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass validation tests but do not. Please fix the errors and try again.\n\n"
exit 1
else
printf "\n\033[42mCOMMIT SUCCEEDED\033[0m\n\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment