/
build.sh
executable file
·151 lines (134 loc) · 5.28 KB
/
build.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
#!/bin/bash
# MIT License
#
# Copyright (c) 2021 MorningSpace
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
function print_step {
local step=$1
local line='........................................................................'
printf "%s%s" "$step" "${line:${#step}}"
}
function gen_macro_doc {
local name=$1
local file=$2
local comment="`sed -n -e "/^#[[:space:]]*@Name:[[:space:]]*$name$/,/^##$/p" $file | sed -e '1d;$d'`"
local summary="`echo "$comment" | grep '^#[[:space:]]*@Description:[[:space:]]*' | sed -n 's/^#[[:space:]]*@Description:[[:space:]]*//p'`"
local author="`echo "$comment" | grep '^#[[:space:]]*@Author:[[:space:]]*' | sed -n 's/^#[[:space:]]*@Author:[[:space:]]*//p'`"
local usage="`echo "$comment" | grep '^#[[:space:]]*@Usage:[[:space:]]*' | sed -n 's/^#[[:space:]]*@Usage:[[:space:]]*//p'`"
local dependencies="`echo "$comment" | grep '^#[[:space:]]*@Dependencies:[[:space:]]*' | sed -n 's/^#[[:space:]]*@Dependencies:[[:space:]]*//p'`"
local description=""
local options=""
local examples=""
local parsing
# Parse annotations
while IFS= read -r line; do
[[ $line =~ ^#[[:space:]]*@Description:[[:space:]]* ]] && parsing=Description && continue
[[ $line =~ ^#[[:space:]]*@Options:[[:space:]]*$ ]] && parsing=Options && continue
[[ $line =~ ^#[[:space:]]*@Examples:[[:space:]]*$ ]] && parsing=Examples && continue
if [[ $parsing == Description ]] && [[ ! $line =~ ^##$ && ! $line =~ ^#[[:space:]]*@ ]]; then
description+="`echo "$line" | sed -n 's/^#[[:space:]]//p'`\n"
fi
if [[ $parsing == Options ]] && [[ ! $line =~ ^#$ ]]; then
if [[ $line =~ '${SELECT_OPTIONS}' ]]; then
options+="${SELECT_OPTIONS_HELP[@]}\n"
elif [[ $line =~ '${GLOBAL_OPTIONS}' ]]; then
options+="${GLOBAL_OPTIONS_HELP[@]}\n"
else
options+="`echo "$line" | sed -n 's/^#[[:space:]][[:space:]]//p'`\n"
fi
fi
if [[ $parsing == Examples ]] && [[ ! $line =~ ^#$ && ! $line =~ ^#[[:space:]]*@ ]]; then
examples+="`echo $line | sed -n 's/^#[[:space:]]*//p'`\n"
fi
done <<< "$comment"
# Clean up the macro doc
cp /dev/null macros/docs/$name.md
# Resolve the placeholders
while IFS= read -r line; do
case $line in
*@Name*|*@Summary*|*@Usage*)
echo -e "$line" | sed -e "s/@Name/$name/g" -e "s/@Summary/$summary/g" -e "s/@Usage/$usage/g" >> macros/docs/$name.md
;;
*@Author*)
author=${author//\//\\/}
echo -e "$line" | sed -e "s/@Author/$author/g" >> macros/docs/$name.md
;;
*@Description*)
echo -e "$description" >> macros/docs/$name.md
;;
*@Options*)
echo -e "$options" >> macros/docs/$name.md
;;
*@Examples*)
echo -e "$examples" >> macros/docs/$name.md
;;
*@Dependencies*)
local dep deps
IFS=',' read -a deps <<< "$dependencies"
if [[ -n ${deps[@]} ]]; then
for dep in ${deps[@]}; do
if [[ -f macros/bin/$dep.sh ]]; then
echo -e "* [$dep](docs/$dep.md)" >> macros/docs/$name.md
else
echo -e "* \`$dep\`" >> macros/docs/$name.md
fi
done
else
echo -e "n/a" >> macros/docs/$name.md
fi
;;
*)
echo "$line" >> macros/docs/$name.md
esac
done < macros/macro-doc-template.md
}
function gen_docs {
# Clean up
echo "Clean up directories and files..."
cp /dev/null macros/_sidebar.md
cp /dev/null macros/macros.sha256
rm -rf macros/docs
mkdir macros/docs
# Generate .md files for macros
# and update _sidebar.md
echo "Start to generate the macro docs..."
local name file i=0
for file in `ls macros/bin/*.sh 2>/dev/null`; do
echo "$i) Found macro $file."
name=${file%.sh}
name=${name#macros/bin/}
# Generate .md file for macro
print_step "Generate ${name}.md file"
gen_macro_doc $name $file
echo "done"
# Update _sidebar.md
print_step "Update _sidebar.md to add the entry for ${name}"
echo "* [${name}](docs/${name}.md)" >> macros/_sidebar.md
echo "done"
# Generate the SHA checksum for macro
print_step "Generate shasum for ${name}"
local shasum=`cat $file | shasum -a 256 | awk '{print $1}'`
echo "$shasum $name" >> macros/macros.sha256
echo "done"
(( i++ ))
done
echo "$i macros in total have been successfully processed."
}
gen_docs $@