summaryrefslogtreecommitdiff
path: root/scripts/pr-scala-common
blob: 01112588f7ca5931a146c6c63ad6d421e3627fec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# 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
}