Skip to content

Commit 93a4b6b

Browse files
committed
Added with-chromedriver.sh tool
1 parent e5c87ad commit 93a4b6b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tool/with-chromedriver.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
3+
ROOT="$DIR/.."
4+
5+
TARGET="$ROOT/.dart_tool/chromedriver"
6+
7+
# Detect chrome version
8+
CHROME_VERSION=$(google-chrome --version | grep -Po '(\d+.\d+.\d+)')
9+
echo "CHROME_VERSION: $CHROME_VERSION"
10+
11+
# Create target file
12+
mkdir -p "$TARGET"
13+
touch "$TARGET/CHROME_VERSION";
14+
15+
# Check that chrome haven't been updated
16+
if [[ $(cat "$TARGET/CHROME_VERSION") != "$CHROME_VERSION" ]]; then
17+
# Clear cache
18+
rm -rf "$TARGET"
19+
mkdir -p "$TARGET"
20+
21+
# Find matching chromedriver version
22+
BASE_URL='https://chromedriver.storage.googleapis.com'
23+
CHROMEDRIVER_VERSION=$(curl -L -s "$BASE_URL/LATEST_RELEASE_$CHROME_VERSION")
24+
echo "CHROMEDRIVER_VERSION: $CHROMEDRIVER_VERSION"
25+
26+
# Detect platform
27+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
28+
PLATFORM='linux64'
29+
elif [[ "$OSTYPE" == "darwin"* ]]; then
30+
PLATFORM='mac64'
31+
else
32+
echo "Unsupported OSTYPE: $OSTYPE"
33+
exit 1
34+
fi
35+
36+
# Download chromedriver
37+
curl -L "$BASE_URL/$CHROMEDRIVER_VERSION/chromedriver_$PLATFORM.zip" \
38+
-o "$TARGET/chromedriver.zip"
39+
40+
# Extract to target
41+
unzip "$TARGET/chromedriver.zip" -d "$TARGET"
42+
43+
# Store CHROME_VERSION so we can easily check if it's outdated
44+
echo "$CHROME_VERSION" > "$TARGET/CHROME_VERSION"
45+
fi
46+
47+
# Start chromedriver and kill it when we exit
48+
"$TARGET/chromedriver" --port=4444 &
49+
CHROMEDRIVER_PID="$!"
50+
trap "kill $CHROMEDRIVER_PID; wait $CHROMEDRIVER_PID 2>/dev/null;" EXIT
51+
52+
# Give chromedriver time to start
53+
sleep 1
54+
55+
# Run whatever command we were told to run
56+
$@

0 commit comments

Comments
 (0)