-
Notifications
You must be signed in to change notification settings - Fork 0
/
newmod
executable file
·118 lines (106 loc) · 3.79 KB
/
newmod
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
#! /usr/bin/bash
################################################################################
# Copyright: [email protected]
# Version: 1.2
# Date: 20230329
# Description: Used to create a module template.
################################################################################
source ./lib/environments.sh
source ./lib/errno.sh
source ./lib/printer.sh
function usage() {
print_bold_green "Usage: newmod [-m|--module-name <ModuleFileName>]"
print_bold_green " [-h|--help]"
print_bold_green "Note:"
print_green " -m <ModuleFileName>, specify the bash module file name; must be [a-zA-Z0-9_]+.sh"
}
function check_and_create_empty_modfile() {
local vRc
mkdir -p lib
vRc=$?
if [[ ${vRc} -ne 0 ]]; then
print_red_t "[${vRc}]Failed to create lib dir of module files."
exit ${EPERM}
fi
local vFileName=$1
if ! [[ ${vFileName} =~ lib/[a-zA-Z0-9_]+.sh ]]; then
print_red_t "Invalid module file name: ${vFileName}"
usage
exit ${EINVAL}
fi
if [[ -f ${vFileName} ]]; then
print_yellow_t "Module file has alread existed: ${vFileName}"
exit ${EEXIST}
fi
touch ${vFileName}
vRc=$?
if [[ ${vRc} -ne 0 ]]; then
print_red_t "[${vRc}]Failed to create empty module file: ${vFileName}"
exit ${EPERM}
fi
}
function main() {
local vDebug=false
local vBashModuleFileName
for((i = 1; i <= $#; ++i)); do
case ${!i} in
'-m' | '--module-name')
i=$((i + 1))
vBashModuleFileName=lib/${!i}
check_and_create_empty_modfile ${vBashModuleFileName}
;;
'-h' | '--help')
usage
exit ${EOK}
;;
*)
usage
exit ${EINVAL}
;;
esac
done
local vCondMacro=${vBashModuleFileName%.sh}
${vDebug} && echo "vCondMacro=${vCondMacro}"
vCondMacro=LIUZAN_${vCondMacro#lib/}_SH
${vDebug} && echo "vCondMacro=${vCondMacro}"
vCondMacro=${vCondMacro^^[a-zA-Z]}
${vDebug} && echo "vCondMacro=${vCondMacro}"
echo '#! /usr/bin/bash' > ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo '################################################################################' >> ${vBashModuleFileName}
echo '# Copyright: [email protected]' >> ${vBashModuleFileName}
echo '# Version: 1.0' >> ${vBashModuleFileName}
echo "# Date: $(date +%Y-%M-%d)" >> ${vBashModuleFileName}
echo '# Description:' >> ${vBashModuleFileName}
echo '################################################################################' >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo "if [[ '\${${vCondMacro}}' != "${vCondMacro}_DEFINED" ]]; then" >> ${vBashModuleFileName}
echo "readonly ${vCondMacro}='${vCondMacro}_DEFINED'" >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo '####Imports of other module files####' >> ${vBashModuleFileName}
echo "source ${ZBASH_LIB_ROOT}/environments.sh" >> ${vBashModuleFileName}
echo "source ${ZBASH_LIB_ROOT}/errno.sh" >> ${vBashModuleFileName}
echo "source ${ZBASH_LIB_ROOT}/printer.sh" >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo '####Definition of global parameters####' >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo '####Definition of functions####' >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo '####Unit test section####' >> ${vBashModuleFileName}
echo 'if false; then' >> ${vBashModuleFileName}
echo ' print_bold_green_t "====Unit test begin===="' >> ${vBashModuleFileName}
echo ' print_bold_green_t "====Unit test end===="' >> ${vBashModuleFileName}
echo 'fi' >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo >> ${vBashModuleFileName}
echo "fi # ${vCondMacro}" >> ${vBashModuleFileName}
}
main "$@"