Sha256: 3b74bd0ba4b14e7ab1e4d6a2918771679bbf5f8bea3ac9f64bfc3f51e04350dd

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

module Qor
  module Test
    class Rubies
      class << self

        def rvm?
          %x(sh -c "command -v rvm").size > 0
        end

        def rbenv?
          %x(sh -c "command -v rbenv").size > 0
        end

        def version_manager_installed?
          rvm? || rbenv?
        end

        def versions
          if rvm?
            `rvm list strings`.split("\n")
          elsif rbenv?
            `rbenv versions | cut -d '(' -f1`.split("\n").map {|x| x.sub(/\*/,'') }.map(&:strip)
          end
        end

        def matched_version(version)
          result = ["^#{version}", version].map do |x|
            versions.select {|v| v =~ Regexp.new(x) }[-1]
          end.compact[0]
          not_installed!(version) unless result
          result
        end

        def switch_ruby_version(version)
          if rvm?
            "[ -f $rvm_path/scripts/rvm ] && source $rvm_path/scripts/rvm; rvm use #{matched_version(version)}"
          elsif rbenv?
            "export RBENV_VERSION=#{matched_version(version)}"
          elsif (`ruby -v` !~ Regexp.new(version))
            not_installed!(version)
          end
        end

        def not_installed!(version)
          puts "ruby '#{version}' is not installed! please install it first!"
          exit
        end

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
qor_test-1.0.1 lib/qor_test/rubies.rb
qor_test-1.0.0 lib/qor_test/rubies.rb
qor_test-0.1.1 lib/qor_test/rubies.rb
qor_test-0.1.0 lib/qor_test/rubies.rb
qor_test-0.0.5 lib/qor_test/rubies.rb
qor_test-0.0.4 lib/qor_test/rubies.rb
qor_test-0.0.3 lib/qor_test/rubies.rb