Skip to content

Commit

Permalink
Bugfix in alias creation, will no longer duplicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneeseguin committed Sep 15, 2010
1 parent 8fb31b4 commit 23c3f7a
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 37 deletions.
79 changes: 65 additions & 14 deletions scripts/alias
Expand Up @@ -78,45 +78,84 @@ alias_delete() {
}

alias_create() {
rvm_environment_identifier="${rvm_environment_identifier:-""}"

alias_name="${alias_name:-""}"

rvm_ruby_string="${rvm_ruby_string:-""}"

if alias_conflicts_with_ruby "$alias_name"; then
if [[ "default" != "$alias_name" ]] && alias_conflicts_with_ruby "$alias_name"; then
# Force it to an empty alias name to trigger the usage.
alias_name=""
fi

if [[ -z "$rvm_environment_identifier" ]] || [[ -z "$alias_name" ]] ; then
"$rvm_path/scripts"/log "error" "\nusage: 'rvm alias [alias_name] [ruby_string]'\n"
if [[ -z "${rvm_environment_identifier:-""}" || -z "$alias_name" ]] ; then

"$rvm_path/scripts"/log "error" \
"\nusage: 'rvm alias [alias_name] [ruby_string]'\n"

result=1
else

if [[ "default" = "$alias_name" ]] ; then
rvm_alias=""
fi

if [[ -z "$rvm_alias" ]] ; then

rvm_ruby_string="$rvm_environment_identifier"

if [[ -z "$rvm_alias_expanded" ]]; then

rvm_expanding_aliases=1

__rvm_become

unset rvm_expanding_aliases

else

rvm_ruby_string="$rvm_environment_identifier"

fi

if [[ -z "$rvm_ruby_string" ]]; then
"$rvm_path/scripts"/log "error" "\nUnknown ruby string '$rvm_ruby_string' specified\n"

"$rvm_path/scripts"/log "error" \
"\nUnknown ruby string '$rvm_ruby_string' specified\n"

result=1
return

return $result
fi
final_environment_identifier="$(__rvm_environment_identifier)"
"$rvm_path/scripts"/log "info" "Creating alias $alias_name for $final_environment_identifier."
ln -nfs "$rvm_path/rubies/$rvm_ruby_string" "$rvm_path/rubies/$alias_name"
"$rvm_path/scripts"/log "info" "Recording alias $alias_name for $final_environment_identifier."

final_environment_identifier="${rvm_environment_identifier:-$(__rvm_environment_identifier)}"

"$rvm_path/scripts"/log "info" \
"Creating alias $alias_name for $final_environment_identifier."

ln -nfs "$rvm_path/rubies/$rvm_ruby_string" "nrvm_path/rubies/$alias_name"

"$rvm_path/scripts"/log "info" \
"Recording alias $alias_name for $final_environment_identifier."

"$rvm_path/scripts"/db "$rvm_path/config/alias" "$alias_name" "$final_environment_identifier"

else

if [[ -d "$rvm_path/rubies/$alias_name" ]] ; then
"$rvm_path/scripts"/log "error" "\n$rvm_path/rubies/$alias_name is taken and is *not* able to be an alias name.\n"

"$rvm_path/scripts"/log "error" \
"\n$rvm_path/rubies/$alias_name is taken and is *not* able to be an alias name.\n"

result=1

else
"$rvm_path/scripts"/log "error" "\n$rvm_path/rubies/$alias_name is already aliased.\n"

"$rvm_path/scripts"/log "error" \
"\n$rvm_path/rubies/$alias_name is already aliased.\n"

result=1

fi
fi
fi
Expand All @@ -130,7 +169,7 @@ alias_list() {

if [[ -L "$item" ]] ; then

echo "$(basename "$item") => $("$rvm_path/scripts"/db "$rvm_path/config/alias" "$(basename "$item")")"
echo "$(basename "$item") => $("$rvm_path/scripts"/db "$rvm_path/config/alias" "$(basename "$item")" | head -n 1)"

fi

Expand All @@ -154,21 +193,33 @@ else
gemset_name=""
fi

if [[ ! -z "$alias_name" ]] ; then
if [[ -n "$alias_name" ]] ; then
rvm_alias="$("$rvm_path/scripts/db" "$rvm_path/config/alias" "$alias_name")"
fi

if [[ "$action" = "delete" ]] ; then

alias_delete

elif [[ "$action" = "create" ]] ; then

alias_create

elif [[ "$action" = "list" ]] ; then

alias_list

elif [[ "$action" = "show" ]]; then

alias_show


elif [[ "$action" = "help" ]]; then

usage

else

usage
exit 1
fi
Expand Down
69 changes: 57 additions & 12 deletions scripts/db
@@ -1,41 +1,86 @@
#!/usr/bin/env bash

usage() {
printf "\nUsage:\n" >&2
printf "\n $rvm_path/scripts/db database_file {{key}} {{value}} # set" >&2
printf "\n $rvm_path/scripts/db database_file {{key}} # get" >&2
printf "\n $rvm_path/scripts/db database_file {{key}} unset # unset\n\n" >&2
printf "
Usage:
db database_file {{key}} {{value}} # set
db database_file {{key}} # get
db database_file {{key}} unset # unset
" >&2
}

if [[ -f "$1" ]] ; then

database_file="$1" ; shift

if [[ ! -f "$database_file" ]] ; then
\mkdir -p $(dirname $database_file)
\touch $database_file

directory=$(dirname "$database_file")

if [[ ! -d "$directory" ]] ; then
mkdir -p "$directory"
fi

touch "$database_file"

fi

else

printf "\n\nDatabase file $1 does not exist.\n\n" >&2

exit 1

fi

key="$1" ; shift

if [[ -z "$key" ]] ; then

usage

exit 1

else

value="$*"
if [[ "unset" = "$value" ]] || [[ "delete" = "$value" ]] ; then
sed -i.tmp -e "s#^$key=.*\$##" -e '/^$/d' $database_file

if [[ "unset" = "$value" || "delete" = "$value" ]] ; then

sed -e "s#^$key=.*\$##" -e '/^$/d' "$database_file" > "$database_file.new"

mv "$database_file.new" "$database_file"

else

if [[ -z "$value" ]] ; then # get
awk -F= '/^'"$key"'=/' $database_file | sed -e "s#^$key=##" -e '/^$/d'

awk -F= '/^'"$key"'=/' "$database_file" | sed -e "s#^$key=##" -e '/^$/d'

else # set
if [[ -z "$(awk -F= "/^'"$key"'=/{print $2}" $database_file)" ]] ; then # append
echo "$key=$value" >> $database_file

sed -e "s#^$key=.*\$##" -e '/^$/d' "$database_file" > "$database_file.new"

mv "$database_file.new" "$database_file"

if [[ -z "$(awk -F= "/^'"$key"'=/{print $2}" "$database_file")" ]] ; then # append

echo "$key=$value" >> "$database_file"

else # overwrite
sed -i.tmp "s#^$key=.*\$#$key=$value#" $database_file

sed -i.tmp "s#^$key=.*\$#$key=$value#" "$database_file" > "$database_file.new"

mv "$database_file.new" "$database_file"

fi

fi

fi

fi

2 changes: 2 additions & 0 deletions scripts/list
Expand Up @@ -295,6 +295,8 @@ args=${args[@]:1} # Strip trailing / leading / extra spacing.

if [[ -z "$action" ]] ; then list_rubies ; exit 0 ; fi

if [[ ${rvm_ruby_selected_flag:-0} -eq 0 ]] ; then __rvm_select ; fi

case "$action" in
known) list_known ;;
known_strings) list_known_strings ;;
Expand Down

0 comments on commit 23c3f7a

Please sign in to comment.