Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor of module/source.sh and rename of _pre_source _source and _p…

…ost_source to _pre_load _load and _post_load
  • Loading branch information...
commit fc856365109fc5cb8dd6b2235a3e708ef9d07dac 1 parent 1702481
James Pic authored
4 break/bashunit/main.sh
@@ -4,9 +4,9 @@
4 4 # the mtests_bashunit submodule.
5 5
6 6 # Sets up an arbitary conf path and break interval for testing.
7   -# @calls break_post_source()
  7 +# @calls break_post_load()
8 8 function Setup() {
9   - break_post_source
  9 + break_post_load
10 10
11 11 break_conf_path=/tmp/break.test
12 12 break_interval=3
4 break/source.sh
@@ -21,13 +21,13 @@
21 21 ## break_previous="1254303082"
22 22
23 23 # Loads functions
24   -function break_source() {
  24 +function break_load() {
25 25 source $(module_get_path break)/functions.sh
26 26 }
27 27
28 28 # This function is responsible for putting the module in a useable state by
29 29 # setting some defaults.
30   -function break_post_source() {
  30 +function break_post_load() {
31 31 break_interval=7200
32 32 break_conf_path=${HOME}/.break
33 33 }
2  conf/auto/functions.sh
@@ -6,7 +6,7 @@
6 6 # supporting arrays.
7 7 # Function conf_auto_load_decorator() decorates conf_load() and so takes a
8 8 # module name as argument. This function can be called in your module
9   -# _post_source() function.
  9 +# _post_load() function.
10 10 # Function conf_auto_save_all calls() conf_save() for all modules which the
11 11 # user choosed to auto save and load.
12 12
8 conf/auto/source.sh
@@ -2,7 +2,7 @@
2 2 # -*- coding: utf-8 -*-
3 3 # This submodule of conf allows the user to define the modules which config
4 4 # should be auto saved and auto loaded.
5   -# It is the only module which _post_source() function, conf_auto_post_source()
  5 +# It is the only module which _post_load() function, conf_auto_post_load()
6 6 # should call conf_load() because other module should have their configuration
7 7 # loaded by conf_auto_load_all().
8 8 # See conf_auto/functions.sh for information about the additionnal API for
@@ -11,17 +11,17 @@
11 11 # module.
12 12
13 13 # Loads the functions that extend the conf API and the conf overloads.
14   -function conf_auto_source() {
  14 +function conf_auto_load() {
15 15 source $(module_get_path conf_auto)/functions.sh
16 16 source $(module_get_path conf_auto)/conf.sh
17 17 }
18 18
19 19 # Sets conf_auto_conf_path and loads the module configuration.
20   -# Note: your modules should not call conf_load in their _post_source() func, it
  20 +# Note: your modules should not call conf_load in their _post_load() func, it
21 21 # should call conf_auto_load_decorator() instead, see functions.sh for more
22 22 # details.
23 23 # @calls conf_load()
24   -function conf_auto_post_source() {
  24 +function conf_auto_post_load() {
25 25 conf_auto_conf_path="$HOME/.conf_auto"
26 26
27 27 conf_load conf_auto
2  conf/source.sh
@@ -9,7 +9,7 @@
9 9
10 10 # This function should be called when the module is loaded. It will load
11 11 # functions it depends on.
12   -function conf_source() {
  12 +function conf_load() {
13 13 source $(module_get_path conf)/functions.sh
14 14 source $(module_get_path conf)/module.sh
15 15 }
2  docs/bashdoc/source.sh
@@ -4,7 +4,7 @@
4 4
5 5 # Blacklists the module.
6 6 # @calls module_blacklist_add()
7   -function docs_bashdoc_post_source() {
  7 +function docs_bashdoc_post_load() {
8 8 module_blacklist_add docs_bashdoc
9 9 }
10 10
2  docs/bwdocs/example/example/source.sh
@@ -20,7 +20,7 @@
20 20 # another multilign
21 21 # item.
22 22 # Link to another another file like example.sh or from another module like
23   -# foomodule/source.sh or other_source.sh
  23 +# foomodule/source.sh or other_load.sh
24 24 # Reference to the $foo, $foobar and $bar variables need the dollar prefix.
25 25
26 26 # This is a typed variable comment block
2  docs/source.sh
@@ -6,7 +6,7 @@
6 6 ## docs # run the generator
7 7
8 8 # Sets up the default path.
9   -function docs_post_source() {
  9 +function docs_post_load() {
10 10 docs_path="/tmp/docs"
11 11 docs_template_path="$(module_get_path docs)/bwdocs/templates"
12 12 docs_template_debug=0
4 hack/source.sh
@@ -4,10 +4,10 @@
4 4 # @Copyright Copyright 2009, James Pic
5 5 # @License Apache
6 6
7   -function hack_source() {
  7 +function hack_load() {
8 8 source $(module_get_path hack)/functions.sh
9 9 }
10 10
11   -function hack_post_source() {
  11 +function hack_post_load() {
12 12 hack_cdpath
13 13 }
14 mlog/source.sh
@@ -4,7 +4,7 @@
4 4 # See mlog().
5 5
6 6 # Loads Bashinator!
7   -function mlog_source() {
  7 +function mlog_load() {
8 8 source $(module_get_path mlog)/bashinator-0.3.sh
9 9 }
10 10
@@ -17,18 +17,10 @@ function mlog_source() {
17 17 ## mlog err "Database is not started"
18 18 ## mlog crit "Database crashed"
19 19 ## mlog alert "Unsecure data"
20   -## mlog emerg "Data lost, probably burning in hell" Example usage:
21   -## mlog debug "Something happenned which might help you figuring what TF"
22   -## mlog info "Some script started correctly"
23   -## mlog notice "Database is 8 days old"
24   -## mlog warning "Database is older than your father"
25   -## mlog err "Database is not started"
26   -## mlog crit "Database crashed"
27   -## mlog alert "Unsecure data"
28   -## mlog emerg "Data lost, probably burning in hell"
  20 +## mlog emerg "Data lost, probably burning in hell"
29 21 function mlog() {
30 22 local level="$1"
31 23 local message="$2"
32 24
33   - __msg $level $message
  25 + __msg $level "$message"
34 26 }
506 module/source.sh
@@ -4,13 +4,15 @@
4 4 # @Copyright Copyright 2009, James Pic
5 5 # @License Apache, unless otherwise specified by a file or a comment.
6 6 #
7   -# <h4>The KISS framework</h4>
  7 +# <h4>The dumb framework</h4>
  8 +#
8 9 # This file is the framework. It is *all* the framework. The framework is
9 10 # nothing else but this file. All other features, such as configuration
10 11 # management, unit testing, documentation, logging etc ... are done in
11 12 # separate, reuseable, consistent, simple modules.
12 13 #
13 14 # <h4>Framework variables</h4>
  15 +#
14 16 # The role of this framework is to manage repositories of modules with the
15 17 # following variables:
16 18 # - $module_repo_paths is an associative array of :
@@ -21,6 +23,7 @@
21 23 # module name => module status (string)
22 24 #
23 25 # <h4>Definition of a module</h4>
  26 +#
24 27 # A module is defined by a subdirectory of a repository with a source.sh file
25 28 # in it. That's all a module need. The module directory name is the name of the
26 29 # module. If hopefully it declares functions or variables then those should be
@@ -30,15 +33,16 @@
30 33 # A module may have dependencies and control of their loading is inversed,
31 34 # which means that specifically named functions may be declared in the module
32 35 # source.sh file if required by the module:
33   -# - modulename_pre_source(): prepare for sourcing dependencies,
34   -# - modulename_source(): load dependencies,
35   -# - modulename_post_source(): prepare to be useable,
  36 +# - modulename_pre_load(): prepare for sourcing dependencies,
  37 +# - modulename_load(): load dependencies,
  38 +# - modulename_post_load(): prepare to be useable,
36 39 #
37 40 # These function may also check if the system its module is being load on is
38   -# suitable or not, and call module_blacklist_add() otherwise. For example, if a
  41 +# suitable or not, and call module_unset() otherwise. For example, if a
39 42 # module requires a linux-vserver kernel or a BSD system.
40 43 #
41 44 # <h4>Inversion of control and polite functions</h4>
  45 +#
42 46 # Inversion of control: the overall program's flow of control is not dictated
43 47 # by the caller, but by the framework. This applies for the framework and
44 48 # several modules, like conf, at a basic level, and in a polite way.
@@ -47,374 +51,291 @@
47 51 # string argument. It should let the actual module to overload what is it about
48 52 # to process. For example, conf_save() is polite, calling `conf_save
49 53 # yourmodule` will first check if yourmodule_conf_save() exists, and run it
50   -# then return 0 if it does. "Civilized coding" sucks way less than reinventing
51   -# OOP in Bash.
  54 +# then return 0 if it does. Note that an overloading function can call its
  55 +# caller. "Civilized coding" sucks way less than reinventing OOP in Bash.
52 56 #
53 57 # That said, this is the general useage example:
54 58 #
55   -## # find all modules and submodules in your repo:
56   -## module_repo_add /path/to/your/repo
57   -## # pre source a module:
58   -## module_pre_source yourmodule # would call yourmodule_pre_source
59   -## # OR pre source all modules:
60   -## module_pre_source # would call yourmodule_pre_source for all modules
61   -## # then source a module:
  59 +## # add your repo:
  60 +## module_repo_add /path/to/yourrepo
  61 +## # find modules and submodules in your repo:
  62 +## module_repo yourrepo
  63 +## # source a module:
62 64 ## module_source yourmodule # would call yourmodule_source
63   -## # OR source all modules:
64   -## module_source # would call yourmodule_call for all modules
65   -## # then post source a module:
66   -## module_post_source yourmodule # would call yourmodule_post_source
67   -## # OR post source all modules:
68   -## module_post_source # would call yourmodule_post_source for all modules
  65 +## # pre load a module:
  66 +## module_pre_load yourmodule # would call yourmodule_pre_load
  67 +## # then load a module:
  68 +## module_load yourmodule # would call yourmodule_load
  69 +## # then post load a module:
  70 +## module_post_load yourmodule # would call yourmodule_post_load
69 71 #
70 72 # Or, run all at once:
71 73 #
72   -## module yourmodule # will do the pre_source, source and post_source
  74 +## module_repo_use /path/to/yourrepo
  75 +## module yourmodule # will do the source, pre_load, load and post_load
73 76 ## module # same, with all modules
74 77 #
75 78 # <h4>Module status</h4>
76 79 #
77 80 # A module status corresponds to the last thing that was done with it, for
78 81 # instance either of the following values:
79   -# - *found*: the module source.sh file was found:
  82 +# - *find*: the module source.sh file was found:
80 83 # it is ready to source,
81 84 # - *source*: the module source.sh file was sourced:
82   -# it is ready to _pre_source(),
83   -# - *pre_source()*: the module _pre_source() function was called:
84   -# it is ready to _source(),
85   -# - *source()*: the module _source() function was called:
86   -# it is ready to _post_source(),
87   -# - *post_source()*: the module _post_source() function was called:
  85 +# it is ready to _pre_load(),
  86 +# - *pre_load*: the module _pre_load() function was called:
  87 +# it is ready to _load(),
  88 +# - *load*: the module _load() function was called:
  89 +# it is ready to _post_load(),
  90 +# - *post_load*: the module _post_load() function was called:
88 91 # it is ready to use,
89   -# - *blacklist*: the module was blacklisted with module_blacklist_add(),
90 92 #
91   -# The role of module.sh is to traverse application repository directories
92   -# and try to load modules. Loading a module consist of:
93   -# - finding source.sh
94   -# - sourcing source.sh
95   -# - running yourmodule_pre_source()
96   -# - running yourmodule_source()
97   -# - running yourmodule_post_source()
98   -# Modules are also able to blacklist themselves.
99   -# In order to find modules, this script expects repository paths either:
100   -# - in the environment variable $MODULES_PATH (same format than $PATH)
101   -# - as arguments of the source call (separate paths with space)
102   -# - as arguments of module_pre_source (separate paths with space)
103   -# Each module must have a source.sh file which can contain this functions:
104   -# - yourmodule_pre_source(): basically set up variables needed by _source()
105   -# - yourmodule_source(): include all dependencies
106   -# - yourmodule_post_source(): initialise the module, ie. default variables,
107   -# conf path, whatever...
108   -# Eventually, your module can have a yourmodule() function defined in source.sh
109   -# to something meaningful. Think of it as an object constructor.
110   -# Submodules are simply subdirectories of modules which contain source.sh. See
111   -# module_pre_source() for more info.
  93 +# <h4>Module dependencies</h4>
  94 +#
  95 +# Some research should be done, a possibility is a "loading queue"
  96 +# implementation.
  97 +#
  98 +# For now, call the module_load_core_modules() function before loading really
  99 +# optionnal modules.
  100 +#
  101 +# <h4>License agreement</h4>
  102 +#
  103 +# You swear that:
  104 +# - you will never use trailing slashes in paths,
  105 +# - you will always prefix your variables and functions correctly,
  106 +# - you will not pollute the environment with temporary variables,
  107 +# - you will not break backward compatibility, unless you warned,
112 108
113 109 # Check bash version. We need at least 4.0.x
114 110 # Lets not use anything like =~ here because
115 111 # that may not work on old bash versions.
116   -#if [[ "$(awk -F. '{print $1 $2}' <<< $BASH_VERSION)" -lt 40 ]]; then
  112 +if [[ "$(awk -F. '{print $1 $2}' <<< $BASH_VERSION)" -lt 40 ]]; then
117 113 echo "Sorry your bash version is too old!"
118 114 echo "You need at least version 3.2 of bash"
119 115 echo "Please install a newer version:"
120 116 echo " * Either use your distro's packages"
121 117 echo " * Or see http://www.gnu.org/software/bash/"
122   -# return 2
123   -#fi
  118 + return 2
  119 +fi
124 120
125 121 # string repo name => string repo absolute path
126 122 declare -A module_repo_paths
  123 +
127 124 # string module name => string module absolute path
128 125 declare -A module_paths
129   -# list of loaded modules
  126 +
  127 +# string module name => string module status
130 128 declare -A module_status
131 129
132   -# (Re)-loads one or several modules.
133   -# Keep in mind that module_source() is reponsible for calling
134   -# yourmodule_pre_source() so for example:
135   -## module hack # will load the hack module
  130 +# Temporary solution against module dependencies
  131 +function module_load_core_modules() {
  132 + module hack conf mlog
  133 +}
  134 +
  135 +# (Re)-loads one or several module repository. See module/source.sh file
  136 +# documentation.
  137 +# @param repository names separated by spaces
  138 +function module_repo() {
  139 + module_repo_add $*
  140 + module_repo_find ${!module_repo_paths[@]}
  141 +}
  142 +
  143 +# (Re)-loads one or several modules. See module/source.sh file documentation.
136 144 # @param module names separated by spaces
137 145 function module() {
138   - module_pre_source $*
139 146 module_source $*
140   - module_post_source $*
  147 + module_pre_load $*
  148 + module_load $*
  149 + module_post_load $*
141 150 }
142 151
143   -# Load a module if not loaded.
144   -# If this was called during a _pre_source or _source() function then it will
145   -# add the module name to the load queue for later loading by
146   -# module_load_queue().
147   -# @param module names separated by spaces
148   -function module_require() {
149   - for module_name in $*; do
150   - case ${module_status[$module_name]} in
151   - loaded)
152   - continue
153   - ;;
154   - blacklist)
155   - mlog error "Required module $module_name is blacklisted"
156   - return 2
157   - ;;
158   - *) # found, queued, but not loaded
159   - module_status[$module_name]="queued"
160   - ;;
161   - esac
162   - done
  152 +# Adds one or several repo path after removing the trailing slash.
  153 +#
  154 +# If anything is strange, then use absolute paths.
  155 +#
  156 +# @param path to repositories separated by spaces
  157 +# @variable $module_repo_paths is updated
  158 +function module_repo_add() {
  159 + local abs_path=""
  160 + local repo_name=""
  161 + local len=0
163 162
164   - if [[ ${FUNCNAME[@]} =~ _post_source ]] || [[ ! ${FUNCNAME[@]} =~ _source ]]; then
165   - module_queue_load
166   - fi
167   -}
  163 + while [[ -n "$1" ]]; do
  164 +
  165 + # in case hack module is not loaded yet, then hack_realpath should
  166 + # not be depended on.
  167 + if [[ $OSTYPE =~ bsd ]]; then
  168 + abs_path="$(realpath $1)"
  169 + else
  170 + abs_path="$(readlink -f $1)"
  171 + fi
168 172
169   -# Calls module() for each module with status "queued".
170   -# @call module()
171   -function module_queue_flush() {
172   - for module_name in ${!module_status[@]}; do
173   - if [[ ${module_status[$module_name]} == "queued" ]]; then
174   - module $module_name
  173 + if [[ ${abs_path:(-1)} == "/" ]]; then
  174 + len=$(( ${#abs_path}-1 ))
  175 + abs_path="${abs_path:0:$len}"
175 176 fi
  177 +
  178 + repo_name="${abs_path##*/}"
  179 + module_repo_paths[$repo_name]="$abs_path"
  180 +
  181 + shift
176 182 done
177 183 }
178 184
179   -# This function finds all modules and nested submodules in a given path and
180   -# registers it.
  185 +# This function finds all modules and nested submodules in a given repo.
  186 +#
181 187 # With this example layout:
182 188 # - /yourpath/
183 189 # - /yourpath/foo/
184 190 # - /yourpath/foo/source.sh
185 191 # - /yourpath/foo/bar/source.sh
  192 +#
186 193 # It will register:
187 194 # - module "foo" with path "/yourpath/foo"
188 195 # - module "foo_bar" with path "/yourpath/foo/bar"
  196 +#
189 197 # It that example case, foo_bar functions should be prefixed by foo_bar_
190 198 # instead of just foo_.
191   -# The blacklist check is done just before adding the module to $module_paths.
192   -# @param Paths to find modules in, separated by space.
193   -# @calls module_blacklist_check
194   -function module_pre_source() {
195   - declare -a paths=($(echo $MODULES_PATH | tr : " "))
196   -
197   - if [[ -n $1 ]]; then
198   - declare -a paths=()
199   -
200   - for path in $*; do
201   - paths+=("$path")
202   - done
203   - fi
204   -
205   - for path in $(module_get_repo_paths); do
206   - paths+=("$path")
207   - done
208   -
  199 +#
  200 +# @param repository names separated by spaces
  201 +# @variable $module_paths and $module_status are updated
  202 +# @variable $module_repo_paths is read
  203 +function module_repo_find() {
  204 + local path=""
  205 + local module_path=""
209 206 local module_name=""
210   - local relative_path=""
211   - local repo_name=""
212   - local len=0
213   -
214   - # register modules and paths for each path
215   - for path in ${paths[@]}; do
216   - if [[ ${path:(-1)} == "/" ]]; then
217   - len=$(( ${#path}-1 ))
218   - path="${path:0:$len}"
219   - fi
  207 + local rel_path=""
220 208
221   - if echo $OSTYPE | grep -q -o bsd; then
222   - path="$(realpath $path)"
223   - else
224   - path="$(readlink -f $path)"
225   - fi
  209 + while [[ -n "$1" ]]; do
  210 +
  211 + path=${module_repo_paths[$1]}
226 212
227 213 for module_path in `find $path -name source.sh -exec dirname {} \;`; do
228   - relative_path="${module_path#*$path/}"
229   - module_name="${relative_path//\//_}"
230   -
231   - # add to module_path if required
232   - if [[ ! "${!module_paths[@]}" =~ ^$module_name$ ]]; then
233   - module_paths["$module_name"]="$module_path"
234   - fi
235 214
236   - module_status[$module_name]="found"
  215 + rel_path="${module_path#*$path/}"
  216 + module_name="${rel_path//\//_}"
  217 +
  218 + module_paths[$module_name]="$module_path"
  219 + module_status[$module_name]="find"
237 220
238 221 done
239   -
240   - repo_name="${path##*/}"
241   - module_repo_paths["$repo_name"]="$path"
  222 +
  223 + shift
242 224 done
243 225 }
244 226
245   -# Source, run _pre_source() and _source() for each modules. Module names can be
246   -# passed as parameters, or all modules that where found will be used. This
247   -# function cares about the possible blacklisting modules.
248   -# It does the following for each module:
249   -# - check blacklist
250   -# - source source.sh
251   -# - check blacklist
252   -# - call _pre_source() function if it is declared
253   -# - check blacklist
254   -# - call _source() function if it is declared
255   -# @param optionnal list of modules, separated by space
256   -# @calls module_blacklist_check
  227 +# Sources the source.sh file of the given modules
  228 +# @param module names, separated by space
  229 +# @variable $module_paths is read
  230 +# @variable $module_status is updated
  231 +# @polite will try yourmodule_source(), useful for reloading control
257 232 function module_source() {
258   - local -a module_names=()
259   - local module_name=""
260   - local module_source_path=""
261   - local module_source=""
262   -
263   - if [[ -n $1 ]]; then
264   - module_names=$*
265   - else
266   - module_names=${!module_paths[@]}
267   - fi
268   -
269   - for module_name in ${module_names[@]}; do
270   - module_source_path="$(module_get_path $module_name)/source.sh"
271   - module_pre_source_function="${module_name}_pre_source"
272   - module_source_function="${module_name}_source"
273   -
274   - # blacklist check
275   - if [[ -n $(module_blacklist_check $module_name) ]]; then
276   - continue
277   - fi
  233 + while [[ -n "$1" ]]; do
  234 +
  235 + local module_source="${1}_source"
  236 + if [[ -n "$(declare -f $module_source)" ]]; then
  237 + $module_source
  238 + else
  239 + source ${module_paths[$1]}/source.sh
  240 + fi
278 241
279   - # source module source path
280   - source $module_source_path || echo "Could not source $module_source_path $module_name"
281   -
282   - # blacklist check
283   - if [[ -n $(module_blacklist_check $module_name) ]]; then
284   - continue
285   - fi
  242 + module_status[$1]="source"
286 243
287   - # run module pre_source function if it is declared
288   - if [[ -n $(declare -f $module_pre_source_function) ]]; then
289   - $module_pre_source_function
290   - fi
  244 + shift
  245 + done
  246 +}
  247 +
  248 +# Run the _pre_load() function of any given module.
  249 +# @param module names, separated by space
  250 +# @variable $module_status is updated
  251 +# @calls yourmodule_pre_load()
  252 +function module_pre_load() {
  253 + while [[ -n "$1" ]]; do
291 254
292   - # blacklist check
293   - if [[ -n $(module_blacklist_check $module_name) ]]; then
294   - continue
  255 + local module_pre_load="${1}_pre_load"
  256 + if [[ -n "$(declare -f $module_pre_load)" ]]; then
  257 + $module_pre_load
295 258 fi
  259 +
  260 + module_status[$1]="pre_load"
296 261
297   - # run module source function if it is declared
298   - if [[ -n $(declare -f $module_source_function) ]]; then
299   - $module_source_function
300   - fi
  262 + shift
301 263 done
302 264 }
303 265
304   -# Run _post_source() function for each selected module. Module names should be
305   -# passed as parameter, separated by space. All found and non blacklisted
306   -# modules are selected if no parameter is passed.
307   -# It does following for each module:
308   -# - check blacklist
309   -# - unset any function or variable starting with blacklist module prefix,
310   -# - call _post_source() function if it is declared.
311   -# @param optionnal list of modules, separated by space
312   -# @calls module_blacklist_check(), module_blacklist_check_unset()
313   -function module_post_source() {
314   - local module_post_source_function=""
315   - local module_names=$*
316   -
317   - if [[ -n $1 ]]; then
318   - module_names=$*
319   - else
320   - module_names=${!module_paths[@]}
321   - fi
322   -
323   - for module_name in ${!module_paths[@]}; do
  266 +# Run the _load() function of any given module.
  267 +# @param module names, separated by space
  268 +# @variable $module_status is updated
  269 +# @calls yourmodule_load()
  270 +function module_load() {
  271 + while [[ -n "$1" ]]; do
324 272
325   - module_post_source_function="${module_name}_post_source"
326   -
327   - if [[ -z "$(module_blacklist_check $module_name)" ]] && \
328   - [[ -n "$(declare -f $module_post_source_function)" ]]; then
329   - $module_post_source_function
  273 + local module_load="${1}_load"
  274 + if [[ -n "$(declare -f $module_load)" ]]; then
  275 + $module_load
330 276 fi
331 277
332   - module_blacklist_check_unset $module_name
333   -
334   - module_status[$module_name]="loaded"
  278 + module_status[$1]="load"
  279 +
  280 + shift
335 281 done
336 282 }
337 283
338   -# Unsets a module if blacklisted, and if not "module"
339   -# @calls module_blacklist_check(), module_unset()
340   -function module_blacklist_check_unset() {
341   - local module_name="$1"
342   -
343   - # blacklist check
344   - if [[ -n $(module_blacklist_check $module_name) ]]; then
345   - # i won't unset myself
346   - if [[ "$module_name" != "module" ]]; then
347   - module_unset $to_unset
  284 +# Run the _post_load() function of any given module.
  285 +# @param module names, separated by space
  286 +# @variable $module_status is updated
  287 +# @calls yourmodule_post_load()
  288 +function module_post_load() {
  289 + while [[ -n "$1" ]]; do
  290 +
  291 + local module_post_load="${1}_post_load"
  292 + if [[ -n "$(declare -f $module_post_load)" ]]; then
  293 + $module_post_load
348 294 fi
349   - fi
  295 +
  296 + module_status[$1]="post_load"
  297 +
  298 + shift
  299 + done
350 300 }
351 301
352   -# Will unset anything starting with a given module prefix.
  302 +# Will unset anything starting with given module names.
  303 +# This function should be called if a module figures that the system is not
  304 +# suitable.
  305 +# @param module names, separated by space
353 306 # @polite Will try to run yourmodule_unset().
354 307 function module_unset() {
355   - local module_name="$1"
356   -
357   - # polite module snippet
358   - local module_overload="${module_name}_unset"
359   - if [[ -n $(declare -f $module_overload) ]]; then
360   - if [[ ! ${FUNCNAME[*]} =~ $module_overload ]]; then
361   - $module_overload
362   - return $?
  308 + local word=""
  309 +
  310 + while [[ -n "$1" ]]; do
  311 +
  312 + # polite module snippet
  313 + local module_overload="${1}_unset"
  314 + if [[ -n "$(declare -f $module_overload)" ]]; then
  315 + if [[ ! ${FUNCNAME[*]} =~ $module_overload ]]; then
  316 + $module_overload
  317 + return $?
  318 + fi
363 319 fi
364   - fi
365 320
  321 + local declared=$(declare | grep -o "^${1}.*")
366 322
367   - local declared=$(declare | grep -o "^${module_name}.*")
368   - local to_unset=""
  323 + for word in $declared; do
  324 + if [[ $word =~ ^$1 ]]; then
  325 + local to_unset=$(echo $word| grep -o "^${1}[^=(]*")
  326 + unset $to_unset
  327 + fi
  328 + done
369 329
370   - for word in $declared; do
371   - if [[ $word =~ ^$module_name ]]; then
372   - to_unset=$(echo $word| grep -o "^${module_name}[^=(]*")
373   - unset $to_unset
374   - fi
  330 + shift
375 331 done
376 332 }
377 333
378   -# This function will output "Yes" if the first parameter it is called with
379   -# is in the blacklist array.
380   -# It will keep quiet (not output anything) it the first parameter is not
381   -# in the blacklist array.
382   -# Modules should not use $module_blacklist directly and should use this
383   -# function to ensure that a module is blacklisted or not.
384   -# Example usage:
385   -# if [[ -n $(module_blacklist_check yourmodule) ]]; then
386   -# echo "yourmodule is blacklisted"
387   -# else
388   -# echo "yourmodule is not blacklisted"
389   -# fi
390   -# @param Module name
391   -function module_blacklist_check() {
392   - if [[ ${module_status[$1]}="blacklist" ]]; then
393   - echo "Yes"
394   - return 0
395   - fi
396   -
397   - return 1
398   -}
399   -
400   -# This function adds the first parameter to the blacklist.
401   -# User modules should not use the $module_blacklist array dirrectly and
402   -# should add modules to the blacklist through this function.
403   -# User modules that do sanity checks should call this function if they
404   -# cannot prepare to be useable.
405   -# Example usage:
406   -# # add yourmodule to the blacklist
407   -# module_blacklist_add yourmodule
408   -# @param Module name
409   -# @polite Will try to call yourmodule_unset()
410   -function module_blacklist_add() {
411   - module_status[$1]="blacklist"
412   -}
413   -
414 334 # This function takes a module name as first parameter and outputs its
415 335 # absolute path.
416 336 # It provides a reliable way for a script in your module to know its own
417 337 # location on the file system.
  338 +# This function is used by any module _load() function.
418 339 # Example usage:
419 340 # source $(module_get_path yourmodule)/functions.sh
420 341 # Example submodule usage:
@@ -424,40 +345,17 @@ function module_get_path() {
424 345 echo ${module_paths[$1]}
425 346 }
426 347
427   -# Outputs the known module repository paths.
428   -function module_get_repo_paths() {
429   - local paths=""
430   -
431   - for module_path in ${module_paths[@]}; do
432   - paths=" ${module_path%/*}"
433   - done
434   -
435   - echo $paths
436   -}
437   -
438 348 # This function dumps all module variables.
439 349 function module_debug() {
440   - echo "For MODULES_PATH: $MODULES_PATH"
441   -
442   - echo "List of loaded modules and paths:"
443   -
444   - for module_name in ${!module_paths[@]}; do
445   - echo " - ${module_name} from ${module_paths[$module_name]}"
446   - done
447   -
448 350 echo "List of repo names and paths":
449 351
450 352 for repo_name in ${!module_repo_paths[@]}; do
451 353 echo " - ${repo_name} from ${module_repo_paths[$repo_name]}"
452 354 done
453 355
454   - echo "List of blacklisted modules:"
  356 + echo "List of loaded modules, statuses and paths:"
455 357
456   - for module_name in ${module_blacklist[@]}; do
457   - echo " - ${module_name} from ${module_paths[$module_name]}"
  358 + for module_name in ${!module_paths[@]}; do
  359 + echo " - ${module_name}, ${module_status[$module_name]} from ${module_paths[$module_name]}"
458 360 done
459 361 }
460   -
461   -# blacklist ourself because of course our structure is slightly different from
462   -# others as we're one step in advance
463   -module_blacklist_add module
4 mpd/source.sh
@@ -7,12 +7,12 @@
7 7 # ncmpc and screen then maybe this module will be useful to you.
8 8
9 9 # Declares module configuration variable names.
10   -function mpd_source() {
  10 +function mpd_load() {
11 11 source $(module_get_path mpd)/functions.sh
12 12 }
13 13
14 14 # It sets some defaults and load the user configuration data.
15   -function mpd_post_source() {
  15 +function mpd_post_load() {
16 16 mpd_host=""
17 17 mpd_port=""
18 18 mpd_password=""
4 mtests/bashunit/source.sh
@@ -6,7 +6,7 @@
6 6 # named "bashunit".
7 7
8 8 # Sources mtest addons for bashunit.
9   -function mtests_bashunit_source() {
  9 +function mtests_bashunit_load() {
10 10 source $(module_get_path mtests_bashunit)/runner.sh
11 11 source $(module_get_path mtests_bashunit)/assertions.sh
12 12 }
@@ -16,7 +16,7 @@ function mtests_bashunit_source() {
16 16 # - listeners
17 17 #
18 18 # Read bashunit/runner.sh for more information.
19   -function mtests_bashunit_post_source() {
  19 +function mtests_bashunit_post_load() {
20 20 if test -z "$BASHUNIT_OUTPUTTER"; then
21 21 BASHUNIT_OUTPUTTER="TextOutputter"
22 22 fi
4 mtests/shunit/source.sh
@@ -6,12 +6,12 @@
6 6 # named "shunit".
7 7
8 8 # Sets $SHUNIT_HOME environment variable required by shunit.
9   -function mtests_shunit_pre_source() {
  9 +function mtests_shunit_pre_load() {
10 10 export SHUNIT_HOME="$(module_get_path mtests_shunit)/current"
11 11 }
12 12
13 13 # Loads shUnitPlus.
14   -function mtests_shunit_source() {
  14 +function mtests_shunit_load() {
15 15 source $(module_get_path mtests_shunit)/current/shUnitPlus >/dev/null 2>&1
16 16 }
17 17
2  os/source.sh
@@ -4,7 +4,7 @@
4 4 # The plan: port multi-os compiles and bin/ symlinks from my .bashrc
5 5
6 6 # Sets $os_type to bsd or linux.
7   -function os_source() {
  7 +function os_load() {
8 8 uname -a | grep -q -i bsd
9 9 if [[ $? -eq 0 ]]; then
10 10 os_type=bsd
2  todo/source.sh
@@ -3,6 +3,6 @@
3 3 # A simple module to manage a todo list.
4 4
5 5 # Sources required functions.
6   -function todo_source() {
  6 +function todo_load() {
7 7 source $(module_get_path todo)/functions.sh
8 8 }
4 vcs/source.sh
@@ -3,12 +3,12 @@
3 3 # VCS wrapper, for git only right now.
4 4
5 5 # Sources functions and aliases.
6   -function vcs_source() {
  6 +function vcs_load() {
7 7 source $(module_get_path vcs)/aliases.sh
8 8 }
9 9
10 10 # Sets variable defaultts.
11   -function vcs_post_source() {
  11 +function vcs_post_load() {
12 12 vcs_src_path=""
13 13 vcs_type=""
14 14 vps_conf_path=$(vcs_get_conf_path)
4 volume/source.sh
@@ -9,13 +9,13 @@
9 9
10 10 # This function should be called when the module is loaded, it will
11 11 # take care of loading the conf and function submodules.
12   -function volume_source() {
  12 +function volume_load() {
13 13 source $(module_get_path volume)/functions.sh
14 14 }
15 15
16 16 # This function is responsible of preparing the module in a useable state
17 17 # by setting a default volume interval and getting the current volume.
18   -function volume_post_source() {
  18 +function volume_post_load() {
19 19 volume_interval=5
20 20 volume_current=$(volume_get_current)
21 21 volume_conf_path=${HOME}/.volume
6 vps/source.sh
@@ -11,7 +11,7 @@
11 11 # Sets the default globals if required.
12 12 # @variable $VPS_DIR
13 13 # @variable $VPS_ETC_DIR
14   -function vps_pre_source() {
  14 +function vps_pre_load() {
15 15 if [[ -z $VPS_DIR ]]; then
16 16 VPS_DIR="/vservers"
17 17 fi
@@ -22,13 +22,13 @@ function vps_pre_source() {
22 22 }
23 23
24 24 # Declares module configuration variable names.
25   -function vps_source() {
  25 +function vps_load() {
26 26 source $(module_get_path vps)/functions.sh
27 27 source $(module_get_path vps)/conf.sh
28 28 }
29 29
30 30 # Unsets all vps variables.
31   -function vps_post_source() {
  31 +function vps_post_load() {
32 32 vps_name=""
33 33 vps_root=""
34 34 vps_id=""
2  wepcrack/source.sh
@@ -3,6 +3,6 @@
3 3 # The wepcrack module wraps around aircrack-ng.
4 4
5 5 # Blacklists the wepcrack module which has not been fully ported yet
6   -function wepcrack_pre_source() {
  6 +function wepcrack_pre_load() {
7 7 module_blacklist_add wepcrack
8 8 }

0 comments on commit fc85636

Please sign in to comment.
Something went wrong with that request. Please try again.