Skip to content

Instantly share code, notes, and snippets.

@sathishmanohar
Created April 15, 2023 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sathishmanohar/6734f35f89cb240aaa2ba96d6ab1d7dd to your computer and use it in GitHub Desktop.
Save sathishmanohar/6734f35f89cb240aaa2ba96d6ab1d7dd to your computer and use it in GitHub Desktop.
rmux : Run rmux inside a rails project to start tmux sessions with panes for terminal, editor, rails server and console
rmux () {
# Ask to exit if inside a tmux session
if [[ -n "$TMUX" ]] { echo >&2 "Exit this tmux session to avoid nesting sessions"; return 1; } fi
git_root="$(git rev-parse --show-toplevel || echo .)"
# If not a git repo assume this isn't a rails project and exit
if [[ $git_root == "." ]] { echo "\nNot a Git repo. Are you sure this is a rails project?"; return 1; } fi
# If not a rails project exit with a message
# Get to git root and look for rails string in Gemfile
grep "rails" "$git_root/Gemfile" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "\nIt's not a Rails project!"; return 1;
else
cd $git_root
goback=1
fi
railsappclass=$(rails runner "puts Rails.application.class.module_parent.name")
tmux has-session -t "$railsappclass" 2>/dev/null
# If session with app class doesn't exist. Setup the session
if [ $? != 0 ]; then
# Use -d to allow the rest of the function to run
# Window 1 terminal
tmux new-session -d -s $railsappclass -n terminal
# Window 2 editor
tmux new-window -d -n editor nvim
# Window 3 rails server and watchers
# To avoid port conflicts set unique port numbers for each project
# Add a project specific port number in bin/dev or Procfile
# Use git assume-unchanged if you want to avoid tracking this change
# Or add it to git exclude if the project doesn't have a bin/dev
if [ -f bin/dev ]
then
tmux new-window -d -n server bin/dev
elif [ -f Procfile ]
then
tmux new-window -d -n server bundle exec foreman start
else
tmux new-window -d -n server bundle exec rails s
fi
# WIndow 4 rails console
tmux new-window -d -n console bundle exec rails console
if [[ $goback -eq 1 ]] { cd - > /dev/null } fi
# -d to detach any other client (which there shouldn't be,
# since you just created the session).
# use the below line if you want to attach to the session
# on running the command
tmux attach-session -d -t $railsappclass
else
if [[ $goback -eq 1 ]] { cd - > /dev/null } fi
# If session already exists simply attach to it
tmux attach-session -d -t $railsappclass
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment