Last active
July 16, 2018 09:57
-
-
Save jdtournier/1550eb4a830239e1cd278a962634679f to your computer and use it in GitHub Desktop.
bash script to simplify handling of MRtrix3 modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ $# != 2 ]]; then | |
cat <<EOD | |
usage: ./module action target | |
where: | |
- action is one of: install update remove | |
- target is the name of the module, either as its full URL, or in the format | |
user/repo. When the install action is used, the user/repo format can be | |
used only if the module is hosted on GitHub, and will be fetched via HTTPS. | |
Examples: | |
./module install https://github.com/user/mymodule | |
./module install user/mymodule | |
./module update user/mymodule | |
./module install [email protected]/other/private_module.git | |
./module update other/private_module | |
EOD | |
exit 1 | |
fi | |
# figure out full URL and short name from single argument: | |
module_URL=$2 | |
module_name=$2 | |
module_name=${module_name%.git} | |
module_name=$(echo $module_name | grep -o '[^/:]*/[^/]*$') | |
if [[ $module_URL == $module_name ]]; then | |
module_URL='https://github.com/'$module_name | |
fi | |
# handle action argument: | |
case $1 in | |
install) | |
if [[ -d modules/$module_name ]]; then | |
echo 'ERROR: module "'$module_name'" already exists - maybe you mean ./module update?' | |
exit 1 | |
fi | |
( | |
set -e | |
echo "## fetching $module_URL into modules/$module_name..." | |
git clone $module_URL modules/$module_name | |
cd modules/$module_name | |
echo '## building module "'$module_name'"...' | |
../../../build | |
echo '## adding module "'$module_name'" to PATH:' | |
../../../set_path | |
) | |
if [[ $? != 0 ]]; then | |
echo 'ERROR occurred - cleaning up...' | |
rm -rf modules/$module_name | |
fi | |
;; | |
remove) | |
if [[ ! -d modules/$module_name ]]; then | |
echo 'ERROR: no such module "'$module_name'"' | |
exit 1 | |
fi | |
echo '## removing module "'$module_name'"...' | |
rm -rf modules/$module_name | |
;; | |
update) | |
( | |
set -e | |
echo '## updating modules "'$module_name'"...' | |
cd modules/$module_name | |
git pull | |
echo '## building module "'$module_name'"...' | |
../../../build | |
echo '## ensure module "'$module_name'" is in PATH:' | |
../../../set_path | |
) | |
;; | |
*) | |
echo 'no such action "'$1'"' | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment