Last active
September 13, 2019 15:47
-
-
Save JenkinsDev/775b130fa688cf6dfeacc575ce77a4da to your computer and use it in GitHub Desktop.
Sublime Support rvm and rbenv ruby versioning
This file contains hidden or 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
import os | |
import subprocess | |
from os.path import expanduser, join, exists | |
# We expect rbenv and rvm to be installed in their default locations. Can | |
# be updated to find them, if necessary. | |
rbenv_path = expanduser('~/.rbenv') | |
rvm_path = expanduser('~/.rvm') | |
def log(msg): | |
print('[Ruby Loader] {}'.format(msg)) | |
def update_path(path): | |
log('Updating path to {}'.format(path)) | |
os.environ['PATH'] = path + ':' + os.environ['PATH'] | |
def use_rbenv(): | |
version_file_path = join(rbenv_path, 'version') | |
with open(version_file_path) as rbenv_version_file: | |
version = rbenv_version_file.read().strip() | |
rbenv_bin_path = join(rbenv_path, 'versions', version, 'bin') | |
update_path(rbenv_bin_path) | |
def use_rvm(): | |
rvm_auto_ruby_exec = join(rvm_path, 'bin', 'rvm-auto-ruby') | |
# example: "ruby 2.6.3p62 (2019-04-16 revision xxxx) ..." | |
curr_ruby_version = subprocess.check_output( | |
[rvm_auto_ruby_exec, '--version']) | |
ruby_version_num = str(curr_ruby_version).split(' ')[1] | |
# We don't need the ruby patch number | |
if 'p' in ruby_version_num: | |
ruby_version_num = ruby_version_num.split('p')[0] | |
ruby_dir_name = 'ruby-{}'.format(ruby_version_num) | |
curr_ruby_bin_path = join(rvm_path, 'rubies', ruby_dir_name, 'bin') | |
update_path(curr_ruby_bin_path) | |
if exists(rbenv_path): | |
use_rbenv() | |
elif exists(rvm_path): | |
use_rvm() | |
log('Sublime is now using ruby: {}'.format( | |
str(subprocess.check_output(["which", "ruby"])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment