qt-wgpu-skel/init.sh

76 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
# set -x # debug
set -e # abort on errors
cd "${0%/*}" # where this script is located
usage() {
{
echo "Usage:"
echo
echo " ./init.sh 'Project Name' [ internal-name ]"
echo
echo "Project name may be anything you like, in any language, with spaces (but not other control characters), etc."
echo
echo "Technical name must be ASCII alphanumeric (a-z, 0-9, also _ and - are allowed), preferably all-lowercase. If omitted, it will be generated from the project name (but that only works if the latter is ASCII too)."
echo
echo "Running this script with no arguments shows this message"
} | fold -s 2>&1
}
name="$1"
[ -n "$name" ] || { usage; exit 0; } 2>&1
echo -n "$name" | LC_ALL=C grep '[[:cntrl:]]' && { echo 'Project name must not contain any control characters'; exit 2; } 2>&1
name="$(echo -n $name)" # normalize whitespace
techname="$2"
if [ -z "$techname" ]; then
techname=$(
echo -n $name |
LC_ALL=C tr ' ' '-' |
LC_ALL=C tr -d -c '[:alnum:]-_' |
LC_ALL=C tr '[:upper:]' '[:lower:]' |
cat
)
echo "Technical name not specified; autogenerated: \"$techname\"" >&2
fi
echo -n $techname | LC_ALL=C grep '[^0-9A-Za-z_-]' && { echo 'Technical name must be ASCII alphanumeric'; exit 2; } 2>&1
echo -n $techname | LC_ALL=C grep -q '^[A-Za-z]' || { echo 'Technical name must start with an ASCII letter'; exit 2; } 2>&1
echo -n $techname | LC_ALL=C grep -q '[A-Za-z0-9]$' || { echo 'Technical name must end with an ASCII letter or digit'; exit 2; } 2>&1
echo "Project name: $name"
echo "Internal name: $techname"
if ! [ -e .git ]; then
alias git='echo skipping:'
fi
# detach from the skeleton repository
# oldurl="$(git remote -v get-url origin)"
git remote remove origin
# remove this script from the project (but keep on disk for now)
git rm --cached init.sh
# escape HTML special characters (&, <, and >), but also sed special characters (& again, \, and /)
htmlname=$(echo -n "$name" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s:[\\/&]:\\&:g')
sed -i "s/PROJECT NAME/$htmlname/" ui/src/main_window.ui
# escape Rust special characters (" and \; don't worry of the others as they are unlikely enough and easy to fix manually), and also sed special characters (&, \, and /)
quotname=$(echo -n "$name" | sed 's/"/\\"/g; s:[\\/&]:\\&:g')
sed -i "s/PROJECT NAME/$quotname/" src/main.rs
# $techname is alphanumeric, nothing to escape
grep -l -r PROJECT-NAME | xargs sed -i "s/PROJECT-NAME/$techname/g"
# replace - with _ to form a valid snake_case identifier
ident=$(echo -n $techname | LC_ALL=C tr '-' '_')
grep -l -r PROJECT_NAME | xargs sed -i "s/PROJECT_NAME/$ident/g"
# black magic to convert snake_case to PascalCase
pascal=$(echo -n $ident | LC_ALL=C tr '_' '\n' | LC_ALL=C awk -v FIELDWIDTHS='1 1000' -v ORS='' '{print toupper($1) $2}')
grep -l -r PROJECTNAME | xargs sed -i "s/PROJECTNAME/$pascal/g"
git commit -a -m "MORPH INTO: $name"