From caa75b5fcfb89bc113bec7b8174e916419302b9f Mon Sep 17 00:00:00 2001 From: numzero Date: Fri, 3 Apr 2026 16:42:21 +0300 Subject: [PATCH] add init script --- README.md | 10 ++++---- init.sh | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) create mode 100755 init.sh diff --git a/README.md b/README.md index 4c819e4..5949dc3 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ This program is intended as a starting point rather than a dependency, so just c * clone the repo: `git clone https://gitea.noteuclid.ru/noteuclid/qt-wgpu-skel.git` * or, download and unpack the [tarball](https://gitea.noteuclid.ru/noteuclid/qt-wgpu-skel/archive/master.tar.gz). 2. Rename the directory to your liking -3. Change the name in all files: - * `PROJECT NAME` to the user-visible project name, - * `PROJECT-NAME` to the technical project name (ASCII, no spaces), - * `PROJECT_NAME` to the same with dashes (`-`) replaced with underscores (`_`), - * `PROJECTNAME` to the same in PascalCase. +3. Run `init.sh` with the project name, like: + * `./init.sh 'My FANCY Project'`, or + * `./init.sh 'My FANCY Project' 'fancy-project'`. + +Running `init.sh` without arguments prints the help. If you want to use Qt5, change the version in both `CMakeLists.txt`, as well as the KF version. If you don’t want to depend on KDE, remove the ECM and KF5 dependencies, as well as `KColorCombo inBackground` from the UI. You’ll need to replace `inBackground->color()` with something else though. diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..450f39c --- /dev/null +++ b/init.sh @@ -0,0 +1,69 @@ +#!/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 + echo "Technical name not specified; trying to generate from the visible name" >&2 + techname=$( + echo -n $name | + LC_ALL=C tr ' ' '-' | + LC_ALL=C tr -d -c '[:alnum:]-_' | + LC_ALL=C tr '[:upper:]' '[:lower:]' | + cat + ) +fi + +echo -n $techname | LC_ALL=C grep '[^0-9A-Za-z_-]' && { echo 'Technical name must be ASCII alphanumeric (a-z, 0-9, also _ and - are allowed); got:' $techname; exit 2; } 2>&1 +echo -n $techname | LC_ALL=C grep -q '^[A-Za-z]' || { echo 'Technical name must start with an ASCII letter (a-z); got:' $techname; 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 (a-z, 0-9); got:' $techname; exit 2; } 2>&1 + +echo "Project name: $name" +echo "Internal name: $techname" + +# 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/&/\&/g; s//\>/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"