Skip to content

Instantly share code, notes, and snippets.

@neg3ntropy
Last active August 8, 2016 14:44
Show Gist options
  • Save neg3ntropy/5019013 to your computer and use it in GitHub Desktop.
Save neg3ntropy/5019013 to your computer and use it in GitHub Desktop.
Bash script to manage per-project shell environment and other settings. Include in the project root, customize and source with the shell to activate. The rest of the boilerplate adds support for: * finding the absolute path of project root, independently of the current shell's working directory * a nice indicator with the project name to the pro…
###############################################################################
# Development environment helper to set up project paths and other variables #
###############################################################################
#
# Copyright (C) 2012 Andrea Ratto <andrearatto at yahoo dot it>.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
# Usage #
###############################################################################
#
# Add your project-specific environment configuration under the sections
# delimited by:
# "###PROJECT ENVIRONMENT SETUP###" and "###PROJECT ENVIRONMENT REVERT###"
#
# Then source this file with your normal bash shell, running
# . project_env.bash +
# or
# source project_env.bash +
#
# revert using "-" instead of "+" or just close the shell.
#
# Optionally add the following lines to your ~/.bashrc to get an alias
# "lastproject" to get quickly to the last project used.
#
## lastproject alias support start
#if [ -f ~/.lastproject.bash ]; then
# . ~/.lastproject.bash
#fi
## lastproject alias support end
###############################################################################
function env_activate() {
# available variables
# $PROJECT_DIR: the absolute path of the folder containting this file
#
###PROJECT ENVIRONMENT SETUP###
#PROJECT_NAME="myproject" # defaults to the folder name if not set
OLDPATH="$PATH"
export PATH="$PROJECT_DIR/bin:$PATH"
###PROJECT ENVIRONMENT SETUP###
}
function env_deactivate() {
###PROJECT ENVIRONMENT REVERT###
export PATH="$OLDPATH"
unset OLDPATH
###PROJECT ENVIRONMENT REVERT###
}
# detect status and errors
case "$1" in
+)
;;
-)
;;
*)
echo "USAGE: source \"$BASH_SOURCE\" <+|->"
return 1
;;
esac
_PROJECT_DIR="$(readlink -f "$(dirname "$BASH_SOURCE")")"
THIS_FILE="$(basename "$BASH_SOURCE")"
if [ ! -z "$PROJECT_DIR" ]; then
if [ "$_PROJECT_DIR" != "$PROJECT_DIR" ]; then
echo "Another project is active in the current shell. Deactivate with:"
echo "source \"$_PROJECT_DIR/$THIS_FILE\" -"
return 1
else
if [ $1 == "+" ]; then
echo "This project is alredy active in the current shell. Optionally deactivate with:"
echo "source \"$BASH_SOURCE\" -"
return 1
fi
fi
else
if [ $1 == "-" ]; then
echo "This project is not active in the current shell. Nothing to do."
return 1
fi
fi
PROJECT_DIR="$_PROJECT_DIR"
unset _PROJECT_DIR
if [ $1 == "+" ]; then
# Variable PROJECT_DIR is your reference to the full path of the folder
# containing this file
PROJECT_NAME=$(basename "$PROJECT_DIR")
env_activate
OLDPS1="$PS1"
PS1="[\$(if [[ \$? == 0 ]]; then echo \"\[\033[0;32m\]\"; else echo \"\[\033[0;31m\]\"; fi)$PROJECT_NAME\[\033[0m\]]$PS1"
unset PROJECT_NAME
# save the alias
echo "alias lastproject='cd \"$PROJECT_DIR\"; source \"$THIS_FILE\" +'" \
> ~/.lastproject.bash
else
PS1="$OLDPS1"
unset OLDPS1
env_deactivate
unset PROJECT_DIR
fi
unset THIS_FILE
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment