<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
      end

      yield(state, config)

      [state, config]
    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

      default_command = (wants_bundle? ? "bundle exec rake" : "rake")
      command = config["script"] || default_command
      command = "#{switch}#{command}"

      sh(env, command)
    end

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

    def gemfile
      config["gemfile"]
    end

    def build_switch_statement
      switch_ruby = Ruby.switch_statement(config["rvm"])
      if switch_ruby.is_a?(Hash)
        env.merge!(switch_ruby)
        switch_ruby = nil
      end
      switch_ruby
    end

    def add_env_from_config
      Shellwords.split(config["env"] || "").each do |part|
        name, value = part.split("=", 2)
        env[name] = value
      end
      env["BUNDLE_GEMFILE"] = gemfile if gemfile
    end

    def committed?(file)
      @committed_files ||= (File.exist?(".git") && `git ls-files`.split("\n")) || []
      @committed_files.include?(file)
    end

    def flock(file)
      File.open(file, "w") do |f|
        begin
          f.flock(File::LOCK_EX)
          yield
        ensure
          f.flock(File::LOCK_UN)
        end
      end
    end

    def sh(*args)
      ::WWTD.send(:sh, *args)
    end
  end
end
<commit_msg>If script is an array, run commands in serial<commit_after>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
      end

      yield(state, config)

      [state, config]
    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

      default_command = (wants_bundle? ? "bundle exec rake" : "rake")
      command = config["script"] || default_command
      command = command.join(" && ") if Array === command
      command = "#{switch}#{command}"

      sh(env, command)
    end

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

    def gemfile
      config["gemfile"]
    end

    def build_switch_statement
      switch_ruby = Ruby.switch_statement(config["rvm"])
      if switch_ruby.is_a?(Hash)
        env.merge!(switch_ruby)
        switch_ruby = nil
      end
      switch_ruby
    end

    def add_env_from_config
      Shellwords.split(config["env"] || "").each do |part|
        name, value = part.split("=", 2)
        env[name] = value
      end
      env["BUNDLE_GEMFILE"] = gemfile if gemfile
    end

    def committed?(file)
      @committed_files ||= (File.exist?(".git") && `git ls-files`.split("\n")) || []
      @committed_files.include?(file)
    end

    def flock(file)
      File.open(file, "w") do |f|
        begin
          f.flock(File::LOCK_EX)
          yield
        ensure
          f.flock(File::LOCK_UN)
        end
      end
    end

    def sh(*args)
      ::WWTD.send(:sh, *args)
    end
  end
end

{'fraction_non_alphanumeric': 0.12933025404157045, 'fraction_numerical': 0.00046189376443418013, 'mean_word_length': 2.3086325439266617, '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': '7008870', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1371, 'n_words': 319}