<commit_before>require 'night_watch/utilities'

module NightWatch
  class RailsApp
    include Utilities::ScriptRunner
    include Utilities::Workspace

    attr_reader :name

    def initialize(name, path)
      self.workspace = path
      @name = name
    end

    def run(&block)
      start
      block.call
    ensure
      stop
    end

    def start
      in_rails_root { sh_with_rvm("bundle exec rails s -d -p 3333") }
      raise "Could not start application #{name}" unless running?
    end

    def stop
      sh("kill -9 #{pid}") if running?
    end

    def prepare(&block)
      bundle_install
      bower_install
      in_workspace(&block) unless block.nil?
    end

    def running?
      !pid.nil?
    end

    def pid
      IO.read(File.join(rails_root, 'tmp', 'pids', 'server.pid'))
    rescue Errno::ENOENT
      nil
    end


  protected

    def rails_root
      workspace
    end

    def in_rails_root
      Dir.chdir(rails_root) { yield }
    end

    def bundle_install
      in_rails_root { sh_with_rvm("bundle install") }
    end

    def bower_install
      in_workspace { "bower install" }
    end

    def sh_with_rvm(command)
      sh("rvm #{rvm_environment} do #{command}")
    end

    def rvm_environment
      @rvm_environment ||= begin
        ruby_version = IO.read(File.join(workspace, '.ruby-version')).chomp
        ruby_gemset = IO.read(File.join(workspace, '.ruby-gemset')).chomp

        "#{ruby_version}@#{ruby_gemset}"
      end
    end

  end
end
<commit_msg>Ensure that rvm gemsets are created before use<commit_after>require 'night_watch/utilities'

module NightWatch
  class RailsApp
    include Utilities::ScriptRunner
    include Utilities::Workspace

    attr_reader :name

    def initialize(name, path)
      self.workspace = path
      @name = name
    end

    def run(&block)
      start
      block.call
    ensure
      stop
    end

    def start
      in_rails_root { sh_with_rvm("bundle exec rails s -d -p 3333") }
      raise "Could not start application #{name}" unless running?
    end

    def stop
      sh("kill -9 #{pid}") if running?
    end

    def prepare(&block)
      ensure_rvm_gemset
      bundle_install
      bower_install
      in_workspace(&block) unless block.nil?
    end

    def running?
      !pid.nil?
    end

    def pid
      IO.read(File.join(rails_root, 'tmp', 'pids', 'server.pid'))
    rescue Errno::ENOENT
      nil
    end


  protected

    def rails_root
      workspace
    end

    def in_rails_root
      Dir.chdir(rails_root) { yield }
    end

    def bundle_install
      in_rails_root { sh_with_rvm("bundle install") }
    end

    def bower_install
      in_workspace { "bower install" }
    end

    def ensure_rvm_gemset
      sh_with_rvm("rvm gemset create #{ruby_gemset}")
    end

    def sh_with_rvm(command)
      sh("rvm #{rvm_environment} do #{command}")
    end

    def rvm_environment
      @rvm_environment ||= "#{ruby_version}@#{ruby_gemset}"
    end

    def ruby_gemset
      @ruby_gemset ||= IO.read(File.join(workspace, '.ruby-gemset')).chomp
    end

    def ruby_version
      @ruby_version ||= IO.read(File.join(workspace, '.ruby-version')).chomp
    end

  end
end

{'fraction_non_alphanumeric': 0.0923606762680025, 'fraction_numerical': 0.0031308703819661866, 'mean_word_length': 2.3702531645569622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1853300', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1042, 'n_words': 259}