-
Notifications
You must be signed in to change notification settings - Fork 0
/
bob
299 lines (260 loc) · 10.4 KB
/
bob
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env bash
#
# Bob Command
#
# Create new charm instances,
# Provide easy access to Bob CLI
# Define colors for fancy output
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
CYAN="\033[36m"
RESET="\033[0m"
function outputRainbow() {
colors=(
"\033[31m" # red
"\033[32m" # green
"\033[33m" # yellow
"\033[34m" # blue
"\033[35m" # magenta
"\033[36m" # cyan
"\033[37m" # white
"\033[90m" # gray
"\033[91m" # bright-red
"\033[92m" # bright-green
"\033[93m" # bright-yellow
"\033[94m" # bright-blue
"\033[95m" # bright-magenta
"\033[96m" # bright-cyan
"\033[97m" # bright-white
)
text=$1
lines=()
while IFS= read -r line; do
lines+=( "$line" )
done < <(printf '%s\n' "$text")
last_color=
for line in "${lines[@]}"; do
new_color=$(( RANDOM % ${#colors[@]} ))
while [[ "$new_color" == "$last_color" ]]; do
new_color=$(( RANDOM % ${#colors[@]} ))
done
last_color=$new_color
echo -e "${colors[$new_color]}$line${RESET}"
done
}
# Set default PHP executable paths
PHP_PATHS=( "php" "/usr/bin/php" "/usr/local/bin/php" "/opt/plesk/php/8.1/bin/php" )
# Find first available PHP executable path
PHP_PATH=""
for path in "${PHP_PATHS[@]}"; do
if command -v "$path" >/dev/null 2>&1; then
PHP_PATH="$path"
break
fi
done
# Check if PHP executable was found
if [ -z "$PHP_PATH" ]; then
echo -e "❌ ${RED}I have a bad feeling about this... PHP not found."
echo -e " Ensure PHP is installed and the path is set correctly.${RESET}"
exit 1
fi
# Check if Composer is installed
if ! command -v composer &> /dev/null
then
echo -e "❌ ${RED}It's a trap! Composer not found."
echo -e " Install Composer and ensure it's available"
echo -e " in your PATH to continue your journey.${RESET}"
exit 1
fi
# Self-updater
if [ "$1" == "self-update" ]; then
# Bob CLI: Self-Update
# ---------------------
# Initiating hyperdrive sequence...
# Fetching latest updates from the galaxy far, far away...
echo -e "${CYAN}Commencing self-update...${RESET}"
# Fetch the latest version of the Bob script from GitHub
curl -fSsL -o /tmp/bob https://raw.githubusercontent.com/neoground/charm-toolkit/main/bob
# Check if the downloaded script is valid and has the necessary permissions
if [ -s /tmp/bob ] && chmod +x /tmp/bob; then
# Replace the existing Bob script with the latest version
echo -e "Moving new version to /usr/local/bin/bob..."
if [[ $(id -u) -ne 0 && -x $(command -v sudo) ]]; then
sudo mv /tmp/bob /usr/local/bin/bob
elif [[ $(id -u) -eq 0 ]]; then
mv /tmp/bob /usr/local/bin/bob
else
echo -e "${RED}Failed moving bob to /usr/local/bin/bob! The Force is not strong with this update...${RESET}"
echo "Please run with sudo or as root."
exit 1
fi
echo -e "${GREEN}✅ Update successful! Bob is now ready!${RESET}"
else
# In case the download failed or the script is not executable
echo -e "${RED}Update failed! The Force is not strong with this update...${RESET}"
fi
exit 0
fi
# Dev server
if [ "$1" == "serve" ]; then
outputRainbow " ██████ ██ ██ █████ ██████ ███ ███"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ████ ████"
outputRainbow "██ ███████ ███████ ██████ ██ ████ ██"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ██ ██ ██"
outputRainbow " ██████ ██ ██ ██ ██ ██ ██ ██ ██"
echo " "
outputRainbow "Welcome to the Charm PHP Dev Server"
echo " "
host="localhost"
port="8000"
# Check the number of arguments
if [ "$#" -eq 2 ]; then
host="$2"
elif [ "$#" -eq 3 ]; then
host="$2"
port="$3"
fi
# Execute the PHP built-in server
"$PHP_PATH" -S "$host:$port"
exit 0
fi
# Create new project
if [ "$1" == "new" ]; then
outputRainbow " ██████ ██ ██ █████ ██████ ███ ███"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ████ ████"
outputRainbow "██ ███████ ███████ ██████ ██ ████ ██"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ██ ██ ██"
outputRainbow " ██████ ██ ██ ██ ██ ██ ██ ██ ██"
echo " "
outputRainbow "Welcome to the Charm PHP Framework!"
echo " "
echo -e "The Charm PHP Framework is a special PHP framework"
echo -e "that's optimized for APIs and business web apps."
echo -e "It's faster and slimmer than other PHP frameworks,"
echo -e "making it the perfect choice for developers who"
echo -e "need high-performance software that can handle"
echo -e "complex business logic."
echo " "
echo -e "Get started with the Charm PHP Framework today"
echo -e "and see what you can build! Let's go!"
echo " "
echo " "
echo -e "${CYAN}Creating new project: $2 ${RESET}"
echo " "
# Check if dir exists
if [ -d "$2" ]; then
echo -e "${RED}Project directory already existing!${RESET}"
exit 1
fi
# Create new project directory
mkdir "$2"
# Clone Charm wireframe
echo -ne "[..] Cloning charm-wireframe...\r"
git clone --depth 1 --quiet https://github.com/neoground/charm-wireframe.git "$2"
echo -ne "${GREEN}[OK]${RESET} Cloned charm-wireframe \n"
# Navigate to the new project directory
cd "$2" || exit 1
# Create required directories and set permissions
echo -ne "[..] Creating directory structure...\r"
mkdir -p var/cache var/logs data app/Config/Environments
chmod -R 0777 var/cache var/logs data
echo -ne "${GREEN}[OK]${RESET} Created directory structure \n"
# Install dependencies using Composer
echo -ne "[..] Installing composer packages...\r"
composer -n -q update
echo -ne "${GREEN}[OK]${RESET} Installed composer packages \n"
# Update main config
echo -ne "[..] Updating config...\r"
sed -i 's/^ *name: charm-bbq$/ name: '"$2-bbq"'/' app/Config/main.yaml
sed -i "s/^ *name: charm$/ name: '$2'/" app/Config/main.yaml
timezone=$(cat /etc/timezone)
timezone="${timezone//@/\\@}"
sed -i "s@timezone:.*@timezone: '$timezone'@g" app/Config/main.yaml
sed -i "s/language:.*/language: $LANG/" app/Config/main.yaml
sed -i "s/auth_salt:.*/auth_salt: '$(openssl rand -base64 29 | tr -d "=+/" | cut -c1-25)'/" app/Config/main.yaml
# Update connections config
sed -i "s/ database:.*/ database: '$2'/" app/Config/connections.yaml
sed -i "s/prefix:.*/prefix: '$2_'/" app/Config/connections.yaml
# Replace composer.json + Readme with minimal template
rm composer.json
mv composer-project.json composer.json
namelowercase=${2,,}
sed -i "s/PROJNAME/$namelowercase\/app/g" composer.json
rm README.md
mv README-project.md README.md
sed -i "s/PROJNAME/$2/g" README.md
# Remove charm-wireframe git stuff and project specific files
rm -Rf .git
rm LICENSE.md
echo -ne "${GREEN}[OK]${RESET} Updated config \n"
echo " "
echo -e "${GREEN}[OK]${RESET} Project created!"
echo " "
# Create local environment
"$PHP_PATH" bob.php c:env Local
if [ -d "/etc/apache2" ]; then
echo " "
echo -e "🌐 Apache2 detected. You might need to adjust your webserver or"
echo -e " the .htaccess in your new project."
echo " "
fi
if [ -d "/etc/nginx" ]; then
echo " "
echo -e "🌐 Nginx detected. You might need to adjust your webserver."
echo -e " See the nginx.conf in your new project."
echo " "
fi
echo " "
echo -e "✅ ${GREEN}The Force is strong with this one!"
echo -e " The Charm PHP Framework has been successfully installed.${RESET}"
echo " "
echo -e "💻 Your journey begins! Access ${GREEN}https://localhost/path/to/$2${RESET}"
echo -e " to view a greeting from a galaxy far, far away."
echo " "
echo -e "📡 Alternatively, unleash the power of ${GREEN}bob serve${RESET}"
echo -e " to create a local development environment accessible at"
echo -e " ${GREEN}https://localhost:8000${RESET}"
echo " "
exit 0
fi
# Check if file exists
if [ -f "bob.php" ]; then
# Call PHP script with arguments
"$PHP_PATH" "bob.php" "${@:1}"
exit 0
fi
# Print the help text with color output
outputRainbow " ██████ ██ ██ █████ ██████ ███ ███"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ████ ████"
outputRainbow "██ ███████ ███████ ██████ ██ ████ ██"
outputRainbow "██ ██ ██ ██ ██ ██ ██ ██ ██ ██"
outputRainbow " ██████ ██ ██ ██ ██ ██ ██ ██ ██"
echo " "
echo -e "${CYAN}Welcome to Bob, the Intergalactic Charm Framework Toolkit!${RESET}"
echo ""
echo -e "In a galaxy far, far away, Bob is your trusty droid companion,"
echo -e "here to assist you with your Charm Framework projects."
echo ""
echo -e "${YELLOW}Usage:${RESET}"
echo -e " ${GREEN}bob command [arguments] [options]${RESET}"
echo ""
echo -e " When in a Charm project, Bob will execute the CLI command \"command\""
echo -e " for this project, passing along arguments and options like a loyal astromech."
echo ""
echo -e "${YELLOW}Create a new Charm project:${RESET}"
echo -e " ${GREEN}bob new <project-name>${RESET}"
echo ""
echo -e " Command your own Millennium Falcon! Bob will create a new directory named"
echo -e " \"<project-name>\", containing a fresh Charm project based on the charm-wireframe."
echo ""
echo -e "${YELLOW}Example:${RESET}"
echo -e " ${GREEN}bob new LightSpeeder${RESET}"
echo ""
echo -e " This will initialize a new Charm project called \"LightSpeeder\" in a"
echo -e " directory named \"LightSpeeder\"."
echo ""
echo -e "${CYAN}May the Force be with you on your journey through the vast"
echo -e "universe of web development with Charm Framework and Bob by your side!${RESET}"
echo ""
exit 0