DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Gemdocs

09.12.2007
| 4988 views |
  • submit to reddit
        Put this snippet into your ~/.bashrc (or alternatives) to browse your gem docs from the command line.


function gemdocs() { 

   declare cmdpath gem_server_pid_file
   declare -a lines

   gem_server_pid_file="${HOME}/.gem_server_pid"

   # kill running gem server
   if [[ -s "${gem_server_pid_file}" ]]; then    # cf. help test
      declare IFS=$'\n'
      lines=( $(< "${HOME}/.gem_server_pid" ) )
      [[ "${#lines[@]}" -gt 0 ]] && kill -TERM "${lines[0]}"
      : > "${gem_server_pid_file}"
   fi


  if cmdpath="$(type -P gem)"; then
     "${cmdpath}" server >/dev/null 2>&1 &
     echo "PID of gem server: ${!}"
     echo "${!}" > "${gem_server_pid_file}"
     sleep 3
     open http://127.0.0.1:8808/
  elif cmdpath="$(type -P gem_server)"; then
     "${cmdpath}" >/dev/null 2>&1 &
     echo "PID of gem_server: ${!}"
     echo "${!}" > "${gem_server_pid_file}"
     sleep 3
     open http://127.0.0.1:8808/
  else
     echo 'Could not find the commands "gem" or "gem_server" in $PATH!'
  fi

  return 0

}

# kill running gem server
function kgs() { 

   declare gem_server_pid_file IFS=$'\n'
   declare -a lines

   gem_server_pid_file="${HOME}/.gem_server_pid"
   lines=( $(< "${gem_server_pid_file}" ) )
   [[ "${#lines[@]}" -gt 0 ]] && kill -TERM "${lines[0]}"
   : > "${gem_server_pid_file}"
   return 0

}


gemdocs

kgs



#---------------------------------------------------



# cf. http://stephencelis.com/2008/06/12/bashfully-yours-gem-shortcuts.html

# ~/.bash_profile
 
export GEMDIR="$(gem env gemdir)"
 
gemdoc() {
  declare gems=( "${GEMDIR}"/doc/"${1}"*/rdoc/index.html )
  open "${gems[@]: -1}"
}
 
complete -o nospace -W '$( $(type -P ls) -1 "${GEMDIR}"/doc )' gemdoc


gemdoc hpricot

gemdoc h[tab]



#---------------------------------------------------



# See:
# http://aussiegeek.net/posts/19-Gembox
# http://rubyforge.org/projects/croc/
# sudo gem install croc

# ~/.bash_profile

export GEMDIR="$(gem env gemdir)"
 
gemdoc() {
  open "${GEMDIR}"/doc/"$( $(type -P ls) -1 "${GEMDIR}"/doc | $(type -P grep) $1 | $(type -P sort) | $(type -P tail) -1 )"/rdoc/index.html
}
 
_gemdocomplete() {
  COMPREPLY=( $(compgen -W '$( $(type -P ls) "${GEMDIR}"/doc)' -- ${COMP_WORDS[COMP_CWORD]}) )
  return 0
}
 
complete -o default -o nospace -F _gemdocomplete gemdoc


gemdoc hpricot

gemdoc h[tab]