-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect-trace.sh
executable file
·64 lines (46 loc) · 1.99 KB
/
collect-trace.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
#!/usr/bin/env bash
set -e
# Usage: ./collect-trace.sh <project-dir> <task-name> <trace-file-prefix>
PROJECT="${1?Project directory not provided}"
TASK="${2?Task not provided}"
TRACE_PREFIX="${3:-trace}"
GRADLE_CMD="${GRADLE_CMD:-./gradlew}"
# Function to print a command before running
# shellcheck disable=SC2145
exe() { echo ""; echo "\$ $@" ; "$@" ; }
cd "$PROJECT"
exe pwd
STORAGE_DIR="$PWD/_trace"
EMPTY_GRADLE_HOME="${EMPTY_GRADLE_HOME:-fresh-gradle-home}"
GRADLE_CMD="$GRADLE_CMD -g $EMPTY_GRADLE_HOME"
# Ignore dependency verification
GRADLE_CMD="$GRADLE_CMD --dependency-verification lenient"
# Make sure no local artifact transform results are present
# shellcheck disable=SC2086
exe $GRADLE_CMD --console=plain clean
# Kill all Gradle daemons to make sure nothing is cached in memory
exe pkill -f "GradleDaemon"
sleep 3
# Killing only daemons of the same version may not be enough, if other daemons hold locks on the fresh Gradle home somehow
#WRAPPER_VERSION="$($GRADLE_CMD --version | grep 'Gradle ' | awk '{print $2}')"
#exe pkill -f "GradleDaemon $WRAPPER_VERSION" || true
# Clean the temporary Gradle home to make sure artifact transform results are not cached on disk
exe rm -rf "./$EMPTY_GRADLE_HOME/daemon"
# Remove everything from caches except `modules-2` which contains downloaded dependencies
exe find "./$EMPTY_GRADLE_HOME/caches" -mindepth 1 -maxdepth 1 ! -name 'modules-2' -print -exec rm -rf {} + || true
# Clean the storage dir
exe rm -rf "$STORAGE_DIR"
# Run task and collect build operations and build scan dump
# shellcheck disable=SC2086
exe $GRADLE_CMD --console=plain --no-build-cache "$TASK" \
-Dorg.gradle.internal.operations.trace="$STORAGE_DIR/$TRACE_PREFIX" \
--scan -Dscan.dump -S
# Find and move scan dump
# https://unix.stackexchange.com/a/305846
find . -name "*.scan" -maxdepth 1 -exec mv {} "$STORAGE_DIR" \;
# Change build scan name to a simple name
mv "$STORAGE_DIR"/*.scan "$STORAGE_DIR"/trace.scan
echo
echo "Collected trace files:"
find "$STORAGE_DIR"/*
cd - >/dev/null