[52] | 1 | function 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] | 96 | typeset -fx bundle |
---|
[52] | 97 | |
---|
| 98 | return |
---|
| 99 | |
---|
| 100 | ################################################################ |
---|
| 101 | # Documentation in POD format (like Perl) |
---|
| 102 | ################################################################ |
---|
| 103 | |
---|
| 104 | =head1 NAME |
---|
| 105 | |
---|
| 106 | bundle - 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 | |
---|
| 125 | C<bundle> can load a new environment in current shell |
---|
[59] | 126 | (source) or load it in a sub-shell. |
---|
| 127 | In this second case, environment could be unload! |
---|
[52] | 128 | |
---|
[59] | 129 | This shell function looks furiously at the command C<module>. |
---|
| 130 | It's wanted but is much simpler (no C<tcl>...). |
---|
| 131 | The arguments are quite the same. |
---|
| 132 | There is no magic done on the current environment variables |
---|
| 133 | (remember, C<bundle> just source shell file). |
---|
[52] | 134 | |
---|
[59] | 135 | C<bundle> launch a new shell (sub-shell) at load and exit it at unload. |
---|
| 136 | It's all! |
---|
| 137 | |
---|
| 138 | No... |
---|
| 139 | In interactive mode, it's save the current history so this one can be use in the sub-shell. |
---|
| 140 | The same thing is done at the end of the sub-shell, |
---|
| 141 | history is then reload in the master shell. |
---|
| 142 | For the user point of vu, it's like there is only one shell! |
---|
| 143 | |
---|
[61] | 144 | But be carrefull with variable, |
---|
[59] | 145 | only export variable could be use in sub-shell. |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | =head1 COMMAND |
---|
| 149 | |
---|
| 150 | =over 12 |
---|
| 151 | |
---|
| 152 | =item B<avail> |
---|
| 153 | |
---|
| 154 | List all available bundle. |
---|
| 155 | Bundle are search in two specific path: |
---|
| 156 | F<${HOME}/.config/bundle/source.d> and F</etc/bundle/source.d> |
---|
| 157 | |
---|
| 158 | Bundle are just shell script with the shell name as extension |
---|
| 159 | (C<.bash> for C<bash> script). |
---|
| 160 | Bundle are first search in user folder. |
---|
| 161 | This allows the user to overloaded a system bundle. |
---|
| 162 | |
---|
| 163 | =item B<show> F<bundle-file> |
---|
| 164 | |
---|
| 165 | Show a small description of the bundle file if available... |
---|
| 166 | Format is done width the command C<pod2text>. |
---|
| 167 | Documentation 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 | |
---|
| 171 | Start a new shell and source bundle file inside. |
---|
| 172 | |
---|
| 173 | =item B<unload> |
---|
| 174 | |
---|
| 175 | End of bundle specific shell. |
---|
| 176 | |
---|
| 177 | =item B<list> |
---|
| 178 | |
---|
| 179 | List loaded bundle. |
---|
| 180 | |
---|
| 181 | =item B<source> F<bundle-file> |
---|
| 182 | |
---|
| 183 | Source bundle in current shell. |
---|
| 184 | Environment could not be unload... |
---|
| 185 | |
---|
| 186 | =item B<status> |
---|
| 187 | |
---|
| 188 | Indicates whether we are in a bundle or not. |
---|
| 189 | |
---|
| 190 | =item B<help> |
---|
| 191 | |
---|
| 192 | Usage line. |
---|
| 193 | |
---|
| 194 | =back |
---|
| 195 | |
---|
| 196 | |
---|
[60] | 197 | =head1 EXAMPLE |
---|
| 198 | |
---|
| 199 | Which is better : load or source ? |
---|
| 200 | |
---|
| 201 | =head2 Sub-Shell |
---|
| 202 | |
---|
| 203 | Load and Unload command must be enclose. |
---|
[109] | 204 | In an interactive shell, |
---|
[60] | 205 | it's very important to unload every bundle loaded! |
---|
| 206 | |
---|
| 207 | bundle load intel/2011.7 |
---|
| 208 | ifort -v |
---|
| 209 | bundle unload |
---|
| 210 | |
---|
| 211 | Load command in better in interactive shell. |
---|
[109] | 212 | |
---|
[60] | 213 | If use in a script file, |
---|
[109] | 214 | you just have to remenber that load start a new shell, |
---|
| 215 | and 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 | |
---|
| 221 | It's better to use quote around END_BUNDLE, |
---|
| 222 | current shell will not evaluate next command before passing them to sub-shell ! |
---|
| 223 | |
---|
| 224 | Maybe 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 | |
---|
| 232 | Source command just source shell script in current environment. |
---|
| 233 | No unload is possible. |
---|
| 234 | |
---|
| 235 | bundle source intel/2011.7 |
---|
| 236 | |
---|
| 237 | Source command is better in batch script |
---|
| 238 | because you have don't have to unload in current case. |
---|
| 239 | |
---|
| 240 | |
---|
[52] | 241 | =head1 SEE ALSO |
---|
| 242 | |
---|
| 243 | sysprofile, module |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | =head1 AUTHORS |
---|
| 247 | |
---|
| 248 | Written by Gabriel Moreau, Grenoble - France |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | =head1 LICENSE AND COPYRIGHT |
---|
| 252 | |
---|
| 253 | GPL version 2 or later |
---|
| 254 | |
---|
[59] | 255 | Copyright (C) 2011-2012 Gabriel Moreau / LEGI - CNRS UMR 5519 - France |
---|