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

@ -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)