source: trunk/bundle/bundle.bash @ 60

Last change on this file since 60 was 60, checked in by g7moreau, 9 years ago
  • Add example in doc
File size: 5.7 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      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      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      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      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      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      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      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
133and it's possible to export shell functions which is not possible width C<module>
134(remember, C<bundle> just source shell file).
135
136C<bundle> launch a new shell (sub-shell) at load and exit it at unload.
137It's all!
138
139No...
140In interactive mode, it's save the current history so this one can be use in the sub-shell.
141The same thing is done at the end of the sub-shell,
142history is then reload in the master shell.
143For the user point of vu, it's like there is only one shell!
144
145But be carrefull width variable,
146only export variable could be use in sub-shell.
147
148
149=head1 COMMAND
150
151=over 12
152
153=item B<avail>
154
155List all available bundle.
156Bundle are search in two specific path:
157F<${HOME}/.config/bundle/source.d> and F</etc/bundle/source.d>
158
159Bundle are just shell script with the shell name as extension
160(C<.bash> for C<bash> script).
161Bundle are first search in user folder.
162This allows the user to overloaded a system bundle.
163
164=item B<show> F<bundle-file>
165
166Show a small description of the bundle file if available...
167Format is done width the command C<pod2text>.
168Documentation can be written at the end of the script after a last command C<return> for C<bash>.
169
170=item B<load> F<bundle-file>
171
172Start a new shell and source bundle file inside.
173
174=item B<unload>
175
176End of bundle specific shell.
177
178=item B<list>
179
180List loaded bundle.
181
182=item B<source> F<bundle-file>
183
184Source bundle in current shell.
185Environment could not be unload...
186
187=item B<status>
188
189Indicates whether we are in a bundle or not.
190
191=item B<help>
192
193Usage line.
194
195=back
196
197
198=head1 EXAMPLE
199
200Which is better : load or source ?
201
202=head2 Sub-Shell
203
204Load and Unload command must be enclose.
205In a shell script,
206it's very important to unload every bundle loaded!
207
208 bundle load intel/2011.7
209   ifort -v
210 bundle unload
211
212Load command in better in interactive shell.
213If use in a script file,
214you can indent your code.
215
216=head2 Source
217
218Source command just source shell script in current environment.
219No unload is possible.
220
221 bundle source intel/2011.7
222
223Source command is better in batch script
224because you have don't have to unload in current case.
225
226
227=head1 SEE ALSO
228
229sysprofile, module
230
231
232=head1 AUTHORS
233
234Written by Gabriel Moreau, Grenoble - France
235
236
237=head1 LICENSE AND COPYRIGHT
238
239GPL version 2 or later
240
241Copyright (C) 2011-2012 Gabriel Moreau / LEGI - CNRS UMR 5519 - France
Note: See TracBrowser for help on using the repository browser.