-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivg
executable file
·504 lines (439 loc) · 20 KB
/
ivg
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/bin/bash
set -e
IVG_VERSION="0.0.1-SNAPSHOT"
IVG_PATH="$0"
IVG_DIR="$(dirname "$IVG_PATH")"
source "$IVG_DIR/ivg-config"
#Current config
gitCommitMessage=$GIT_COMMIT_MESSAGE
fixup=$FIXUP
autosquash=$AUTOSQUASH
push=$PUSH
pushOrigin=$PUSH_ORIGIN
pushBranch=$PUSH_BRANCH
pushOptions=$PUSH_OPTIONS
versionSuffix=$VERSION_SUFFIX
onlyChangeSuffix=$ONLY_CHANGE_SUFFIX
strict=$STRICT
propertiesFile=$PROPERTIES_FILE
useErrorExitCode=$USE_ERROR_EXIT_CODE
quiet=$QUIET
verbose=$VERBOSE
SHOW_MESSAGE_HEADER=true
#Goals
GOAL_VERSION_VALIDATION="VERSION VALIDATION"
GOAL_VERSION_GENERATION="VERSION GENERATION"
GOAL_GET_VERSION="GET VERSION"
GOAL_GET_VERSION_REFERENCE="GET VERSION REFERENCE"
showMessage() {
if $SHOW_MESSAGE_HEADER ; then
SHOW_MESSAGE_HEADER=false
showMessage "info" "\e[1m\e[96mInfinity Version Generator (InfinityRefactoring/infinity-version-generator)\e[39m\e[0m" true true
fi
messageType=$1
message=$2
if [ $messageType = "info" ]; then
messageType="[\e[94m\e[1mINFO\e[0m\e[39m]"
elif [ $messageType = "warning" ]; then
messageType="[\e[93m\e[1mWARNING\e[0m\e[39m]"
elif [ $messageType = "success" ]; then
messageType="[\e[94m\e[1mINFO\e[0m\e[39m]"
message="\e[92m\e[1m$message\e[0m\e[39m"
elif [ $messageType = "error" ]; then
messageType="[\e[91m\e[1mERROR\e[0m\e[39m]"
elif [ $messageType = "fail" ]; then
messageType="[\e[94m\e[1mINFO\e[0m\e[39m]"
message="\e[91m\e[1m$message\e[0m\e[39m"
fi
if [ "$3" = true ] && ! $quiet ; then
echo $(showMessage "info" "\e[1m------------------------------------------------------------------------\e[0m")
fi
if [ "$1" = "error" ] || [ "$1" = "fail" ]; then
echo -e "$messageType $message" 1>&2
elif ! $quiet ; then
echo -e "$messageType $message"
fi
if [ "$4" = true ] && ! $quiet ; then
echo $(showMessage "info" "\e[1m------------------------------------------------------------------------\e[0m")
fi
}
getBooleanValue() {
if [ "$2" = "$1" ]; then
if [ "$4" == "" ]; then
echo $3
else
echo $4
fi
elif [[ "$2" =~ ^$1=.* ]]; then
value=$(echo $2 | cut -d '=' -f 2)
if [ "$value" = "true" ]; then
echo true
elif [ "$value" = "false" ]; then
echo false
else
showMessage "error" "invalid argument, use $1, $1=true or $1=false"
exit 1
fi
fi
echo $3
}
getStringValue() {
if [[ "$2" = $1 ]] || [[ "$2" =~ ^$1=.* ]]; then
if [[ $2 =~ ^$1=.* ]]; then
value=$(echo $2 | cut -d '=' -f 2)
if [ "$value" != "" ]; then
echo "$value"
exit 0
fi
fi
showMessage "error" "invalid argument, use $4"
exit 1
fi
echo $3
}
getProperty() {
echo $(grep "^$1=" $2)
}
getPropertyValue() {
echo $(grep "^$1=" $2 | cut -d'=' -f 2)
}
getProjectVersion() {
showMessage "error" "No defined function to get the project version."
exit 1
}
setProjectVersion() {
showMessage "warning" "No defined function to set the project version."
}
getVersionReference() {
echo $(git rev-list --since="$1" HEAD | tail -n 1)
}
formatVersion() {
echo "$1$2"
}
loadGitInfo() {
gitCommitTime=$(git show --quiet --format="%ci")
}
createPropertiesFile() {
mkdir -p $(dirname "$propertiesFile")
echo "#Auto generated by Infinity Version Generator (InfinityRefactoring/infinity-version-generator), do not edit this file" > "$propertiesFile"
echo "" >> "$propertiesFile"
echo "$PROPERTY_PROJECT_VERSION=$v" >> "$propertiesFile"
echo "$PROPERTY_PROJECT_VERSION_TIME=$1" >> "$propertiesFile"
echo "$PROPERTY_PROJECT_VERSION_SUFFIX=$2" >> "$propertiesFile"
echo "$PROPERTY_GIT_COMMIT_TIME=$gitCommitTime" >> "$propertiesFile"
}
hasUncommittedChanges() {
if [ "$(git status --short)" = "" ]; then
echo false
else
echo true
fi
}
commitChanges() {
if [ "$(hasUncommittedChanges)" = "true" ]; then
git add .
if $fixup ; then
git commit --fixup HEAD
else
git commit -m "${gitCommitMessage/$VERSION_PATTERN/$v}"
fi
fi
}
saveProjectVersion() {
v=$(formatVersion $1 $2)
createPropertiesFile $1 $2
setProjectVersion "$verbose" "$v"
if $verbose ; then
commitChanges
else
{
commitChanges
} 1>&-
fi
}
importExtension() {
if [ "$1" != "" ] && [ "$loadedExtension" != "true" ]; then
ivgExtFile="$1"
required="$2"
if [ "$IVG_PATH" = "$ivgExtFile" ]; then
showMessage "error" "The extension script path is equal to the ivg path ($IVG_PATH)"
exit 1
elif [ -f "$ivgExtFile" ]; then
if $verbose ; then
showMessage "info" "Importing script from: $ivgExtFile"
fi
source "$ivgExtFile"
loadedExtension=true
fi
fi
}
importExtension "$IVG_EXT_DIR/$IVG_EXT_FILE_DEFAULT"
importExtension "$(pwd)/$IVG_EXT_FILE"
importExtension "$IVG_EXT_DIR/$IVG_EXT_FILE"
for var in "$@"; do
if [ "$var" = "--version" ]; then
showMessage "info" "Version: $IVG_VERSION"
exit 0
elif [ "$var" = "-h" ] || [ "$var" = "--help" ]; then
quiet=false
showMessage "info" "A flexible version generator for projects that uses the Git as control version."
showMessage "info" "The generated version is based in the git commit time and an optional suffix."
showMessage "info"
showMessage "info" "\e[1mUsage\e[0m:"
showMessage "info" " ivg [<goal>] [<options>]"
showMessage "info" " ivg [<options>] [<goal>]"
showMessage "info"
showMessage "info" "\e[1mExamples\e[0m:"
showMessage "info" " ivg generate"
showMessage "info" " ivg validate"
showMessage "info" " ivg get-version"
showMessage "info" " ivg get-reference"
showMessage "info" " ivg generate --verbose"
showMessage "info" " ivg validate --suffix=-SNAPSHOT --verbose"
showMessage "info"
showMessage "info" "\e[1mGoals\e[0m:"
showMessage "info" "validate Validates the current project version with the version defined in the version properties file. Returns 0 if the project is valid otherwise 1."
showMessage "info" "generate Analyses the project and if necessary generates a new version number."
showMessage "info" "get-version Returns the current project version."
showMessage "info" "get-reference Returns the git commit hash that is reference to the current version."
showMessage "info"
showMessage "info" "\e[1mOptions\e[0m:"
showMessage "info" "Long options has priority over short options."
showMessage "info" "--import The path of the extension script that will be imported."
showMessage "info" " This is useful to import a custom implementation of the \"getProjectVersion()\" and \"setProjectVersion()\" functions"
showMessage "info" " Default values:"
if [ "$IVG_EXT_FILE_DEFAULT" != "$IVG_EXT_FILE" ]; then
showMessage "info" " $(pwd)/$IVG_EXT_FILE"
fi
showMessage "info" " $(pwd)/$IVG_EXT_FILE_DEFAULT"
showMessage "info" " $IVG_EXT_DIR/$IVG_EXT_FILE_DEFAULT"
showMessage "info" " Example: --import=$IVG_EXT_FILE"
showMessage "info" " Available extension scripts:"
for ext in $IVG_EXT_DIR/*
do
if [ -f $ext ]; then
showMessage "info" " --import=$(basename "$ext")"
fi
done
showMessage "info" "--commit-message The git commit message. The pattern \"$VERSION_PATTERN\" will be override by the version number"
showMessage "info" " Default value: $GIT_COMMIT_MESSAGE"
showMessage "info" " Example: --commit-message=$GIT_COMMIT_MESSAGE"
showMessage "info" "--fixup Enable the use of the \"git commit --fixup\" command to commit the changes"
showMessage "info" " Default value: $FIXUP"
showMessage "info" " Example: --fixup, --fixup=false, --fixup=true"
showMessage "info" "--autosquash Enable the use of the \"git rebase -i --autosquash\" command to join the new commit with the previous commit. This only is enabled if the \"--fixup\" too is enabled"
showMessage "info" " Default value: $FIXUP"
showMessage "info" " Example: --autosquash, --autosquash=false, --autosquash=true"
showMessage "info" "--push Execute the \"git push\" command if a new version is generated"
showMessage "info" " Default value: $PUSH"
showMessage "info" " Example: --push, --push=false, --push=true"
showMessage "info" "--push-origin The origin that will be used to push the changes"
showMessage "info" " Default value: $PUSH_ORIGIN"
showMessage "info" " Example: --push-origin=$PUSH_ORIGIN"
showMessage "info" "--push-branch The origin branch that will be used to push the changes"
showMessage "info" " Default value: $PUSH_BRANCH"
showMessage "info" " Example: --push-branch=master"
showMessage "info" "--push-options The options that will be used to push the changes"
showMessage "info" " Default value: $PUSH_OPTIONS"
showMessage "info" " Example: --push-options=-u"
showMessage "info" "--suffix The version number suffix"
showMessage "info" " Default value: $VERSION_SUFFIX"
showMessage "info" " Example: --suffix=some-suffix"
showMessage "info" "--only-change-suffix Enables only the change in the version suffix"
showMessage "info" " Default value: $ONLY_CHANGE_SUFFIX"
showMessage "info" " Example: --only-change-suffix, --only-change-suffix=false, --only-change-suffix=true"
showMessage "info" "--strict Enable the strict mode"
showMessage "info" " Default value: $STRICT"
showMessage "info" " Example: --strict, --strict=false, --strict=true"
showMessage "info" "--properties-file The version properties file path"
showMessage "info" " Default value: $PROPERTIES_FILE"
showMessage "info" " Example: --properties-file=$PROPERTIES_FILE"
showMessage "info" "--use-error-exit-code (Only works in the generate goal) Returns the exit code 1 if the generation has been success"
showMessage "info" " Default value: $useErrorExitCode"
showMessage "info" " Example: --use-error-exit-code, --use-error-exit-code=false, --use-error-exit-code=true"
showMessage "info" "--quiet Disable the messages"
showMessage "info" " Default value: $QUIET"
showMessage "info" " Example: --quiet, --quiet=false, --quiet=true"
showMessage "info" "--verbose Enable the verbose mode"
showMessage "info" " Examples: --verbose, --verbose=false, --verbose=true"
showMessage "info" "--version Display the version information and exit"
showMessage "info" "-h, --help Display this help and exit"
showMessage "info"
showMessage "info" "\e[1mhttps://github.com/InfinityRefactoring/infinity-version-generator\e[0m"
exit 0
elif [ "$var" = "generate" ]; then
goal=$GOAL_VERSION_GENERATION
elif [ "$var" = "validate" ]; then
goal=$GOAL_VERSION_VALIDATION
elif [ "$var" = "get-version" ]; then
goal=$GOAL_GET_VERSION
elif [ "$var" = "get-reference" ]; then
goal=$GOAL_GET_VERSION_REFERENCE
else
quiet=$(getBooleanValue "--quiet" "$var" $quiet true)
ivgExtFile2=$(getStringValue "--import" "$var" "$ivgExtFile2" "--import=$IVG_EXT_FILE")
gitCommitMessage=$(getStringValue "--commit-message" "$var" "$gitCommitMessage" "--commit-message=$GIT_COMMIT_MESSAGE")
fixup=$(getBooleanValue "--fixup" "$var" $fixup true)
autosquash=$(getBooleanValue "--autosquash" "$var" $autosquash true)
push=$(getBooleanValue "--push" "$var" $push true)
pushOrigin=$(getStringValue "--push-origin" "$var" "$pushOrigin" "--push-origin=$PUSH_ORIGIN")
pushBranch=$(getStringValue "--push-branch" "$var" "$pushBranch" "--push-branch=master")
pushOptions=$(getStringValue "--push-options" "$var" "$pushOptions" "--push-options=$PUSH_OPTIONS")
versionSuffix=$(getStringValue "--suffix" "$var" "$versionSuffix" "--suffix=-some-suffix")
onlyChangeSuffix=$(getBooleanValue "--only-change-suffix" "$var" $onlyChangeSuffix true)
strict=$(getBooleanValue "--strict" "$var" $strict true)
propertiesFile=$(getStringValue "--properties-file" $var "$propertiesFile" "--properties-file=$PROPERTIES_FILE")
useErrorExitCode=$(getBooleanValue "--use-error-exit-code" "$var" $useErrorExitCode true)
verbose=$(getBooleanValue "--verbose" "$var" $verbose true)
fi
done
if [ "$ivgExtFile2" != "" ]; then
loadedExtension=false
if [[ "$ivgExtFile2" =~ ^\/.* ]]; then
importExtension "$ivgExtFile2"
else
importExtension "$(pwd)/$ivgExtFile2"
importExtension "$IVG_EXT_DIR/$ivgExtFile2"
fi
fi
if [ "$loadedExtension" != "true" ]; then
showMessage "error" "No extension script was loaded."
exit 1
fi
if [ "$goal" = "" ] ; then
showMessage "error" "No goal enabled."
exit 1
fi
if [ "$goal" = "$GOAL_GET_VERSION" ] ; then
getProjectVersion
exit 0
fi
if [ "$goal" = "$GOAL_VERSION_VALIDATION" ] && $onlyChangeSuffix ; then
showMessage "error" "The argument --only-change-suffix only is available in the generation goal."
exit 1
fi
showMessage "info" "Goal: \e[1m$goal\e[0m"
showMessage "info" "Project directory: $(pwd)"
if [ ! -d ".git" ]; then
showMessage "error" "The current directory is not a git repository."
exit 1
fi
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [ "$currentBranch" = "HEAD" ]; then
currentBranch=$(git rev-parse HEAD)
fi
showMessage "info" "Current git branch: \e[1m$currentBranch\e[0m"
if [ "$(hasUncommittedChanges)" = "true" ]; then
showMessage "error" "The project has uncommitted changes, commit before run it."
if $verbose ; then
git status
fi
exit 1
fi
versionTime=$(TZ=UTC git show --quiet --date="$VERSION_TIME_FORMAT" --format="%cd")
version=$(formatVersion $versionTime $versionSuffix)
if [ -f "$propertiesFile" ]; then
savedVersion=$(getPropertyValue "$PROPERTY_PROJECT_VERSION" "$propertiesFile")
savedVersionTime=$(getPropertyValue "$PROPERTY_PROJECT_VERSION_TIME" "$propertiesFile")
savedVersionSuffix=$(getPropertyValue "$PROPERTY_PROJECT_VERSION_SUFFIX" "$propertiesFile")
savedGitCommitTime=$(getPropertyValue "$PROPERTY_GIT_COMMIT_TIME" "$propertiesFile")
if [ "$savedVersion" != "" ] && [ "$savedVersionTime" != "" ] && [ "$savedGitCommitTime" != "" ]; then
versionReference=$(getVersionReference "$savedGitCommitTime")
if [ "$goal" = "$GOAL_GET_VERSION_REFERENCE" ]; then
echo $versionReference
exit 0
fi
showMessage "info" "Saved version: \e[1m$savedVersion" true
showMessage "info" "Version reference: \e[1m$versionReference" false true
tempBranch="$TEMP_BRANCH_PREFIX$(date +%s)"
git checkout -q -b "$tempBranch" "$versionReference"
loadGitInfo
if ! $strict ; then
gitCommitTime="$savedGitCommitTime"
fi
if $onlyChangeSuffix ; then
saveProjectVersion "$savedVersionTime" "$savedVersionSuffix"
else
saveProjectVersion "$savedVersionTime" "$versionSuffix"
fi
showMessage "info" "Checking differences between saved version and head..."
set +e
if $verbose ; then
git diff --exit-code "$currentBranch" "$tempBranch"
else
git diff --exit-code "$currentBranch" "$tempBranch" >&-
fi
hasChanges=$?
set -e
if [ $hasChanges -eq 0 ]; then
hasChanges=false
showMessage "info" "No changes."
else
hasChanges=true
if $verbose ; then
showMessage "info" "Found changes."
else
showMessage "info" "Found changes. Enable the verbose mode to see the difference."
fi
fi
git checkout -q "$currentBranch"
git branch -q -D "$tempBranch"
if $hasChanges ; then
if [ "$goal" = "$GOAL_VERSION_VALIDATION" ]; then
showMessage "fail" "$GOAL_VERSION_VALIDATION FAILED" true true
exit 1
elif $onlyChangeSuffix ; then
showMessage "error" "Cannot is possible change only the version suffix, because was found changes."
showMessage "fail" "$GOAL_VERSION_GENERATION FAILED" true true
exit 1
fi
else
if [ "$goal" = "$GOAL_VERSION_VALIDATION" ]; then
showMessage "success" "$GOAL_VERSION_VALIDATION SUCCESS" true true
exit 0
elif $onlyChangeSuffix && [ "$savedVersion" != "$(formatVersion $savedVersionTime $versionSuffix)" ] ; then
versionTime=$savedVersionTime
version=$(formatVersion $savedVersionTime $versionSuffix)
gitCommitTime=$savedGitCommitTime
else
showMessage "success" "$GOAL_VERSION_GENERATION SKIPPED" true true
exit 0
fi
fi
else
showMessage "warning" "The \e[4m$propertiesFile\e[24m version properties file is corrupted. This file is generated automatically, do not edit it."
if [ "$goal" = "$GOAL_VERSION_VALIDATION" ] || [ "$goal" = "$GOAL_GET_VERSION_REFERENCE" ]; then
showMessage "fail" "$goal FAILED" true true
exit 1
fi
fi
else
if [ "$goal" = "$GOAL_VERSION_VALIDATION" ] || [ "$goal" = "$GOAL_GET_VERSION_REFERENCE" ]; then
showMessage "info" "Not found the \e[4m$propertiesFile\e[24m version properties file."
showMessage "fail" "$goal FAILED" true true
exit 1
elif $onlyChangeSuffix ; then
showMessage "error" "Cannot is possible change only the version suffix, because was not found the \e[4m$propertiesFile\e[24m version properties file."
showMessage "fail" "$GOAL_VERSION_GENERATION FAILED" true true
exit 1
fi
fi
if ! $onlyChangeSuffix ; then
loadGitInfo
fi
showMessage "info" "Generating new version: \e[1m$version\e[0m"
saveProjectVersion "$versionTime" "$versionSuffix"
showMessage "success" "$GOAL_VERSION_GENERATION SUCCESS" true true
if $autosquash && $fixup ; then
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash HEAD~2
fi
if $push ; then
if $quiet ; then
pushOptions="-q $pushOptions"
fi
git push $pushOptions $pushOrigin $pushBranch
fi
if $useErrorExitCode; then
showMessage "warning" "Using error exit code."
exit 1
fi