Skip to content

Instantly share code, notes, and snippets.

@jonnitto
Created November 25, 2017 19:04
Show Gist options
  • Save jonnitto/8fa13c00905891f2f9b3a9b40d152e12 to your computer and use it in GitHub Desktop.
Save jonnitto/8fa13c00905891f2f9b3a9b40d152e12 to your computer and use it in GitHub Desktop.
#!/bin/bash
# To be able to use the script it must be made executable:
# chmod +x CopyBoilerplate.sh
#
# Call of the script:
# ~/CopyBoilerplate.sh
cloneInSameFolder=false
# Clone via SSH or HTTPS?
cloneType="HTTPS"
# Name of your Boilerplate (username/name)
BoilerplateRepo="jonnitto/Boilerplate"
yellowStyle="$(tput setaf 11)"
headlineStyle="$(tput bold)$(tput setaf 15)"
redStyle="$(tput setaf 9)"
greenStyle="$(tput setaf 10)"
resetStyle="$(tput sgr0)"
prompt() {
echo
echo $1
echo -n "» "
read inputvalue
}
echoStatus() {
echo
echo
echo "-----[" $1 "]-----"
echo
}
echoError() {
echo
echo
echo ${redStyle}$1${resetStyle}
echo
}
exitInstall() {
echo
echo
echoStatus "${yellowStyle}No actions were taken${resetStyle}"
echo
echo
exit 1
}
testFolder() {
if [ "$cloneInSameFolder" = true ] ; then
if [ "$(ls -A)" ] ; then
echoError "The current folder must be empty"
exit 0
fi
fi
}
setRepoName() { Please specify the URL of the empty repository
prompt "Please enter the URL of the ${yellowStyle}empty repository${resetStyle} (e. g. https://github.com/jonnitto/WebsiteCom)"
case $inputvalue in
exit|Exit|EXIT|abort|stop|ABORT|STOP) exitInstall ;;
*)
# String prüfen
if echo "$inputvalue" | grep -q "\.git$" ; then
echoError "Please specify the URL without .git"
setRepoName
elif echo "$inputvalue" | grep -q "\/$" ; then
echoError "Please specify name without a slash (/) at the end"
setRepoName
elif echo "$inputvalue" | grep -q "^https:\/\/github\.com" ; then
export REPO_URL=${inputvalue}
export REPO_URL_PLAIN="${inputvalue/https:\/\/github.com\//}"
export REPO_NAME="${REPO_URL_PLAIN#*\/}"
if [ "$cloneInSameFolder" = true ] ; then
export REPO_FOLDER="."
else
export REPO_FOLDER=$REPO_NAME
fi
else
echoError "Please provide complete URL (including https://github.com)"
setRepoName
fi
;;
esac
}
copyBoilerplate() {
if [ "$cloneType" = "HTTPS" ] ; then
git clone https://github.com/${BoilerplateRepo}.git ${REPO_FOLDER}
elif [ "$cloneType" = "SSH" ] ; then
git clone [email protected]:${BoilerplateRepo}.git ${REPO_FOLDER}
fi
cd ${REPO_FOLDER} && rm -rf .git
}
initGit() {
git init
if [ "$cloneType" = "HTTPS" ] ; then
git remote add origin https://github.com/${REPO_URL_PLAIN}.git
elif [ "$cloneType" = "SSH" ] ; then
git remote add origin [email protected]:${REPO_URL_PLAIN}.git
fi
git fetch
if [ "$(find .git/objects -type f)" ] ; then
echoError "Please specify empty repository"
if [ "$cloneInSameFolder" = true ] ; then
rm -rf ..?* .[!.]* *
else
cd ..
rm -rf ${REPO_FOLDER}
fi
exit 0
else
git add .
git commit -m "TASK: Copy from Boilerplate"
git push -u origin master
fi
}
installComponents() {
composer update
yarn
}
cloneFeedback() {
echo
echo
echo
echoStatus "${greenStyle}Copy boilerplate with ${cloneType}${resetStyle}"
}
cloneActions() {
clear
testFolder
cloneFeedback
setRepoName
copyBoilerplate
initGit
installComponents
}
cloneActions
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment