From 3948255445bc4445f5688b4b305a06feac31d4ec Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 31 Jul 2018 14:22:38 +0200 Subject: [PATCH] lang/ruby: rewrite rbenv support; add rvm support --- modules/lang/ruby/autoload.el | 17 +++++++++++ modules/lang/ruby/config.el | 56 ++++++++++++++++------------------- modules/lang/ruby/packages.el | 6 ++-- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/modules/lang/ruby/autoload.el b/modules/lang/ruby/autoload.el index 8f9519283..980fdd2ce 100644 --- a/modules/lang/ruby/autoload.el +++ b/modules/lang/ruby/autoload.el @@ -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))))) diff --git a/modules/lang/ruby/config.el b/modules/lang/ruby/config.el index 5c668a533..d787ffaf0 100644 --- a/modules/lang/ruby/config.el +++ b/modules/lang/ruby/config.el @@ -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) + diff --git a/modules/lang/ruby/packages.el b/modules/lang/ruby/packages.el index 661a8d1b8..5bcef5610 100644 --- a/modules/lang/ruby/packages.el +++ b/modules/lang/ruby/packages.el @@ -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))