forked from ArturWincenciak/ReSharper_CleanupCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·248 lines (208 loc) · 5.88 KB
/
entrypoint.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Exit codes
SUCCESS=0
INVALID_ARGUMENT_ERROR=1
EXIT_WITH_FAST_FAIL=2
INPUT_SOLUTION=$1
INPUT_FAIL_ON_REFORMAT_NEEDED=$2
INPUT_AUTO_COMMIT=$3
INPUT_JB_CLEANUP_CODE_ARG=$4
INPUT_COMMIT_MESSAGE=$5
INPUT_COMMIT_CREATOR_EMAIL=$6
INPUT_COMMIT_CREATOR_NAME=$7
INPUT_ONLY_REFORMAT_CHANGED_FILES=$8
echo ""
echo "--- --- ---"
echo "Alright GitHub Action Cleanup Code Command-Line Tool"
echo "Your setup:"
echo "- Solution: [${INPUT_SOLUTION}]"
echo "- Fail on re-format needed: [${INPUT_FAIL_ON_REFORMAT_NEEDED}]"
echo "- Auto commit re-formatted code: [${INPUT_AUTO_COMMIT}]"
echo "- ReSharper CLI CleanupCode arguments: [${INPUT_JB_CLEANUP_CODE_ARG}]"
echo "- Commit message: [${INPUT_COMMIT_MESSAGE}]"
echo "- Commit creator (git user) e-mail: [${INPUT_COMMIT_CREATOR_EMAIL}]"
echo "- Commit creator (git user) name: [${INPUT_COMMIT_CREATOR_NAME}]"
echo "- Only reformat changed files: [${INPUT_ONLY_REFORMAT_CHANGED_FILES}]"
if [ "${INPUT_FAIL_ON_REFORMAT_NEEDED}" = "yes" ] && [ "${INPUT_AUTO_COMMIT}" = "yes" ]; then
echo "NOTICE: you have set that the execution will fast fail on re-format needed"
echo "NOTICE: auto commit will not be executed because the execution will terminate with fail when re-format is needed"
echo "NOTICE: if you want to auto commit execute call the script with '-f no -a yes' arguments"
fi
echo "--- --- ---"
echo ""
echo ""
echo "--- --- ---"
echo "Current working directory stuff:"
ls -F --color=auto --show-control-chars -a
echo "--- --- ---"
echo ""
if [ "${INPUT_FAIL_ON_REFORMAT_NEEDED}" != "yes" ] && [ "${INPUT_FAIL_ON_REFORMAT_NEEDED}" != "no" ]; then
echo ""
echo "--- --- ---"
echo "INVALID ARGUMENT OF '-f' equals '${INPUT_FAIL_ON_REFORMAT_NEEDED}'"
echo "Set 'yes' or 'no' or omit to use default equals 'no'"
echo "--- --- ---"
echo ""
exit ${INVALID_ARGUMENT_ERROR}
fi
if [ "${INPUT_AUTO_COMMIT}" != "yes" ] && [ "${INPUT_AUTO_COMMIT}" != "no" ]; then
echo ""
echo "--- --- ---"
echo "INVALID ARGUMENT OF '-a' equals '${INPUT_AUTO_COMMIT}'"
echo "Set 'yes' or 'no' or omit to use default equals 'no'"
echo "--- --- ---"
echo ""
exit ${INVALID_ARGUMENT_ERROR}
fi
if [ "${INPUT_ONLY_REFORMAT_CHANGED_FILES}" != "yes" ] && [ "${INPUT_ONLY_REFORMAT_CHANGED_FILES}" != "no" ]; then
echo ""
echo "--- --- ---"
echo "INVALID ARGUMENT OF '-a' equals '${INPUT_ONLY_REFORMAT_CHANGED_FILES}'"
echo "Set 'yes' or 'no' or omit to use default equals 'no'"
echo "--- --- ---"
echo ""
exit ${INVALID_ARGUMENT_ERROR}
fi
if [ "${INPUT_ONLY_REFORMAT_CHANGED_FILES}" == "yes" ]; then
GIT_HASH=`git rev-parse HEAD`
echo ""
echo "--- --- ---"
echo "Checking difference in files between current commit and main branch"
echo "Current commit hash: [${GIT_HASH}]"
echo "--- --- ---"
echo ""
git fetch origin main:refs/remotes/origin/main
FILE_INPUT=$(git diff --name-only origin/main ${GIT_HASH})
else
echo ""
echo "--- --- ---"
echo "Checking difference in the whole solution"
echo "--- --- ---"
echo ""
FILE_INPUT=${INPUT_SOLUTION}
fi
echo ""
echo "--- --- ---"
echo "file(s) to be re-formatted: [${FILE_INPUT}]"
echo "--- --- ---"
echo ""
#
# Parse arguments and put them into an array to call command
# I'm at a loss for words to describe how I managed to nail this function
# Once again I understand why all Ops always looks stressed out
#
ARG_DELIMITER="--"
S=${INPUT_JB_CLEANUP_CODE_ARG}${ARG_DELIMITER}
COMMAND_ARG_ARRAY=()
while [[ $S ]]; do
ITEM="${S%%"$ARG_DELIMITER"*}"
if [ -n "${ITEM}" ]; then
if [ "${ITEM:0-1}" == " " ]; then
ITEM="${ITEM::-1}"
fi
COMMAND_ARG_ARRAY+=("--${ITEM}")
fi
S=${S#*"$ARG_DELIMITER"}
done
echo ""
echo "--- --- ---"
echo "Let's get started, keep calm and wait, it may take few moments"
for arg in "${COMMAND_ARG_ARRAY[@]}"; do
echo "Command argument: [${arg}]"
done
echo "--- --- ---"
echo ""
dotnet tool restore
dotnet tool update --global JetBrains.ReSharper.GlobalTools
jb cleanupcode "${COMMAND_ARG_ARRAY[@]}" $FILE_INPUT
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 [ "${INPUT_FAIL_ON_REFORMAT_NEEDED}" = "yes" ]; then
echo ""
echo "--- --- ---"
echo "Exit with re-formatted code needed fail status"
echo "The following files need to be re-formatted:"
echo "${REFORMATTED_FILES}"
echo "--- --- ---"
echo ""
GIT_CHANGES=$(git diff)
echo ""
echo "--- --- ---"
echo "Detailed View"
echo "The following changes need to happen:"
echo "${GIT_CHANGES}"
echo "--- --- ---"
echo ""
exit ${EXIT_WITH_FAST_FAIL}
fi
if [ "${INPUT_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 is re-formatted code to be committed"
echo "--- --- ---"
echo ""
git diff --name-only
echo ""
echo "--- --- ---"
echo "Git Diff"
echo "--- --- ---"
echo ""
git diff
echo ""
echo "--- --- ---"
echo "Add all changes to stage"
echo "--- --- ---"
echo ""
git add .
echo ""
echo "--- --- ---"
echo "Staged files to be committed"
echo "--- --- ---"
echo ""
git diff --staged --name-only
echo ""
echo "--- --- ---"
echo "Creating commit"
echo "--- --- ---"
echo ""
git config --global user.email "${INPUT_COMMIT_CREATOR_EMAIL}"
git config --global user.name "${INPUT_COMMIT_CREATOR_NAME}"
git commit -m "${INPUT_COMMIT_MESSAGE}"
echo ""
echo "--- --- ---"
echo "Commit has been created"
echo "--- --- ---"
echo ""
git status
echo ""
echo "--- --- ---"
echo "Push the commit"
echo "--- --- ---"
echo ""
git push
echo ""
echo "--- --- ---"
echo "Commit has been pushed"
echo "--- --- ---"
echo ""
git status
echo ""
echo "--- --- ---"
echo "All re-formatted code has been committed and pushed with success"
echo "--- --- ---"
echo ""
exit ${SUCCESS}