<commit_before>require 'yaml'

class TravisYmlScript
  def initialize(attrs = {})
    @cmds = []
    @file = attrs[:file]
  end

  def build
    reset

    conf = YAML.load(@file.read)

    build_env(conf)
    build_proc(conf)
    build_post_proc(conf)
  end

  def to_s
    [ header, @cmds, footer ].flatten.join("\n")
  end

  private

  def header
    return << __HEADER__
              #!/usr/bin/env bash

              capture_result() {
                local result=$1
                export TRAVIS_JENKINS_RESULT=$(( ${TRAVIS_JENKINS_RESULT:-0} | $(($result != 0)) ))
              }

              export CI=1
              export CONTINUOUS_INTEGRATION=1
              export TRAVIS_JENKINS_RESULT=0

              __HEADER__
  end

  def footer
    return << __FOOTER__
              exit $TRAVIS_JENKINS_RESULT
              __FOOTER__
  end

  def reset
    @cmds = []
  end

  def build_env(conf)
    values(conf, "env").each do |env|
      export(env)
    end
  end

  def build_proc(conf)
    %w[ before_install install before_script script after_script ].each do |k|
      values(conf, k).each do |cmd|
        run_if("$TRAVIS_JENKINS_RESULT -eq 0") do
          echo(cmd)
          run(cmd)
          capture_result
        end
      end
    end
  end

  def build_post_proc(conf)
    values(conf, "after_success").each do |cmd|
      run_if("$TRAVIS_JENKINS_RESULT -eq 0") do
        echo(cmd)
        run(cmd)
      end
    end

    values(conf, "after_failure").each do |cmd|
      run_if("$TRAVIS_JENKINS_RESULT -ne 0") do
        echo(cmd)
        run(cmd)
      end
    end
  end

  def values(conf, key)
    [ conf[key] || [] ].flatten.map(&:to_s)
  end

  def export(cmd)
    run("export #{cmd}")
  end

  def echo(cmd)
    run("echo '$ #{cmd}'")
  end

  def capture_result
    run("capture_result $?")
  end

  def run(cmd)
    @cmds.push(cmd)
  end

  def run_if(cond)
    if block_given?
      run("if [ #{cond} ]; then")
      yield
      run("fi")
    end
  end
end
<commit_msg>changed to shellescape to solve #9 as seen in old PR<commit_after>require 'yaml'
require 'shellwords'

class TravisYmlScript
  def initialize(attrs = {})
    @cmds = []
    @file = attrs[:file]
  end

  def build
    reset

    conf = YAML.load(@file.read)

    build_env(conf)
    build_proc(conf)
    build_post_proc(conf)
  end

  def to_s
    [ header, @cmds, footer ].flatten.join("\n")
  end

  private

  def header
    return << __HEADER__
              #!/usr/bin/env bash

              capture_result() {
                local result=$1
                export TRAVIS_JENKINS_RESULT=$(( ${TRAVIS_JENKINS_RESULT:-0} | $(($result != 0)) ))
              }

              export CI=1
              export CONTINUOUS_INTEGRATION=1
              export TRAVIS_JENKINS_RESULT=0

              __HEADER__
  end

  def footer
    return << __FOOTER__
              exit $TRAVIS_JENKINS_RESULT
              __FOOTER__
  end

  def reset
    @cmds = []
  end

  def build_env(conf)
    values(conf, "env").each do |env|
      export(env)
    end
  end

  def build_proc(conf)
    %w[ before_install install before_script script after_script ].each do |k|
      values(conf, k).each do |cmd|
        run_if("$TRAVIS_JENKINS_RESULT -eq 0") do
          echo(cmd)
          run(cmd)
          capture_result
        end
      end
    end
  end

  def build_post_proc(conf)
    values(conf, "after_success").each do |cmd|
      run_if("$TRAVIS_JENKINS_RESULT -eq 0") do
        echo(cmd)
        run(cmd)
      end
    end

    values(conf, "after_failure").each do |cmd|
      run_if("$TRAVIS_JENKINS_RESULT -ne 0") do
        echo(cmd)
        run(cmd)
      end
    end
  end

  def values(conf, key)
    [ conf[key] || [] ].flatten.map(&:to_s)
  end

  def export(cmd)
    run("export #{cmd}")
  end

  def echo(cmd)
    run("echo $" + Shellwords.shellescape(cmd))
  end

  def capture_result
    run("capture_result $?")
  end

  def run(cmd)
    @cmds.push(cmd)
  end

  def run_if(cond)
    if block_given?
      run("if [ #{cond} ]; then")
      yield
      run("fi")
    end
  end
end

{'fraction_non_alphanumeric': 0.11119239209948793, 'fraction_numerical': 0.004633016337478664, 'mean_word_length': 1.9574621485219899, 'pattern_counts': {'":': 0, '<': 11, '<?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': '6510609', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1440, 'n_words': 330}