<commit_before>#!/bin/bash

set -e

readonly GS_STONE_NAME="travis"
readonly GS_DEVKIT_DOWNLOAD="https://github.com/GsDevKit/GsDevKit_home.git"
readonly GS_DEVKIT_BRANCH="master"
export GS_HOME="${SMALLTALK_CI_BUILD}/GsDevKit_home"

################################################################################
# Clone the GsDevKit_home project.
################################################################################
gemstone::prepare_gsdevkit_home() {
  travis_fold start clone_gsdevkit "Cloning GsDevKit..."
    timer_start

    pushd "${SMALLTALK_CI_BUILD}" || exit 1
      git clone "${GS_DEVKIT_DOWNLOAD}" || exit 1
      cd "${GS_HOME}" || exit 1
      git checkout "${GS_DEVKIT_BRANCH}" || exit 1

      # pre-clone /sys/local, so that travis can skip backups
      $GS_HOME/bin/private/clone_sys_local || exit 1
      # arrange to skip backups
      cp $GS_HOME/tests/sys/local/client/tode-scripts/* $GS_HOME/sys/local/client/tode-scripts || exit 1

      # Operating system setup already performed
      touch $GS_HOME/bin/.gsdevkitSysSetup || exit 1
    popd || exit 1

    export GS_TRAVIS=true # install special key files for running GemStone on Travis hosts

    timer_finish
  travis_fold end clone_gsdevkit
}

################################################################################
# Create a GemStone stone.
# Arguments:
#   config_smalltalk
################################################################################
gemstone::prepare_stone() {
  local gemstone_version

  gemstone_version="$(echo $1 | cut -f2 -d-)"

  local gemstone_cached_extent_file="${SMALLTALK_CI_CACHE}/gemstone/extents/${gemstone_version}_extent0.tode.dbf"
  local pharo_image_file="Pharo-3.0.image"
  local pharo_changes_file="Pharo-3.0.changes"

  travis_fold start install_server "Installing server..."
    timer_start

    $GS_HOME/bin/installServer || print_error_and_exit "installServer failed."

    timer_finish
  travis_fold end install_server

  travis_fold start prepare_cache "Preparing travis caches..."
    timer_start
    if ! is_dir "${SMALLTALK_CI_VMS}/Pharo-3.0"; then
      mkdir "${SMALLTALK_CI_VMS}/Pharo-3.0"
      echo "Downloading Pharo-3.0 vm to cache" 
      pushd "${SMALLTALK_CI_VMS}/Pharo-3.0" > /dev/null
        pharo_zeroconf="$(download_file "get.pharo.org/vm30")" || print_error_and_exit "Pharo-3.0 vm download failed."
	bash -c "${pharo_zeroconf}"  || print_error_and_exit "Pharo-3.0 vm download failed."
      popd > /dev/null
    fi

    if ! is_file "${SMALLTALK_CI_CACHE}/Pharo-3.0.image"; then
      echo "Downloading Pharo-3.0 image to cache" 
      pushd ${SMALLTALK_CI_CACHE} > /dev/null
        pharo_zeroconf="$(download_file "get.pharo.org/30")" || print_error_and_exit "Pharo-3.0 image download failed."
	bash -c "${pharo_zeroconf}"  || print_error_and_exit "Pharo-3.0 image download failed."
	mv "Pharo.image" "Pharo-3.0.image"
        mv "Pharo.changes" "Pharo-3.0.changes"
      popd > /dev/null
    fi

    if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone"; then
      echo "Creating GemStone extent cache" 
      mkdir "${SMALLTALK_CI_CACHE}/gemstone"
      if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone/extents"; then
        mkdir "${SMALLTALK_CI_CACHE}/gemstone/extents"
      fi
      if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone/pharo"; then
        mkdir "${SMALLTALK_CI_CACHE}/gemstone/pharo"
      fi
    fi

    if is_file "${SMALLTALK_CI_CACHE}/${pharo_image_file}"; then
      if is_file "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image"; then
        echo "Utilizing cached gsDevKitCommandLine image" 
        cp "${SMALLTALK_CI_CACHE}/${pharo_image_file}" $GS_HOME/shared/pharo/Pharo.image
        cp "${SMALLTALK_CI_CACHE}/${pharo_changes_file}" $GS_HOME/shared/pharo/Pharo.changes
        ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo" $GS_HOME/shared/pharo/pharo
        ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo-ui" $GS_HOME/shared/pharo/pharo-ui
        ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo-vm" $GS_HOME/shared/pharo/pharo-vm
        cp "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image" $GS_HOME/shared/pharo/
        cp "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.changes" $GS_HOME/shared/pharo/
      fi
    fi

    timer_finish
  travis_fold end prepare_cache

  travis_fold start create_stone "Creating stone..."
    timer_start

    if ! is_file "$gemstone_cached_extent_file"; then
      $GS_HOME/bin/createStone "$GS_STONE_NAME" $gemstone_version || print_error_and_exit "createStone failed."
      cp "$GS_HOME/server/stones/$GS_STONE_NAME/snapshots/extent0.tode.dbf" "$gemstone_cached_extent_file"
    else
      $GS_HOME/bin/createStone -t "$gemstone_cached_extent_file" "$GS_STONE_NAME" $gemstone_version || print_error_and_exit "createStone failed."
    fi

    if ! is_file "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image"; then
      cp $GS_HOME/shared/pharo/gsDevKitCommandLine.* "${SMALLTALK_CI_CACHE}/gemstone/pharo/"
    fi

    timer_finish
  travis_fold end create_stone

}

################################################################################
# Load project into GemStone stone and run tests.
# Locals:
#   config_project_home
#   config_ston
# Globals:
#   SMALLTALK_CI_HOME
# Returns:
#   Status code of project loading
################################################################################
gemstone::load_and_test_project() {<commit_msg>make caching logic dependent upon caching actually being enabled<commit_after>#!/bin/bash

set -e

readonly GS_STONE_NAME="travis"
readonly GS_DEVKIT_DOWNLOAD="https://github.com/GsDevKit/GsDevKit_home.git"
readonly GS_DEVKIT_BRANCH="master"
export GS_HOME="${SMALLTALK_CI_BUILD}/GsDevKit_home"
readonly GS_TRAVIS_CACHE_ENABLED="$CASHER_DIR"

################################################################################
# Clone the GsDevKit_home project.
################################################################################
gemstone::prepare_gsdevkit_home() {
  travis_fold start clone_gsdevkit "Cloning GsDevKit..."
    timer_start

    pushd "${SMALLTALK_CI_BUILD}" || exit 1
      git clone "${GS_DEVKIT_DOWNLOAD}" || exit 1
      cd "${GS_HOME}" || exit 1
      git checkout "${GS_DEVKIT_BRANCH}" || exit 1

      # pre-clone /sys/local, so that travis can skip backups
      $GS_HOME/bin/private/clone_sys_local || exit 1
      # arrange to skip backups
      cp $GS_HOME/tests/sys/local/client/tode-scripts/* $GS_HOME/sys/local/client/tode-scripts || exit 1

      # Operating system setup already performed
      touch $GS_HOME/bin/.gsdevkitSysSetup || exit 1
    popd || exit 1

    export GS_TRAVIS=true # install special key files for running GemStone on Travis hosts

    timer_finish
  travis_fold end clone_gsdevkit
}

################################################################################
# Create a GemStone stone.
# Arguments:
#   config_smalltalk
################################################################################
gemstone::prepare_stone() {
  local gemstone_version

  gemstone_version="$(echo $1 | cut -f2 -d-)"

  local gemstone_cached_extent_file="${SMALLTALK_CI_CACHE}/gemstone/extents/${gemstone_version}_extent0.tode.dbf"
  local pharo_image_file="Pharo-3.0.image"
  local pharo_changes_file="Pharo-3.0.changes"

  travis_fold start install_server "Installing server..."
    timer_start

    $GS_HOME/bin/installServer || print_error_and_exit "installServer failed."

    timer_finish
  travis_fold end install_server

  if [ "${GS_TRAVIS_CACHE_ENABLED}x" = "x" ] ; then
    echo "Travis dependency cache not being used"
  else
    travis_fold start prepare_cache "Preparing travis caches..."
      timer_start
      if ! is_dir "${SMALLTALK_CI_VMS}/Pharo-3.0"; then
        mkdir "${SMALLTALK_CI_VMS}/Pharo-3.0"
        echo "Downloading Pharo-3.0 vm to cache" 
        pushd "${SMALLTALK_CI_VMS}/Pharo-3.0" > /dev/null
          pharo_zeroconf="$(download_file "get.pharo.org/vm30")" || print_error_and_exit "Pharo-3.0 vm download failed."
  	bash -c "${pharo_zeroconf}"  || print_error_and_exit "Pharo-3.0 vm download failed."
        popd > /dev/null
      fi
  
      if ! is_file "${SMALLTALK_CI_CACHE}/Pharo-3.0.image"; then
        echo "Downloading Pharo-3.0 image to cache" 
        pushd ${SMALLTALK_CI_CACHE} > /dev/null
          pharo_zeroconf="$(download_file "get.pharo.org/30")" || print_error_and_exit "Pharo-3.0 image download failed."
  	bash -c "${pharo_zeroconf}"  || print_error_and_exit "Pharo-3.0 image download failed."
  	mv "Pharo.image" "Pharo-3.0.image"
          mv "Pharo.changes" "Pharo-3.0.changes"
        popd > /dev/null
      fi
  
      if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone"; then
        echo "Creating GemStone extent cache" 
        mkdir "${SMALLTALK_CI_CACHE}/gemstone"
        if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone/extents"; then
          mkdir "${SMALLTALK_CI_CACHE}/gemstone/extents"
        fi
        if ! is_dir "${SMALLTALK_CI_CACHE}/gemstone/pharo"; then
          mkdir "${SMALLTALK_CI_CACHE}/gemstone/pharo"
        fi
      fi
  
      if is_file "${SMALLTALK_CI_CACHE}/${pharo_image_file}"; then
        if is_file "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image"; then
          echo "Utilizing cached gsDevKitCommandLine image" 
          cp "${SMALLTALK_CI_CACHE}/${pharo_image_file}" $GS_HOME/shared/pharo/Pharo.image
          cp "${SMALLTALK_CI_CACHE}/${pharo_changes_file}" $GS_HOME/shared/pharo/Pharo.changes
          ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo" $GS_HOME/shared/pharo/pharo
          ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo-ui" $GS_HOME/shared/pharo/pharo-ui
          ln -s "${SMALLTALK_CI_VMS}/Pharo-3.0/pharo-vm" $GS_HOME/shared/pharo/pharo-vm
          cp "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image" $GS_HOME/shared/pharo/
          cp "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.changes" $GS_HOME/shared/pharo/
        fi
      fi
  
      timer_finish
    travis_fold end prepare_cache
  fi

  travis_fold start create_stone "Creating stone..."
    timer_start

    if [ "${GS_TRAVIS_CACHE_ENABLED}x" = "x" ] ; then
      $GS_HOME/bin/createStone "$GS_STONE_NAME" $gemstone_version || print_error_and_exit "createStone failed."
    else
      if ! is_file "$gemstone_cached_extent_file"; then
        $GS_HOME/bin/createStone "$GS_STONE_NAME" $gemstone_version || print_error_and_exit "createStone failed."
        cp "$GS_HOME/server/stones/$GS_STONE_NAME/snapshots/extent0.tode.dbf" "$gemstone_cached_extent_file"
      else
        $GS_HOME/bin/createStone -t "$gemstone_cached_extent_file" "$GS_STONE_NAME" $gemstone_version || print_error_and_exit "createStone failed."
      fi
  
      if ! is_file "${SMALLTALK_CI_CACHE}/gemstone/pharo/gsDevKitCommandLine.image"; then
        cp $GS_HOME/shared/pharo/gsDevKitCommandLine.* "${SMALLTALK_CI_CACHE}/gemstone/pharo/"
      fi
    fi

    timer_finish
  travis_fold end create_stone

}

################################################################################
# Load project into GemStone stone and run tests.
# Locals:
#   config_project_home
#   config_ston
# Globals:
#   SMALLTALK_CI_HOME
# Returns:
#   Status code of project loading
################################################################################
gemstone::load_and_test_project() {
{'fraction_non_alphanumeric': 0.1900739176346357, 'fraction_numerical': 0.008799718409010912, 'mean_word_length': 4.042147293700089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4774764', 'n_tokens_mistral': 4002, 'n_tokens_neox': 3803, 'n_words': 693}