summaryrefslogblamecommitdiff
path: root/scripts/pr-scala-common
blob: 01112588f7ca5931a146c6c63ad6d421e3627fec (plain) (tree)



































































































































































































































































                                                                                                                                                                                                         
# This is for forcibly stopping the job from a subshell (see test
# below).
trap "exit 1" TERM
export TOP_PID=$$
set -e

# Known problems : does not fare well with interrupted, partial
# compilations. We should perhaps have a multi-dependency version
# of do_i_have below

BASEDIR="$(pwd)"

LOGGINGDIR="$BASEDIR/logs"
mkdir -p $LOGGINGDIR

unset SBT_HOME
SBT_HOME="$BASEDIR/.sbt"
mkdir -p $SBT_HOME
IVY_CACHE="$BASEDIR/.ivy2"
mkdir -p $IVY_CACHE
rm -rf $IVY_CACHE/cache/org.scala-lang

# temp dir where all 'non-build' operation are performed
TMP_ROOT_DIR=$(mktemp -d -t pr-scala.XXXX)
TMP_DIR="${TMP_ROOT_DIR}/tmp"
mkdir "${TMP_DIR}"


# detect sed version and how to enable extended regexes
SEDARGS="-n$(if (echo "a" | sed -nE "s/a/b/" &> /dev/null); then echo E; else echo r; fi)"


# :docstring getOrUpdate:
# Usage : getOrUpdate <directory> <url> <reference> <n>
#
# Updates or clones the checkout of <reference> taken from the
# git repo at <url> into the local directory
# <directory> and cleans the checkout.
#
# :end docstring:

function getOrUpdate(){
    if [ ! -d $1 ]; then
        git clone --depth 1 $2
    fi
    pushd $1

    git fetch $2 $3

    git checkout -q FETCH_HEAD

    git reset --hard FETCH_HEAD

    git clean -fxd

    git log --oneline -1

    git status

    popd
}


# :docstring parse_properties:
# Usage: parse_properties javaPropertyFile
# Exports variables set in property file under $BASEDIR, mangling names by replacing dots with _
# :end docstring:

function parse_properties(){
    propFile="$BASEDIR/$1"
    if [ ! -f $propFile ]; then
      say "Property file $propFile not found."
      exit 1
    else
      awk -f "$scriptsDir/readproperties.awk" "$propFile" > "$propFile.sh"
      . "$propFile.sh" # yeah yeah, not that secure, improvements welcome (I tried, but bash made me cry again)
    fi
}


# :docstring test:
# Usage: test <argument ..>
# Executes <argument ..>, logging the launch of the command to the
# main log file, and kills global script execution with the TERM
# signal if the commands ends up failing.
# DO NOT USE ON FUNCTIONS THAT DECLARE VARIABLES,
# AS YOU'LL BE RUNNING IN A SUBSHELL AND VARIABLE DECLARATIONS WILL BE LOST
# :end docstring:

function test() {
    echo "### $@"
    "$@"
    status=$?
    if [ $status -ne 0 ]; then
        say "### ERROR with $1"
        kill -s TERM $TOP_PID
    fi
}

# :docstring say:
# Usage: say <argument ..>
# Prints <argument ..> to both console and the main log file.
# :end docstring:

function say(){
    (echo "$@") | tee -a $LOGGINGDIR/compilation-$SCALADATE-$SCALAHASH.log
}

# General debug logging
# $* - message
function debug () {
  echo "----- $*"
}


## TAKEN FROM UBER-BUILD, except that it "returns" (via $RES) true/false
# Check if an artifact is available
# $1 - groupId
# $2 - artifacId
# $3 - version
# $4 - extra repository to look in (optional)
# return value in $RES
function checkAvailability () {
  cd "${TMP_DIR}"
  rm -rf *

# pom file for the test project
  cat > pom.xml << EOF
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.typesafe</groupId>
  <artifactId>typesafeDummy</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Dummy</name>
  <url>http://127.0.0.1</url>
  <dependencies>
    <dependency>
      <groupId>$1</groupId>
      <artifactId>$2</artifactId>
      <version>$3</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>sonatype.snapshot</id>
      <name>Sonatype maven snapshot repository</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <snapshots>
        <updatePolicy>daily</updatePolicy>
      </snapshots>
    </repository>
EOF

  if [ -n "$4" ]
  then
# adds the extra repository
    cat >> pom.xml << EOF
    <repository>
      <id>extrarepo</id>
      <name>extra repository</name>
      <url>$4</url>
    </repository>
EOF
  fi

  cat >> pom.xml << EOF
  </repositories>
</project>
EOF

  set +e
  mvn "${MAVEN_ARGS[@]}" compile &> "${TMP_DIR}/mvn.log"
  RES=$?
  # Quiet the maven, but allow diagnosing problems.
  grep -i downloading "${TMP_DIR}/mvn.log"
  grep -i exception "${TMP_DIR}/mvn.log"
  grep -i error "${TMP_DIR}/mvn.log"
  set -e

# log the result
  if [ ${RES} == 0 ]
  then
    debug "$1:$2:jar:$3 found !"
    RES=true
  else
    debug "$1:$2:jar:$3 not found !"
    RES=false
  fi
}


# :docstring preparesbt:
# Usage: preparesbt
# This lets sbt know to look for the local maven repository.
# :end docstring:

# don't share any caches, sbt dirs, repos,... to avoid concurrent writes
# keep them local to the workspace also lets us diagnose problems more easily
# Make sure this is an absolute path with preceding '/'
LOCAL_M2_REPO="$BASEDIR/m2repo"
GENMVNOPTS="-e -B -X -Dmaven.repo.local=${LOCAL_M2_REPO}"
# otherwise this just keeps growing and growing due to the -$sha-SNAPSHOT approach
[[ -d "$LOCAL_M2_REPO"/org/scala-lang ]] && rm -rf "$LOCAL_M2_REPO"/org/scala-lang

function preparesbt(){
    # Am I using sbt-extras? I.e., do we need to use -sbt-dir?
    set +e
    sbt -h 2>&1 | grep -qe "-sbt-dir"
    sbt_extraed=$?
    set -e
    export DEST_REPO_FILE=$SBT_HOME/repositories
    if [ $sbt_extraed -eq 0 ]; then
        # sbt-extras does not honor an explicit -Dsbt.global.base
        export SBT_ARGS="-verbose -debug -no-colors -sbt-dir $SBT_HOME -ivy $IVY_CACHE"
        say "### sbt-extras detected, using args $SBT_ARGS"
    else
        # don't pass -verbose or -debug, they tend to break sbt...
        export SBT_ARGS="-Dsbt.global.base=$SBT_HOME -Dsbt.ivy.home=$IVY_CACHE"
        say "### vanilla sbt detected, using args $SBT_ARGS"
    fi

    if [ -f $DEST_REPO_FILE ]; then
        export OLD_SBT_REPO_FILE=$(mktemp -t sbtreposXXX)
        cat $DEST_REPO_FILE > $OLD_SBT_REPO_FILE
    fi
    cat > $DEST_REPO_FILE <<EOF
[repositories]
  maven-central
  local
  typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]
  sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots
  sonatype-releases: https://oss.sonatype.org/content/repositories/releases
  mavenLocal: file://$LOCAL_M2_REPO
  prRepo: $prRepoUrl
EOF
}

# :docstring cleanupsbt:
# Usage: cleanupsbt
# This reestablishes the previous .sbt/repositories.
# :end docstring:

function cleanupsbt(){
    say "### cleaning up $DEST_REPO_FILE"
    if [[ ! -z $OLD_SBT_REPO_FILE ]]; then
        mv $OLD_SBT_REPO_FILE $DEST_REPO_FILE
    else
        rm $DEST_REPO_FILE
    fi
}

function main() {
    SCALAHASH=$sha # passed in by jenkins
    SCALADIR="$BASEDIR/scala/"
    if [[ -z $SCALAHASH ]]; then
        echo "No Scala sha provided!"
        exit 1
    fi
}