-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotless.sh
executable file
·88 lines (71 loc) · 2.89 KB
/
spotless.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
#!/bin/bash
#
# Script that runs spotlessApply for a single file.
# In case no file path is provided, the formatting is applied to the whole project.
#
# Advantage over calling Gradle for a single file directly is identifying a subproject and only running the task there.
# Also, Gradle is only run for certain file extensions (in case this can't bbe filtered easily when calling the script)
#
# We can't avoid the Gradle configuration phase which takes relatively long due to the big number of subprojects,
# But when targeting individual projects we can speed up the process using the on-demand configuration feature
# (see https://docs.gradle.org/current/userguide/multi_project_configuration_and_execution.html).
#
# Configuration as file watcher in IntelliJ:
#
# 1. Install File Watchers plugin
# 2. Settings -> Tools -> File Watchers -> Add
#
# Program: $ProjectFileDir$/spotless.sh
# Arguments: $FilePath$
# Output paths to refresh: $FilePath$
# Working directory: $ProjectFileDir$
#
# Disable auto save on editing running the tool while editing
#
set -e
# Define the list of allowed file extensions
allowed_extensions=("java" "md" "groovy" "gradle")
# Check if a file path is provided
if [ -z "$1" ]; then
echo "No file path provided"
exec ./gradlew spotlessApply --parallel
exit 0
fi
# Get the file extension
file_extension="${1##*.}"
# Check if the file extension is in the allowed list
if [[ ! "${allowed_extensions[@]}" =~ "${file_extension}" ]]; then
echo "Error: The file extension .$file_extension is not allowed."
exit 0
fi
# Get the absolute path of the provided file
file_path=$(realpath "$1")
# Get the directory containing the file
current_dir=$(dirname "$file_path")
# Get the directory where the script resides
script_dir=$(dirname "$(realpath "$0")")
# Traverse up the directory tree to find the first parent directory with build.gradle
while [ "$current_dir" != "/" ]; do
if [ -f "$current_dir/build.gradle" ]; then
# Determine the relative path from the script's directory
relative_path=$(realpath --relative-to="$script_dir" "$current_dir")
# Check if the folder is the same as the script directory
if [ "$current_dir" == "$script_dir" ]; then
echo "Found build.gradle in the project root: $relative_path"
exec ./gradlew :spotlessApply "-PspotlessIdeHook=$file_path" --parallel --configure-on-demand
else
# Replace / with : in the relative path
formatted_path=$(echo "$relative_path" | tr '/' ':')
echo "Found build.gradle in a subfolder: $formatted_path"
exec ./gradlew ":$formatted_path:spotlessApply" "-PspotlessIdeHook=$file_path" --parallel --configure-on-demand
fi
exit 0
fi
# Stop if we reach the script's directory
if [ "$current_dir" == "$script_dir" ]; then
break
fi
current_dir=$(dirname "$current_dir")
done
echo "No build.gradle found within the script's directory."
exit 1