forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_descriptions_to_readme.sh
executable file
·107 lines (79 loc) · 2.33 KB
/
add_descriptions_to_readme.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
#!/usr/bin/env bash
# execute: ./scripts/add_descriptions_to_readme.sh
# define new line character to be u"$SED_COMMAND" in `"$SED_COMMAND"`
nl='
'
SED_COMMAND="sed"
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_COMMAND="gsed"
fi
# enable regex (see: https://stackoverflow.com/a/42740385)
shopt -s extglob
scriptName="$(dirname "$0")/$(basename "$0")"
function getHelp() { # get descriptions and commands from Makefile
i=0
commands=()
descriptions=()
while read -r line; do
if (( i % 2 == 0 ));
then
descriptions+=( "${line//#:*( )}" )
else
commands+=( "$(echo "$line" | cut -d : -f 1)" )
fi
((i++))
done < <(
# https://stackoverflow.com/a/59087509
grep -B1 -E "^[a-zA-Z0-9_-]+:([^\=]|$)" ./Makefile \
| grep -v -- --
)
}
FILE=README.md
# returns `commands` and `descriptions` arrays
getHelp
startLine=$(( $( grep -n "^#### Available Commands" $FILE | cut -d : -f 1 ) + 2 ))
endLine=$(( $( grep -n "^#### File Structure" $FILE | cut -d : -f 1 ) - 2 ))
# Updates "Available Commands" section
if (( startLine <= endLine));
then
# Deletes previous descriptions
"$SED_COMMAND" -i "$startLine,${endLine}d" "$FILE"
fi
function createMultipleAsterisks() {
numOfAsterisks=$1
asterisks=$( eval printf "\*%.0s" "{1..${numOfAsterisks}}" )
echo "$asterisks"
}
function printAvailableCommands() {
curLine=$startLine
stringToWrite="<!--- GENERATED by $scriptName --->"
commentLen=$(( ${#stringToWrite} - 11 ))
i=0
"$SED_COMMAND" -i "${curLine}i\\${stringToWrite}" "$FILE"
# https://www.shellcheck.net/wiki/SC2219
(( curLine++ )) || true
# empty line
"$SED_COMMAND" -i "${curLine}i${nl}" "$FILE"
(( curLine++ )) || true
echo 'Writing the following commands with their descriptions:'
while (( i < ${#commands[@]} ))
do
stringToWrite="- \`make ${commands[$i]}\`: ${descriptions[$i]}."
stringToWrite=${stringToWrite//\'/\\\'}
echo "$stringToWrite"
"$SED_COMMAND" -i "${curLine}i\\${stringToWrite}" "$FILE"
(( curLine++ )) || true
(( i++ )) || true
done
# empty line
"$SED_COMMAND" -i "${curLine}i${nl}" "$FILE"
(( curLine++ )) || true
# multiple '*'
asterisks=$(createMultipleAsterisks $commentLen)
stringToWrite="<!--- ${asterisks} --->"
"$SED_COMMAND" -i "${curLine}i\\${stringToWrite}" "$FILE"
(( curLine++ )) || true
}
echo 'Updating "Available Commands" section...'
printAvailableCommands
echo 'Done.'