-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
353 lines (282 loc) · 11.6 KB
/
Copy pathsetup.php
File metadata and controls
353 lines (282 loc) · 11.6 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?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", "git@github.com:{{BASIS_PROJECT_VENDOR_SLUG}}/{{BASIS_PROJECT_SLUG}}.git"),
'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("production 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"),
'MAIL_FROM' => array("email address to send emails from", "info@{{BASIS_PRODUCTION_DOMAIN}}"),
'MAIL_FROM_NAME' => array("name that emails should be sent from", "{{BASIS_PROJECT_NAME}}"),
'GOOGLE_ANALYTICS_ID' => array("production Google Analytics ID", ""),
'SOPS_AGE_PUBLIC_KEY' => array("SOPS AGE Public Key", ""),
'SMTP2GO_API_KEY' => array("SMTP2GO API Key", ""),
'ACF_API_KEY' => array("Advance Custom 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',
'reloader.php',
);
$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/flexible-content-modules']);
unset($composer_json_contents['require']['fivefifteen-plugin/guidebook']);
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);
}
}
function do_theme_data_updating() {
$theme_name = parse_vars(getenv('BASIS_THEME_DATA_NAME') ?: '');
$theme_description = parse_vars(getenv('BASIS_THEME_DATA_DESCRIPTION') ?: '');
$theme_author = parse_vars(getenv('BASIS_THEME_DATA_AUTHOR') ?: '');
$theme_author_uri = parse_vars(getenv('BASIS_THEME_DATA_AUTHOR_URI') ?: '');
$theme_stylesheet = parse_vars(getenv('BASIS_THEME_DATA_STYLESHEET') ?: 'content/themes/{{BASIS_PROJECT_SLUG}}/scss/style.scss');
if ($theme_stylesheet && ($theme_name || $theme_description) && file_exists($theme_stylesheet)) {
$theme_data = file_get_contents($theme_stylesheet, false, null, 0, 8 * 1024);
if ($theme_name) {
$theme_data = preg_replace('/(Theme Name:\s*)(.*)/', "$1{$theme_name}", $theme_data);
}
if ($theme_description) {
$theme_data = preg_replace('/(Description:\s*)(.*)/', "$1{$theme_description}", $theme_data);
}
if ($theme_author) {
$theme_data = preg_replace('/(Author:\s*)(.*)/', "$1{$theme_author}", $theme_data);
}
if ($theme_author_uri) {
$theme_data = preg_replace('/(Author URI:\s*)(.*)/', "$1{$theme_author_uri}", $theme_data);
}
write("Updating theme data in {$theme_stylesheet}...");
file_put_contents($theme_stylesheet, $theme_data);
}
}
/* Logo */
write(' ____ _ ');
write('| _ \ (_) ');
write('| |_) | __ _ ___ _ ___ ');
write('| _ < / _` / __| / __| ');
write('| |_) | (_| \__ \ \__ \ ');
write('|____/ \__,_|___/_|___/ ');
write('');
/* Perform Actions */
do_var_prompts();
$theme_install_cmd = getenv('BASIS_THEME_INSTALL_CMD');
$post_theme_install_cmd = getenv('BASIS_POST_THEME_INSTALL_CMD');
$primer_version = getenv('BASIS_PRIMER_VERSION') ?: null;
$using_primer = !$theme_install_cmd || $primer_version;
if (!$theme_install_cmd) {
if ($primer_version) $primer_version = " \"{$primer_version}\"";
$theme_install_cmd = "composer create-project fivefifteen/primer content/themes/{{BASIS_PROJECT_SLUG}}{$primer_version} --no-install";
}
write('Installing theme...');
system(parse_vars($theme_install_cmd));
if ($using_primer) {
do_theme_data_updating();
if ($vars['FIVEFIFTEEN_API_KEY'][1]) {
$flex_modules_path = parse_vars('content/themes/{{BASIS_PROJECT_SLUG}}/flex-modules');
write("Creating {$flex_modules_path}...");
mkdir($flex_modules_path, 0755);
touch("{$flex_modules_path}/.gitkeep");
}
if ($vars['ACF_API_KEY'][1]) {
$acf_json_path = parse_vars('content/themes/{{BASIS_PROJECT_SLUG}}/acf-json');
write("Creating {$acf_json_path}...");
mkdir($acf_json_path, 0755);
touch("{$acf_json_path}/.gitkeep");
}
}
if ($post_theme_install_cmd) {
write('Running post theme install command...');
system(parse_vars($post_theme_install_cmd));
}
do_file_processing();
do_file_deletion();
do_file_renaming();
do_json_updating();
if (!$vars['FIVEFIFTEEN_API_KEY'][1]) {
$lando_yml_filename = '.lando.yml';
write("Making one last small edit to {$lando_yml_filename}...");
$lando_yml_contents = file_get_contents($lando_yml_filename);
$lando_yml_contents = preg_replace('/\s+# Binds local flex-modules[\S\s]+target: \/var\/www\/flex-modules/', '', $lando_yml_contents);
file_put_contents($lando_yml_filename, $lando_yml_contents);
$deploy_yml_filename = 'deploy.yml';
write("Making one last small edit to {$deploy_yml_filename}...");
$deploy_yml_contents = file_get_contents($deploy_yml_filename);
$deploy_yml_contents = preg_replace('/\s+FCM_REPO_PATH: \S+/', '', $deploy_yml_contents);
$deploy_yml_contents = preg_replace('/\s+FCM_SCSS_IMPORTS_FILE: \S+/', '', $deploy_yml_contents);
file_put_contents($deploy_yml_filename, $deploy_yml_contents);
}
write('Done!');
/* Post Actions */
if (!getenv('BASIS_NO_GIT_INIT')) {
system('git init');
}
if ($post_setup_cmd = getenv('BASIS_POST_SETUP_CMD')) {
write('Running post setup command...');
system(parse_vars($post_setup_cmd));
}
if (!getenv('BASIS_NO_START')) {
system('lando start');
}
if ($post_start_cmd = getenv('BASIS_POST_START_CMD')) {
write('Running post start command...');
system(parse_vars($post_start_cmd));
}
?>