Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active November 6, 2018 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andkirby/76c5f09d8c6bd55d0fe21a906d72defe to your computer and use it in GitHub Desktop.
Save andkirby/76c5f09d8c6bd55d0fe21a906d72defe to your computer and use it in GitHub Desktop.
Magento 2.x binary file auto-complete installation in Bash CLI.
#!/usr/bin/env bash
###################################
# Auto-complete for Magento2 console "bin/magento" file
#
# Download and install:
# $ curl -Ls https://gist.github.com/andkirby/76c5f09d8c6bd55d0fe21a906d72defe/raw/bash-magento2-autocomplete-install.sh | bash
#
# MANUAL INSTALLATION
# "bash-completion" installation:
# $ yum install -y bash-completion
# Install a package bamarni/symfony-console-autocomplete for your user:
# $ composer global require bamarni/symfony-console-autocomplete
#
# References:
# https://github.com/bamarni/symfony-console-autocomplete
# https://www.cyberciti.biz/faq/fedora-redhat-scientific-linuxenable-bash-completion/
##############################################################################
set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace
sudo_cmd() {
local docroot sud
# use sudo if possible
sud_cmd=$(sudo -h > /dev/null 2>&1 && echo sudo || true)
docroot=${DOCROOT_DIR:-/var/www/html}
# use only root or sudo
if [ $(whoami) != 'root' ] && [ -z "${sud_cmd}" ]; then
echo 'error: It cannot be done without root permissions. Seem you have no sudo. Login with root then.' > /dev/stderr
exit 2
fi
}
bash_completion_installed? () {
if [ ${package_manager} == 'yum' ]; then
${sud} ${package_manager} list installed bash-completion
elif [ ${package_manager} == 'apt-get' ]; then
${sud} dpkg -s bash-completion
fi
}
install_bash_completion() {
local package_manager=yum
if ! ${package_manager} --help 2> /dev/null 1> /dev/null; then
package_manager=apt-get
fi
if ! ${package_manager} --help 2> /dev/null 1> /dev/null; then
echo 'error: Cannot install bash-completion package. Could not find suitable package package manager: neither YUM or APT-GET.' > /dev/stderr
exit 2
fi
if ! bash_completion_installed? 1> /dev/null 2> /dev/null; then
${sud} ${package_manager} update
${sud} ${package_manager} install -y bash-completion
fi
}
install_autocomplete_owner() {
if [ -n "${DOCROOT_OWNER_USER:-}" ]; then
echo ${DOCROOT_OWNER_USER:-}
elif grep "magento:" /etc/passwd > /dev/null; then
echo "magento"
elif grep "www-data:" /etc/passwd > /dev/null; then
echo "www-data"
else
#echo "warning: Could not determine docroot owner. Using root." > /dev/stderr
echo "root"
fi
}
install_symfony_autocomplete() {
local sources_dir='/usr/share/symfony-autocomplete'
local binary="${sources_dir}/vendor/bin/symfony-autocomplete" \
link='/usr/bin/symfony-autocomplete' \
scripts_owner=''
if type symfony-autocomplete > /dev/null 2> /dev/null || [ -d ${link} ]; then
# binary already declared
return
fi
scripts_owner=$(install_autocomplete_owner)
mkdir -p "${sources_dir}"
chown ${scripts_owner} -R ${sources_dir}
su ${scripts_owner} -c \
"composer --working-dir=${sources_dir} require bamarni/symfony-console-autocomplete"
ln -s ${binary} ${link}
# set readable and accessible
chmod +rX ${sources_dir}
echo 'Installed binary:'
ls -l ${link} | cut -d' ' -f9-
}
install_magento_bin() {
local binary="${DOCROOT_DIR:-/var/www/html}/${APP_MAGENTO_BIN:-bin/magento}" \
link='/usr/bin/magento'
if type magento > /dev/null 2> /dev/null || [ -L ${link} ]; then
# binary already declared
return
fi
ln -s ${binary} ${link}
echo 'Installed binary:'
ls -l ${link} | cut -d' ' -f9-
}
install_complete_script() {
if [ -f /etc/bash_completion.d/console ]; then
echo 'It looks like you do have bash-completion.'
echo 'File /etc/bash_completion.d/console found.'
return
fi
# Copy and load file for autocompletion
${sud} sh -c 'curl -Ls https://gist.github.com/andkirby/76c5f09d8c6bd55d0fe21a906d72defe/raw/z-replace-autocomplete.sh \
--output /etc/bash_completion.d/console'
${sud} chmod +r /etc/bash_completion.d/console
echo 'Set auto-complete scripts to file: /etc/bash_completion.d/console'
echo 'It uses caching. Please remove /tmp/bash_completion_* files to clean up.'
}
install_bashrc_autoload_code() {
cat <<-'EOF'
# bash_completion
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
# EOB bash_completion
EOF
}
# inject loading /etc/bash_completion in .bashrc file
install_bashrc_autoload() {
# run it under root due to requiring a password for "su" by a user
if [ $(whoami) == 'root' ]; then
if ! grep "bash_completion" $(cd;pwd)/.bashrc > /dev/null; then
install_bashrc_autoload_code >> $(cd;pwd)/.bashrc
echo 'Added loading /etc/bash_completion in '$(cd;pwd)/.bashrc
fi
if ! grep "bash_completion" $(su $(install_autocomplete_owner) -c 'cd;pwd')/.bashrc > /dev/null; then
install_bashrc_autoload_code >> $(su $(install_autocomplete_owner) -c 'cd;pwd')/.bashrc
echo 'Added loading /etc/bash_completion in '$(su $(install_autocomplete_owner) -c 'cd;pwd')/.bashrc
fi
else
echo 'Please add this code into your ~/.bashrc file:'
install_bashrc_autoload_code
fi
}
install_autocomplete () {
local sud package_manager
sud="$(sudo_cmd)"
install_bash_completion
# not required installation
# it's need only for generation "autocomplete" script
#install_symfony_autocomplete
install_magento_bin
install_complete_script
install_bashrc_autoload
}
install_autocomplete
#!/bin/bash
_symfony()
{
local cur script com opts
COMPREPLY=()
_get_comp_words_by_ref -n : cur words
# for an alias, get the real script behind it
if [[ $(type -t ${words[0]}) == "alias" ]]; then
script=$(alias ${words[0]} | sed -E "s/alias ${words[0]}='(.*)'/\1/")
else
script=${words[0]}
fi
# lookup for command
for word in ${words[@]:1}; do
if [[ $word != -* ]]; then
com=$word
break
fi
done
# completing for an option
if [[ ${cur} == --* ]] ; then
opts=$script
[[ -n $com ]] && opts=$opts" -h "$com
# cache output into a file
cache_file=/tmp/bash_completion_$(echo $opts | tr -d ' :/?!')
if [ ! -f ${cache_file} ]; then
$opts --no-ansi 2>/dev/null > ${cache_file}
fi
opts=$(cat ${cache_file} | sed -n '/Options/,/^$/p' \
| sed -e '1d;$d' | sed 's/[^--]*\(--.*\)/\1/' \
| sed -En 's/[^ ]*(-(-[[:alnum:]]+){1,}).*/\1/p' \
| awk '{$1=$1};1'; exit ${PIPESTATUS[0]});
[[ $? -eq 0 ]] || return 0;
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
__ltrim_colon_completions "$cur"
return 0
fi
# completing for a command
if [[ $cur == $com ]]; then
# cache output into a file
cache_file=/tmp/bash_completion_$(echo $script'-list' | tr -d ' :/?!')
if [ ! -f ${cache_file} ]; then
$script list --raw 2>/dev/null > ${cache_file}
fi
coms=$(cat ${cache_file} | awk '{print $1}'; exit ${PIPESTATUS[0]})
[[ $? -eq 0 ]] || return 0;
COMPREPLY=($(compgen -W "${coms}" -- ${cur}))
__ltrim_colon_completions "$cur"
return 0;
fi
}
complete -o default -F _symfony console
complete -o default -F _symfony composer
complete -o default -F _symfony php-cs-fixer
complete -o default -F _symfony phpspec
complete -o default -F _symfony behat
complete -o default -F _symfony magento
complete -o default -F _symfony n98-magerun2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment