<commit_before># Copyright 2011 (c) MaestroDev.  All rights reserved.
require 'shell'

# Common methods for ruby/rvm management
module Maestro
  module Plugin
    module RubyHelper

      def script_prefix
        return '' unless @use_rvm

        # Note double quotes in error... that is so it doesn't trigger unit-test errors
        prefix = <<-SCRIPT
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
  \# First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
  \# Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"
else
  printf "ER""ROR: An RVM installation was not found.\n"
fi
#{@environment.empty? ? "": "#{Maestro::Util::Shell::ENV_EXPORT_COMMAND} #{@environment}" } 
SCRIPT
      end
    
      # Update ruby and rubygems if needed
      def update_ruby_rubygems
        return unless @use_rvm

        write_output "\nUsing RVM w/Ruby Version #{@ruby_version}"
      
        exit_code = update_ruby(@ruby_version)
        if exit_code.success?
          if !@rubygems_version.empty?
            write_output "\nUsing RubyGems Version #{@rubygems_version}\n"
            update_rubygems(@ruby_version, @rubygems_version)
          end
        end
      end

      def update_ruby(version)
        write_output("\nEnsuring ruby version #{version}\n", :buffer => true)
        use = Maestro::Util::Shell.new
        use.create_script("#{script_prefix}#{@rvm_executable} use #{version}")
        use.run_script_with_delegate(self, :on_output)

        Maestro.log.debug("RVM use: #{use.to_s} #{!use.exit_code.success? or use.to_s.include?("ERROR:")}")
        if !use.exit_code.success? or use.to_s.include?("ERROR:")
          write_output("\nInstalling ruby version #{version}\n", :buffer => true)
          install = Maestro::Util::Shell.new
          install.create_script("#{@rvm_executable} install #{version}")

          install.run_script_with_delegate(self, :on_output)

          if install.exit_code.success?
            @installed_ruby_verion = version
            write_output("\nRuby version #{version} installed successfully")
          else
            # There have been reports that the install apparently fails, but the next time round
            # the correct version of ruby is installed.
            # So if the install "fails" we shall re-do the 'use' test and if that succeeds then
            # we shall consider the install a success
            use.run_script
            if use.exit_code.success?
              @installed_ruby_verion = version
              write_output("\nRuby version #{version} install indicated failure, but appears to be installed successfully")
            else
              write_output("\nRuby version #{version} not installed, exit code #{install.exit_code.exit_code}")
            end
          end

          return install.exit_code
        else
          @installed_ruby_version = version
        end

        return use.exit_code
      end

      def is_using_rubygems_version?(version)
        rg = Maestro::Util::Shell.run_command("#{script_prefix} #{@rvm_executable} #{@ruby_version} do gem --version")

        return rg[0].success? && rg[1].include?(version)
      end
    
      def run_update_rubygems(ruby_version, rubygems_version)
        write_output("Installing rubygems version #{rubygems_version}", :buffer => true)
        script =  "#{script_prefix} #{@rvm_executable} #{ruby_version} do rvm rubygems #{rubygems_version}"
        shell = Maestro::Util::Shell.new      <commit_msg>MAESTRO-3164 typo - missing s in version, doh<commit_after># Copyright 2011 (c) MaestroDev.  All rights reserved.
require 'shell'

# Common methods for ruby/rvm management
module Maestro
  module Plugin
    module RubyHelper

      def script_prefix
        return '' unless @use_rvm

        # Note double quotes in error... that is so it doesn't trigger unit-test errors
        prefix = <<-SCRIPT
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
  \# First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
  \# Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"
else
  printf "ER""ROR: An RVM installation was not found.\n"
fi
#{@environment.empty? ? "": "#{Maestro::Util::Shell::ENV_EXPORT_COMMAND} #{@environment}" } 
SCRIPT
      end
    
      # Update ruby and rubygems if needed
      def update_ruby_rubygems
        return unless @use_rvm

        write_output "\nUsing RVM w/Ruby Version #{@ruby_version}"
      
        exit_code = update_ruby(@ruby_version)
        if exit_code.success?
          if !@rubygems_version.empty?
            write_output "\nUsing RubyGems Version #{@rubygems_version}\n"
            update_rubygems(@ruby_version, @rubygems_version)
          end
        end
      end

      def update_ruby(version)
        write_output("\nEnsuring ruby version #{version}\n", :buffer => true)
        use = Maestro::Util::Shell.new
        use.create_script("#{script_prefix}#{@rvm_executable} use #{version}")
        use.run_script_with_delegate(self, :on_output)

        Maestro.log.debug("RVM use: #{use.to_s} #{!use.exit_code.success? or use.to_s.include?("ERROR:")}")
        if !use.exit_code.success? or use.to_s.include?("ERROR:")
          write_output("\nInstalling ruby version #{version}\n", :buffer => true)
          install = Maestro::Util::Shell.new
          install.create_script("#{@rvm_executable} install #{version}")

          install.run_script_with_delegate(self, :on_output)

          if install.exit_code.success?
            @installed_ruby_version = version
            write_output("\nRuby version #{version} installed successfully")
          else
            # There have been reports that the install apparently fails, but the next time round
            # the correct version of ruby is installed.
            # So if the install "fails" we shall re-do the 'use' test and if that succeeds then
            # we shall consider the install a success
            use.run_script
            if use.exit_code.success?
              @installed_ruby_version = version
              write_output("\nRuby version #{version} install indicated failure, but appears to be installed successfully")

              return use.exit_code
            else
              write_output("\nRuby version #{version} not installed, exit code #{install.exit_code.exit_code}")
            end
          end

          return install.exit_code
        else
          @installed_ruby_version = version
        end

        return use.exit_code
      end

      def is_using_rubygems_version?(version)
        rg = Maestro::Util::Shell.run_command("#{script_prefix} #{@rvm_executable} #{@ruby_version} do gem --version")

        return rg[0].success? && rg[1].include?(version)
      end
    
      def run_update_rubygems(ruby_version, rubygems_version)
        write_output("Installing rubygems version #{rubygems_version}", :buffer => true)
        script =  "#{script_prefix} #{@rvm_executable} #{ruby_version} do rvm rubygems #{rubygems_version}"
        shell = Maestro::Util::Shell.new      
{'fraction_non_alphanumeric': 0.1064659977703456, 'fraction_numerical': 0.002229654403567447, 'mean_word_length': 2.624747474747475, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469219', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2092, 'n_words': 564}