-
Notifications
You must be signed in to change notification settings - Fork 12
/
install.sh
executable file
·188 lines (162 loc) · 6.74 KB
/
install.sh
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
#!/usr/bin/env bash
MAVEN_METADATA=$(curl -sL http://central.maven.org/maven2/org/arquillian/smart/testing/smart-testing-parent/maven-metadata.xml)
LATEST=$(echo ${MAVEN_METADATA} | grep '<latest>' | sed -e 's,.*<latest>\([^<]*\)</latest>.*,\1,g')
VERSION=${LATEST}
INSTALL_SPECIFIC_VERSION="0"
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Installs Arquillian Smart Testing Extension"
echo "options:"
echo "-l, --latest installs latest version of the extension (default)"
echo "-v, --version=VERSION installs defined version (doesn't check if exists!)"
exit 0
;;
-l|--latest)
shift
VERSION=${LATEST}
shift
;;
-v)
shift
if test $# -gt 0; then
VERSION=$1
INSTALL_SPECIFIC_VERSION="1"
else
echo "No version specified."
exit 1
fi
shift
;;
--version*)
VERSION=`echo $1 | sed -e 's/^[^=]*=//g'`
INSTALL_SPECIFIC_VERSION="1"
shift
;;
*)
echo "$1 is not a recognized flag!"
exit -1
;;
esac
done
function install_shaded_library() {
if [ -z "$M2_HOME" ]; then
echo "Please set M2_HOME pointing to your Maven installation."
exit 1
fi
SHADED_JAR="maven-lifecycle-extension-${VERSION}-shaded.jar"
echo "Installing ${SHADED_JAR} into ${M2_HOME}/lib/ext"
wget http://central.maven.org/maven2/org/arquillian/smart/testing/maven-lifecycle-extension/${VERSION}/${SHADED_JAR}
echo -n "We want to move shaded jar to M2_HOME with sudo. Can we? [y/N] "
read -r response < /dev/tty
case "$response" in
[yY][eE][sS]|[yY])
sudo mv $SHADED_JAR $M2_HOME/lib/ext
;;
*)
;;
esac
}
function install_extension() {
# Needs to be without new lines, otherwise my sed skills below will fail badly :\
EXTENSION="<extension><groupId>org.arquillian.smart.testing</groupId><artifactId>maven-lifecycle-extension</artifactId><version>${VERSION}</version></extension>"
[ -d .mvn ] || mkdir .mvn
if [ ! -f .mvn/extensions.xml ]; then
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<extensions>
${EXTENSION}
</extensions>" > .mvn/extensions.xml
else
EXTENSION_REGISTERED=$(cat .mvn/extensions.xml | grep 'org.arquillian.smart.testing' -A 2 | grep '<version>' | sed -e 's,.*<version>\([^<]*\)</version>.*,\1,g');
if [ ! ${EXTENSION_REGISTERED} ]; then
EXTENSION=$(echo ${EXTENSION} | sed -e "s#/#\\\/#g");
sed -i -E 's/(.*<extensions>)(.*)/\1\n'$EXTENSION'\2/g' .mvn/extensions.xml
echo "Installed Smart Testing Extension ${VERSION}"
else
echo -e "Smart Testing Extension already registered with version ${EXTENSION_REGISTERED}\c"
if [ ${INSTALL_SPECIFIC_VERSION} -eq 1 ]; then
if [ "${EXTENSION_REGISTERED}" != "${VERSION}" ]; then
echo -e " - overwriting with ${VERSION}."
override_version $VERSION
echo -e "Updated Smart Testing Extension to ${VERSION}\c"
fi
echo "."
elif [ $EXTENSION_REGISTERED != $LATEST ]; then
echo -n ". Do you want to override with latest ${LATEST}? [y/N] "
read -r response < /dev/tty
case "$response" in
[yY][eE][sS]|[yY])
override_version ${LATEST}
echo "Updated Smart Testing Extension to ${LATEST}"
;;
*)
;;
esac
else
echo " which is the latest stable version."
fi
fi
fi
mv .mvn/extensions.xml .mvn/extensions-unformatted.xml
xmllint --format .mvn/extensions-unformatted.xml > .mvn/extensions.xml
rm .mvn/extensions-unformatted.xml
}
function override_version() {
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<xsl:param name=\"version\"/>
<xsl:template match=\"node()|@*\">
<xsl:copy>
<xsl:apply-templates select=\"node()|@*\"/>
</xsl:copy>
</xsl:template>
<xsl:template match=\"/extensions/extension[groupId='org.arquillian.smart.testing' and artifactId='maven-lifecycle-extension']/version/text()\">
<xsl:value-of select=\"\$version\"/>
</xsl:template>
</xsl:stylesheet>
" >> .mvn/updateversion.xslt
xsltproc --stringparam version $1 .mvn/updateversion.xslt .mvn/extensions.xml > .mvn/extensions-new.xml
mv .mvn/extensions-new.xml .mvn/extensions.xml
rm .mvn/updateversion.xslt
}
function ignore_smart_testing_artifacts() {
cat .gitignore 2>&1 | grep -q '.smart-testing' && EXISTS=1 || EXISTS=0
if [ ${EXISTS} == 0 ]; then
echo -n "Do you want to add Smart Testing execution artifacts to .gitignore? [Y/n] "
read -r response < /dev/tty
case "$response" in
[nN][oO]|[nN])
;;
*)
if [ ! -f .gitignore ]; then
touch ./.gitignore
fi
echo -e "\n# Smart Testing Exclusions\n.smart-testing/\n" >> ./.gitignore
;;
esac
fi
}
function command_exists {
# Sample usage "if command_exists foo; then echo it exists; fi"
type "$1" &> /dev/null
}
## MAIN LOGIC
command_exists mvn 2>&1 || { echo >&2 "Cannot find Maven (mvn). Make sure you have it installed."; exit 1; }
command_exists xmllint >/dev/null 2>&1 || { echo >&2 "This script requires xmllint. Make sure you have it installed."; exit 1; }
command_exists xsltproc >/dev/null 2>&1 || { echo >&2 "This script requires xsltproc. Make sure you have it installed."; exit 1; }
if [[ ! -f pom.xml ]]; then
echo >&2 "Cannot find pom.xml file. Is it a Maven project? Make sure you are in the project's root directory."
exit 1
fi
MVN_VERSION=$(mvn --version | head -n1 | cut -d' ' -f3)
if [[ $MVN_VERSION =~ ^[3].[3-9].[0-9]$ ]]; then
echo "Installing extension in .mvn/extensions.xml"
install_extension
ignore_smart_testing_artifacts
elif [[ $MVN_VERSION =~ ^[3].[1-2].[0-9]$ ]]; then
echo "Installing extension in M2_HOME/lib/ext"
install_shaded_library
ignore_smart_testing_artifacts
else
echo "Version ${MVN_VERSION} is not supported.";
fi