-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathwith-chromedriver.sh
executable file
·68 lines (56 loc) · 1.91 KB
/
with-chromedriver.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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$DIR/.."
CHROMEDRIVER='chromedriver'
if which "$CHROMEDRIVER" > /dev/null; then
echo 'Found chromedriver on PATH'
else
TARGET="$ROOT/.dart_tool/chromedriver"
# Detect chrome version
CHROME_VERSION=$(google-chrome --version | grep -Po '(\d+.\d+.\d+)')
echo "CHROME_VERSION: $CHROME_VERSION"
# Create target file
mkdir -p "$TARGET"
touch "$TARGET/CHROME_VERSION";
# Check that chrome haven't been updated
if [[ $(cat "$TARGET/CHROME_VERSION") != "$CHROME_VERSION" ]]; then
# Clear cache
rm -rf "$TARGET"
mkdir -p "$TARGET"
# Find matching chromedriver version
BASE_URL='https://chromedriver.storage.googleapis.com'
CHROMEDRIVER_VERSION=$(curl -L -s "$BASE_URL/LATEST_RELEASE_$CHROME_VERSION")
echo "CHROMEDRIVER_VERSION: $CHROMEDRIVER_VERSION"
# Detect platform
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
PLATFORM='linux64'
elif [[ "$OSTYPE" == "darwin"* ]]; then
PLATFORM='mac64'
elif [[ "$OSTYPE" == "cygwin" ]]; then
PLATFORM='win32'
elif [[ "$OSTYPE" == "msys" ]]; then
PLATFORM='win32'
elif [[ "$OSTYPE" == "win32" ]]; then
PLATFORM='win32'
else
echo "Unsupported OSTYPE: $OSTYPE"
exit 1
fi
# Download chromedriver
curl -L "$BASE_URL/$CHROMEDRIVER_VERSION/chromedriver_$PLATFORM.zip" \
-o "$TARGET/chromedriver.zip"
# Extract to target
unzip "$TARGET/chromedriver.zip" -d "$TARGET"
# Store CHROME_VERSION so we can easily check if it's outdated
echo "$CHROME_VERSION" > "$TARGET/CHROME_VERSION"
fi
CHROMEDRIVER="$TARGET/chromedriver"
fi
# Start chromedriver and kill it when we exit
"$CHROMEDRIVER" --port=4444 &
CHROMEDRIVER_PID="$!"
trap "kill $CHROMEDRIVER_PID; wait $CHROMEDRIVER_PID 2>/dev/null;" EXIT
# Give chromedriver time to start
sleep 2
# Run whatever command we were told to run
$@