source: trunk/bundle/bundle.bash

Last change on this file was 109, checked in by g7moreau, 9 years ago
  • Commit some very old doc !
File size: 6.0 KB
Line 
1function bundle {
2   local ext=$(basename ${SHELL})
3   local folder="${HOME}/.config/bundle/source.d /etc/bundle/source.d"
4
5   case "$1" in
6      av|avail)
7         for d in ${folder}
8         do
9            if [ -d "$d" ]
10            then
11               echo "   --- folder $d"
12               ( cd "$d" ; find . -type f -a -name "*.${ext}" | sed -e "s/.\///; s/\.${ext}\$//" )
13            fi
14         done
15         ;;
16
17      lo|load)
18         pack=$(find ${folder} -type f -a -name "*.${ext}" -print0 2> /dev/null | grep -FzZ "/$2.${ext}" | head -n 1 )
19         if [ -f "${pack}" ]
20         then
21            export _bundle_name="$2"
22            let _bundle_level++
23            export _bundle_level
24            export _bundle_status="${_bundle_status}:$2"
25            [ -n "$PS1" ] && history -w
26            $VERBOSE && echo "Begin bundle: ${_bundle_name}"
27            ${SHELL} --rcfile ${pack}
28            $VERBOSE && echo "End bundle: ${_bundle_name}"
29            [ -n "$PS1" ] && history -r
30            unset _bundle_name
31            let _bundle_level--
32            [ ${_bundle_level} -eq 0 ] && unset _bundle_level
33            export _bundle_status=$(echo "${_bundle_status}" | sed 's/:[^:]*$//')
34            [ "${_bundle_status}" == "" ] && unset _bundle_status
35         else
36            echo "unable to load bundle file: $2"
37            echo "Usage: bundle load bundle-file"
38         fi
39         ;;
40
41      un|unload)
42         if [ ${_bundle_level} > 0 ]
43         then
44            [ -n "$PS1" ] && history -w
45            exit
46         else
47            echo "Error: not a bundle environment!"
48         fi
49         ;;
50
51      sh|show)
52         pack=$(find ${folder} -type f -a -name "*.${ext}" -print0 2> /dev/null | grep -FzZ "/$2.${ext}" | head -n 1 )
53         if [ -f "${pack}" ]
54         then
55            pod2text ${pack}
56         else
57            echo "unable to find bundle file: $2"
58            echo "Usage: bundle show bundle-file"
59         fi
60         ;;
61
62      li|list)
63         if [ ${_bundle_level} > 0 ]
64         then
65            echo "${_bundle_status}" | sed 's/^://'
66         else
67            echo "Error: not a bundle environment!"
68         fi
69         ;;
70
71      st|status)
72         if [ ${_bundle_level} > 0 ]
73         then
74            echo "bundle environment active"
75         else
76            echo "not a bundle environment"
77         fi
78         ;;
79
80      so|source)
81         pack=$(find ${folder} -type f -a -name "*.${ext}" -print0 2> /dev/null | grep -FzZ "/$2.${ext}" | head -n 1 )
82         if [ -f "${pack}" ]
83         then
84            . ${pack}
85         else
86            echo "Usage: bundle source bundle-file"
87         fi
88         ;;
89
90      *)
91         echo "Usage: bundle list|avail|load|unload|show|status|source|help"
92         ;;
93   esac
94   }
95
96typeset -fx bundle
97
98return
99
100################################################################
101# Documentation in POD format (like Perl)
102################################################################
103
104=head1 NAME
105
106bundle - load specific environment in current shell
107
108=head1 SYNOPSIS
109
110 bundle avail
111 bundle show bundle-file
112
113 bundle load bundle-file
114 bundle unload
115 bundle list
116
117 bundle source bundle-file
118
119 bundle status
120
121 bundle help
122
123=head1 DESCRIPTION
124
125C<bundle> can load a new environment in current shell
126(source) or load it in a sub-shell.
127In this second case, environment could be unload!
128
129This shell function looks furiously at the command C<module>.
130It's wanted but is much simpler (no C<tcl>...).
131The arguments are quite the same.
132There is no magic done on the current environment variables
133(remember, C<bundle> just source shell file).
134
135C<bundle> launch a new shell (sub-shell) at load and exit it at unload.
136It's all!
137
138No...
139In interactive mode, it's save the current history so this one can be use in the sub-shell.
140The same thing is done at the end of the sub-shell,
141history is then reload in the master shell.
142For the user point of vu, it's like there is only one shell!
143
144But be carrefull with variable,
145only export variable could be use in sub-shell.
146
147
148=head1 COMMAND
149
150=over 12
151
152=item B<avail>
153
154List all available bundle.
155Bundle are search in two specific path:
156F<${HOME}/.config/bundle/source.d> and F</etc/bundle/source.d>
157
158Bundle are just shell script with the shell name as extension
159(C<.bash> for C<bash> script).
160Bundle are first search in user folder.
161This allows the user to overloaded a system bundle.
162
163=item B<show> F<bundle-file>
164
165Show a small description of the bundle file if available...
166Format is done width the command C<pod2text>.
167Documentation can be written at the end of the script after a last command C<return> for C<bash>.
168
169=item B<load> F<bundle-file>
170
171Start a new shell and source bundle file inside.
172
173=item B<unload>
174
175End of bundle specific shell.
176
177=item B<list>
178
179List loaded bundle.
180
181=item B<source> F<bundle-file>
182
183Source bundle in current shell.
184Environment could not be unload...
185
186=item B<status>
187
188Indicates whether we are in a bundle or not.
189
190=item B<help>
191
192Usage line.
193
194=back
195
196
197=head1 EXAMPLE
198
199Which is better : load or source ?
200
201=head2 Sub-Shell
202
203Load and Unload command must be enclose.
204In an interactive shell,
205it's very important to unload every bundle loaded!
206
207 bundle load intel/2011.7
208   ifort -v
209 bundle unload
210
211Load command in better in interactive shell.
212
213If use in a script file,
214you just have to remenber that load start a new shell,
215and unload just exit it (but not work in script file)
216
217 bundle load intel/2011.7 <<'END_BUNDLE'
218   ifort -v
219 END_BUNDLE
220
221It's better to use quote around END_BUNDLE,
222current shell will not evaluate next command before passing them to sub-shell !
223
224Maybe it's then simpler to use source command and integrated sub-shell
225
226 ( bundle source intel/2011.7
227   ifort -v
228 )
229
230=head2 Source
231
232Source command just source shell script in current environment.
233No unload is possible.
234
235 bundle source intel/2011.7
236
237Source command is better in batch script
238because you have don't have to unload in current case.
239
240
241=head1 SEE ALSO
242
243sysprofile, module
244
245
246=head1 AUTHORS
247
248Written by Gabriel Moreau, Grenoble - France
249
250
251=head1 LICENSE AND COPYRIGHT
252
253GPL version 2 or later
254
255Copyright (C) 2011-2012 Gabriel Moreau / LEGI - CNRS UMR 5519 - France
Note: See TracBrowser for help on using the repository browser.