-
Notifications
You must be signed in to change notification settings - Fork 4
/
local-dev-cleanup-code.sh
164 lines (138 loc) · 3.26 KB
/
local-dev-cleanup-code.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
#!/bin/bash
# Exit codes
SUCCESS=0
INVALID_ARGUMENT_ERROR=1
YOU_NEED_NO_CHANGES_BEFORE_RUN_CLEANUP_ERROR=3
# Default arguments' values
AUTO_COMMIT=yes
echo ""
echo "--- --- ---"
echo "Alright Cleanup Code Command-Line Tool"
echo "Default settings:"
echo "- auto commit re-formatted code (-a): '$AUTO_COMMIT'"
echo "--- --- ---"
echo ""
while getopts a: flag
do
case "${flag}" in
a) AUTO_COMMIT=${OPTARG};;
*) echo ""
echo "--- --- ---"
echo "Invalid argument's flag is not handled"
echo "--- --- ---"
echo ""
exit $INVALID_ARGUMENT_ERROR ;;
esac
done
if [ "$AUTO_COMMIT" != "yes" ] && [ "$AUTO_COMMIT" != "no" ]
then
echo ""
echo "--- --- ---"
echo "INVALID ARGUMENT OF '-a' equals '$AUTO_COMMIT'"
echo "Set 'yes' or 'no' or omit to use default equals 'no'"
echo "--- --- ---"
echo ""
exit $INVALID_ARGUMENT_ERROR
fi
UNSTAGED_CHANGES=$(git diff --name-only)
if [ -z "$UNSTAGED_CHANGES" ]
then
echo ""
echo "--- --- ---"
echo "Right, there are no unstaged changes"
echo "--- --- ---"
echo ""
else
echo ""
echo "--- --- ---"
echo "There are unstaged changes"
echo "Commit them before run the script"
echo "--- --- ---"
echo ""
git diff --name-only
exit $YOU_NEED_NO_CHANGES_BEFORE_RUN_CLEANUP_ERROR
fi
STAGED_UNCOMMITTED=$(git diff --staged --name-only)
if [ -z "$STAGED_UNCOMMITTED" ]
then
echo ""
echo "--- --- ---"
echo "Right, there is no any changes, repo is ready to cleanup"
echo "--- --- ---"
echo ""
else
echo ""
echo "--- --- ---"
echo "There are staged, uncommitted changes"
echo "Commit them before run the script"
echo "--- --- ---"
echo ""
git diff --staged --name-only
exit $YOU_NEED_NO_CHANGES_BEFORE_RUN_CLEANUP_ERROR
fi
echo ""
echo "--- --- ---"
echo "Restore dotnet tools (the JetBrains CleanupCode Tool)"
echo "--- --- ---"
echo ""
dotnet tool restore
dotnet jb cleanupcode --version
echo ""
echo "--- --- ---"
echo "Let's get started, keep calm and wait, it may take few moments"
echo "--- --- ---"
echo ""
dotnet jb cleanupcode ReSharperCleanupCodeDemo.sln --verbosity=WARN
REFORMATTED_FILES=$(git diff --name-only)
if [ -z "$REFORMATTED_FILES" ]
then
echo ""
echo "--- --- ---"
echo "No files re-formatted, everything is clean, congratulation!"
echo "--- --- ---"
echo ""
exit $SUCCESS
fi
if [ "$AUTO_COMMIT" = "no" ]
then
echo ""
echo "--- --- ---"
echo "There is re-formatted code but it will not be auto committed"
echo "--- --- ---"
echo ""
exit $SUCCESS
fi
echo ""
echo "--- --- ---"
echo "There are re-formatted files to be committed"
echo "--- --- ---"
echo ""
git diff --name-only
for FILE in "${REFORMATTED_FILES[@]}"
do
git add ${FILE}
done
echo ""
echo "--- --- ---"
echo "Staged files to be committed"
echo "--- --- ---"
echo ""
git diff --staged --name-only
echo ""
echo "--- --- ---"
echo "Create commit"
echo "--- --- ---"
echo ""
git commit -m "Cleanup: re-format code by JetBrains CleanupCode Tool"
echo ""
echo "--- --- ---"
echo "Commit created"
echo "--- --- ---"
echo ""
git status
echo ""
echo "--- --- ---"
echo "All re-formatted code has been committed with success"
echo "--- --- ---"
echo ""
exit $SUCCESS