Created
October 21, 2013 17:53
-
-
Save TheBurnDoc/7088055 to your computer and use it in GitHub Desktop.
Shell code which provides a rudimentary project management command ('proj'). Set PROJ_ROOT to your projects parent directory and, assuming this file has been sourced, type 'proj' to get going.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Project management (proj command) | |
export PROJ_ROOT=$HOME/projects | |
export PROJ_CONF=.proj.conf | |
_proj_complete () | |
{ | |
local suggest=$(compgen -d $PROJ_ROOT/${COMP_WORDS[COMP_CWORD]} | awk -F "/" '{print $NF}') | |
if [[ $(echo $suggest | wc -w | grep -o '[0-9]') == 1 && -d $PROJ_ROOT/$suggest ]]; then | |
COMPREPLY=$suggest | |
else | |
unset COMPREPLY | |
fi | |
} | |
proj () | |
{ | |
# If a project ID is specified as an argument | |
if [[ "$1" != "" && -d "$PROJ_ROOT/$1" ]]; then | |
export PROJ_HOME=$PROJ_ROOT/$1 | |
cd $PROJ_HOME | |
# Get config options | |
local name="" | |
local run="" | |
if [[ -f $PROJ_HOME/$PROJ_CONF ]]; then | |
name=$(grep '^name=' $PROJ_HOME/$PROJ_CONF | cut -d '=' -f2- | head -1) | |
run=$(grep '^run=' $PROJ_HOME/$PROJ_CONF | cut -d '=' -f2-) | |
fi | |
# Apply default project name | |
if [[ $name == "" ]]; then | |
name=$1 | |
fi | |
# Print project name and change terminal title | |
echo "Project: $name" | |
# Run commands from config | |
if [[ $run != "" ]]; then | |
eval "$run" | |
fi | |
# If 'proj' is typed on its own | |
else | |
echo | |
echo "Current Projects:" | |
# For each project | |
for proj in $(ls -d $PROJ_ROOT/*/ | awk -F "/" '{print $(NF-1)}') | |
do | |
local name="" | |
# If the config has a name then print it too | |
if [[ -f $PROJ_ROOT/$proj/$PROJ_CONF ]]; then | |
echo -n " - $proj" | |
name=$(grep '^name=' $PROJ_ROOT/$proj/$PROJ_CONF | cut -d '=' -f2- | head -1) | |
if [[ $name != "" ]]; then | |
echo " ($name)" | |
fi | |
# Otherwise just print the ID | |
else | |
echo " - $proj" | |
fi | |
done | |
echo | |
echo "\$PROJ_ROOT=$PROJ_ROOT" | |
echo "\$PROJ_CONF=$PROJ_CONF" | |
echo | |
fi | |
} | |
complete -F _proj_complete proj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment