Last active
May 13, 2016 21:40
-
-
Save noprompt/891c6ae2ff0fc7f82acd5a9e04e7f735 to your computer and use it in GitHub Desktop.
Emacs helper to conditionally run `irb`, `pry`, `bundle console`, or `rails console` with respect to the current working directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(when (not (boundp 'inf-ruby-implementations)) | |
(setq inf-ruby-implementations nil)) | |
(defun ~/paths-to-root (file-name) | |
(let* ((path-parts (s-split "/" (file-name-directory file-name) t)) | |
(build-path (lambda (path-parts) | |
(concat "/" (s-join "/" path-parts)))) | |
(paths (reduce (lambda (state _) | |
(let* ((old-path-parts (cdr state)) | |
(new-path-parts (-butlast old-path-parts)) | |
(old-paths (car state)) | |
(new-paths (cons (funcall build-path new-path-parts) | |
old-paths))) | |
`(,new-paths . ,new-path-parts))) | |
path-parts | |
:initial-value | |
`(,(list (funcall build-path path-parts)) | |
. ,path-parts)))) | |
(car paths))) | |
(defun ~/bundler-installed-p () | |
(stringp (executable-find "bundle"))) | |
(defun ~/bundler-project-p () | |
(-any-p 'file-exists-p | |
(-map (lambda (path) | |
(concat path "/Gemfile")) | |
(~/paths-to-root (buffer-file-name))))) | |
(defun ~/rbenv-installed-p () | |
(stringp (executable-find "rbenv"))) | |
(defun ~/pry-installed-p () | |
(stringp (executable-find "pry"))) | |
(defun ~/rails-project-p () | |
(-any-p 'file-exists-p | |
(-map (lambda (path) | |
(concat path "/bin/rails")) | |
(~/paths-to-root (buffer-file-name))))) | |
(defun ~/conditionally-run-ruby () | |
(interactive) | |
(let* ((ruby-command (if (and (~/bundler-installed-p) (~/bundler-project-p)) | |
(if (~/rbenv-installed-p) | |
(if (~/rails-project-p) | |
"rbenv exec bundle exec rails console" | |
"rbenv exec bundle console") | |
"bundle console") | |
(if (~/pry-installed-p) | |
"pry" | |
"irb --prompt default --noreadline -r irb/completion")))) | |
(setq inf-ruby-implementations | |
(cons `("ruby" . ,ruby-command) | |
(delq (assoc "ruby" inf-ruby-implementations) | |
inf-ruby-implementations))) | |
(setq inf-ruby-default-implementation "ruby") | |
(run-ruby))) | |
(define-key ruby-mode-map (kbd "C-c M-j") '~/conditionally-run-ruby) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment