<commit_before>module WWTD
  class Run
    def initialize(config, env, lock)
      @config, @env, @lock = config, env, lock
      add_env_from_config
      @switch = build_switch_statement
    end

    def execute(&block)
      state = if Ruby.available?(config["rvm"])
        yield(:start, config)
        success? ? :success : :failure
      else
        :missing_ruby_version
      end

      yield(state, config)

      [state, config]
    end

    # internal api
    def env_and_command_for_section(key)
      unless command = config[key]
        return [env, "#{'bundle exec ' if wants_bundle?}rake"] if key == "script"
        return
      end

      command = [command] unless Array === command
      command = command.map { |cmd| "#{switch}#{cmd}" }.join(" && ")

      [env, command]
    end

    private

    attr_reader :config, :env, :lock, :switch


    def success?
      if wants_bundle?
        flock File.join(lock, (config["rvm"] || "rvm").to_s) do
          default_bundler_args = "--deployment --path #{Dir.pwd}/vendor/bundle" if committed?("#{gemfile || DEFAULT_GEMFILE}.lock")
          bundle_command = "#{switch}bundle install #{config["bundler_args"] || default_bundler_args}"
          return false unless sh(env, "#{bundle_command.strip} --quiet")
        end
      end

      ["before_install", "install", "before_script", "script", "after_script"].each do |section|
        env_and_command = env_and_command_for_section(section)
        if env_and_command
          result = sh(*env_and_command)
          return result if not result
        end
      end
    end

    def wants_bundle?
      gemfile || File.exist?(DEFAULT_GEMFILE)
    end

    def gemfile
      config["gemfile"]<commit_msg>Fix tests<commit_after>module WWTD
  class Run

    SECTIONS = ["before_install", "install", "before_script", "script", "after_script"]

    def initialize(config, env, lock)
      @config, @env, @lock = config, env, lock
      add_env_from_config
      @switch = build_switch_statement
    end

    def execute(&block)
      state = if Ruby.available?(config["rvm"])
        yield(:start, config)
        success? ? :success : :failure
      else
        :missing_ruby_version
      end

      yield(state, config)

      [state, config]
    end

    # internal api
    def env_and_command_for_section(key)
      unless command = config[key]
        if key == "script"
          return [env, wants_bundle? ? "#{switch}bundle exec rake" : "#{switch}rake"]
        else
          return false
        end
      end

      command = [command] unless Array === command
      command = command.map { |cmd| "#{switch}#{cmd}" }.join(" && ")

      [env, command]
    end

    private

    attr_reader :config, :env, :lock, :switch

    def success?
      if wants_bundle?
        flock File.join(lock, (config["rvm"] || "rvm").to_s) do
          default_bundler_args = "--deployment --path #{Dir.pwd}/vendor/bundle" if committed?("#{gemfile || DEFAULT_GEMFILE}.lock")
          bundle_command = "#{switch}bundle install #{config["bundler_args"] || default_bundler_args}"
          return false unless sh(env, "#{bundle_command.strip} --quiet")
        end
      end

      SECTIONS.each do |section|
        env_and_command = env_and_command_for_section(section)
        if env_and_command
          return unless sh(*env_and_command)
        end
      end
    end

    def wants_bundle?
      gemfile || File.exist?(DEFAULT_GEMFILE)
    end

    def gemfile
      config["gemfile"]
{'fraction_non_alphanumeric': 0.11971428571428572, 'fraction_numerical': 0.0, 'mean_word_length': 2.501, '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': '58169', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1049, 'n_words': 264}