Skip to content

Instantly share code, notes, and snippets.

@emrcftci
Last active November 9, 2023 15:38
Show Gist options
  • Save emrcftci/0072ecdae6be33773cb5a7f47a38fa1e to your computer and use it in GitHub Desktop.
Save emrcftci/0072ecdae6be33773cb5a7f47a38fa1e to your computer and use it in GitHub Desktop.
Shitty script to generate storyboard names.
#!/bin/sh
# https://gist.github.com/cci-emciftci/0072ecdae6be33773cb5a7f47a38fa1e
# Internal Field Separator(IFS) doc
# https://www.baeldung.com/linux/ifs-shell-variable
echo 'Checking changes...'
# Line number in UIStoryboard+Additions.swift to be changed.
LINE_TO_BE_CHANGED=24
# Insert new line with " case ..." in UIStoryboard+Additions.
# $1: Lowercased name of the newly created Scene.
# $2: UIStoryboard+Additions path to insert new line.
function insert_line {
sed -i "" "$LINE_TO_BE_CHANGED a \
case $1
" $2
}
# List of newly created files with comma separated format.
PATHS=$(git --no-pager diff --name-only HEAD | tr '\n' ',' | sed 's/\(.*\),/\1 /')
# Save default Interan Field Separator to set again after splitting.
DEFAULT_IFS=$IFS
# Change default Internal Field Separator to newline ','.
IFS=','
# Split newly created file list from ','.
FILES=(${PATHS})
# Re-assign default IFS.
IFS=$DEFAULT_IFS
# Newly created ViewController if there is.
CREATED_CONTROLLER=''
# Newly created lowercased Scene name.
CREATED_SCENE_PATH=''
# Additions name.
ADDITION='UIStoryboard+Additions.swift'
# Iterate over created file list.
for (( i=0; i<${#FILES[@]}; i++ ))
do
# Check if there is a newly created ViewController.
if [[ ${FILES[$i]} == *"ViewController"* ]]; then
echo "A ViewController detected!"
CREATED_CONTROLLER=${FILES[$i]}
SCENE=(${CREATED_CONTROLLER//'ViewController.swift'/})
CREATED_SCENE_PATH=$(echo "$SCENE" | tr '[:upper:]' '[:lower:]')
ADDITION_PATH=$(find .. -name "${ADDITION}")
# Change directory to the addition's.
ADDITION_DIR=$(dirname ${ADDITION_PATH})
cd ${ADDITION_DIR}
pwd
# Sample case to check if the file already contains.
SAMPLE_CASE=" case ${CREATED_SCENE_PATH##*/}
"
# If the created view controller has Scenes/ in its path.
case "${SCENE}" in
*Scenes/*)
# If additions file already contains the case.
if ! grep -q "$SAMPLE_CASE" "$ADDITION"; then
echo 'Generating names...'
insert_line "${CREATED_SCENE_PATH##*/}" "${ADDITION}"
echo 'Generating names finished!'
else
echo 'File contains the case!'
fi
;;*)
echo 'It is not a scene file.'
esac
fi
done
echo 'Exiting...'
exit
@emrcftci
Copy link
Author

emrcftci commented Jan 1, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment