forked from infinum/eightshift-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetup.php
More file actions
117 lines (98 loc) · 3.29 KB
/
Setup.php
File metadata and controls
117 lines (98 loc) · 3.29 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
<?php
/**
* Script used to run project setup and installing all plugins, themes and core.
*
* @package EightshiftLibs
*/
declare(strict_types=1);
use EightshiftLibs\Cli\CliHelpers;
/**
* Update project and setup all plugins, themes and core
*
* @param string $projectRootPath Root of the project where config is located.
* @param array $args Optional arguments.
* @param string $setupFile Define setup file name.
*
* @return void
*/
function setup(string $projectRootPath, array $args = [], string $setupFile = 'setup.json')
{
// Check if optional parameters exists.
$skipCore = $args['skip_core'] ?? false;
$skipPlugins = $args['skip_plugins'] ?? false;
$skipPluginsCore = $args['skip_plugins_core'] ?? false;
$skipPluginsGithub = $args['skip_plugins_github'] ?? false;
$skipThemes = $args['skip_themes'] ?? false;
// Change execution folder.
if (!is_dir($projectRootPath)) {
CliHelpers::cliError("Folder doesn't exist on this path: {$projectRootPath}.");
}
chdir($projectRootPath);
// Check if setup exists.
if (!file_exists($setupFile)) {
CliHelpers::cliError("setup.json is missing at this path: {$setupFile}.");
}
// Parse json file to array.
$data = json_decode(implode(' ', (array)file($setupFile)), true);
if (empty($data)) {
CliHelpers::cliError("{$setupFile} is empty.");
}
// Check if core key exists in config.
if (!$skipCore) {
$core = $data['core'] ?? '';
// Install core version.
if (!empty($core)) {
WP_CLI::runcommand("core update --version={$core} --force");
WP_CLI::log('--------------------------------------------------');
} else {
WP_CLI::warning('No core version is defined. Skipping.');
}
}
// Check if plugins key exists in config.
if (!$skipPlugins) {
$plugins = $data['plugins'] ?? [];
if (!empty($plugins)) {
if (!$skipPluginsCore) {
// Check if plugins core key exists in config.
$pluginsCore = $plugins['core'] ?? [];
// Install core plugins.
if (!empty($pluginsCore)) {
foreach ($pluginsCore as $name => $version) {
WP_CLI::runcommand("plugin install {$name} --version={$version} --force");
WP_CLI::log('--------------------------------------------------');
}
} else {
WP_CLI::warning('No core plugins are defined. Skipping.');
}
}
if (!$skipPluginsGithub) {
// Check if plugins github key exists in config.
$pluginsGithub = $plugins['github'] ?? [];
// Install github plugins.
if (!empty($pluginsGithub)) {
foreach ($pluginsGithub as $name => $version) {
WP_CLI::runcommand("plugin install https://github.com/{$name}/archive/{$version}.zip --force");
WP_CLI::log('--------------------------------------------------');
}
} else {
WP_CLI::warning('No Github plugins are defined. Skipping.');
}
}
}
}
// Check if themes key exists in config.
if (!$skipThemes) {
$themes = $data['themes'] ?? [];
// Install themes.
if (!empty($themes)) {
foreach ($themes as $name => $version) {
WP_CLI::runcommand("theme install {$name} --version={$version} --force");
WP_CLI::log('--------------------------------------------------');
}
} else {
WP_CLI::warning('No themes are defined. Skipping.');
}
}
WP_CLI::success('All commands are finished.');
WP_CLI::log('--------------------------------------------------');
}