summaryrefslogtreecommitdiff
path: root/Meta/new-project.sh
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-08-12 15:26:12 +0000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-08-13 21:12:17 +0430
commit1d2bde24030d7a71dd117a0772dc224f0344b364 (patch)
tree94dacbde5e508db44dfe828f6cfd93f8194d78d6 /Meta/new-project.sh
parentd456af1cd311ab4895778bc138e6a7d9bac4a897 (diff)
downloadserenity-1d2bde24030d7a71dd117a0772dc224f0344b364.zip
Meta: Add new-project.sh :^)
This script will instantiate a HackStudio template into a project on the host. It currently supports all templates used by HackStudio. To avoid having to maintain compatibility between other shells and the Serenity shell in the postcreate scripts, we build the Serenity shell with Lagom and use that to run the script.
Diffstat (limited to 'Meta/new-project.sh')
-rwxr-xr-xMeta/new-project.sh100
1 files changed, 100 insertions, 0 deletions
diff --git a/Meta/new-project.sh b/Meta/new-project.sh
new file mode 100755
index 0000000000..be60745732
--- /dev/null
+++ b/Meta/new-project.sh
@@ -0,0 +1,100 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+script_path="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
+cd "${script_path}/.."
+
+script_name="$(basename "$0")"
+
+function list_templates() {
+ echo "Available templates:"
+ for file in ./Base/res/devel/templates/*.ini; do
+ printf ' %s - ' "$(basename "${file%%.ini}")"
+ awk -F "=" '/Description/ { print $2 }' "$file"
+ done
+}
+
+function usage() {
+ local return_code=${1:-0}
+
+ cat <<EOF
+Usage: $script_name TEMPLATE DESTINATION
+
+Instantiates a HackStudio template into a project at the given destination.
+The last component of the destination path is used as the project name, and must
+be a valid identifier (but hyphens are allowed and will be converted into
+underscores).
+
+Parameters:
+ TEMPLATE - The HackStudio template to use.
+ DESTINATION - The destination directory.
+
+$(list_templates)
+EOF
+ exit "$return_code"
+}
+
+while [[ $# -ge 1 ]]; do
+ case "$1" in
+ -h|--help) usage;;
+ -*) echo "$script_name: unknown parameter $1"; usage 1;;
+ *) break;;
+ esac
+done
+
+[[ $# -ne 2 ]] && echo "$script_name: takes exactly 2 parameters" && usage 1
+
+TEMPLATE="$1"
+DESTINATION="$2"
+
+TEMPLATE_SOURCE_DIRECTORY="./Base/res/devel/templates/$TEMPLATE"
+TEMPLATE_INI="./Base/res/devel/templates/$TEMPLATE.ini"
+TEMPLATE_POSTCREATE="./Base/res/devel/templates/$TEMPLATE.postcreate"
+
+if [[ ! -f "$TEMPLATE_INI" ]]; then
+ echo "$script_name: unknown template \"$TEMPLATE\"."
+ list_templates
+ exit 1
+fi
+
+PROJECT_NAME="$(basename "$DESTINATION")"
+
+if ! echo "$PROJECT_NAME" | grep -q '^[a-zA-Z][a-zA-Z0-9_\-]*$'; then
+ echo "$script_name: The destination directory name contains invalid characters."
+ echo "The destination directory name must be a valid identifier, with the exception of hyphens."
+ exit 1
+fi
+
+if [[ -d "$DESTINATION" ]]; then
+ echo "$script_name: Path already exists: $DESTINATION"
+ echo "Refusing to overwrite it."
+ exit 1
+fi
+
+sh="Build/lagom/shell"
+
+if [[ ! -x $sh ]]; then
+ echo "Building the Serenity shell, please wait..."
+ Meta/serenity.sh build lagom shell_lagom
+fi
+
+echo "Instantiating template \"$TEMPLATE\" at \"$DESTINATION\"..."
+mkdir -p "$DESTINATION"
+
+if [[ -d "$TEMPLATE_SOURCE_DIRECTORY" ]]; then
+ printf ' Copying template contents... '
+ cp -R "$TEMPLATE_SOURCE_DIRECTORY"/* "$DESTINATION"
+ echo "OK"
+fi
+
+if [[ -f "$TEMPLATE_POSTCREATE" ]]; then
+ printf ' Running postcreate script... '
+
+ namespace_safe_name="${PROJECT_NAME//-/_}"
+ $sh "$TEMPLATE_POSTCREATE" "$PROJECT_NAME" "$(realpath "$DESTINATION")" "$namespace_safe_name"
+
+ echo "OK"
+fi
+
+echo "Project created successfully at $(realpath "$DESTINATION")."