From 5c6505ce38a198c26fb28b3e043af47b1566f4a1 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Mon, 11 Nov 2024 16:47:38 -0800 Subject: [PATCH] Implements generated setup bash scripts in Crystal for better cross platform compatibility. Fixes #872 --- fixtures/src_template/expected/Procfile.dev | 2 +- .../expected/script/helpers/function_helpers | 67 ------------------- .../script/helpers/function_helpers.cr | 0 .../expected/script/helpers/text_helpers | 32 --------- fixtures/src_template/expected/script/setup | 38 ----------- .../src_template/expected/script/setup.cr | 28 ++++++++ .../src_template/expected/script/system_check | 34 ---------- .../expected/script/system_check.cr | 18 +++++ .../expected/Procfile.dev | 2 +- .../expected/script/helpers/function_helpers | 67 ------------------- .../script/helpers/function_helpers.cr | 0 .../expected/script/helpers/text_helpers | 32 --------- .../expected/script/setup | 38 ----------- .../expected/script/setup.cr | 28 ++++++++ .../expected/script/system_check | 34 ---------- .../expected/script/system_check.cr | 18 +++++ .../expected/Procfile.dev | 2 +- .../expected/script/helpers/function_helpers | 67 ------------------- .../script/helpers/function_helpers.cr | 0 .../expected/script/helpers/text_helpers | 32 --------- .../expected/script/setup | 45 ------------- .../expected/script/setup.cr | 35 ++++++++++ .../expected/script/system_check | 37 ---------- .../expected/script/system_check.cr | 21 ++++++ .../expected/Procfile.dev | 2 +- .../expected/script/helpers/function_helpers | 67 ------------------- .../script/helpers/function_helpers.cr | 0 .../expected/script/helpers/text_helpers | 32 --------- .../expected/script/setup | 45 ------------- .../expected/script/setup.cr | 35 ++++++++++ .../expected/script/system_check | 37 ---------- .../expected/script/system_check.cr | 21 ++++++ src/generators/web.cr | 2 +- src/lucky_cli/src_template.cr | 15 ++--- src/web_app_skeleton/Procfile.dev.ecr | 2 +- .../script/helpers/function_helpers.cr | 33 +++++++++ .../script/helpers/function_helpers.ecr | 67 ------------------- .../script/helpers/text_helpers.ecr | 32 --------- src/web_app_skeleton/script/setup.cr.ecr | 37 ++++++++++ src/web_app_skeleton/script/setup.ecr | 47 ------------- .../script/system_check.cr.ecr | 23 +++++++ src/web_app_skeleton/script/system_check.ecr | 39 ----------- 42 files changed, 308 insertions(+), 905 deletions(-) delete mode 100644 fixtures/src_template/expected/script/helpers/function_helpers create mode 100644 fixtures/src_template/expected/script/helpers/function_helpers.cr delete mode 100644 fixtures/src_template/expected/script/helpers/text_helpers delete mode 100755 fixtures/src_template/expected/script/setup create mode 100644 fixtures/src_template/expected/script/setup.cr delete mode 100755 fixtures/src_template/expected/script/system_check create mode 100644 fixtures/src_template/expected/script/system_check.cr delete mode 100644 fixtures/src_template__api_only/expected/script/helpers/function_helpers create mode 100644 fixtures/src_template__api_only/expected/script/helpers/function_helpers.cr delete mode 100644 fixtures/src_template__api_only/expected/script/helpers/text_helpers delete mode 100755 fixtures/src_template__api_only/expected/script/setup create mode 100644 fixtures/src_template__api_only/expected/script/setup.cr delete mode 100755 fixtures/src_template__api_only/expected/script/system_check create mode 100644 fixtures/src_template__api_only/expected/script/system_check.cr delete mode 100644 fixtures/src_template__generate_auth/expected/script/helpers/function_helpers create mode 100644 fixtures/src_template__generate_auth/expected/script/helpers/function_helpers.cr delete mode 100644 fixtures/src_template__generate_auth/expected/script/helpers/text_helpers delete mode 100755 fixtures/src_template__generate_auth/expected/script/setup create mode 100644 fixtures/src_template__generate_auth/expected/script/setup.cr delete mode 100755 fixtures/src_template__generate_auth/expected/script/system_check create mode 100644 fixtures/src_template__generate_auth/expected/script/system_check.cr delete mode 100644 fixtures/src_template__sec_tester/expected/script/helpers/function_helpers create mode 100644 fixtures/src_template__sec_tester/expected/script/helpers/function_helpers.cr delete mode 100644 fixtures/src_template__sec_tester/expected/script/helpers/text_helpers delete mode 100755 fixtures/src_template__sec_tester/expected/script/setup create mode 100644 fixtures/src_template__sec_tester/expected/script/setup.cr delete mode 100755 fixtures/src_template__sec_tester/expected/script/system_check create mode 100644 fixtures/src_template__sec_tester/expected/script/system_check.cr create mode 100644 src/web_app_skeleton/script/helpers/function_helpers.cr delete mode 100644 src/web_app_skeleton/script/helpers/function_helpers.ecr delete mode 100644 src/web_app_skeleton/script/helpers/text_helpers.ecr create mode 100755 src/web_app_skeleton/script/setup.cr.ecr delete mode 100755 src/web_app_skeleton/script/setup.ecr create mode 100755 src/web_app_skeleton/script/system_check.cr.ecr delete mode 100755 src/web_app_skeleton/script/system_check.ecr diff --git a/fixtures/src_template/expected/Procfile.dev b/fixtures/src_template/expected/Procfile.dev index 9cca7346..dc39e8fe 100644 --- a/fixtures/src_template/expected/Procfile.dev +++ b/fixtures/src_template/expected/Procfile.dev @@ -1,2 +1,2 @@ -system_check: script/system_check && sleep 100000 +system_check: crystal script/system_check.cr web: lucky watch diff --git a/fixtures/src_template/expected/script/helpers/function_helpers b/fixtures/src_template/expected/script/helpers/function_helpers deleted file mode 100644 index 388fa67e..00000000 --- a/fixtures/src_template/expected/script/helpers/function_helpers +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used as helpers -# for various tasks. Read the examples for each one for -# more information. Feel free to put any additional helper -# functions you may need for your app - - -# Returns true if the command $1 is not found -# example: -# if command_not_found "yarn"; then -# echo "no yarn" -# fi -command_not_found() { - ! command -v $1 > /dev/null - return $? -} - -# Returns true if the command $1 is not running -# You must supply the full command to check as an argument -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running" -# fi -command_not_running() { - $1 - if [ $? -ne 0 ]; then - true - else - false - fi -} - -# Returns true if the OS is macOS -# example: -# if is_mac; then -# echo "do mac stuff" -# fi -is_mac() { - if [[ "$OSTYPE" == "darwin"* ]]; then - true - else - false - fi -} - -# Returns true if the OS is linux based -# example: -# if is_linux; then -# echo "do linux stuff" -# fi -is_linux() { - if [[ "$OSTYPE" == "linux"* ]]; then - true - else - false - fi -} - -# Prints error and exit. -# example: -# print_error "Redis is not running. Run it with some_command" -print_error() { - printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n" - printf "${BOLD_RED_COLOR}$1 \n\n" | indent - exit 1 -} diff --git a/fixtures/src_template/expected/script/helpers/function_helpers.cr b/fixtures/src_template/expected/script/helpers/function_helpers.cr new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/src_template/expected/script/helpers/text_helpers b/fixtures/src_template/expected/script/helpers/text_helpers deleted file mode 100644 index 34b77a8c..00000000 --- a/fixtures/src_template/expected/script/helpers/text_helpers +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used to format text, -# and make printing text a little easier. Feel free to put -# any additional functions you need for formatting your shell -# output text. - -# Colors -BOLD_RED_COLOR="\e[1m\e[31m" - -# Indents the text 2 spaces -# example: -# printf "Hello" | indent -indent() { - while read LINE; do - echo " $LINE" || true - done -} - -# Prints out an arrow to your custom notice -# example: -# notice "Installing new magic" -notice() { - printf "\n▸ $1\n" -} - -# Prints out a check mark and Done. -# example: -# print_done -print_done() { - printf "✔ Done\n" | indent -} diff --git a/fixtures/src_template/expected/script/setup b/fixtures/src_template/expected/script/setup deleted file mode 100755 index 5a97531e..00000000 --- a/fixtures/src_template/expected/script/setup +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# Exit if any subcommand fails -set -e -set -o pipefail - -source script/helpers/text_helpers - - -notice "Running System Check" -./script/system_check -print_done - - -notice "Installing shards" -shards install --ignore-crystal-version | indent - -if [ ! -f ".env" ]; then - notice "No .env found. Creating one." - touch .env - print_done -fi - -notice "Creating the database" -lucky db.create | indent - -notice "Verifying postgres connection" -lucky db.verify_connection | indent - -notice "Migrating the database" -lucky db.migrate | indent - -notice "Seeding the database with required and sample records" -lucky db.seed.required_data | indent -lucky db.seed.sample_data | indent - -print_done -notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template/expected/script/setup.cr b/fixtures/src_template/expected/script/setup.cr new file mode 100644 index 00000000..d253f88f --- /dev/null +++ b/fixtures/src_template/expected/script/setup.cr @@ -0,0 +1,28 @@ +require "./helpers/*" + +notice "Running System Check" + +require "./system_check" + +print_done + + +notice "Installing shards" +run_command "shards", "install" + +if !File.exists?(".env") + notice "No .env found. Creating one." + File.touch ".env" + print_done +end + +notice "Setting up the database" + +run_command "lucky", "db.setup" + +notice "Seeding the database with required and sample records" +run_command "lucky", "db.seed.required_data" +run_command "lucky", "db.seed.sample_data" + +print_done +notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template/expected/script/system_check b/fixtures/src_template/expected/script/system_check deleted file mode 100755 index eea2533a..00000000 --- a/fixtures/src_template/expected/script/system_check +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash - -source script/helpers/text_helpers -source script/helpers/function_helpers - -# Use this script to check the system for required tools and process that your app needs. -# A few helper functions are provided to make writing bash a little easier. See the -# script/helpers/function_helpers file for more examples. -# -# A few examples you might use here: -# * 'lucky db.verify_connection' to test postgres can be connected -# * Checking that elasticsearch, redis, or postgres is installed and/or booted -# * Note: Booting additional processes for things like mail, background jobs, etc... -# should go in your Procfile.dev. - - -if command_not_found "createdb"; then - MSG="Please install the postgres CLI tools, then try again." - if is_mac; then - MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html." - fi - MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions." - - print_error "$MSG" -fi - - -## CUSTOM PRE-BOOT CHECKS ## -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running." -# fi - - diff --git a/fixtures/src_template/expected/script/system_check.cr b/fixtures/src_template/expected/script/system_check.cr new file mode 100644 index 00000000..49007e34 --- /dev/null +++ b/fixtures/src_template/expected/script/system_check.cr @@ -0,0 +1,18 @@ +require "./helpers/*" + +# Use this script to check the system for required tools and process that your app needs. +# A few helper functions are provided to keep the code simple. See the +# script/helpers/function_helpers.cr file for more examples. +# +# A few examples you might use here: +# * 'lucky db.verify_connection' to test postgres can be connected +# * Checking that elasticsearch, redis, or postgres is installed and/or booted +# * Note: Booting additional processes for things like mail, background jobs, etc... +# should go in your Procfile.dev. + + +## CUSTOM PRE-BOOT CHECKS ## +# example: +# if command_not_running "redis-cli", "ping" +# print_error "Redis is not running." +# end diff --git a/fixtures/src_template__api_only/expected/Procfile.dev b/fixtures/src_template__api_only/expected/Procfile.dev index 9cca7346..dc39e8fe 100644 --- a/fixtures/src_template__api_only/expected/Procfile.dev +++ b/fixtures/src_template__api_only/expected/Procfile.dev @@ -1,2 +1,2 @@ -system_check: script/system_check && sleep 100000 +system_check: crystal script/system_check.cr web: lucky watch diff --git a/fixtures/src_template__api_only/expected/script/helpers/function_helpers b/fixtures/src_template__api_only/expected/script/helpers/function_helpers deleted file mode 100644 index 388fa67e..00000000 --- a/fixtures/src_template__api_only/expected/script/helpers/function_helpers +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used as helpers -# for various tasks. Read the examples for each one for -# more information. Feel free to put any additional helper -# functions you may need for your app - - -# Returns true if the command $1 is not found -# example: -# if command_not_found "yarn"; then -# echo "no yarn" -# fi -command_not_found() { - ! command -v $1 > /dev/null - return $? -} - -# Returns true if the command $1 is not running -# You must supply the full command to check as an argument -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running" -# fi -command_not_running() { - $1 - if [ $? -ne 0 ]; then - true - else - false - fi -} - -# Returns true if the OS is macOS -# example: -# if is_mac; then -# echo "do mac stuff" -# fi -is_mac() { - if [[ "$OSTYPE" == "darwin"* ]]; then - true - else - false - fi -} - -# Returns true if the OS is linux based -# example: -# if is_linux; then -# echo "do linux stuff" -# fi -is_linux() { - if [[ "$OSTYPE" == "linux"* ]]; then - true - else - false - fi -} - -# Prints error and exit. -# example: -# print_error "Redis is not running. Run it with some_command" -print_error() { - printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n" - printf "${BOLD_RED_COLOR}$1 \n\n" | indent - exit 1 -} diff --git a/fixtures/src_template__api_only/expected/script/helpers/function_helpers.cr b/fixtures/src_template__api_only/expected/script/helpers/function_helpers.cr new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/src_template__api_only/expected/script/helpers/text_helpers b/fixtures/src_template__api_only/expected/script/helpers/text_helpers deleted file mode 100644 index 34b77a8c..00000000 --- a/fixtures/src_template__api_only/expected/script/helpers/text_helpers +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used to format text, -# and make printing text a little easier. Feel free to put -# any additional functions you need for formatting your shell -# output text. - -# Colors -BOLD_RED_COLOR="\e[1m\e[31m" - -# Indents the text 2 spaces -# example: -# printf "Hello" | indent -indent() { - while read LINE; do - echo " $LINE" || true - done -} - -# Prints out an arrow to your custom notice -# example: -# notice "Installing new magic" -notice() { - printf "\n▸ $1\n" -} - -# Prints out a check mark and Done. -# example: -# print_done -print_done() { - printf "✔ Done\n" | indent -} diff --git a/fixtures/src_template__api_only/expected/script/setup b/fixtures/src_template__api_only/expected/script/setup deleted file mode 100755 index 5a97531e..00000000 --- a/fixtures/src_template__api_only/expected/script/setup +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# Exit if any subcommand fails -set -e -set -o pipefail - -source script/helpers/text_helpers - - -notice "Running System Check" -./script/system_check -print_done - - -notice "Installing shards" -shards install --ignore-crystal-version | indent - -if [ ! -f ".env" ]; then - notice "No .env found. Creating one." - touch .env - print_done -fi - -notice "Creating the database" -lucky db.create | indent - -notice "Verifying postgres connection" -lucky db.verify_connection | indent - -notice "Migrating the database" -lucky db.migrate | indent - -notice "Seeding the database with required and sample records" -lucky db.seed.required_data | indent -lucky db.seed.sample_data | indent - -print_done -notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__api_only/expected/script/setup.cr b/fixtures/src_template__api_only/expected/script/setup.cr new file mode 100644 index 00000000..d253f88f --- /dev/null +++ b/fixtures/src_template__api_only/expected/script/setup.cr @@ -0,0 +1,28 @@ +require "./helpers/*" + +notice "Running System Check" + +require "./system_check" + +print_done + + +notice "Installing shards" +run_command "shards", "install" + +if !File.exists?(".env") + notice "No .env found. Creating one." + File.touch ".env" + print_done +end + +notice "Setting up the database" + +run_command "lucky", "db.setup" + +notice "Seeding the database with required and sample records" +run_command "lucky", "db.seed.required_data" +run_command "lucky", "db.seed.sample_data" + +print_done +notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__api_only/expected/script/system_check b/fixtures/src_template__api_only/expected/script/system_check deleted file mode 100755 index eea2533a..00000000 --- a/fixtures/src_template__api_only/expected/script/system_check +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash - -source script/helpers/text_helpers -source script/helpers/function_helpers - -# Use this script to check the system for required tools and process that your app needs. -# A few helper functions are provided to make writing bash a little easier. See the -# script/helpers/function_helpers file for more examples. -# -# A few examples you might use here: -# * 'lucky db.verify_connection' to test postgres can be connected -# * Checking that elasticsearch, redis, or postgres is installed and/or booted -# * Note: Booting additional processes for things like mail, background jobs, etc... -# should go in your Procfile.dev. - - -if command_not_found "createdb"; then - MSG="Please install the postgres CLI tools, then try again." - if is_mac; then - MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html." - fi - MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions." - - print_error "$MSG" -fi - - -## CUSTOM PRE-BOOT CHECKS ## -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running." -# fi - - diff --git a/fixtures/src_template__api_only/expected/script/system_check.cr b/fixtures/src_template__api_only/expected/script/system_check.cr new file mode 100644 index 00000000..49007e34 --- /dev/null +++ b/fixtures/src_template__api_only/expected/script/system_check.cr @@ -0,0 +1,18 @@ +require "./helpers/*" + +# Use this script to check the system for required tools and process that your app needs. +# A few helper functions are provided to keep the code simple. See the +# script/helpers/function_helpers.cr file for more examples. +# +# A few examples you might use here: +# * 'lucky db.verify_connection' to test postgres can be connected +# * Checking that elasticsearch, redis, or postgres is installed and/or booted +# * Note: Booting additional processes for things like mail, background jobs, etc... +# should go in your Procfile.dev. + + +## CUSTOM PRE-BOOT CHECKS ## +# example: +# if command_not_running "redis-cli", "ping" +# print_error "Redis is not running." +# end diff --git a/fixtures/src_template__generate_auth/expected/Procfile.dev b/fixtures/src_template__generate_auth/expected/Procfile.dev index d9a8173a..17ab4309 100644 --- a/fixtures/src_template__generate_auth/expected/Procfile.dev +++ b/fixtures/src_template__generate_auth/expected/Procfile.dev @@ -1,3 +1,3 @@ -system_check: script/system_check && sleep 100000 +system_check: crystal script/system_check.cr web: lucky watch --reload-browser assets: yarn watch diff --git a/fixtures/src_template__generate_auth/expected/script/helpers/function_helpers b/fixtures/src_template__generate_auth/expected/script/helpers/function_helpers deleted file mode 100644 index 388fa67e..00000000 --- a/fixtures/src_template__generate_auth/expected/script/helpers/function_helpers +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used as helpers -# for various tasks. Read the examples for each one for -# more information. Feel free to put any additional helper -# functions you may need for your app - - -# Returns true if the command $1 is not found -# example: -# if command_not_found "yarn"; then -# echo "no yarn" -# fi -command_not_found() { - ! command -v $1 > /dev/null - return $? -} - -# Returns true if the command $1 is not running -# You must supply the full command to check as an argument -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running" -# fi -command_not_running() { - $1 - if [ $? -ne 0 ]; then - true - else - false - fi -} - -# Returns true if the OS is macOS -# example: -# if is_mac; then -# echo "do mac stuff" -# fi -is_mac() { - if [[ "$OSTYPE" == "darwin"* ]]; then - true - else - false - fi -} - -# Returns true if the OS is linux based -# example: -# if is_linux; then -# echo "do linux stuff" -# fi -is_linux() { - if [[ "$OSTYPE" == "linux"* ]]; then - true - else - false - fi -} - -# Prints error and exit. -# example: -# print_error "Redis is not running. Run it with some_command" -print_error() { - printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n" - printf "${BOLD_RED_COLOR}$1 \n\n" | indent - exit 1 -} diff --git a/fixtures/src_template__generate_auth/expected/script/helpers/function_helpers.cr b/fixtures/src_template__generate_auth/expected/script/helpers/function_helpers.cr new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/src_template__generate_auth/expected/script/helpers/text_helpers b/fixtures/src_template__generate_auth/expected/script/helpers/text_helpers deleted file mode 100644 index 34b77a8c..00000000 --- a/fixtures/src_template__generate_auth/expected/script/helpers/text_helpers +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used to format text, -# and make printing text a little easier. Feel free to put -# any additional functions you need for formatting your shell -# output text. - -# Colors -BOLD_RED_COLOR="\e[1m\e[31m" - -# Indents the text 2 spaces -# example: -# printf "Hello" | indent -indent() { - while read LINE; do - echo " $LINE" || true - done -} - -# Prints out an arrow to your custom notice -# example: -# notice "Installing new magic" -notice() { - printf "\n▸ $1\n" -} - -# Prints out a check mark and Done. -# example: -# print_done -print_done() { - printf "✔ Done\n" | indent -} diff --git a/fixtures/src_template__generate_auth/expected/script/setup b/fixtures/src_template__generate_auth/expected/script/setup deleted file mode 100755 index bf0bc317..00000000 --- a/fixtures/src_template__generate_auth/expected/script/setup +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# Exit if any subcommand fails -set -e -set -o pipefail - -source script/helpers/text_helpers - - -notice "Running System Check" -./script/system_check -print_done - -notice "Installing node dependencies" -yarn install --no-progress | indent - -notice "Compiling assets" -yarn dev | indent - -print_done - -notice "Installing shards" -shards install --ignore-crystal-version | indent - -if [ ! -f ".env" ]; then - notice "No .env found. Creating one." - touch .env - print_done -fi - -notice "Creating the database" -lucky db.create | indent - -notice "Verifying postgres connection" -lucky db.verify_connection | indent - -notice "Migrating the database" -lucky db.migrate | indent - -notice "Seeding the database with required and sample records" -lucky db.seed.required_data | indent -lucky db.seed.sample_data | indent - -print_done -notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__generate_auth/expected/script/setup.cr b/fixtures/src_template__generate_auth/expected/script/setup.cr new file mode 100644 index 00000000..401ae5dd --- /dev/null +++ b/fixtures/src_template__generate_auth/expected/script/setup.cr @@ -0,0 +1,35 @@ +require "./helpers/*" + +notice "Running System Check" + +require "./system_check" + +print_done + +notice "Installing node dependencies" +run_command "yarn", "install", "--no-progress" + +notice "Compiling assets" +run_command "yarn", "dev" + +print_done + +notice "Installing shards" +run_command "shards", "install" + +if !File.exists?(".env") + notice "No .env found. Creating one." + File.touch ".env" + print_done +end + +notice "Setting up the database" + +run_command "lucky", "db.setup" + +notice "Seeding the database with required and sample records" +run_command "lucky", "db.seed.required_data" +run_command "lucky", "db.seed.sample_data" + +print_done +notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__generate_auth/expected/script/system_check b/fixtures/src_template__generate_auth/expected/script/system_check deleted file mode 100755 index c27c926b..00000000 --- a/fixtures/src_template__generate_auth/expected/script/system_check +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash - -source script/helpers/text_helpers -source script/helpers/function_helpers - -# Use this script to check the system for required tools and process that your app needs. -# A few helper functions are provided to make writing bash a little easier. See the -# script/helpers/function_helpers file for more examples. -# -# A few examples you might use here: -# * 'lucky db.verify_connection' to test postgres can be connected -# * Checking that elasticsearch, redis, or postgres is installed and/or booted -# * Note: Booting additional processes for things like mail, background jobs, etc... -# should go in your Procfile.dev. - -if command_not_found "yarn"; then - print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." -fi - -if command_not_found "createdb"; then - MSG="Please install the postgres CLI tools, then try again." - if is_mac; then - MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html." - fi - MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions." - - print_error "$MSG" -fi - - -## CUSTOM PRE-BOOT CHECKS ## -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running." -# fi - - diff --git a/fixtures/src_template__generate_auth/expected/script/system_check.cr b/fixtures/src_template__generate_auth/expected/script/system_check.cr new file mode 100644 index 00000000..a43fb34b --- /dev/null +++ b/fixtures/src_template__generate_auth/expected/script/system_check.cr @@ -0,0 +1,21 @@ +require "./helpers/*" + +# Use this script to check the system for required tools and process that your app needs. +# A few helper functions are provided to keep the code simple. See the +# script/helpers/function_helpers.cr file for more examples. +# +# A few examples you might use here: +# * 'lucky db.verify_connection' to test postgres can be connected +# * Checking that elasticsearch, redis, or postgres is installed and/or booted +# * Note: Booting additional processes for things like mail, background jobs, etc... +# should go in your Procfile.dev. + +if command_not_found "yarn" + print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." +end + +## CUSTOM PRE-BOOT CHECKS ## +# example: +# if command_not_running "redis-cli", "ping" +# print_error "Redis is not running." +# end diff --git a/fixtures/src_template__sec_tester/expected/Procfile.dev b/fixtures/src_template__sec_tester/expected/Procfile.dev index d9a8173a..17ab4309 100644 --- a/fixtures/src_template__sec_tester/expected/Procfile.dev +++ b/fixtures/src_template__sec_tester/expected/Procfile.dev @@ -1,3 +1,3 @@ -system_check: script/system_check && sleep 100000 +system_check: crystal script/system_check.cr web: lucky watch --reload-browser assets: yarn watch diff --git a/fixtures/src_template__sec_tester/expected/script/helpers/function_helpers b/fixtures/src_template__sec_tester/expected/script/helpers/function_helpers deleted file mode 100644 index 388fa67e..00000000 --- a/fixtures/src_template__sec_tester/expected/script/helpers/function_helpers +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used as helpers -# for various tasks. Read the examples for each one for -# more information. Feel free to put any additional helper -# functions you may need for your app - - -# Returns true if the command $1 is not found -# example: -# if command_not_found "yarn"; then -# echo "no yarn" -# fi -command_not_found() { - ! command -v $1 > /dev/null - return $? -} - -# Returns true if the command $1 is not running -# You must supply the full command to check as an argument -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running" -# fi -command_not_running() { - $1 - if [ $? -ne 0 ]; then - true - else - false - fi -} - -# Returns true if the OS is macOS -# example: -# if is_mac; then -# echo "do mac stuff" -# fi -is_mac() { - if [[ "$OSTYPE" == "darwin"* ]]; then - true - else - false - fi -} - -# Returns true if the OS is linux based -# example: -# if is_linux; then -# echo "do linux stuff" -# fi -is_linux() { - if [[ "$OSTYPE" == "linux"* ]]; then - true - else - false - fi -} - -# Prints error and exit. -# example: -# print_error "Redis is not running. Run it with some_command" -print_error() { - printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n" - printf "${BOLD_RED_COLOR}$1 \n\n" | indent - exit 1 -} diff --git a/fixtures/src_template__sec_tester/expected/script/helpers/function_helpers.cr b/fixtures/src_template__sec_tester/expected/script/helpers/function_helpers.cr new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/src_template__sec_tester/expected/script/helpers/text_helpers b/fixtures/src_template__sec_tester/expected/script/helpers/text_helpers deleted file mode 100644 index 34b77a8c..00000000 --- a/fixtures/src_template__sec_tester/expected/script/helpers/text_helpers +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used to format text, -# and make printing text a little easier. Feel free to put -# any additional functions you need for formatting your shell -# output text. - -# Colors -BOLD_RED_COLOR="\e[1m\e[31m" - -# Indents the text 2 spaces -# example: -# printf "Hello" | indent -indent() { - while read LINE; do - echo " $LINE" || true - done -} - -# Prints out an arrow to your custom notice -# example: -# notice "Installing new magic" -notice() { - printf "\n▸ $1\n" -} - -# Prints out a check mark and Done. -# example: -# print_done -print_done() { - printf "✔ Done\n" | indent -} diff --git a/fixtures/src_template__sec_tester/expected/script/setup b/fixtures/src_template__sec_tester/expected/script/setup deleted file mode 100755 index bf0bc317..00000000 --- a/fixtures/src_template__sec_tester/expected/script/setup +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# Exit if any subcommand fails -set -e -set -o pipefail - -source script/helpers/text_helpers - - -notice "Running System Check" -./script/system_check -print_done - -notice "Installing node dependencies" -yarn install --no-progress | indent - -notice "Compiling assets" -yarn dev | indent - -print_done - -notice "Installing shards" -shards install --ignore-crystal-version | indent - -if [ ! -f ".env" ]; then - notice "No .env found. Creating one." - touch .env - print_done -fi - -notice "Creating the database" -lucky db.create | indent - -notice "Verifying postgres connection" -lucky db.verify_connection | indent - -notice "Migrating the database" -lucky db.migrate | indent - -notice "Seeding the database with required and sample records" -lucky db.seed.required_data | indent -lucky db.seed.sample_data | indent - -print_done -notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__sec_tester/expected/script/setup.cr b/fixtures/src_template__sec_tester/expected/script/setup.cr new file mode 100644 index 00000000..401ae5dd --- /dev/null +++ b/fixtures/src_template__sec_tester/expected/script/setup.cr @@ -0,0 +1,35 @@ +require "./helpers/*" + +notice "Running System Check" + +require "./system_check" + +print_done + +notice "Installing node dependencies" +run_command "yarn", "install", "--no-progress" + +notice "Compiling assets" +run_command "yarn", "dev" + +print_done + +notice "Installing shards" +run_command "shards", "install" + +if !File.exists?(".env") + notice "No .env found. Creating one." + File.touch ".env" + print_done +end + +notice "Setting up the database" + +run_command "lucky", "db.setup" + +notice "Seeding the database with required and sample records" +run_command "lucky", "db.seed.required_data" +run_command "lucky", "db.seed.sample_data" + +print_done +notice "Run 'lucky dev' to start the app" diff --git a/fixtures/src_template__sec_tester/expected/script/system_check b/fixtures/src_template__sec_tester/expected/script/system_check deleted file mode 100755 index c27c926b..00000000 --- a/fixtures/src_template__sec_tester/expected/script/system_check +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash - -source script/helpers/text_helpers -source script/helpers/function_helpers - -# Use this script to check the system for required tools and process that your app needs. -# A few helper functions are provided to make writing bash a little easier. See the -# script/helpers/function_helpers file for more examples. -# -# A few examples you might use here: -# * 'lucky db.verify_connection' to test postgres can be connected -# * Checking that elasticsearch, redis, or postgres is installed and/or booted -# * Note: Booting additional processes for things like mail, background jobs, etc... -# should go in your Procfile.dev. - -if command_not_found "yarn"; then - print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." -fi - -if command_not_found "createdb"; then - MSG="Please install the postgres CLI tools, then try again." - if is_mac; then - MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html." - fi - MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions." - - print_error "$MSG" -fi - - -## CUSTOM PRE-BOOT CHECKS ## -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running." -# fi - - diff --git a/fixtures/src_template__sec_tester/expected/script/system_check.cr b/fixtures/src_template__sec_tester/expected/script/system_check.cr new file mode 100644 index 00000000..a43fb34b --- /dev/null +++ b/fixtures/src_template__sec_tester/expected/script/system_check.cr @@ -0,0 +1,21 @@ +require "./helpers/*" + +# Use this script to check the system for required tools and process that your app needs. +# A few helper functions are provided to keep the code simple. See the +# script/helpers/function_helpers.cr file for more examples. +# +# A few examples you might use here: +# * 'lucky db.verify_connection' to test postgres can be connected +# * Checking that elasticsearch, redis, or postgres is installed and/or booted +# * Note: Booting additional processes for things like mail, background jobs, etc... +# should go in your Procfile.dev. + +if command_not_found "yarn" + print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." +end + +## CUSTOM PRE-BOOT CHECKS ## +# example: +# if command_not_running "redis-cli", "ping" +# print_error "Redis is not running." +# end diff --git a/src/generators/web.cr b/src/generators/web.cr index 8e5432fe..f2959cde 100644 --- a/src/generators/web.cr +++ b/src/generators/web.cr @@ -74,7 +74,7 @@ class LuckyCli::Generators::Web #{green_arrow} cd into #{project_location.colorize(:green)} #{green_arrow} check database settings in #{"config/database.cr".colorize(:green)} - #{green_arrow} run #{"script/setup".colorize(:green)} + #{green_arrow} run #{"crystal script/setup.cr".colorize(:green)} #{green_arrow} run #{"lucky dev".colorize(:green)} to start the server TEXT diff --git a/src/lucky_cli/src_template.cr b/src/lucky_cli/src_template.cr index 01fb2e55..985d7222 100644 --- a/src/lucky_cli/src_template.cr +++ b/src/lucky_cli/src_template.cr @@ -109,19 +109,14 @@ class SrcTemplate end end root_dir.add_folder("script") do |script_dir| - script_dir.add_file("setup", 0o755) do |io| - ECR.embed("#{__DIR__}/../web_app_skeleton/script/setup.ecr", io) + script_dir.add_file("setup.cr") do |io| + ECR.embed("#{__DIR__}/../web_app_skeleton/script/setup.cr.ecr", io) end - script_dir.add_file("system_check", 0o755) do |io| - ECR.embed("#{__DIR__}/../web_app_skeleton/script/system_check.ecr", io) + script_dir.add_file("system_check.cr") do |io| + ECR.embed("#{__DIR__}/../web_app_skeleton/script/system_check.cr.ecr", io) end script_dir.add_folder("helpers") do |helpers_dir| - helpers_dir.add_file("function_helpers") do |io| - ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/function_helpers.ecr", io) - end - helpers_dir.add_file("text_helpers") do |io| - ECR.embed("#{__DIR__}/../web_app_skeleton/script/helpers/text_helpers.ecr", io) - end + helpers_dir.add_file("function_helpers.cr") end end root_dir.insert_folder("spec", spec_folder) diff --git a/src/web_app_skeleton/Procfile.dev.ecr b/src/web_app_skeleton/Procfile.dev.ecr index b9d3f58d..53563d99 100644 --- a/src/web_app_skeleton/Procfile.dev.ecr +++ b/src/web_app_skeleton/Procfile.dev.ecr @@ -1,4 +1,4 @@ -system_check: script/system_check && sleep 100000 +system_check: crystal script/system_check.cr <%- if api_only? -%> web: lucky watch <%- else -%> diff --git a/src/web_app_skeleton/script/helpers/function_helpers.cr b/src/web_app_skeleton/script/helpers/function_helpers.cr new file mode 100644 index 00000000..df384f10 --- /dev/null +++ b/src/web_app_skeleton/script/helpers/function_helpers.cr @@ -0,0 +1,33 @@ +require "colorize" + +# These are helper methods provided to help keep your code +# clean. Add new methods, or alter these as needed. + + +def notice(message : String) : Nil + puts "\n▸ #{message}" +end + +def print_done : Nil + puts "✔ Done" +end + +def print_error(message : String) : Nil + puts "There is a problem with your system setup:\n".colorize.red.bold + puts "#{message}\n".colorize.red.bold + Process.exit(1) +end + +def command_not_found(command : String) : Bool + Process.find_executable(command).nil? +end + +def command_not_running(command : String, *args) : Bool + output = IO::Memory.new + code = Process.run(command, args, output: output).exit_code + code > 0 +end + +def run_command(command : String, *args) : Nil + Process.run(command, args, output: STDOUT, error: STDERR, input: STDIN) +end diff --git a/src/web_app_skeleton/script/helpers/function_helpers.ecr b/src/web_app_skeleton/script/helpers/function_helpers.ecr deleted file mode 100644 index 388fa67e..00000000 --- a/src/web_app_skeleton/script/helpers/function_helpers.ecr +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used as helpers -# for various tasks. Read the examples for each one for -# more information. Feel free to put any additional helper -# functions you may need for your app - - -# Returns true if the command $1 is not found -# example: -# if command_not_found "yarn"; then -# echo "no yarn" -# fi -command_not_found() { - ! command -v $1 > /dev/null - return $? -} - -# Returns true if the command $1 is not running -# You must supply the full command to check as an argument -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running" -# fi -command_not_running() { - $1 - if [ $? -ne 0 ]; then - true - else - false - fi -} - -# Returns true if the OS is macOS -# example: -# if is_mac; then -# echo "do mac stuff" -# fi -is_mac() { - if [[ "$OSTYPE" == "darwin"* ]]; then - true - else - false - fi -} - -# Returns true if the OS is linux based -# example: -# if is_linux; then -# echo "do linux stuff" -# fi -is_linux() { - if [[ "$OSTYPE" == "linux"* ]]; then - true - else - false - fi -} - -# Prints error and exit. -# example: -# print_error "Redis is not running. Run it with some_command" -print_error() { - printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n" - printf "${BOLD_RED_COLOR}$1 \n\n" | indent - exit 1 -} diff --git a/src/web_app_skeleton/script/helpers/text_helpers.ecr b/src/web_app_skeleton/script/helpers/text_helpers.ecr deleted file mode 100644 index 34b77a8c..00000000 --- a/src/web_app_skeleton/script/helpers/text_helpers.ecr +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -# This file contains a set of functions used to format text, -# and make printing text a little easier. Feel free to put -# any additional functions you need for formatting your shell -# output text. - -# Colors -BOLD_RED_COLOR="\e[1m\e[31m" - -# Indents the text 2 spaces -# example: -# printf "Hello" | indent -indent() { - while read LINE; do - echo " $LINE" || true - done -} - -# Prints out an arrow to your custom notice -# example: -# notice "Installing new magic" -notice() { - printf "\n▸ $1\n" -} - -# Prints out a check mark and Done. -# example: -# print_done -print_done() { - printf "✔ Done\n" | indent -} diff --git a/src/web_app_skeleton/script/setup.cr.ecr b/src/web_app_skeleton/script/setup.cr.ecr new file mode 100755 index 00000000..a83d4ce2 --- /dev/null +++ b/src/web_app_skeleton/script/setup.cr.ecr @@ -0,0 +1,37 @@ +require "./helpers/*" + +notice "Running System Check" + +require "./system_check" + +print_done + +<%- if browser? -%> +notice "Installing node dependencies" +run_command "yarn", "install", "--no-progress" + +notice "Compiling assets" +run_command "yarn", "dev" + +print_done +<%- end -%> + +notice "Installing shards" +run_command "shards", "install" + +if !File.exists?(".env") + notice "No .env found. Creating one." + File.touch ".env" + print_done +end + +notice "Setting up the database" + +run_command "lucky", "db.setup" + +notice "Seeding the database with required and sample records" +run_command "lucky", "db.seed.required_data" +run_command "lucky", "db.seed.sample_data" + +print_done +notice "Run 'lucky dev' to start the app" diff --git a/src/web_app_skeleton/script/setup.ecr b/src/web_app_skeleton/script/setup.ecr deleted file mode 100755 index a266946e..00000000 --- a/src/web_app_skeleton/script/setup.ecr +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env bash - -# Exit if any subcommand fails -set -e -set -o pipefail - -source script/helpers/text_helpers - - -notice "Running System Check" -./script/system_check -print_done - -<%- if browser? -%> -notice "Installing node dependencies" -yarn install --no-progress | indent - -notice "Compiling assets" -yarn dev | indent - -print_done -<%- end -%> - -notice "Installing shards" -shards install --ignore-crystal-version | indent - -if [ ! -f ".env" ]; then - notice "No .env found. Creating one." - touch .env - print_done -fi - -notice "Creating the database" -lucky db.create | indent - -notice "Verifying postgres connection" -lucky db.verify_connection | indent - -notice "Migrating the database" -lucky db.migrate | indent - -notice "Seeding the database with required and sample records" -lucky db.seed.required_data | indent -lucky db.seed.sample_data | indent - -print_done -notice "Run 'lucky dev' to start the app" diff --git a/src/web_app_skeleton/script/system_check.cr.ecr b/src/web_app_skeleton/script/system_check.cr.ecr new file mode 100755 index 00000000..fdec0204 --- /dev/null +++ b/src/web_app_skeleton/script/system_check.cr.ecr @@ -0,0 +1,23 @@ +require "./helpers/*" + +# Use this script to check the system for required tools and process that your app needs. +# A few helper functions are provided to keep the code simple. See the +# script/helpers/function_helpers.cr file for more examples. +# +# A few examples you might use here: +# * 'lucky db.verify_connection' to test postgres can be connected +# * Checking that elasticsearch, redis, or postgres is installed and/or booted +# * Note: Booting additional processes for things like mail, background jobs, etc... +# should go in your Procfile.dev. + +<%- if browser? -%> +if command_not_found "yarn" + print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." +end +<%- end -%> + +## CUSTOM PRE-BOOT CHECKS ## +# example: +# if command_not_running "redis-cli", "ping" +# print_error "Redis is not running." +# end diff --git a/src/web_app_skeleton/script/system_check.ecr b/src/web_app_skeleton/script/system_check.ecr deleted file mode 100755 index 88726eee..00000000 --- a/src/web_app_skeleton/script/system_check.ecr +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash - -source script/helpers/text_helpers -source script/helpers/function_helpers - -# Use this script to check the system for required tools and process that your app needs. -# A few helper functions are provided to make writing bash a little easier. See the -# script/helpers/function_helpers file for more examples. -# -# A few examples you might use here: -# * 'lucky db.verify_connection' to test postgres can be connected -# * Checking that elasticsearch, redis, or postgres is installed and/or booted -# * Note: Booting additional processes for things like mail, background jobs, etc... -# should go in your Procfile.dev. - -<%- if browser? -%> -if command_not_found "yarn"; then - print_error "Yarn is not installed\n See https://yarnpkg.com/lang/en/docs/install/ for install instructions." -fi -<%- end -%> - -if command_not_found "createdb"; then - MSG="Please install the postgres CLI tools, then try again." - if is_mac; then - MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html." - fi - MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions." - - print_error "$MSG" -fi - - -## CUSTOM PRE-BOOT CHECKS ## -# example: -# if command_not_running "redis-cli ping"; then -# print_error "Redis is not running." -# fi - -