-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
252 lines (200 loc) · 7.9 KB
/
setup.php
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
<?php
// Basis Setup Script
/* Config */
$vars = array(
'PROJECT_SLUG' => array("project's slug", basename(dirname(__FILE__))),
'PROJECT_NAME' => array("project's name", "My New WordPress Website"),
'PROJECT_VENDOR_SLUG' => array("vendor/author slug of the project", "my-company"),
'PROJECT_REPOSITORY' => array("project's git repository URL", "[email protected]:{{BASIS_PROJECT_VENDOR_SLUG}}/{{BASIS_PROJECT_SLUG}}.git"),
'MAIL_FROM' => array("email address to send emails from", "info@{{BASIS_PROJECT_SLUG}}.com"),
'MAIL_FROM_NAME' => array("name that emails should be sent from", "{{BASIS_PROJECT_NAME}}"),
'DATABASE_NAME' => array("name of the local database", "wp_{{BASIS_PROJECT_SLUG}}"),
'DEPLOY_PATH' => array("remote path that the project should be deployed to", "/var/www/public_html"),
'CURRENT_PATH' => array("remote public path", "/var/www/public"),
'STAGING_BRANCH' => array("staging branch", "development"),
'STAGING_IP' => array("staging server's IP address", '123.456.789.100'),
'STAGING_USER' => array("staging server's username", "www-data"),
'STAGING_URL' => array("staging URL", "https://staging.{{BASIS_PROJECT_SLUG}}.com"),
'STAGING_DATABASE_HOST' => array("staging server's database host", "127.0.0.1:3306"),
'STAGING_DATABASE_NAME' => array("staging server's database name", "{{BASIS_DATABASE_NAME}}"),
'STAGING_DATABASE_USER' => array("staging server's database username", "user"),
'STAGING_DATABASE_PASS' => array("staging server's database password", "pass"),
'PRODUCTION_BRANCH' => array("prodction branch", "main"),
'PRODUCTION_IP' => array("production server's IP address", "123.456.789.100"),
'PRODUCTION_USER' => array("production server's username", "www-data"),
'PRODUCTION_URL' => array("production server's URL", "https://{{BASIS_PROJECT_SLUG}}.com"),
'PRODUCTION_DOMAIN' => array("production server's domain", "{{BASIS_PROJECT_SLUG}}.com"),
'PRODUCTION_DATABASE_HOST' => array("production server's database host", "127.0.0.1:3306"),
'PRODUCTION_DATABASE_NAME' => array("production server's database name", "{{BASIS_DATABASE_NAME}}"),
'PRODUCTION_DATABASE_USER' => array("production server's database username", "user"),
'PRODUCTION_DATABASE_PASS' => array("production server's database password", "pass"),
'GOOGLE_ANALYTICS_ID' => array("production Google Analytics ID", ""),
'SOPS_AGE_PUBLIC_KEY' => array("SOPS AGE Public Key", ""),
'SENDGRID_API_KEY' => array("SendGrid API Key", ""),
'ACF_API_KEY' => array("Advance Cuustom Fields API Key", ""),
'FIVEFIFTEEN_API_KEY' => array("Five Fifteen Plugins API Key", ""),
'PHP_VERSION' => array("desired PHP version", "8.2"),
'COMPOSER_VERSION' => array("desired Composer version", "2.8.3"),
'MYSQL_VERSION' => array("desired MySql version", "8.0.40")
);
$file_process_list = array(
'.lando.yml',
'.sops.yaml',
'auth.template.json',
'composer.template.json',
'deploy.template.yml',
'readme.template.md'
);
$file_delete_list = array(
'assets',
'composer.json',
'composer.lock',
'license.md',
'readme.md',
'setup.php',
'content/themes/{{BASIS_PROJECT_SLUG}}/license.md',
);
$file_rename_list = array(
'auth.template.json' => 'auth.json',
'composer.template.json' => 'composer.json',
'deploy.template.yml' => 'deploy.yml',
'readme.template.md' => 'readme.md'
);
/* Safety-Net */
if (basename(dirname(__FILE__)) === 'basis') {
echo "\nThis script should only be ran on a new copy of Basis.\n\n";
exit;
}
/* Helpers */
function get_json($file) {
write("Loading {$file}...");
return json_decode(file_get_contents($file), true);
}
function parse_vars($str) {
global $vars;
foreach($vars as $key => $var) {
$str = str_replace('{{BASIS_' . $key . '}}', $var[1], $str);
}
return $str;
}
function remove_directory($path) {
$files = glob(preg_replace('/(\*|\?|\[)/', '[$1]', $path) . '/{,.}*', GLOB_BRACE);
foreach ($files as $file) {
if ($file == $path . '/.' || $file == $path . '/..') continue;
is_dir($file) ? remove_directory($file) : unlink($file);
}
rmdir($path);
}
function write($str) {
echo $str . "\n";
}
function write_json($file, $arr, $force_object = false) {
write("Updating {$file}...");
$flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | ($force_object ? JSON_FORCE_OBJECT : null);
return file_put_contents($file, json_encode($arr, $flags));
}
/* Actions */
function do_var_prompts() {
global $vars;
foreach($vars as $key => &$var) {
$default = parse_vars(getenv('BASIS_' . $key) ?: $var[1]);
if (!($response = readline("Enter the {$var[0]} ($default): "))) {
$response = $default;
}
$var[1] = $response;
}
}
function do_file_processing() {
global $file_process_list;
foreach($file_process_list as $file_path) {
if (file_exists($file_path)) {
write("Processing $file_path...");
try {
$file_contents = parse_vars(file_get_contents($file_path));
file_put_contents($file_path, $file_contents);
} catch (Exception $e) {
write("Error: " . $e->getMessage());
}
} else {
write("Error: Could not find $file_path");
}
}
}
function do_file_deletion() {
global $file_delete_list;
foreach($file_delete_list as $file_path) {
$file_path = parse_vars($file_path);
if (file_exists($file_path)) {
write("Deleting $file_path...");
try {
if (is_dir($file_path)) {
remove_directory($file_path);
} else {
unlink($file_path);
}
} catch (Exception $e) {
write("Error: " . $e->getMessage());
}
} else {
write("Error: Could not find $file_path");
}
}
}
function do_file_renaming() {
global $file_rename_list;
foreach($file_rename_list as $file_path => $new_name) {
if (file_exists($file_path)) {
$new_name = parse_vars($new_name);
write("Renaming $file_path to $new_name...");
try {
rename($file_path, $new_name);
} catch (Exception $e) {
write("Error: " . $e->getMessage());
}
} else {
write("Error: Could not find $file_path");
}
}
}
function do_json_updating() {
global $vars;
$auth_json_contents = get_json('auth.json');
$composer_json_contents = get_json('composer.json');
$json_files_updated = false;
if (!$vars['FIVEFIFTEEN_API_KEY'][1]) {
$json_files_updated = true;
unset($auth_json_contents['http-basic']['plugins.fivefifteen.com']);
unset($composer_json_contents['repositories'][1]);
unset($composer_json_contents['require']['fivefifteen-plugin/tidydash']);
unset($composer_json_contents['require']['fivefifteen-plugin/whitelist-addon-for-wp-mail-smtp']);
unset($composer_json_contents['require']['fivefifteen-vendor/gravityforms']);
}
if (!$vars['ACF_API_KEY'][1]) {
$json_files_updated = true;
unset($auth_json_contents['http-basic']['connect.advancedcustomfields.com']);
unset($composer_json_contents['repositories'][2]);
unset($composer_json_contents['require']['wpengine/advanced-custom-fields-pro']);
}
if ($json_files_updated) {
write_json('auth.json', $auth_json_contents, true);
write_json('composer.json', $composer_json_contents);
}
}
/* Logo */
write(' ____ _ ');
write('| _ \ (_) ');
write('| |_) | __ _ ___ _ ___ ');
write('| _ < / _` / __| / __| ');
write('| |_) | (_| \__ \ \__ \ ');
write('|____/ \__,_|___/_|___/ ');
write('');
/* Perform Actions */
do_var_prompts();
system(parse_vars('composer create-project fivefifteen/primer content/themes/{{BASIS_PROJECT_SLUG}} --no-install'));
do_file_processing();
do_file_deletion();
do_file_renaming();
do_json_updating();
write('Done!');
system('lando start');
?>