Cleaned up kvm.sh and kvmsetup.sh

Removed some redundant code
This commit is contained in:
Graeme Christie 2014-06-03 18:34:49 +08:00
parent 5b6febecdc
commit 030af218fb
2 changed files with 22 additions and 58 deletions

44
kvm.sh
View File

@ -1,21 +1,12 @@
# kvm.sh # kvm.sh
# Source this file from your .bash-profile or script to use # Source this file from your .bash-profile or script to use
# With inspriation from nvm.sh
SCRIPTPATH="$_"
#Exit script when any command returns non 0 exit code
_kvm_has() { _kvm_has() {
type "$1" > /dev/null 2>&1 type "$1" > /dev/null 2>&1
return $? return $?
} }
# Make zsh glob matching behave same as bash
# This fixes the "zsh: no matches found" errors
if _kvm_has "unsetopt"; then if _kvm_has "unsetopt"; then
unsetopt nomatch 2>/dev/null unsetopt nomatch 2>/dev/null
KVM_CD_FLAGS="-q"
fi fi
if [ -z "$KRE_USER_HOME" ]; then if [ -z "$KRE_USER_HOME" ]; then
@ -28,32 +19,6 @@ KRE_X86=
KRE_X64= KRE_X64=
KRE_NUGET_API_URL="https://www.myget.org/F/aspnetvnext/api/v2" KRE_NUGET_API_URL="https://www.myget.org/F/aspnetvnext/api/v2"
# Traverse up in directory tree to find containing folder
_kvm_find_up() {
local path
path=$PWD
while [ "$path" != "" ] && [ ! -f "$path/$1" ]; do
path=${path%/*}
done
echo "$path"
}
_kvm_find_kvmrc() {
local dir="$(_kvm_find_up '.kvmrc')"
if [ -e "$dir/.kvmrc" ]; then
echo "$dir/.kvmrc"
fi
}
# Obtain kvm version from rc file
_kvm_rc_version() {
local KVMRC_PATH="$(_kvm_find_kvmrc)"
if [ -e "$KVMRC_PATH" ]; then
_kvm_rc_version=`cat "$KVMRC_PATH" | head -n 1`
echo "Found '$KVMRC_PATH' with version <$_kvm_rc_version>"
fi
}
_kvm_find_latest() { _kvm_find_latest() {
local platform="mono45" local platform="mono45"
local architecture="x86" local architecture="x86"
@ -139,7 +104,7 @@ _kvm_unpack() {
} }
# This is not really needed. Placeholder until I get clarification on the supported platforms for mono # This is not currently required. Placeholder for the case when we have multiple platforms (ie if we bundle mono)
_kvm_requested_platform() { _kvm_requested_platform() {
local default=$1 local default=$1
@ -148,7 +113,7 @@ _kvm_requested_platform() {
echo $default echo $default
} }
# Ditto - waiting for clarification on mono-x64 packages # This is not currently required. Placeholder for the case where we have multiple architectures (ie if we bundle mono)
_kvm_requested_architecture() { _kvm_requested_architecture() {
local default=$1 local default=$1
@ -181,7 +146,6 @@ _kvm_requested_version_or_alias() {
# This will be more relevant if we support global installs # This will be more relevant if we support global installs
_kvm_locate_kre_bin_from_full_name() { _kvm_locate_kre_bin_from_full_name() {
local kreFullName=$1 local kreFullName=$1
[ -e "$KRE_USER_PACKAGES/$kreFullName/bin" ] && echo "$KRE_USER_PACKAGES/$kreFullName/bin" && return [ -e "$KRE_USER_PACKAGES/$kreFullName/bin" ] && echo "$KRE_USER_PACKAGES/$kreFullName/bin" && return
} }
@ -289,9 +253,9 @@ kvm()
PATH=`_kvm_strip_path "$PATH" "/bin"` PATH=`_kvm_strip_path "$PATH" "/bin"`
if [[ -n $persistent && -e "$KRE_USER_HOME/alias/default.alias" ]]; then if [[ -n $persistent && -e "$KRE_USER_HOME/alias/default.alias" ]]; then
echo "Setting default KRE to none" echo "Setting default KRE to none"
rm "$KRE_USER_HOME/alias/default.alias" rm "$KRE_USER_HOME/alias/default.alias"
fi fi
return 0 return 0
fi fi

View File

@ -1,7 +1,5 @@
#!/bin/bash #!/bin/bash
set -e
_kvmsetup_has() { _kvmsetup_has() {
type "$1" > /dev/null 2>&1 type "$1" > /dev/null 2>&1
return $? return $?
@ -11,7 +9,7 @@ _kvmsetup_update_profile() {
local profile="$1" local profile="$1"
local sourceString="$2" local sourceString="$2"
if ! grep -qc 'kvm.sh' $profile; then if ! grep -qc 'kvm.sh' $profile; then
echo "=> Appending source string to $profile" echo "Appending source string to $profile"
echo "" >> "$profile" echo "" >> "$profile"
echo $sourceString >> "$profile" echo $sourceString >> "$profile"
else else
@ -35,9 +33,9 @@ fi
# Downloading to $KVM_DIR # Downloading to $KVM_DIR
mkdir -p "$KRE_USER_HOME/kvm" mkdir -p "$KRE_USER_HOME/kvm"
if [ -s "$KRE_USER_HOME/kvm/kvm.sh" ]; then if [ -s "$KRE_USER_HOME/kvm/kvm.sh" ]; then
echo "=> kvm is already installed in $KRE_USER_HOME/kvm, trying to update" echo "kvm is already installed in $KRE_USER_HOME/kvm, trying to update"
else else
echo "=> Downloading kvm as script to '$KRE_USER_HOME/kvm'" echo "Downloading kvm as script to '$KRE_USER_HOME/kvm'"
fi fi
curl -s "$KVM_SOURCE" -o "$KRE_USER_HOME/kvm/kvm.sh" || { curl -s "$KVM_SOURCE" -o "$KRE_USER_HOME/kvm/kvm.sh" || {
@ -58,31 +56,33 @@ if [ -z "$PROFILE" ]; then
fi fi
fi fi
if [ -f "$HOME/.zshrc" ]; then if [ -z "$PROFILE" ]; then
ZPROFILE="$HOME/.zshrc" if [ -f "$HOME/.zshrc" ]; then
ZPROFILE="$HOME/.zshrc"
fi
fi fi
SOURCE_STR="[ -s \"$KRE_USER_HOME/kvm/kvm.sh\" ] && . \"$KRE_USER_HOME/kvm/kvm.sh\" # this loads kvm" SOURCE_STR="[ -s \"$KRE_USER_HOME/kvm/kvm.sh\" ] && . \"$KRE_USER_HOME/kvm/kvm.sh\" # Load kvm"
if [ -z "$PROFILE" -a -z "$ZPROFILE" ] || [ ! -f "$PROFILE" -a ! -f "$ZPROFILE" ] ; then if [ -z "$PROFILE" -a -z "$ZPROFILE" ] || [ ! -f "$PROFILE" -a ! -f "$ZPROFILE" ] ; then
if [ -z "$PROFILE" ]; then if [ -z "$PROFILE" ]; then
echo "=> Profile not found. Tried ~/.bash_profile ~/.zshrc and ~/.profile." echo "Profile not found. Tried ~/.bash_profile ~/.zshrc and ~/.profile."
echo "=> Create one of them and run this script again" echo "Create one of them and run this script again"
elif [ ! -f "$PROFILE" ]; then elif [ ! -f "$PROFILE" ]; then
echo "=> Profile $PROFILE not found" echo "Profile $PROFILE not found"
echo "=> Create it (touch $PROFILE) and run this script again" echo "Create it (touch $PROFILE) and run this script again"
else else
echo "=> Profile $ZPROFILE not found" echo "Profile $ZPROFILE not found"
echo "=> Create it (touch $ZPROFILE) and run this script again" echo "Create it (touch $ZPROFILE) and run this script again"
fi fi
echo " OR" echo " OR"
echo "=> Append the following line to the correct file yourself:" echo "Append the following line to the correct file yourself:"
echo echo
echo " $SOURCE_STR" echo " $SOURCE_STR"
echo echo
else else
[ -n "$PROFILE" ] && _kvmsetup_update_profile "$PROFILE" "$SOURCE_STR" [ -n "$PROFILE" ] && _kvmsetup_update_profile "$PROFILE" "$SOURCE_STR"
[ -n "$ZPROFILE" ] && _kvmsetup_update_profile "$ZPROFILE" "$SOURCE_STR" [ -n "$ZPROFILE" ] && _kvmsetup_update_profile "$ZPROFILE" "$SOURCE_STR"
fi fi
echo "=> Type 'source $KRE_USER_HOME/kvm/kvm.sh' to start using kvm" echo "Type 'source $KRE_USER_HOME/kvm/kvm.sh' to start using kvm"