Skip to content

Commit 5d711db

Browse files
committed
refactor: simplify setup.sh per simplify review
- color funcs: use printf+$ESC style consistently (was mixed) - cleanup_on_exit: drop redundant SETUP_COMPLETE flag, .env existence check is single source of truth - final block: chmod only top-level dirs; recursive perms on WP core tree deferred to `make fix-permissions` (avoids re-walking thousands of files on every setup run) - fix-permissions: combine two find traversals into one using -or branching (halves stat syscalls) - compose validate: flatten 3-level nested if into guard + assignment + single assertion
1 parent 9cb4eee commit 5d711db

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

setup.sh

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# gl0bal01 - WpQuickDev
66
set -euo pipefail
77

8-
# Color output functions
9-
red() { echo -e "\033[31m$1\033[0m"; }
10-
green() { echo -e "\033[32m$1\033[0m"; }
11-
yellow() { echo -e "\033[33m$1\033[0m"; }
12-
blue() { echo -e "\033[34m$1\033[0m"; }
8+
ESC=$(printf '\033')
9+
red() { printf '%s[31m%s%s[0m\n' "$ESC" "$1" "$ESC"; }
10+
green() { printf '%s[32m%s%s[0m\n' "$ESC" "$1" "$ESC"; }
11+
yellow() { printf '%s[33m%s%s[0m\n' "$ESC" "$1" "$ESC"; }
12+
blue() { printf '%s[34m%s%s[0m\n' "$ESC" "$1" "$ESC"; }
1313

1414
PROJECT_NAME="${1:-wordpress-dev}"
1515

@@ -22,10 +22,9 @@ fi
2222

2323
echo "$(blue "🚀 Creating WordPress Development Environment: $PROJECT_NAME")"
2424

25-
# Cleanup on interrupt before .env is written
26-
SETUP_COMPLETE=0
25+
# Remove partial project dir if setup aborts before .env is written
2726
cleanup_on_exit() {
28-
if [ "$SETUP_COMPLETE" -eq 0 ] && [ -n "${PROJECT_DIR:-}" ] && [ -d "$PROJECT_DIR" ] && [ ! -f "$PROJECT_DIR/.env" ]; then
27+
if [ -n "${PROJECT_DIR:-}" ] && [ -d "$PROJECT_DIR" ] && [ ! -f "$PROJECT_DIR/.env" ]; then
2928
red "⚠️ Setup interrupted — removing partial project directory: $PROJECT_DIR"
3029
rm -rf "$PROJECT_DIR"
3130
fi
@@ -395,9 +394,8 @@ fix-permissions: ## Fix file permissions for development
395394
@echo "🔧 Fixing permissions..."
396395
@mkdir -p wordpress plugins themes uploads backups .docker/mysql-logs
397396
@chmod 0700 backups 2>/dev/null || true
398-
@find wordpress plugins themes uploads -type d -exec chmod 0775 {} + 2>/dev/null || true
399-
@find wordpress plugins themes uploads -type f -exec chmod 0664 {} + 2>/dev/null || true
400-
@$(DOCKER_COMPOSE) exec -T wordpress bash -lc 'install -d -m 0775 /var/www/html/wp-content/plugins /var/www/html/wp-content/themes /var/www/html/wp-content/uploads 2>/dev/null || true; find /var/www/html -type d -exec chmod 0775 {} + 2>/dev/null || true; find /var/www/html -type f -exec chmod 0664 {} + 2>/dev/null || true'
397+
@find wordpress plugins themes uploads \( -type d -exec chmod 0775 {} + \) -o \( -type f -exec chmod 0664 {} + \) 2>/dev/null || true
398+
@$(DOCKER_COMPOSE) exec -T wordpress bash -lc 'install -d -m 0775 /var/www/html/wp-content/plugins /var/www/html/wp-content/themes /var/www/html/wp-content/uploads 2>/dev/null || true; find /var/www/html \( -type d -exec chmod 0775 {} + \) -o \( -type f -exec chmod 0664 {} + \) 2>/dev/null || true'
401399
@echo "✅ Permissions fixed"
402400
403401
plugin: fix-permissions ## Create plugin (usage: make plugin name=my-plugin)
@@ -1103,30 +1101,26 @@ mkdir -p plugins themes uploads backups .docker/mysql-logs wordpress
11031101
# Create .gitkeep files to maintain directory structure
11041102
touch plugins/.gitkeep themes/.gitkeep
11051103

1106-
# Dev-friendly perms so both www-data and wpcli can write
1107-
find wordpress plugins themes uploads -type d -exec chmod 0775 {} + 2>/dev/null || true
1108-
find wordpress plugins themes uploads -type f -exec chmod 0664 {} + 2>/dev/null || true
1104+
# Dev-friendly perms on the freshly-created top-level dirs only.
1105+
# Recursive perms on WP core are handled later by `make fix-permissions`.
1106+
chmod 0775 wordpress plugins themes uploads 2>/dev/null || true
11091107
chmod 0700 backups 2>/dev/null || true
11101108

11111109
# Set proper permissions for scripts
11121110
chmod +x backup.sh restore.sh health-check.sh plugin-status.sh
11131111

11141112
# Validate generated docker-compose.yml syntax
1115-
if command -v docker >/dev/null 2>&1; then
1116-
if docker compose version >/dev/null 2>&1; then
1117-
if ! docker compose config >/dev/null 2>&1; then
1118-
red "❌ Generated docker-compose.yml is invalid"
1119-
exit 1
1120-
fi
1121-
elif command -v docker-compose >/dev/null 2>&1; then
1122-
if ! docker-compose config >/dev/null 2>&1; then
1123-
red "❌ Generated docker-compose.yml is invalid"
1124-
exit 1
1125-
fi
1126-
fi
1113+
if docker compose version >/dev/null 2>&1; then
1114+
COMPOSE_VALIDATE="docker compose"
1115+
elif command -v docker-compose >/dev/null 2>&1; then
1116+
COMPOSE_VALIDATE="docker-compose"
1117+
else
1118+
COMPOSE_VALIDATE=""
1119+
fi
1120+
if [ -n "$COMPOSE_VALIDATE" ] && ! $COMPOSE_VALIDATE config >/dev/null 2>&1; then
1121+
red "❌ Generated docker-compose.yml is invalid"
1122+
exit 1
11271123
fi
1128-
1129-
SETUP_COMPLETE=1
11301124

11311125
echo ""
11321126
green "✅ Enhanced WordPress Development Environment created with Plugin Development Workflow!"

0 commit comments

Comments
 (0)