forked from mybizna/mybizna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mybizna_install.sh
284 lines (214 loc) · 7.59 KB
/
mybizna_install.sh
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
#!/bin/bash
# chmod +x mybizna_install.sh && ./mybizna_install.sh . testlaravel
# Function to validate folder path
validate_folder_path() {
local folder_path=$1
# Check if folder path is provided
if [[ -z "$folder_path" ]]; then
echo "Error: Folder path cannot be empty."
return 1
fi
# Check if folder path exists
if [[ ! -d "$folder_path" ]]; then
echo "Error: Folder path '$folder_path' does not exist."
return 1
fi
return 0
}
# Function to validate script name
validate_string() {
local name=$1
# Check if script name is provided
if [[ -z "$name" ]]; then
return 1
fi
# Add additional script name validation logic here if required
return 0
}
# Check if folder path and script name are provided as parameters
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <folder_path> <project_name>"
exit 1
fi
# Extract folder path and script name from the parameters
folder_path=$1
project_name=$2
# Validate folder path
if ! validate_folder_path "$folder_path"; then
exit 1
fi
# Validate script name
if ! validate_string "$project_name"; then
echo "Error: Project name cannot be empty."
exit 1
fi
# Prompt for Mysql Host
read -p "Enter Mysql Host (default is localhost): " -e db_host
db_host="${name:-localhost}"
if ! validate_string "$db_host"; then
echo "Error: Mysql Host cannot be empty."
exit 1
fi
# Prompt for Mysql Host
read -p "Enter Mysql Port (default is 3306):" -e db_port
db_port="${name:-3306}"
if ! validate_string "$db_port"; then
echo "Error: Mysql Port cannot be empty."
exit 1
fi
# Prompt for Mysql Username
read -p "Enter Mysql Username: " db_username
if ! validate_string "$db_username"; then
echo "Error: Mysql Username cannot be empty."
exit 1
fi
# Prompt for Mysql Password
read -s -p "Enter Mysql Password: " db_password
if ! validate_string "$db_password"; then
echo "Error: Mysql Password cannot be empty."
exit 1
fi
echo ""
# Prompt for Mysql Database
read -p "Enter Mysql Database: " db_name
if ! validate_string "$db_name"; then
echo "Error: Mysql Database cannot be empty."
exit 1
fi
mysql_cmd="mysql -h $db_host -P $db_port -u $db_username -p$db_password"
echo $mysql_cmd
# Test the connection by executing a simple query
query="SELECT 1"
if echo "$query" | $mysql_cmd -D "$db_name" >/dev/null 2>&1; then
echo "MySQL connected successfully."
else
read -p "Failed to connect to MySQL. Do you want to create the database? (default is Y) [y/N]: " create_db
create_db="${name:-y}"
if [[ $create_db =~ ^[Yy]$ ]]; then
# Create the database
create_db_query="CREATE DATABASE $db_name;"
echo "$create_db_query" | $mysql_cmd -u "$db_username" -p"$db_password" >/dev/null 2>&1
if echo "$query" | $mysql_cmd -D "$db_name" >/dev/null 2>&1; then
echo "MySQL connected successfully after creating the database."
else
echo "Error: Failed to connect to MySQL after creating the database."
exit 1
fi
else
echo "MySQL connection aborted. Database not created."
exit 1
fi
fi
# Construct the full path for script installation
install_path="$folder_path/$project_name"
composer create-project laravel/laravel:^9.0 $install_path
# Copy the script to the installation path
cd $install_path
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sed -i "s/^DB_HOST=.*/DB_HOST=127.0.0.1/" .env
sed -i "s/^DB_PORT=.*/DB_PORT=3306/" .env
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=$db_name/" .env
sed -i "s/^DB_USERNAME=.*/DB_USERNAME=$db_username/" .env
sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD=$db_password/" .env
sed -i "s/^CACHE_DRIVER=.*/CACHE_DRIVER=database/" .env
sed -i "s/^SESSION_DRIVER=.*/SESSION_DRIVER=database/" .env
sed -i '/^SESSION_LIFETIME=.*/aSANCTUM_STATEFUL_DOMAINS=' .env
sed -i '/^SESSION_LIFETIME=.*/aSESSION_DOMAIN=' .env
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Specify the path to the User.php file
user_model_path="app/Models/User.php"
# Check if the User.php file exists
if [[ -f "$user_model_path" ]]; then
# Add the "use Spatie\Permission\Traits\HasRoles;" line after "use Laravel\Sanctum\HasApiTokens;"
sed -i '/use Laravel\\Sanctum\\HasApiTokens;/a\
use Spatie\\Permission\\Traits\\HasRoles;\
' "$user_model_path"
# Add the "use HasRoles;" line on the first occurrence of "{"
sed -i '/{/{s/{/{\
use HasRoles;/;:a;n;ba}' "$user_model_path"
# Add the lines to the User model
sed -i "/protected \$fillable = \[/a\
'username',\n\
'is_admin',\n\
'phone',\
" "$user_model_path"
echo "User model updated successfully."
else
echo "Error: User model file not found at $user_model_path."
fi
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Specify the path to the Kernel.php file
kernel_file_path="app/Http/Kernel.php"
# Check if the file exists
if [[ -f "$kernel_file_path" ]]; then
# Remove the comment from the specified line
sed -i 's#// \\Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful::class,#\\Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful::class,#' "$kernel_file_path"
echo "Comment removed successfully."
else
echo "Error: Kernel.php file not found at $kernel_file_path."
fi
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Prompt for ERP Admin Username
read -p "Enter ERP Admin Name: " erp_name
if ! validate_string "$erp_name"; then
echo "Error: ERP Admin Name cannot be empty."
exit 1
fi
# Prompt for ERP Admin Username
read -p "Enter ERP Admin Username: " erp_username
if ! validate_string "$erp_username"; then
echo "Error: ERP Admin Username cannot be empty."
exit 1
fi
# Prompt for ERP Admin Password
read -p "Enter ERP Admin Password: " erp_password
if ! validate_string "$erp_password"; then
echo "Error: ERP Admin Password cannot be empty."
exit 1
fi
# Prompt for ERP Admin Email
read -p "Enter ERP Admin Email: " erp_email
if ! validate_string "$erp_email"; then
echo "Error: ERP Admin Email cannot be empty."
exit 1
fi
# Prompt for ERP Admin Email
read -p "Enter ERP Admin Phone: " erp_phone
if ! validate_string "$erp_phone"; then
echo "Error: ERP Admin Phone cannot be empty."
exit 1
fi
php artisan tinker <<EOF
use Illuminate\Support\Facades\Hash;
\$user = new App\Models\User();
\$user->password = Hash::make('$erp_password');
\$user->email = '$erp_email';
\$user->name = '$erp_name';
\$user->is_admin = 1;
\$user->username = '$erp_username';
\$user->phone = '$erp_phone';
\$user->save();
exit
EOF
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
composer require mybizna/account
composer require mybizna/mail
composer require mybizna/product
composer require mybizna/sale
php artisan cache:table
php artisan session:table
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan vendor:publish --provider="Mybizna\Assets\Providers\MybiznaAssetsProvider"
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
php artisan automigrator:migrate
php artisan module:enable
php artisan mybizna:dataprocessor
php artisan key:generate
echo "Script installed successfully at $install_path."