Last active
June 15, 2022 17:19
-
-
Save eaceto/743a8bbdb0696be36c2c03142002626f to your computer and use it in GitHub Desktop.
EASwiftLibraryTemplate - Setup Library Script
This file contains 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/sh | |
# Check that script is run from | |
SWIFT_PACKAGE="Package.swift" | |
if [ ! -f "$SWIFT_PACKAGE" ]; then | |
logError "Script should be run from the directory where $SWIFT_PACKAGE exists." | |
logError 'run as: /bin/zsh -c "$(curl -fsSL https://gist.githubusercontent.com/eaceto/743a8bbdb0696be36c2c03142002626f/raw/setup.sh)"' | |
exit -1 | |
fi | |
## Constants | |
DEFINITIONS_FILE="definitions.json" | |
LIBRARY_TEMPLATE_NAME="EASwiftLibraryTemplate" | |
LIBRARY_TEMPLATE_PODSPEC="${LIBRARY_TEMPLATE_NAME}.podspec" | |
LIBRARY_TEMPLATE_TEST_FILE="Tests/${LIBRARY_TEMPLATE_NAME}Tests/StringMaskerTests.swift" | |
LIBRARY_TEMPLATE_RESOURCE_BUNDLE="${LIBRARY_TEMPLATE_NAME}_${LIBRARY_TEMPLATE_NAME}" | |
LIBRARY_TEMPLATE_GITHUB_PAGE_URL="https://eaceto\.github\.io/EASwiftLibraryTemplate/" | |
LIBRARY_TEMPLATE_HTTPS_URL="https://github\.com/eaceto/EASwiftLibraryTemplate\.git" | |
LIBRARY_TEMPLATE_SSH_URL="git@github\.com:eaceto/EASwiftLibraryTemplate\.git" | |
LIBRARY_TEMPLATE_GITHUB_URL="https://github\.com/eaceto/EASwiftLibraryTemplate" | |
LIBRARY_DOCS_ENGINE="jazzy" | |
logProcess() { | |
echo "\033[38;5;208m$* | |
\033[0m" | |
} | |
logError() { | |
echo "\033[38;5;196m$* | |
\033[0m" | |
} | |
echo " | |
\033[38;5;034m @@ | |
\033[38;5;034m @@@@@@ | |
\033[38;5;034m @@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@ | |
\033[38;5;034m @@@@@@ | |
\033[38;5;034m @@ | |
\033[38;5;034m @@@@@@@@@@@@ @@@@@@@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;034m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;226m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;226m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;226m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;226m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;208m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;208m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;208m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;208m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;196m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;196m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;196m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;196m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;129m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;129m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;129m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;129m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;038m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;038m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;038m @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
\033[38;5;038m @@@@@@@@@ @@@@@@@@@ | |
\033[0m" | |
echo "" | |
echo " | |
\033[38;5;196m Library template maintened by Ezequiel (Kimi) Aceto | |
\033[38;5;196m =================================================== | |
\033[0m" | |
# Has swiftlint? | |
export PATH=/usr/local/bin:/opt/homebrew/bin:$PATH | |
SWIFTLINT=$(which swiftlint) | |
if [[ ! -e "${SWIFTLINT}" ]]; then | |
logError "SwiftLint is not present in your environment, download from https://github.com/realm/SwiftLint" | |
exit 1 | |
fi | |
trimString() { | |
local var="$*" | |
# remove leading whitespace characters | |
var="${var#"${var%%[![:space:]]*}"}" | |
# remove trailing whitespace characters | |
var="${var%"${var##*[![:space:]]}"}" | |
echo "$var" | |
} | |
## Set library configuration | |
LIBRARY_ORIGIN="$(git config --get remote.origin.url)" | |
LIBRARY_NAME="" | |
LIBRARY_SSH_URL="" | |
LIBRARY_HTTPS_URL="" | |
LIBRARY_GITHUB_PAGE_URL="" | |
LIBRARY_GITHUB_URL="" | |
LIBRARY_VERSION="" | |
LIBRARY_RESOURCE_BUNDLE="" | |
### Enable (local) git hooks | |
logProcess "Setting up GitHooks..." | |
git config core.hooksPath .githooks | |
### Set repo URL (ssh and https) | |
if [[ $LIBRARY_ORIGIN == git* ]]; then | |
LIBRARY_SSH_URL=$LIBRARY_ORIGIN | |
LIBRARY_HTTPS_URL=$(git remote get-url origin | sed -E 's,^git@([^:]*):/*(.*)$,https://\1/\2,') | |
else | |
LIBRARY_HTTPS_URL=$LIBRARY_ORIGIN | |
LIBRARY_SSH_URL=$(git remote get-url origin | sed -E 's,^https://([^/]*)/(.*)$,git@\1:\2,') | |
fi | |
setopt BASH_REMATCH | |
REPOSITORY_REGEX="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)(.git)*$" | |
if [[ "$LIBRARY_HTTPS_URL" =~ $REPOSITORY_REGEX ]]; then | |
protocol=${BASH_REMATCH[2]} | |
separator=${BASH_REMATCH[3]} | |
hostname=${BASH_REMATCH[4]} | |
user=${BASH_REMATCH[5]} | |
repo=${BASH_REMATCH[6]} | |
github_io_url="https://${user}.github.io/${repo}" | |
suffix=".git" | |
LIBRARY_GITHUB_PAGE_URL="${github_io_url/%$suffix}/" | |
LIBRARY_GITHUB_URL="${LIBRARY_HTTPS_URL/%$suffix}" | |
fi | |
echo " | |
\033[38;5;038mEnter the Library name without spaces (i.e.: MySuperProLibrary). | |
\033[38;5;038mThis will also be the Target and Product name of the Swift library. | |
\033[0m" | |
read LIBRARY_NAME | |
LIBRARY_NAME="$(trimString "$LIBRARY_NAME")" | |
echo " | |
\033[38;5;038mEnter the initial version of your Library in SemVer format (x.y.z) | |
\033[0m" | |
read LIBRARY_VERSION | |
LIBRARY_VERSION="$(trimString "$LIBRARY_VERSION")" | |
echo " | |
\033[38;5;038mDo you want to use 'docs' instead of 'jazzy' as docs generator (BETA) [y/N]? | |
\033[0m" | |
old_stty_cfg=$(stty -g) | |
stty raw -echo | |
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) | |
stty $old_stty_cfg | |
if echo "$answer" | grep -iq "^y" ;then | |
LIBRARY_DOCS_ENGINE="docc" | |
else | |
LIBRARY_DOCS_ENGINE="jazzy" | |
fi | |
## Confirm setup | |
echo " | |
\033[38;5;034m====== Configuration to apply ====== | |
Please verify that the following information is correct | |
\033[38;5;208mLibrary name:\033[0m '$LIBRARY_NAME' | |
\033[38;5;208mLibrary initial version:\033[0m '$LIBRARY_VERSION' | |
\033[38;5;208mLibrary homepage url:\033[0m '$LIBRARY_GITHUB_URL' | |
\033[38;5;208mLibrary repository url (SSH):\033[0m '$LIBRARY_SSH_URL' | |
\033[38;5;208mLibrary repository url (HTTPS):\033[0m '$LIBRARY_HTTPS_URL' | |
\033[38;5;208mLibrary GitHub Page:\033[0m '$LIBRARY_GITHUB_PAGE_URL' | |
\033[38;5;208mDocs using:\033[0m '$LIBRARY_DOCS_ENGINE' | |
\033[38;5;034m==================================== | |
\033[0m" | |
echo "" | |
echo " | |
\033[38;5;034mIs this information correct? [y/N] | |
\033[0m" | |
old_stty_cfg=$(stty -g) | |
stty raw -echo | |
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) | |
stty $old_stty_cfg | |
if echo "$answer" | grep -iq "^y" ;then | |
logProcess "Configuring project" | |
else | |
return 0 | |
fi | |
rm -rf .build/ .swiftpm/ DerivedData/ | |
### Update Package.swift | |
logProcess "Updating $SWIFT_PACKAGE" | |
logProcess "Running: sed -i \"\" \"s;^let libraryName.*$;let libraryName = \"$LIBRARY_NAME\";g\" $SWIFT_PACKAGE" | |
sed -i "" "s;^let libraryName.*$;let libraryName = \"$LIBRARY_NAME\";g" $SWIFT_PACKAGE | |
logProcess "Running: sed -i \"\" \"s;^let libraryVersion.*$;let libraryVersion = \"$LIBRARY_VERSION\";g\" $SWIFT_PACKAGE" | |
sed -i "" "s;^let libraryVersion.*$;let libraryVersion = \"$LIBRARY_VERSION\";g" $SWIFT_PACKAGE | |
### Podspec | |
logProcess "Updating Podspec" | |
logProcess "Running: sed -i \"\" \"s;^libraryName.*;libraryName = \"$LIBRARY_NAME\";g\" $LIBRARY_TEMPLATE_PODSPEC" | |
sed -i "" "s;^libraryName.*;libraryName = \"$LIBRARY_NAME\";g" $LIBRARY_TEMPLATE_PODSPEC | |
logProcess "Running: sed -i \"\" \"s;^libraryVersion.*;libraryVersion = \"$LIBRARY_VERSION\";g\" $LIBRARY_TEMPLATE_PODSPEC" | |
sed -i "" "s;^libraryVersion.*;libraryVersion = \"$LIBRARY_VERSION\";g" $LIBRARY_TEMPLATE_PODSPEC | |
logProcess "Running: sed -i \"\" \"s;^gitRemote.*$;gitRemote = \"$LIBRARY_SSH_URL\";g\" $LIBRARY_TEMPLATE_PODSPEC" | |
sed -i "" "s;^gitRemote.*;gitRemote = \"$LIBRARY_SSH_URL\";g" $LIBRARY_TEMPLATE_PODSPEC | |
logProcess "Running: sed -i \"\" \"s;^gitHomepage.*;gitHomepage = \"$LIBRARY_GITHUB_URL\";g\" $LIBRARY_TEMPLATE_PODSPEC" | |
sed -i "" "s;^gitHomepage.*;gitHomepage = \"$LIBRARY_GITHUB_URL\";g" $LIBRARY_TEMPLATE_PODSPEC | |
### Change podspec name | |
logProcess "Running: git mv $LIBRARY_TEMPLATE_PODSPEC \"$LIBRARY_NAME.podspec\"" | |
git mv $LIBRARY_TEMPLATE_PODSPEC "$LIBRARY_NAME.podspec" | |
logProcess "Running: sed -i \"\" \"s/@testable import ${LIBRARY_TEMPLATE_NAME}/@testable import ${LIBRARY_NAME}/g\" $LIBRARY_TEMPLATE_TEST_FILE" | |
sed -i "" "s/@testable import ${LIBRARY_TEMPLATE_NAME}/@testable import ${LIBRARY_NAME}/g" $LIBRARY_TEMPLATE_TEST_FILE | |
logProcess "Changing resource bundle helper" | |
LIBRARY_RESOURCE_BUNDLE="${LIBRARY_NAME}_${LIBRARY_NAME}" | |
### Change sources directoy name | |
logProcess "Changing source code directory: Sources/$LIBRARY_TEMPLATE_NAME -> Sources/$LIBRARY_NAME" | |
logProcess "Running: git mv -f \"Sources/${LIBRARY_TEMPLATE_NAME}\" \"Sources/${LIBRARY_NAME}\"" | |
git mv -f "Sources/${LIBRARY_TEMPLATE_NAME}" "Sources/${LIBRARY_NAME}" | |
### Change tests directoy name | |
logProcess "Changing tests directory: Tests/${LIBRARY_TEMPLATE_NAME} -> Tests/$LIBRARY_NAME" | |
logProcess "Running: git mv -f \"Tests/${LIBRARY_TEMPLATE_NAME}Tests\" \"Tests/${LIBRARY_NAME}Tests\"" | |
git mv -f "Tests/${LIBRARY_TEMPLATE_NAME}Tests" "Tests/${LIBRARY_NAME}Tests" | |
### Update definitions.json file | |
logProcess "Generating definitions file (definitions.json)" | |
DEFINITIONS_JSON_FMT='{\n\t"name":"%s",\n\t"version":"%s",\n\t"homepage":"%s",\n\t"git":"%s",\n\t"docs_generator":"%s"\n}' | |
DEFINITIONS_JSON=$(printf "$DEFINITIONS_JSON_FMT" "$LIBRARY_NAME" "$LIBRARY_VERSION" "$LIBRARY_HTTPS_URL" "$LIBRARY_SSH_URL" "$LIBRARY_DOCS_ENGINE") | |
echo $DEFINITIONS_JSON > $DEFINITIONS_FILE | |
### Update README | |
logProcess "Saving template README as 'TEMPLATE_README.md'" | |
mv README.md TEMPLATE_README.md | |
logProcess "Copying project README" | |
cp Template/docs/PROJECT_README.md README.md | |
logProcess "Update README.md file" | |
logProcess "Running: sed -i \"\" \"s;${LIBRARY_TEMPLATE_GITHUB_PAGE_URL};${LIBRARY_GITHUB_PAGE_URL};g\" README.md" | |
sed -i "" "s;${LIBRARY_TEMPLATE_GITHUB_PAGE_URL};${LIBRARY_GITHUB_PAGE_URL};g" README.md | |
logProcess "Running: sed -i \"\" \"s;${LIBRARY_TEMPLATE_HTTPS_URL};${LIBRARY_HTTPS_URL};g\" README.md" | |
sed -i "" "s;${LIBRARY_TEMPLATE_HTTPS_URL};${LIBRARY_HTTPS_URL};g" README.md | |
logProcess "Running: sed -i \"\" \"s;${LIBRARY_TEMPLATE_SSH_URL};${LIBRARY_SSH_URL};g\" README.md" | |
sed -i "" "s;${LIBRARY_TEMPLATE_SSH_URL};${LIBRARY_SSH_URL};g" README.md | |
logProcess "Running: sed -i \"\" \"s;${LIBRARY_TEMPLATE_GITHUB_URL};${LIBRARY_GITHUB_URL};g\" README.md" | |
sed -i "" "s;${LIBRARY_TEMPLATE_GITHUB_URL};${LIBRARY_GITHUB_URL};g" README.md | |
logProcess "Running: sed -i \"\" \"s;${LIBRARY_TEMPLATE_NAME};${LIBRARY_NAME};g\" README.md" | |
sed -i "" "s;${LIBRARY_TEMPLATE_NAME};${LIBRARY_NAME};g" README.md | |
logProcess "Running: \"s/(.*\/releases\/tag)\/(.*)\)/\1\/$LIBRARY_VERSION)/g;t\" < README.md > NEW_README.md" | |
sed -E "s/(.*\/releases\/tag)\/(.*)\)/\1\/$LIBRARY_VERSION)/g;t" < README.md > README.draft | |
logProcess "Running: sed -E \"s/(.*\/Latests%20release)-(.*)-(blue.*)/\1-$LIBRARY_VERSION-\3/g;t\" < README.draft > README.md" | |
sed -E "s/(.*\/Latests%20release)-(.*)-(blue.*)/\1-$LIBRARY_VERSION-\3/g;t" < README.draft > README.md | |
rm -rf README.draft | |
### Clean docs/index.md | |
logProcess "Update docs/index.md" | |
logProcess "Running: sed -i \"\" \"s/$LIBRARY_TEMPLATE_NAME/$LIBRARY_NAME/g\" docs/index.md" | |
sed -i "" "s/$LIBRARY_TEMPLATE_NAME/$LIBRARY_NAME/g" "docs/index.md" | |
### Clean CHANGELOG.md | |
logProcess "Cleaning CHANGELOG.md" | |
logProcess "Running: printf \"\" > CHANGELOG.md" | |
printf "" > CHANGELOG.md | |
rm -rf .swiftpm .build DerivedData | |
BUILD_LIBRARY="n" | |
echo " | |
\033[38;5;034mWould you like to build the library in order to check everything is OK? [y/N] | |
\033[0m" | |
old_stty_cfg=$(stty -g) | |
stty raw -echo | |
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) | |
stty $old_stty_cfg | |
if echo "$answer" | grep -iq "^y" ;then | |
BUILD_LIBRARY="y" | |
fi | |
if [[ $BUILD_LIBRARY =~ ^[Yy]$ ]] | |
then | |
logProcess "Running: swift build" | |
swift build | |
echo "" | |
logProcess "Running: swift test" | |
swift test | |
fi | |
COMMIT_CHANGES="n" | |
echo " | |
\033[38;5;034mWould you like to commit these changes? [y/N] | |
\033[0m" | |
old_stty_cfg=$(stty -g) | |
stty raw -echo | |
answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) | |
stty $old_stty_cfg | |
if echo "$answer" | grep -iq "^y" ;then | |
COMMIT_CHANGES="y" | |
fi | |
if [[ $COMMIT_CHANGES =~ ^[Yy]$ ]] | |
then | |
git add --all | |
git commit -m "Setup library as $LIBRARY_NAME" | |
fi | |
echo "" | |
logProcess "Everything is ready!" | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment