lang/ruby: rewrite rbenv support; add rvm support

This commit is contained in:
Henrik Lissner 2018-07-31 14:22:38 +02:00
parent 560d16d651
commit 3948255445
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 46 additions and 33 deletions

View file

@ -13,3 +13,20 @@ buffers open."
(when (processp process)
(kill-process (get-buffer-process inf-buffer))
(kill-buffer inf-buffer)))))))
;;;###autoload
(defun +ruby-version ()
"Return the currently installed version of ruby on your system (the first
ruby executable found in your PATH).
This is not necessarily aware of env management tools like virtualenv, pyenv or
pipenv, unless those tools have modified the PATH that Emacs picked up when you
started it."
(unless (executable-find "ruby")
(user-error "Couldn't find ruby executable in PATH"))
(with-temp-buffer
(let ((p (call-process "ruby" nil (current-buffer) nil "--version"))
(output (string-trim (buffer-string))))
(unless (zerop p)
(user-error "ruby --version failed: %s" output))
(nth 1 (split-string output " " t)))))

View file

@ -1,10 +1,9 @@
;;; lang/ruby/config.el -*- lexical-binding: t; -*-
(defvar +ruby-rbenv-versions nil
"Available versions of ruby in rbenv.")
(defvar-local +ruby-current-version nil
"The currently active ruby version.")
(defvar +ruby-mode-name-functions '(+ruby-version)
"A list of functions to retrieve a version or environment string from. The
first to return non-nil will have its result appended to the ruby-mode
`mode-name' and displayed in the mode-line.")
;;
@ -18,47 +17,24 @@
:mode "\\.\\(?:pry\\|irb\\)rc\\'"
:mode "/\\(?:Gem\\|Cap\\|Vagrant\\|Rake\\|Pod\\|Puppet\\|Berks\\)file\\'"
:config
(set-env! "RBENV_ROOT")
(set-electric! 'enh-ruby-mode :words '("else" "end" "elsif"))
(set-repl-handler! 'enh-ruby-mode #'inf-ruby) ; `inf-ruby'
;; so class and module pairs work
(setq-hook! 'enh-ruby-mode-hook sp-max-pair-length 6)
;; Version management with rbenv
(defun +ruby|add-version-to-modeline ()
"Add version string to the major mode in the modeline."
(setq mode-name
(if +ruby-current-version
(format "Ruby %s" +ruby-current-version)
(if-let* ((result (run-hook-with-args-until-success '+ruby-mode-name-functions)))
(format "Ruby %s" result)
"Ruby")))
(add-hook 'enh-ruby-mode-hook #'+ruby|add-version-to-modeline)
(if (not (executable-find "rbenv"))
(setq-default +ruby-current-version (string-trim (shell-command-to-string "ruby --version 2>&1 | cut -d' ' -f2")))
(setq +ruby-rbenv-versions (split-string (shell-command-to-string "rbenv versions --bare") "\n" t))
(defun +ruby|detect-rbenv-version ()
"Detect the rbenv version for the current project and set the relevant
environment variables."
(when-let* ((version-str (shell-command-to-string "RBENV_VERSION= ruby --version 2>&1 | cut -d' ' -f2")))
(setq version-str (string-trim version-str)
+ruby-current-version version-str)
(when (member version-str +ruby-rbenv-versions)
(setenv "RBENV_VERSION" version-str))))
(add-hook 'enh-ruby-mode-hook #'+ruby|detect-rbenv-version)))
(add-hook 'enh-ruby-mode-hook #'+ruby|add-version-to-modeline))
(def-package! yard-mode :hook enh-ruby-mode)
(def-package! rbenv
:after enh-ruby-mode
:config
(when (executable-find "rbenv")
(global-rbenv-mode +1)))
(def-package! rubocop
:hook (enh-ruby-mode . rubocop-mode)
:config
@ -127,3 +103,21 @@ environment variables."
:after inf-ruby
:config (set-company-backend! 'inf-ruby-mode 'company-inf-ruby))
;;
;; Version managers
;;
(def-package! rbenv
:when (featurep! +rbenv)
:after enh-ruby-mode
:config
(set-env! "RBENV_ROOT")
(when (executable-find "rbenv")
(global-rbenv-mode +1)))
(def-package! rvm
:when (featurep! +rvm)
:after enh-ruby-mode)

View file

@ -4,7 +4,6 @@
;; requires ruby ruby-lint
(package! enh-ruby-mode)
(package! rbenv)
(package! rubocop)
(package! inf-ruby)
(package! rspec-mode)
@ -15,4 +14,7 @@
(when (featurep! :completion company)
(package! company-inf-ruby))
(when (featurep! +rbenv)
(package! rbenv))
(when (featurep! +rvm)
(package! rvm))