Skip to content

Instantly share code, notes, and snippets.

@bgaze
Last active March 15, 2021 06:02
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 bgaze/4538df7d65260ef87d55af67cd5d6a82 to your computer and use it in GitHub Desktop.
Save bgaze/4538df7d65260ef87d55af67cd5d6a82 to your computer and use it in GitHub Desktop.
A collection of console dev helpers
################################################################################
# INSTALLATION #
# #
# Simply save this file somewhere on your computer then import it into #
# your "~/.bashrc" file : #
# #
# if [ -f ~/path/to/bash-aliases ]; then #
# . ~/path/to/bash-aliases #
# fi #
# #
# Note: #
# My system default text editor is mousepad. Don't forget to replace #
# "mousepad" occurences with your editor command in this file #
# #
################################################################################
################################################################################
### GIT
# Display current Git branch near console prompt
function git_colorize_console {
local __user_and_host="\[\033[1;0m\]\u@\h"
local __cur_location="\[\033[1;0m\]\w"
local __git_branch_color="\[\033[31m\]"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
local __prompt_tail="\[\033[34m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
git_colorize_console
# Display Git history in a pretty way.
alias gitlog='git log --graph --full-history --all --no-prefix --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%x1b[0m%x20%s%x1b[36m%x20%an%x1b[0m%x20"'
################################################################################
### LAMP
# Give permissions to LAMP user on current dir and subdirs using setfacl.
lampperm () {
sudo setfacl -R -m u:www-data:rwX -m u:$(whoami):rwX ${1:-$PWD} && sudo setfacl -dR -m u:www-data:rwx -m u:$(whoami):rwx ${1:-$PWD}
echo "${1:-$PWD} permissions updated"
}
# Restart Apache service.
alias lamprst='sudo service apache2 restart'
# Open hosts file.
alias lamphosts='sudo mousepad /etc/hosts'
# Show Apache error log.
alias lamplog='tail -f /var/log/apache2/error.log'
# Open default localhost acces configuration.
alias lampdef='sudo mousepad /etc/apache2/sites-enabled/000-default.conf'
################################################################################
### CURRENT DIRECTORY VHOST
# Create a vhost for current dir (if it doesn't exists already) then open vhost and hosts file.
lampvhost(){
# Get current folder name
local NAME=${PWD##*/}
# Get vhost file.
local FILE=/etc/apache2/sites-available/$NAME.conf
# Get root dir (use public subfloder if exists)
if [ -d "$PWD/public" ]; then
local DIR="$PWD/public"
else
local DIR=$PWD
fi
# Create conf file if and host if necessary
# Then open conf file and hosts file
if [ ! -f $FILE ]; then
read -r -d '' TEMPLATE << EOM
<VirtualHost *:80 *:443>
ServerName www.$NAME.local
ServerAlias $NAME.local
ServerAdmin webmaster@localhost
DocumentRoot $DIR
<Directory $DIR/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOM
echo "$TEMPLATE" | sudo tee "$FILE" \
&& echo "127.0.0.1 $NAME.local www.$NAME.local" | sudo tee -a /etc/hosts \
&& sudo mousepad $FILE /etc/hosts
else
sudo mousepad $FILE /etc/hosts
fi
}
# Disable current dir vhost.
alias lampdis="sudo a2dissite ${PWD##*/}.conf"
# Enable current dir vhost.
alias lampen="sudo a2ensite ${PWD##*/}.conf"
################################################################################
### PHP
# Switch between installed PHP version
# Example: enter "phpv 7.1" to switch to PHP 7.1
# If no argument is provided, the function displays the current PHP version and the loaded ini file.
phpv() {
local V=$(php -v | grep -oP "(?<=PHP )(\d\.\d)")
if [ -z "$1" ]; then
php -v | grep --color=never "PHP $V" && php -i | grep --color=never "Loaded Configuration File"
elif [ $1 = $V ]; then
echo "PHP is already at $1 version."
elif [ ! -f "/usr/bin/php$1" ]; then
echo "Requested PHP version is not available."
else
sudo update-alternatives --set php /usr/bin/php$1 \
&& printf "\n" \
&& sudo a2dismod php$V \
&& printf "\n" \
&& sudo a2enmod php$1 \
&& sudo service apache2 restart \
&& printf "\n" \
&& phpv \
&& printf "\n"
fi
}
# Open PHP ini file.
phpini(){
sudo mousepad "/etc/php/$(php -v | grep -oP "(?<=PHP )(\d\.\d)")/apache2/php.ini"
}
# Open PHP CLI ini file.
phpclini(){
sudo mousepad "/etc/php/$(php -v | grep -oP "(?<=PHP )(\d\.\d)")/cli/php.ini"
}
################################################################################
### LARAVEL (from the root of a Laravel application)
# A quick alias to "php artisan" command.
alias lr='php artisan'
# Clear all Laravel caches.
alias lrc='php artisan ca:cl && php artisan co:cl && php artisan r:cl && php artisan v:cl'
# Clear all Laravel caches and dump autoload.
alias lrcf='php artisan ca:cl && php artisan co:cl && php artisan r:cl && php artisan v:cl && composer dump-autoload'
# Refresh and seed database.
alias lrdb='php artisan mi:fr --seed'
# Watch Laravel log.
alias lrlog='tail -f storage/logs/laravel.log'
# Set required permissions.
alias lrperm='lampperm storage; lampperm bootstrap/cache'
# Parse app url from a Laravel dotenv file then open the app in Firefox
# Accepts the doetenv file as first argument and an optional segment as second argument:
# Usage: lropen ".env" "admin/articles/1/edit"
lropen() {
if [ -z "$1" ]; then
echo "Dotenv file name is required."
elif [ ! -f "$1" ]; then
echo "This dotenv file does not exists."
else
URL="$(pcregrep -o1 '^APP_URL=(.+)' $1 | sed 's/^\///;s/\/$//')"
if [ -z "$URL" ]; then
echo "App url was not found."
else
echo "Opening $URL/$2 ..."
open -a firefox "$URL/$2"
fi
fi
}
# Environments frontend
alias lro='lropen ".env"'
alias lrs='lropen ".env.staging"'
alias lrp='lropen ".env.production"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment