source: trunk/bundle/bundle.bash @ 167

Last change on this file since 167 was 109, checked in by g7moreau, 10 years ago
  • Commit some very old doc !
File size: 6.0 KB
RevLine 
[52]1function bundle {
[53]2   local ext=$(basename ${SHELL})
[56]3   local folder="${HOME}/.config/bundle/source.d /etc/bundle/source.d"
[52]4
5   case "$1" in
[61]6      av|avail)
[55]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
[52]15         ;;
16
[61]17      lo|load)
[52]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
[54]24            export _bundle_status="${_bundle_status}:$2"
[59]25            [ -n "$PS1" ] && history -w
[52]26            $VERBOSE && echo "Begin bundle: ${_bundle_name}"
27            ${SHELL} --rcfile ${pack}
28            $VERBOSE && echo "End bundle: ${_bundle_name}"
[59]29            [ -n "$PS1" ] && history -r
[52]30            unset _bundle_name
31            let _bundle_level--
32            [ ${_bundle_level} -eq 0 ] && unset _bundle_level
[54]33            export _bundle_status=$(echo "${_bundle_status}" | sed 's/:[^:]*$//')
34            [ "${_bundle_status}" == "" ] && unset _bundle_status
[52]35         else
36            echo "unable to load bundle file: $2"
37            echo "Usage: bundle load bundle-file"
38         fi
39         ;;
40
[61]41      un|unload)
[52]42         if [ ${_bundle_level} > 0 ]
43         then
[59]44            [ -n "$PS1" ] && history -w
[52]45            exit
46         else
47            echo "Error: not a bundle environment!"
48         fi
49         ;;
50
[61]51      sh|show)
[58]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
[61]62      li|list)
[54]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
[61]71      st|status)
[54]72         if [ ${_bundle_level} > 0 ]
73         then
74            echo "bundle environment active"
75         else
76            echo "not a bundle environment"
77         fi
78         ;;
79
[61]80      so|source)
[52]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      *)
[58]91         echo "Usage: bundle list|avail|load|unload|show|status|source|help"
[52]92         ;;
93   esac
94   }
95
[53]96typeset -fx bundle
[52]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
[56]110 bundle avail
[58]111 bundle show bundle-file
[52]112
113 bundle load bundle-file
114 bundle unload
[56]115 bundle list
[52]116
[59]117 bundle source bundle-file
[52]118
[56]119 bundle status
120
[52]121 bundle help
122
123=head1 DESCRIPTION
124
125C<bundle> can load a new environment in current shell
[59]126(source) or load it in a sub-shell.
127In this second case, environment could be unload!
[52]128
[59]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).
[52]134
[59]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
[61]144But be carrefull with variable,
[59]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
[60]197=head1 EXAMPLE
198
199Which is better : load or source ?
200
201=head2 Sub-Shell
202
203Load and Unload command must be enclose.
[109]204In an interactive shell,
[60]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.
[109]212
[60]213If use in a script file,
[109]214you just have to remenber that load start a new shell,
215and unload just exit it (but not work in script file)
[60]216
[109]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
[60]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
[52]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
[59]255Copyright (C) 2011-2012 Gabriel Moreau / LEGI - CNRS UMR 5519 - France
Note: See TracBrowser for help on using the repository browser.