Skip to content

Instantly share code, notes, and snippets.

@zeroliu
Last active September 16, 2015 04:21
Show Gist options
  • Save zeroliu/9610719 to your computer and use it in GitHub Desktop.
Save zeroliu/9610719 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Constants
PUMP="$HOME/.pump"
PUMP_REPO="git@git.gree-dev.net:zero-liu/art-tool.git"
mkdir -p "$PUMP" "$HOME/bin"
log_error(){
local colors="\033[31m[x] $1\033[0m\n"
local nocolors="[x] $1\n"
local display_text=
if [ -t 2 ]; then display_text="$colors"; else display_text="$nocolors"; fi
printf "$display_text" >&2
}
log_pass(){
local colors="\033[32m[o] $1\033[0m\n"
local nocolors="[x] $1\n"
local display_text=
if [ -t 2 ]; then display_text="$colors"; else display_text="$nocolors"; fi
printf "$display_text" >&2
}
log_info(){
echo "--- $1" >&2
}
die(){
log_error "$*"
exit 1
}
# Functions for installation
install_package(){
package=$1
log_info "Installing $package..."
install_$package || die "Install $package failed"
}
install_brew(){
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
}
install_python(){
brew install python
}
install_pip(){
(
INSTALL_PACKAGE_PATH="$PUMP/install_packages"
cd "$INSTALL_PACKAGE_PATH/pip"
sudo python setup.py install
)
}
install_imagemagick(){
brew install imagemagick
}
install_wand(){
sudo pip install wand
}
install_pump(){
git clone -q $PUMP_REPO "$PUMP"
}
# Functions for installation end
check_cmd_exist(){
local cmd=$1
type $cmd > /dev/null 2>&1
return $?
}
check_cmd(){
local package=$1; shift
local cmd=
[ -z "$1" ] && cmd=$package || cmd=$1; shift
check_cmd_exist $cmd && log_pass "$package installed" || install_package $package
}
check_python_package(){
local package=$1; shift
local output=$(pip show $package)
[ ! -z "$output" ] && log_pass "$package installed" || install_package $package
}
check_pump(){
if [ ! -e "$PUMP/.git" ]; then
install_package "pump"
else
log_info "Updating pump..."
"$PUMP/bin/pump" update "$@"
fi
create_shortcut
add_shortcut_to_profile
}
create_shortcut(){
cat > "$HOME/bin/pump" <<EOF
#!/bin/bash
export PUMP="$PUMP"
exec "$PUMP/bin/pump" "\$@"
EOF
chmod +x "$HOME/bin/pump"
}
create_setting(){
SETTING_FILE_PATH="$PUMP/setting.py"
if [ ! -f "$SETTING_FILE_PATH" ]; then
DEFAULT_PATH="$HOME/Desktop/pump_workspace"
log_info "Please enter the workspace path [$DEFAULT_PATH]:"
read path < /dev/tty
if [ ! -d $path ]; then
log_info "$path is invalid. Use $DEFAULT_PATH instead"
path=""
fi
path=${path:-$DEFAULT_PATH}
mkdir -p $path
log_info "workspace is set to $path"
cat > "$SETTING_FILE_PATH" <<EOF
ADOBE_FLASH_VERSION = "Adobe Flash CS6"
WORKSPACE_DIR = "$path"
EOF
fi
}
add_shortcut_to_profile(){
if ! type pump >/dev/null 2>&1; then
for profile in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bashrc"; do
if [ -e "$profile" ]; then
log_info "Adding \$HOME/bin to your \$PATH in $profile ..."
echo "export PATH=\$HOME/bin:\$PATH" >> "$profile"
log_info "Please run 'source $profile' or open a new terminal."
break
fi
done
fi
}
main(){
check_pump
check_cmd "brew"
check_cmd "python"
check_cmd "pip"
check_cmd "imagemagick" "identify"
check_python_package "wand"
create_setting
log_pass "Install complete!"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment