Created
January 13, 2015 18:45
-
-
Save perifer/4dcb51fe7d99fe1b9b21 to your computer and use it in GitHub Desktop.
Improved prepare.sh for https://github.com/jdevera/sub/
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
#!/usr/bin/env bash | |
chmod_files() | |
{ | |
for file in "$1"/*; do | |
[[ ! -d $file ]] && chmod +x "$file" | |
done | |
} | |
make_new_path() | |
{ | |
local path="$1" | |
local dir="$(dirname "$path")" | |
local base="$(basename "$path" | sed "s/^sub/${SUBNAME}/g")" | |
echo "$dir/$base" | |
} | |
prepare_sub() { | |
local path="$1" | |
local newpath="$(make_new_path "$path")" | |
if [[ -d $path ]]; then | |
for file in "$path"/sub-*; do | |
prepare_sub "$file" | |
done | |
chmod_files "$path" | |
elif [[ -f $path ]]; then | |
sed -i "" "s/sub/$SUBNAME/g;s/_SUB_ROOT/_$ENVNAME/g" "$path" | |
fi | |
mv "$path" "$newpath" | |
} | |
NAME="$1" | |
if [[ -z $NAME ]]; then | |
echo "usage: prepare.sh NAME_OF_YOUR_SUB" >&2 | |
exit 1 | |
fi | |
SUBNAME=$(echo $NAME | tr '[A-Z]' '[a-z]') | |
ENVNAME="$(echo $NAME | tr '[a-z-]' '[A-Z_]')_ROOT" | |
echo "Preparing your '$SUBNAME' sub!" | |
if [[ $NAME != "sub" ]]; then | |
mv bin/sub bin/$SUBNAME | |
for file in **/sub*; do | |
prepare_sub "$file" | |
done | |
chmod_files "libexec" | |
fi | |
rm README.md | |
rm prepare.sh | |
cat << DONEMSG | |
Done! Enjoy your new sub! If you're happy with your sub, run: | |
rm -rf .git | |
git init | |
git add . | |
git commit -m 'Starting off $SUBNAME' | |
./bin/$SUBNAME init | |
You can remove the example when you no longer need it: | |
rm -r libexec/${SUBNAME}-example | |
Made a mistake? Want to make a different sub? Run: | |
git add . | |
git checkout -f | |
Thanks for making a sub! | |
DONEMSG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment