new-project.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. script_path="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
  4. cd "${script_path}/.."
  5. script_name="$(basename "$0")"
  6. function list_templates() {
  7. echo "Available templates:"
  8. for file in ./Base/res/devel/templates/*.ini; do
  9. printf ' %s - ' "$(basename "${file%%.ini}")"
  10. awk -F "=" '/Description/ { print $2 }' "$file"
  11. done
  12. }
  13. function usage() {
  14. local return_code=${1:-0}
  15. cat <<EOF
  16. Usage: $script_name TEMPLATE DESTINATION
  17. Instantiates a HackStudio template into a project at the given destination.
  18. The last component of the destination path is used as the project name, and must
  19. be a valid identifier (but hyphens are allowed and will be converted into
  20. underscores).
  21. Parameters:
  22. TEMPLATE - The HackStudio template to use.
  23. DESTINATION - The destination directory.
  24. $(list_templates)
  25. EOF
  26. exit "$return_code"
  27. }
  28. while [[ $# -ge 1 ]]; do
  29. case "$1" in
  30. -h|--help) usage;;
  31. -*) echo "$script_name: unknown parameter $1"; usage 1;;
  32. *) break;;
  33. esac
  34. done
  35. [[ $# -ne 2 ]] && echo "$script_name: takes exactly 2 parameters" && usage 1
  36. TEMPLATE="$1"
  37. DESTINATION="$2"
  38. TEMPLATE_SOURCE_DIRECTORY="./Base/res/devel/templates/$TEMPLATE"
  39. TEMPLATE_INI="./Base/res/devel/templates/$TEMPLATE.ini"
  40. TEMPLATE_POSTCREATE="./Base/res/devel/templates/$TEMPLATE.postcreate"
  41. if [[ ! -f "$TEMPLATE_INI" ]]; then
  42. echo "$script_name: unknown template \"$TEMPLATE\"."
  43. list_templates
  44. exit 1
  45. fi
  46. PROJECT_NAME="$(basename "$DESTINATION")"
  47. if ! echo "$PROJECT_NAME" | grep -q '^[a-zA-Z][a-zA-Z0-9_\-]*$'; then
  48. echo "$script_name: The destination directory name contains invalid characters."
  49. echo "The destination directory name must be a valid identifier, with the exception of hyphens."
  50. exit 1
  51. fi
  52. if [[ -d "$DESTINATION" ]]; then
  53. echo "$script_name: Path already exists: $DESTINATION"
  54. echo "Refusing to overwrite it."
  55. exit 1
  56. fi
  57. sh="Build/lagom/shell"
  58. if [[ ! -x $sh ]]; then
  59. echo "Building the Serenity shell, please wait..."
  60. Meta/serenity.sh build lagom shell_lagom
  61. fi
  62. echo "Instantiating template \"$TEMPLATE\" at \"$DESTINATION\"..."
  63. mkdir -p "$DESTINATION"
  64. if [[ -d "$TEMPLATE_SOURCE_DIRECTORY" ]]; then
  65. printf ' Copying template contents... '
  66. cp -R "$TEMPLATE_SOURCE_DIRECTORY"/* "$DESTINATION"
  67. echo "OK"
  68. fi
  69. if [[ -f "$TEMPLATE_POSTCREATE" ]]; then
  70. printf ' Running postcreate script... '
  71. namespace_safe_name="${PROJECT_NAME//-/_}"
  72. $sh "$TEMPLATE_POSTCREATE" "$PROJECT_NAME" "$(realpath "$DESTINATION")" "$namespace_safe_name"
  73. echo "OK"
  74. fi
  75. echo "Project created successfully at $(realpath "$DESTINATION")."