-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbashsrc
More file actions
executable file
·212 lines (183 loc) · 5.28 KB
/
bashsrc
File metadata and controls
executable file
·212 lines (183 loc) · 5.28 KB
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
#!/bin/bash
# Copyright 2018 Juliano Santos [SHAMAN]
#
# This file is part of bashsrc.
#
# bashsrc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# bashsrc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bashsrc. If not, see <http://www.gnu.org/licenses/>.
#
readonly BASENAME=${0##*/}
readonly BASHSRC_VERSION=2.0.0
if [ ! -v BASHSRC_PATH ]; then
echo "$BASENAME: erro: 'BASHSRC_PATH' variável de ambiente não configurada." 1>&2
exit 1
fi
# Ativa globbing
shopt -s extglob
set +f
# Flags de informação.
readonly -a IFLAGS=(
NAME
SYNOPSIS
DESCRIPTION
VERSION
AUTHORS
BUGS
COPYRIGHT
LINKS
DEPENDS
EMAILS
PLATFORMS
)
# Flags de ajuda.
readonly -a HFLAGS=(
FUNCTION
TYPE
STRUCT
CONST
MAP
ARRAY
)
# Regex/Grupos
readonly -A RE=(
[flag]='^\s*#+\s*\.'
[nocomm]='^[^#]*$'
[typedef]='^[^#]*\s*typedef\s+([a-zA-Z_][a-zA-Z0-9_]*)'
[func]="${RE[flag]}(FUNCTION)\s+"
[type]="${RE[flag]}(TYPE|STRUCT|CONST|MAP|ARRAY)\s+"
)
function usage()
{
cat << _eof
$BASENAME: ferramenta de ambiente e documentação.
Uso: $BASENAME [OPÇÕES]
Argumentos obrigatórios para opções longas também são para opções curtas.
-l, --list - Lista os sources disponíveis em '\$BASHSRC_PATH'.
-e, --env - Exibe o ambiente configurado em '\$BASHSRC_PATH'.
-d, --doc <source>[.<function>|<type>] - Exibe a documentação da biblioteca, função ou tipo.
-h, --help - Exibe ajuda e sai.
-c, --check-conflicts - Verifica se há conflito de tipos entre bibliotecas em '\$BASHSRC_PATH'.
-v, --version - Exibe a versão e sai.
Desenvolvido por: Juliano Santos [SHAMAN]
Reportar falhas: <[email protected]>
Wiki: <https://github.com/shellscriptx/bashsrc/wiki>
_eof
}
function list_sources()
{
local dir file
local srcdir=${1:-$BASHSRC_PATH/src}
for dir in "${srcdir[@]}"; do
for file in "$dir/"*; do
if [[ -d "$file" ]]; then
list_sources "$file"
else
[[ $file =~ \.sh$ ]] &&
echo "$file"
fi
done
done
return 0
}
function view_doc()
{
local srcdir=$BASHSRC_PATH/src
local hflags=${HFLAGS[@]}
local iflags=${IFLAGS[@]}
local srcname=${1##*/}
local srcfile=${1%%.*}.sh
local comp match help
hflags=${RE[flag]}"(${hflags// /|})\s+(.+)$"
iflags=${RE[flag]}"(${iflags// /|})$"
if [[ ! $1 ]]; then
printf '%s: doc: requer nome da biblioteca\n' "$BASENAME" 1>&2
return 1
elif [[ ! -e "$srcdir/$srcfile" ]]; then
printf '%s: doc: "%s" biblioteca não encontrada\n' "$BASENAME" "$1" 1>&2
return 1
fi
# Obtem nomenclatura composta.
IFS='.' read _ comp <<< "$srcname"
while read -r line; do
if [[ $line =~ ${RE[nocomm]} ]]; then
help=
# Informação
elif [[ ! $comp && $line =~ $iflags ]]; then
match=true
echo "${BASH_REMATCH[1]}"
help=true
continue
# Ajuda
elif [[ ! $comp && $line =~ $hflags ]]; then
match=true
echo "${BASH_REMATCH[1],,} ${BASH_REMATCH[2]}"
continue
# Funções e Tipos
elif [[ $line =~ ${RE[func]}(${srcname#builtin.}.*)$ ]] ||
[[ $line =~ ${RE[type]}(${srcname#*.})$ ]]; then
match=true
help=true
echo "${BASH_REMATCH[1],,} ${BASH_REMATCH[2]}"
continue
fi
# Exibe detalhes do subitem caso seja especificado.
[[ $help ]] && printf '%4s%s\n' '' "${line#+(#)}"
# Define o status de retorno.
# Se nehuma referência for encontrada status será igual à 'false'.
${match:-false}
done < "$srcdir/$srcfile" || {
# status: false
printf "%s: doc: '%s' referência não encontrada\n" "$BASENAME" "$srcname" 1>&2
return 1
}
return 0
}
check_conflicts()
{
local srcfile line err
local -A srctypes
# Lista todas as bibliotecas disponíveis.
while read srcfile; do
while read -r line; do
# Trata a linha enquanto houver tipo definido (typedef)
while [[ $line =~ ${RE[typedef]} ]]; do
# Verifica se o tipo já existe, caso já exista imprime na saída
# padrão as bibliotecas conflitantes.
if [[ ${srctypes[${BASH_REMATCH[1]}]} ]]; then
printf '== Conflito ==\n\nTipo: %s\n\nBiblioteca: %s\nBiblioteca: %s\n\n' \
"${BASH_REMATCH[1]}" \
"${srctypes[${BASH_REMATCH[1]}]}" \
"$srcfile"
err=1 # status
else
# Anexa o tipo.
srctypes[${BASH_REMATCH[1]}]=$srcfile
fi
line=${line/typedef+( )${BASH_REMATCH[1]}/} # Atualiza linha.
done
done < "$srcfile" # Biblioteca
done < <(list_sources)
return ${err:-0}
}
# Parâmetros.
case $1 in
-v|--version) printf '%s-%s\n' "$BASENAME" "$BASHSRC_VERSION";;
-e|--env) printf 'BASHSRC_PATH=%s\nPATH=%s\n' "$BASHSRC_PATH" "$PATH";;
-l|--list) list_sources;;
-d|--doc) view_doc "$2";;
-c|--check-conflicts) check_conflicts;;
-h|--help) usage;;
*) printf "Uso: %s [OPÇÕES]\nTente: '%s --help' para obter mais informações.\n" "$BASENAME" "$BASENAME";;
esac
exit $?
# /* BASHSRC */