diff --git a/README.md b/README.md index ab2a88f..5320c2e 100755 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ This way, I don't have to worry about the things I've mentioned, plus I gain the - Choose to [**setup**](./scripts/setup.sh) the lara stacker first, which will install everything necessary and eventually create a [[done-setup.flag](./done-setup.flag)] file in the directory. -- Then choose to [**create**](./scripts/create.sh) your first stacked project and continue onwards... +- Then choose to either create a [**stacked**](./scripts/create.sh) laravel application or just a [**raw**](./scripts/create_raw.sh) one and continue onwards... That's it. You'll have your first project accessible in the end (displaying the site's URL too). JUST be PATIENT! `:)` diff --git a/lara-stacker.sh b/lara-stacker.sh index 4d5cc6b..1bb5415 100755 --- a/lara-stacker.sh +++ b/lara-stacker.sh @@ -8,7 +8,7 @@ current_version="???" if [[ -f $changelog_dir ]]; then current_version=$(grep -E "^## v[0-9]+" $changelog_dir | head -1 | awk '{print $2}') fi -echo -e "-=|[ Lara-Stacker [$current_version] ]|=-\n" +echo -e "-=|[ LARA-STACKER [$current_version] ]|=-\n" # * =========== # * Validation @@ -42,7 +42,7 @@ fi # Double check the environment variables env_example_vars=$(grep -oE '^[A-Z_]+=' .env.example | sort) env_vars=$(grep -oE '^[A-Z_]+=' .env | sort) -diff <(echo "$env_example_vars") <(echo "$env_vars") &> /dev/null +diff <(echo "$env_example_vars") <(echo "$env_vars") &>/dev/null if [ $? -ne 0 ]; then prompt "Aborted for different environment variables." "Ensure that [.env.example] variables match [.env] ones." true false fi @@ -55,6 +55,9 @@ SCRIPTS=( "./scripts/create_raw.sh" "./scripts/delete.sh" "./scripts/update.sh" + "./scripts/databases/list.sh" + "./scripts/databases/create.sh" + "./scripts/databases/delete.sh" "./scripts/helpers/permit.sh" ) for script in "${SCRIPTS[@]}"; do @@ -114,25 +117,27 @@ counter=0 while true; do counter=$((counter + 1)) - echo -e "-=|[ Lara-Stacker [$current_version] ]|=-\n" + echo -e "-=|[ LARA-STACKER [$current_version] ]|=-\n" echo -e "Supported Stacks:\n" echo -e "- TALL (TailwindCSS, AlpineJS, Livewire, Laravel)\n" echo -e "Available Operations:\n" - options=("1. List Projects" "2. Create Project" "3. Create Raw Project" "4. Delete Project" "5. Exit") + options=("1. Stacked Laravel Projects" "2. Manage MySQL Databases" "3. Create A Raw Laravel Project" "4. Exit") # Conditional options + include_zero=false if [[ -f "/tmp/updated-lara-stacker.flag" ]]; then rm /tmp/updated-lara-stacker.flag update_available=false fi if [ "$update_available" == true ]; then - options+=("6. Download Updates") + options+=("5. Download Updates") fi if [[ ! -f "$lara_stacker_dir/done-setup.flag" ]]; then options+=("0. Initial Setup") + include_zero=true fi options_count=$((${#options[@]} - 1)) @@ -145,7 +150,11 @@ while true; do if [[ $counter -eq 1 && "$1" ]]; then choice="$1" else - read -p "Choose an operation (0-$options_count): " choice + if [ "$include_zero" == true ]; then + read -p "Choose an operation (0-$options_count): " choice + else + read -p "Choose an operation (1-$options_count): " choice + fi fi clear @@ -153,36 +162,99 @@ while true; do case $choice in 0) if [[ -f "$lara_stacker_dir/done-setup.flag" ]]; then - prompt "-=|[ Lara-Stacker [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false + prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false else sudo RAN_MAIN_SCRIPT="true" ./scripts/setup.sh fi ;; 1) - RAN_MAIN_SCRIPT="true" ./scripts/list.sh + while true; do + clear + + echo -e "-=|[ Lara-Stacker |> STACKED PROJECTS ]|=-\n" + + echo -e "Available Operations:\n" + + echo "1. List All Projects" + echo "2. Stack A New Project" + echo "3. Delete A Project" + echo -e "4. Go Back To Main Menu\n" + + read -p "Choose an operation (1-3): " stack_choice + + case $stack_choice in + 1) + RAN_MAIN_SCRIPT="true" ./scripts/list.sh + ;; + 2) + sudo RAN_MAIN_SCRIPT="true" ./scripts/create.sh + ;; + 3) + sudo RAN_MAIN_SCRIPT="true" ./scripts/delete.sh + ;; + 4) + clear + break + ;; + *) + clear + prompt "-=|[ Lara-Stacker |> STACKED PROJECTS ]|=-" "Invalid option! Please type one the of digits in the list..." false false true + ;; + esac + done ;; 2) - sudo RAN_MAIN_SCRIPT="true" ./scripts/create.sh + while true; do + clear + + echo -e "-=|[ Lara-Stacker |> DATABASE MANAGEMENT ]|=-\n" + + echo -e "Available Operations:\n" + + echo "1. List All Databases" + echo "2. Create A Database" + echo "3. Delete A Database" + echo -e "4. Go Back To Main Menu\n" + + read -p "Choose an operation (1-3): " db_choice + + case $db_choice in + 1) + RAN_MAIN_SCRIPT="true" ./scripts/databases/list.sh + ;; + 2) + RAN_MAIN_SCRIPT="true" ./scripts/databases/create.sh + ;; + 3) + RAN_MAIN_SCRIPT="true" ./scripts/databases/delete.sh + ;; + 4) + clear + break + ;; + *) + clear + prompt "-=|[ Lara-Stacker |> DATABASE MANAGEMENT ]|=-" "Invalid option! Please type one the of digits in the list..." false false true + ;; + esac + done ;; 3) sudo RAN_MAIN_SCRIPT="true" ./scripts/create_raw.sh ;; 4) - sudo RAN_MAIN_SCRIPT="true" ./scripts/delete.sh - ;; - 5) echo -e "\nExiting Lara-Stacker...\n" exit 0 ;; - 6) + 5) if [ "$update_available" == false ]; then - prompt "-=|[ Lara-Stacker [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false + prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false else sudo RAN_MAIN_SCRIPT="true" ./scripts/update.sh fi ;; *) - prompt "-=|[ Lara-Stacker [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false + prompt "-=|[ LARA-STACKER [$current_version] ]|=-" "Invalid option! Please type one the of digits in the list..." false false true ;; esac done diff --git a/scripts/create.sh b/scripts/create.sh index 99b688b..4bb801c 100755 --- a/scripts/create.sh +++ b/scripts/create.sh @@ -3,7 +3,7 @@ clear # Status indicator -echo -e "-=|[ Lara-Stacker |> CREATE ]|=-\n" +echo -e "-=|[ Lara-Stacker |> Stacked Projects |> CREATE ]|=-\n" # * =========== # * Validation diff --git a/scripts/databases/create.sh b/scripts/databases/create.sh new file mode 100755 index 0000000..f55c5fa --- /dev/null +++ b/scripts/databases/create.sh @@ -0,0 +1,105 @@ +#!/bin/bash + +clear + +# Status indicator +echo -e "-=|[ Lara-Stacker |> Database Management |> CREATE ]|=-\n" + +# * =========== +# * Validation +# * ========= + +# Check if prompt function exists and source it +function_path="./scripts/functions/prompt.sh" +if [[ ! -f $function_path ]]; then + echo -e "Error: Working directory isn't the script's main.\n" + + echo -e "Tip: Maybe run [cd ~/Downloads/lara-stacker/ && sudo ./lara-stacker.sh] commands.\n" + + echo -n "Press any key to exit..." + read whatever + + clear + exit 1 +fi +source $function_path + +# Ensure the script isn't ran directly +if [[ -z "$RAN_MAIN_SCRIPT" ]]; then + prompt "Aborted for direct execution flow." "Please use the main [lara-stacker.sh] script." true false +fi + +# Confirm if setup script isn't run +if [ ! -e "$PWD/done-setup.flag" ]; then + echo -n "Setup script isn't run yet. Are you sure you want to continue? (y/n) " + read confirmation + + case "$confirmation" in + n|N|no|No|NO|nope|Nope|NOPE) + echo -e "\nAborting...\n" + + echo -n "Press any key to continue..." + read whatever + + clear + exit 1 + ;; + esac +fi + +# * ============ +# * Preparation +# * ========== + +# Get environment variables and defaults +lara_stacker_dir=$PWD +source $lara_stacker_dir/.env + +# Setting the echoing level +conditional_quiet="--quiet" +cancel_suppression=false +case $LOGGING_LEVEL in +# Notifications Only +1) + exec 3>&1 + exec > /dev/null 2>&1 + ;; +# Notifications + Errors + Warnings +2) + exec 3>&1 + exec > /dev/null + ;; +# Everything +*) + exec 3>&1 + conditional_quiet="" + cancel_suppression=true + ;; +esac + +# * ======== +# * Process +# * ====== + +# Get the db name +echo -ne "Enter the database name: " >&3 +read db_name + +# Escape and format the name +db_name=$(echo "$db_name" | tr ' ' '-' | tr '_' '-' | tr '[:upper:]' '[:lower:]') +db_name=${db_name// /} +db_name=$(echo "$db_name" | sed 's/\([[:lower:]]\)\([[:upper:]]\)/\1_\2/g' | sed 's/\([[:upper:]]\)\([[:upper:]][[:lower:]]\)/\1_\2/g' | tr '-' '_' | tr '[:upper:]' '[:lower:]' | sed 's/__/_/g' | sed 's/^_//') + +# DB Creation +export MYSQL_PWD=$DB_PASSWORD +if mysql -u root -e "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='$db_name'" | grep "$db_name" > /dev/null; then + echo -e "\nMySQL database '$db_name' already exists!" >&3 +else + mysql -u root -e "CREATE DATABASE $db_name;" + echo -e "\nCreated '$db_name' MySQL database." >&3 +fi + +echo -ne "\nPress any key to continue..." >&3 +read whatever + +clear >&3 diff --git a/scripts/databases/delete.sh b/scripts/databases/delete.sh new file mode 100755 index 0000000..2e19888 --- /dev/null +++ b/scripts/databases/delete.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +clear + +# Status indicator +echo -e "-=|[ Lara-Stacker |> Database Management |> DELETE ]|=-\n" + +# * =========== +# * Validation +# * ========= + +# Check if prompt function exists and source it +function_path="./scripts/functions/prompt.sh" +if [[ ! -f $function_path ]]; then + echo -e "Error: Working directory isn't the script's main.\n" + + echo -e "Tip: Maybe run [cd ~/Downloads/lara-stacker/ && sudo ./lara-stacker.sh] commands.\n" + + echo -n "Press any key to exit..." + read whatever + + clear + exit 1 +fi +source $function_path + +# Ensure the script isn't ran directly +if [[ -z "$RAN_MAIN_SCRIPT" ]]; then + prompt "Aborted for direct execution flow." "Please use the main [lara-stacker.sh] script." true false +fi + +# Confirm if setup script isn't run +if [ ! -e "$PWD/done-setup.flag" ]; then + echo -n "Setup script isn't run yet. Are you sure you want to continue? (y/n) " + read confirmation + + case "$confirmation" in + n|N|no|No|NO|nope|Nope|NOPE) + echo -e "\nAborting...\n" + + echo -n "Press any key to continue..." + read whatever + + clear + exit 1 + ;; + esac +fi + +# * ============ +# * Preparation +# * ========== + +# Get environment variables and defaults +lara_stacker_dir=$PWD +source $lara_stacker_dir/.env + +# Setting the echoing level +conditional_quiet="--quiet" +cancel_suppression=false +case $LOGGING_LEVEL in +# Notifications Only +1) + exec 3>&1 + exec > /dev/null 2>&1 + ;; +# Notifications + Errors + Warnings +2) + exec 3>&1 + exec > /dev/null + ;; +# Everything +*) + exec 3>&1 + conditional_quiet="" + cancel_suppression=true + ;; +esac + +# * ======== +# * Process +# * ====== + +# Get the db name +echo -ne "Enter the database name: " >&3 +read db_name + +# Escape and format the name +db_name=$(echo "$db_name" | tr ' ' '-' | tr '_' '-' | tr '[:upper:]' '[:lower:]') +db_name=${db_name// /} +db_name=$(echo "$db_name" | sed 's/\([[:lower:]]\)\([[:upper:]]\)/\1_\2/g' | sed 's/\([[:upper:]]\)\([[:upper:]][[:lower:]]\)/\1_\2/g' | tr '-' '_' | tr '[:upper:]' '[:lower:]' | sed 's/__/_/g' | sed 's/^_//') + +# DB Deletion +export MYSQL_PWD=$DB_PASSWORD +if mysql -u root -e "use $db_name"; then + mysql -u root -e "DROP DATABASE $db_name;" + + echo -e "\nDatabase '$db_name' Deleted." >&3 +else + echo -e "\nDatabase '$db_name' doesn't exist!" >&3 +fi + +echo -ne "\nPress any key to continue..." >&3 +read whatever + +clear >&3 diff --git a/scripts/databases/list.sh b/scripts/databases/list.sh new file mode 100755 index 0000000..af126b3 --- /dev/null +++ b/scripts/databases/list.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +clear + +# Status indicator +echo -e "-=|[ Lara-Stacker |> Database Management |> LIST ]|=-\n" + +# * =========== +# * Validation +# * ========= + +# Check if prompt script exists before sourcing +prompt_function_dir="./scripts/functions/prompt.sh" +if [[ ! -f $prompt_function_dir ]]; then + echo -e "Error: Working directory isn't the script's main.\n" + + echo -e "Tip: Maybe run [cd ~/Downloads/lara-stacker/ && sudo ./lara-stacker.sh] commands.\n" + + echo -n "Press any key to exit..." + read whatever + + clear + exit 1 +fi +source $prompt_function_dir + +# Ensure the script isn't ran directly +if [[ -z "$RAN_MAIN_SCRIPT" ]]; then + prompt "Aborted for direct execution flow." "Please use the main [lara-stacker.sh] script." true false +fi + +# * ============ +# * Preparation +# * ========== + +# Get environment variables and defaults +lara_stacker_dir=$PWD +source $lara_stacker_dir/.env + +# * ============ +# * The Listing +# * ========== + +# Get MySQL databases +export MYSQL_PWD=$DB_PASSWORD +mysql -u root -e "SHOW DATABASES;" + +# * ======== +# * The End +# * ====== + +echo -e "" +read -p "Press any key to continue..." whatever + +clear diff --git a/scripts/delete.sh b/scripts/delete.sh index 2510c3f..88346e2 100755 --- a/scripts/delete.sh +++ b/scripts/delete.sh @@ -3,7 +3,7 @@ clear # Status indicator -echo -e "-=|[ Lara-Stacker |> DELETE ]|=-\n" +echo -e "-=|[ Lara-Stacker |> Stacked Projects |> DELETE ]|=-\n" # * =========== # * Validation diff --git a/scripts/functions/prompt.sh b/scripts/functions/prompt.sh index 886298f..d3df13f 100755 --- a/scripts/functions/prompt.sh +++ b/scripts/functions/prompt.sh @@ -4,6 +4,7 @@ prompt() { local second_sentence="${2:-}" local should_exit="${3:-true}" local muted_mode="${4:-true}" + local without_warning="${5:-false}" local mute="" if [ "$muted_mode" == "true" ]; then @@ -15,7 +16,11 @@ prompt() { if [ "$should_exit" == "false" ]; then error_or_warning="Warning: " fi - eval echo -e \"$error_or_warning $first_sentence\\n\" $mute + if [ "$without_warning" == true ]; then + eval echo -e \"$first_sentence\\n\" $mute + else + eval echo -e \"$error_or_warning $first_sentence\\n\" $mute + fi # Display a tip if available if [[ -n $second_sentence ]]; then diff --git a/scripts/list.sh b/scripts/list.sh index 8845efa..e24744f 100755 --- a/scripts/list.sh +++ b/scripts/list.sh @@ -3,7 +3,7 @@ clear # Status indicator -echo -e "-=|[ Lara-Stacker |> LIST ]|=-\n" +echo -e "-=|[ Lara-Stacker |> Stacked Projects |> LIST ]|=-\n" # * =========== # * Validation