forked from nystudio107/craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nys-setup
executable file
·330 lines (300 loc) · 9.21 KB
/
nys-setup
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
#!/usr/bin/env php
<?php
/**
* @author nystudio107
* @copyright Copyright (c) 2017 nystudio107
* @link https://nystudio107.com/
* @package nystudio107/craft
* @since 1.0.0
* @license MIT
*/
use yii\helpers\Console;
use mikehaertl\shellcommand\Command as ShellCommand;
const CRAFT_SCRIPTS_SETUP = [
'srclink' => 'vendor/nystudio107/craft-scripts/scripts',
'destlink' => 'scripts',
'srcpath' => 'scripts/craft3-example.env.sh',
'destpath' => '.env.sh',
'replacements' => [
'GLOBAL_DB_TABLE_PREFIX=""' => [
'substr' => '""',
'env' => 'DB_TABLE_PREFIX',
'default' => '',
],
'GLOBAL_DB_DRIVER="mysql"' => [
'substr' => '"mysql"',
'env' => 'DB_DRIVER',
'default' => 'mysql',
],
'LOCAL_ROOT_PATH="REPLACE_ME"' => [
'substr' => '"REPLACE_ME"',
'env' => '',
'default' => '',
],
'LOCAL_ASSETS_PATH=${LOCAL_ROOT_PATH}"REPLACE_ME"' => [
'substr' => '"REPLACE_ME"',
'env' => '',
'default' => 'web/assets/',
],
'LOCAL_DB_NAME="REPLACE_ME"' => [
'substr' => '"REPLACE_ME"',
'env' => 'DB_DATABASE',
'default' => 'REPLACE_ME',
],
'LOCAL_DB_PASSWORD="REPLACE_ME"' => [
'substr' => '"REPLACE_ME"',
'env' => 'DB_PASSWORD',
'default' => 'REPLACE_ME',
],
'LOCAL_DB_USER="REPLACE_ME"' => [
'substr' => '"REPLACE_ME"',
'env' => 'DB_USER',
'default' => 'REPLACE_ME',
],
'LOCAL_DB_HOST="localhost"' => [
'substr' => '"localhost"',
'env' => 'DB_SERVER',
'default' => 'localhost',
],
'LOCAL_DB_PORT="3306"' => [
'substr' => '"3306"',
'env' => 'DB_PORT',
'default' => '3306',
],
'LOCAL_DB_SCHEMA="public"' => [
'substr' => '"public"',
'env' => 'DB_SCHEMA',
'default' => 'public',
],
],
];
// Set path constants
define('CRAFT_BASE_PATH', __DIR__);
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');
// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';
// Load the .env file Craft created
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
$dotEnv = Dotenv\Dotenv::create(CRAFT_BASE_PATH);
$dotEnv->load();
}
// By default, run the setup script
if (empty($argv[1])) {
setupNysCraft();
} else {
// See what command we were passed in
switch ($argv[1]) {
case 'welcome':
// Display a welcome message
welcomeNysCraft();
break;
case 'update':
// Display a welcome message
updateNysCraft();
break;
default:
// Set up all the things!
setupNysCraft();
break;
}
}
/**
* Display a welcome message
*/
function welcomeNysCraft()
{
outputString(PHP_EOL.'To set up your Craft install, from your project directory, run:', Console::FG_YELLOW);
$script = './craft setup';
outputString(PHP_EOL.' '.$script, Console::FG_GREEN);
outputString(PHP_EOL.'Craft-Scripts, run:', Console::FG_YELLOW);
$script = './nys-setup';
outputString(PHP_EOL.' '.$script, Console::FG_GREEN);
outputString(PHP_EOL.'Your setup is not complete until you run these two commands.', Console::FG_YELLOW);
}
/**
* Update our Craft-Scripts symlinks as needed!
*/
function updateNysCraft()
{
// Say hello
outputString(PHP_EOL.'Updating nys-setup', Console::FG_YELLOW);
// Set up Craft-Scripts
setupCraftScripts();
// Say goodbye
outputString(PHP_EOL.'Update complete. Have a nice day!', Console::FG_YELLOW);
}
/**
* Set up all the things!
*/
function setupNysCraft()
{
// Say hello
outputString(PHP_EOL.'Welcome to nys-setup', Console::FG_YELLOW);
// Set up Craft-Scripts
setupCraftScripts();
// Install the NodeJS packages
installNodePackages();
// Say goodbye
outputString(PHP_EOL.'Setup complete. Have a nice day!', Console::FG_YELLOW);
}
/**
* Set up Craft-Scripts
*/
function setupCraftScripts()
{
// Set up Craft-Scripts if the file scripts/.env.sh doesn't exist already
outputString(PHP_EOL.'Setting up Craft-Scripts', Console::FG_YELLOW);
if (!file_exists('scripts/.env.sh')) {
$fileInfo = CRAFT_SCRIPTS_SETUP;
// Create the scripts symlink
createSymLink($fileInfo['srclink'], $fileInfo['destlink']);
if (!file_exists('.env.sh')) {
// Create the default .env.sh
if (copyFile($fileInfo['srcpath'], $fileInfo['destpath'])) {
$replacements = $fileInfo['replacements'];
if (!empty($replacements)) {
// We need to swap in some dynamic variables for specific settings replacements
$replacements['LOCAL_ROOT_PATH="REPLACE_ME"']['default'] = CRAFT_BASE_PATH.DIRECTORY_SEPARATOR;
replaceInFile($fileInfo['destpath'], $replacements);
}
}
}
// Create the scripts/.env.sh symlink
createSymLink(CRAFT_BASE_PATH.DIRECTORY_SEPARATOR.'.env.sh', 'scripts/.env.sh');
} else {
outputString('### scripts/.env.sh file already exists, exiting...', Console::FG_RED);
}
}
function installNodePackages()
{
$command = '';
if (shellCommandExists('npm')) {
$command = 'npm install';
} elseif (shellCommandExists('yarn')) {
$command = 'yarn';
}
outputString(PHP_EOL.'Installing NodeJS packages via '.$command. ' (this may take a while)', Console::FG_YELLOW);
if (!empty($command)) {
$result = executeShellCommand($command);
outputString($result);
} else {
outputString('### unable to install NodeJS packages, yarn/npm not found', Console::FG_RED);
}
}
/**
* Output a string to the console, using optional $args to color it, if supported
*
* @param string $string
*
* @return mixed
*/
function outputString($string)
{
$stream = \STDOUT;
if (Console::streamSupportsAnsiColors($stream)) {
$args = func_get_args();
array_shift($args);
$string = Console::ansiFormat($string, $args);
}
return Console::stdout($string.PHP_EOL);
}
/**
* Create a symlink from $srcPath to $destPath
*
* @param string $srcPath
* @param string $destPath
*
* @return bool
*/
function createSymLink(string $srcPath, string $destPath)
{
$result = @symlink($srcPath, $destPath);
if ($result) {
outputString('- created symlink from '.$srcPath.' to '.$destPath);
} else {
outputString('### error creating symlink from '.$srcPath.' to '.$destPath, Console::FG_RED);
}
return $result;
}
/**
* Copy the $srcPath file to $destPath
*
* @param string $srcPath
* @param string $destPath
*
* @return bool
*/
function copyFile(string $srcPath, string $destPath)
{
$result = @copy($srcPath, $destPath);
if ($result) {
outputString('- copied '.$srcPath.' to '.$destPath);
} else {
outputString('### error copying '.$srcPath.' to '.$destPath, Console::FG_RED);
}
return $result;
}
/**
* Replace strings in $filePath and write out the modified file
*
* @param string $filePath
* @param array $replacements
*/
function replaceInFile(string $filePath, array $replacements)
{
if (!empty($replacements)) {
outputString('- setting up '.$filePath);
$fileContents = file_get_contents($filePath);
foreach ($replacements as $findValue => $params) {
$replaceValue = empty($params['env']) ? $params['default'] : getenv($params['env']) ?? $params['default'];
if (!empty($params['substr'])) {
$subStr = $params['substr'];
$wrapChar = '';
// If the $subStr is quoted, quote the $replaceValue, too
if ($subStr[0] == "'" || $subStr[0] == '"') {
$wrapChar = $subStr[0];
}
$replaceValue = str_replace($subStr, $wrapChar.$replaceValue.$wrapChar, $findValue);
}
$fileContents = str_replace($findValue, $replaceValue, $fileContents);
}
// Write the file out
file_put_contents($filePath, $fileContents);
}
}
/**
* Execute a shell command
*
* @param string $command
*
* @return string
*/
function executeShellCommand(string $command): string
{
// Create the shell command
$shellCommand = new ShellCommand();
$shellCommand->setCommand($command);
// If we don't have proc_open, maybe we've got exec
if (!function_exists('proc_open') && function_exists('exec')) {
$shellCommand->useExec = true;
}
// Return the result of the command's output or error
if ($shellCommand->execute()) {
$result = $shellCommand->getOutput();
} else {
$result = $shellCommand->getError();
}
return $result;
}
/**
* Return whether a shell command exists or not
*
* @param string $command
*
* @return bool
*/
function shellCommandExists(string $command): bool
{
$result = executeShellCommand('which '.$command);
return !empty($result);
}