Skip to content

Commit

Permalink
[v4.0.0] Reworked for consistent syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodM4ven committed May 8, 2024
1 parent 7e5137d commit b63283c
Show file tree
Hide file tree
Showing 33 changed files with 629 additions and 751 deletions.
105 changes: 66 additions & 39 deletions lara-stacker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@

clear

# ? Display a status indicator
changelog_dir="./CHANGELOG.md"
# * ===========================
# * Display a status indicator
# * =========================

current_version="???"
if [[ -f $changelog_dir ]]; then
current_version=$(grep -E "^## v[0-9]+" $changelog_dir | head -1 | awk '{print $2}')
is_updateable=false
script_dir="$(pwd)"

if command -v git &> /dev/null && [ -d ".git" ]; then
is_updateable=true

# ? Add the current script directory to the Git safe list
git config --global --add safe.directory "$script_dir"
fi

if [[ "$is_updateable" == true ]]; then
# ? Fetch the latest commit message that starts with a version tag
current_version=$(git log --pretty=format:'%s' | grep -o '\[v[0-9]*\.[0-9]*\.[0-9]*\]' | head -1 | tr -d '[]')
fi
echo -e "-=|[ LARA-STACKER [$current_version] ]|=-\n"

echo -e "-=|[ LARA-STACKER $current_version ]|=-\n"

# * ===========
# * Validation
# * =========

# ? Check if prompt script exists before sourcing
prompt_function_dir="./scripts/functions/prompt.sh"
# ? ================================================
# ? Source prompt script or abort if it isn't found
# ? ==============================================

prompt_function_dir="./scripts/functions/helpers/prompt.sh"
if [[ ! -f $prompt_function_dir ]]; then
echo -e "Error: Working directory isn't the script's main.\n"

Expand All @@ -27,52 +44,49 @@ if [[ ! -f $prompt_function_dir ]]; then
clear
exit 1
fi

chmod +x $prompt_function_dir
source $prompt_function_dir

# ? Check if the script is run with sudo
# ? Abort if the script isn't run with sudo
if [ "$EUID" -ne 0 ]; then
prompt "Aborted for missing super-user (sudo) permission." "Run the script using [sudo ./lara-stacker.sh] command."
fi

# ? Ensure that .env file exists
# ? Ensure that the environment file exists
if [ ! -f "./.env" ]; then
prompt "Aborted for missing [.env] file." "Copy one using [cp .env.example .env] command then fill its values."
fi

# ? Double check the environment variables
# ? =================================================
# ? Ensure that there is no placeholders in the file
# ? ===============================================

placeholders=("<your-username>" "<your-password>")

while IFS= read -r line; do
for placeholder in "${placeholders[@]}"; do
if [[ "$line" == *"$placeholder"* ]]; then
prompt "Aborted because [.env] file contains a placeholder." "Please replace placeholders with values."
fi
done
done < "./.env"

# ? ===================================================
# ? Double check for environment variables consistency
# ? =================================================

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

if [ $? -ne 0 ]; then
prompt "Aborted for different environment variables." "Ensure that [.env.example] variables match [.env] ones."
fi

# ? Ensure all side scripts are executable
SCRIPTS=(
"./scripts/update.sh"
"./scripts/setup.sh"
"./scripts/create_raw.sh"
"./scripts/TALL/list.sh"
"./scripts/TALL/create.sh"
"./scripts/TALL/import.sh"
"./scripts/TALL/delete.sh"
"./scripts/mysql/list.sh"
"./scripts/mysql/create.sh"
"./scripts/mysql/delete.sh"
"./scripts/apache/list.sh"
"./scripts/apache/enable.sh"
"./scripts/apache/disable.sh"
"./scripts/helpers/permit.sh"
)
for script in "${SCRIPTS[@]}"; do
if [[ -f "$script" ]]; then
if [[ ! -x "$script" ]]; then
chmod +x "$script"
fi
else
prompt "Aborted for missing [$script] file." "Clone the latest [GoodM4ven/lara-stacker] github repository."
fi
done
find ./scripts -type f -not -path "*/functions/*" ! -perm -111 -exec chmod +x {} +

# * ============
# * Preparation
Expand All @@ -86,7 +100,10 @@ source $lara_stacker_dir/.env
# * Process
# * ======

# ? =====================
# ? Checking for updates
# ? ===================

if [[ -f "/tmp/updated-lara-stacker.flag" ]]; then
rm /tmp/updated-lara-stacker.flag
fi
Expand All @@ -98,9 +115,19 @@ sleep 1
echo -en "."

update_available=false
latest_version=$(wget -qO- "https://api.github.com/repos/GoodM4ven/lara-stacker/releases/latest" | jq -r .tag_name)
if [[ "$current_version" != "$latest_version" ]]; then
update_available=true
latest_version=""

# ? Check for updates if it's possible
if [[ "$is_updateable" == true ]]; then
git fetch origin

LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "origin/main")

if [ "$LOCAL" != "$REMOTE" ]; then
update_available=true
latest_version=$(git ls-remote --tags origin | grep -o 'v[0-9]*\.[0-9]*\.[0-9]*$' | sort -V | tail -1)
fi
fi

echo -en "."
Expand All @@ -117,7 +144,7 @@ counter=0
while true; do
counter=$((counter + 1))

echo -e "-=|[ LARA-STACKER [$current_version] ]|=-\n"
echo -e "-=|[ LARA-STACKER $current_version ]|=-\n"

echo -e "Available Operations:\n"

Expand Down
91 changes: 32 additions & 59 deletions scripts/TALL/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,27 @@ echo -e "-=|[ Lara-Stacker |> TALL Projects 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; as \"prompt\" function is missing.\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

# ? A function to check for a function existence in order to source it
sourceIfAvailable() {
local functionNameCamel=$1
local functionNameSnake=$(echo $1 | sed -r 's/([a-z])([A-Z])/\1_\L\2/g')
local functionPath="./scripts/functions/${functionNameSnake}.sh"

if [[ ! -f $functionPath ]]; then
prompt "Working directory isn't the script's main; as \"${functionNameCamel^}\" function is missing." \
"Maybe run [cd ~/Downloads/lara-stacker/ && sudo ./lara-stacker.sh] commands."
else
source $functionPath
# ? Source the helper function scripts first
functions=(
"./scripts/functions/helpers/prompt.sh"
"./scripts/functions/helpers/sourcer.sh"
)
for script in "${functions[@]}"; do
if [[ ! -f "$script" ]] || ! chmod +x "$script" || ! source "$script"; then
echo -e "Error: The essential script '$script' was not found. Exiting..."
exit 1
fi
}

# * Source the necessary functions
sourceIfAvailable "apacheUp"
sourceIfAvailable "viteUp"
sourceIfAvailable "mysqlUp"
sourceIfAvailable "minioUp"
sourceIfAvailable "workspaceUp"
sourceIfAvailable "xdebugUp"
done

# ? 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."
fi

# ? Confirm if setup script isn't run already
sourcer "helpers.continueOrAbort"
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
continueOrAbort "Setup script isn't run yet." "Aborting..."
fi

# * ============
Expand All @@ -76,7 +39,6 @@ fi
# ? Get environment variables and defaults
lara_stacker_dir=$PWD
source $lara_stacker_dir/.env
projects_directory=/var/www/html

# ? Set the echoing level
conditional_quiet="--quiet"
Expand All @@ -85,12 +47,12 @@ case $LOGGING_LEVEL in
# Notifications Only
1)
exec 3>&1
exec > /dev/null 2>&1
exec >/dev/null 2>&1
;;
# Notifications + Errors + Warnings
2)
exec 3>&1
exec > /dev/null
exec >/dev/null
;;
# Everything
*)
Expand All @@ -100,7 +62,7 @@ case $LOGGING_LEVEL in
;;
esac

# ? Check for VSCodium or VSC existence
# ? Check if VSCodium or VSC is installed
USING_VSC=false
if command -v codium >/dev/null 2>&1 || command -v code >/dev/null 2>&1; then
USING_VSC=true
Expand All @@ -110,6 +72,8 @@ fi
# * Input
# * ====

projects_directory=/var/www/html

# ? Get the project name from the user
echo -ne "Enter the project name: " >&3
read project_name
Expand All @@ -126,31 +90,40 @@ fi
# * Initialization
# * =============

# ? Source the procedural function scripts now
sourcer "apacheUp"
sourcer "viteUp"
sourcer "mysqlUp"
sourcer "minioUp"
sourcer "workspaceUp"
sourcer "xdebugUp"

# ? =====================================================
# ? Create the Laravel project in the projects directory
# ? ===================================================

echo -e "\nInstalling the project via Composer..." >&3

cd $projects_directory/

composer create-project laravel/laravel $escaped_project_name -n $conditional_quiet

sudo $lara_stacker_dir/scripts/helpers/permit.sh $projects_directory/$escaped_project_name

# ? Create the Apache site
apacheUp $escaped_project_name $USERNAME $cancel_suppression $lara_stacker_dir
apacheUp $escaped_project_name $cancel_suppression

# ? Link the site to Vite's configuration
viteUp $projects_directory $escaped_project_name $lara_stacker_dir
viteUp $escaped_project_name

# ? Generate a MySQL database if doesn't exit
mysqlUp $escaped_project_name $projects_directory $DB_PASSWORD
mysqlUp $escaped_project_name

# ? Set up launch.json for debugging (Xdebug), if VSC is used
xdebugUp $USING_VSC $escaped_project_name $lara_stacker_dir $projects_directory
xdebugUp $USING_VSC $escaped_project_name

# ? Set up a MinIO storage
minioUp $escaped_project_name $USERNAME $lara_stacker_dir
minioUp $escaped_project_name

# * ==============
# * Configuration
Expand All @@ -175,7 +148,7 @@ if [ "$OPINIONATED" == true ]; then

if [ "$USING_VSC" == true ]; then
# ? Create a dedicated VSC workspace in Desktop
workspaceUp $escaped_project_name $USERNAME $lara_stacker_dir
workspaceUp $escaped_project_name
fi
fi

Expand Down
Loading

0 comments on commit b63283c

Please sign in to comment.