Skip to content

Commit b13d97c

Browse files
committed
Adding install command and fixing the base configs
1 parent 9735def commit b13d97c

File tree

6 files changed

+498
-14
lines changed

6 files changed

+498
-14
lines changed

app/commands/InstallCommand.php

Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
<?php
2+
3+
use Illuminate\Console\Command;
4+
use Symfony\Component\Console\Input\InputOption;
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Symfony\Component\Console\Output\StreamOutput;
7+
8+
class InstallCommand extends Command {
9+
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'syntax:install';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Run the everything needed to get a syntax site up and running.';
23+
24+
/**
25+
* An array of available syntax packages
26+
*
27+
* @var string[]
28+
*/
29+
protected $syntaxPackages = ['chat', 'forum'];
30+
31+
/**
32+
* An array of packages that will need a config loaded
33+
*
34+
* @var string[]
35+
*/
36+
protected $syntaxPackagesWithConfig = ['chat'];
37+
38+
/**
39+
* An object containing the remote config details
40+
*
41+
* @var string[]
42+
*/
43+
protected $remoteDetails;
44+
45+
/**
46+
* An object containing the database config details
47+
*
48+
* @var string[]
49+
*/
50+
protected $databaseDetails;
51+
52+
/**
53+
* An object containing the core syntax config details
54+
*
55+
* @var string[]
56+
*/
57+
protected $syntaxCoreDetails;
58+
59+
/**
60+
* The URL for the this site
61+
*
62+
* @var string
63+
*/
64+
protected $siteUrl;
65+
66+
/**
67+
* The output stream for any artisan commands
68+
*
69+
* @var string
70+
*/
71+
protected $stream;
72+
73+
/**
74+
* Create a new command instance.
75+
*
76+
* @return void
77+
*/
78+
public function __construct()
79+
{
80+
parent::__construct();
81+
82+
$this->remoteDetails = new stdClass();
83+
$this->databaseDetails = new stdClass();
84+
$this->syntaxCoreDetails = new stdClass();
85+
}
86+
87+
/**
88+
* Execute the console command.
89+
*
90+
* @return mixed
91+
*/
92+
public function fire()
93+
{
94+
// Set up the variables
95+
$this->stream = fopen('php://output', 'w');
96+
97+
// Set up the configs
98+
$this->setUpRemote();
99+
$this->setUpDatabase();
100+
$this->setUpSyntaxCore();
101+
$this->setUpApp();
102+
103+
// Get out syntax packages
104+
$this->updateSyntax();
105+
106+
// Run the installation
107+
$this->runArtisan();
108+
109+
// Run gulp commands
110+
$this->runGulp();
111+
112+
// Clean up
113+
$this->cleanUp();
114+
115+
$this->comment('Installation complete!');
116+
}
117+
118+
/********************************************************************
119+
* Unique Methods
120+
*******************************************************************/
121+
protected function runArtisan()
122+
{
123+
$this->comment('Running artisan commands...');
124+
Artisan::call('key:generate', null, new StreamOutput($this->stream));
125+
Artisan::call('migrate:install', null, new StreamOutput($this->stream));
126+
Artisan::call('syntax:database', null, new StreamOutput($this->stream));
127+
Artisan::call('syntax:gulp', null, new StreamOutput($this->stream));
128+
}
129+
130+
protected function runGulp()
131+
{
132+
$this->comment('Running gulp commands...');
133+
$commands = [
134+
'cd '. base_path(),
135+
'gulp install'
136+
];
137+
138+
SSH::run($commands, function ($line) {
139+
echo $line.PHP_EOL;
140+
});
141+
}
142+
143+
protected function cleanUp()
144+
{
145+
$this->comment('Running clean up commands...');
146+
$commands = [
147+
'cd '. base_path(),
148+
'chmod 755 public',
149+
'chmod 755 public/index'
150+
];
151+
152+
SSH::run($commands, function ($line) {
153+
echo $line.PHP_EOL;
154+
});
155+
}
156+
157+
protected function confirmConfig($type, $configDetails = null)
158+
{
159+
$this->line('Your '. $type .' configuration will be set to the following.');
160+
161+
switch ($type) {
162+
case 'remote':
163+
$this->line(print_r($this->remoteDetails, 1));
164+
if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) {
165+
return $this->setUpRemote();
166+
} else {
167+
return $this->configureRemote();
168+
}
169+
break;
170+
case 'database':
171+
$this->line(print_r($this->databaseDetails, 1));
172+
if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) {
173+
return $this->setUpDatabase();
174+
} else {
175+
return $this->configureDatabase();
176+
}
177+
break;
178+
case 'core':
179+
$this->line(print_r($this->syntaxCoreDetails, 1));
180+
if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) {
181+
return $this->setUpSyntaxCore();
182+
} else {
183+
return $this->configureSyntaxCore();
184+
}
185+
break;
186+
default:
187+
$this->line(print_r($configDetails, 1));
188+
if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) {
189+
return $this->setUpSyntaxCore();
190+
} else {
191+
return $this->configureSyntaxCore();
192+
}
193+
break;
194+
}
195+
}
196+
197+
protected function updateSyntax()
198+
{
199+
foreach ($this->syntaxPackages as $package) {
200+
if ($this->confirm('Do you wish to install syntax\\'. $package .'? [yes|no]')) {
201+
$this->comment('Installing syntax\\'. $package .'...');
202+
203+
$commands = [
204+
'cd '. base_path(),
205+
'composer require syntax/'. $package .':dev-master',
206+
];
207+
208+
if (in_array($package, $this->syntaxPackagesWithConfig) && !File::exists(app_path('config/packages/syntax/chat'))) {
209+
$commands[] = 'php artisan config:publish syntax/'. $package;
210+
}
211+
212+
SSH::run($commands, function ($line) {
213+
echo $line.PHP_EOL;
214+
});
215+
216+
$this->setUpSyntax($package);
217+
}
218+
}
219+
}
220+
221+
/********************************************************************
222+
* Set Up Methods
223+
*******************************************************************/
224+
protected function setUpRemote()
225+
{
226+
// Set up our remote config
227+
$this->comment('Setting up remote config options...');
228+
$this->remoteDetails->host = $this->ask('What is your remote host? (for ports add :<PORT> to the end)');
229+
$this->remoteDetails->username = $this->ask('What is your remote username?');
230+
$this->remoteDetails->password = $this->secret('What is your remote password?');
231+
$this->remoteDetails->key = $this->ask('Where is your remote rsa key?');
232+
$this->remoteDetails->keyphrase = $this->ask('What is the passphrase?');
233+
$this->remoteDetails->root = $this->ask('Where is your remote root?');
234+
235+
$this->confirmConfig('remote');
236+
}
237+
238+
protected function setUpDatabase()
239+
{
240+
// Set up our database config
241+
$this->comment('Setting up datatabase details...');
242+
$this->databaseDetails->driver = $this->ask('What is your database driver? [Hit enter to use mysql driver]', 'mysql');
243+
$this->databaseDetails->host = $this->ask('What is your database host?');
244+
$this->databaseDetails->database = $this->ask('What is the database name?');
245+
$this->databaseDetails->username = $this->ask('What is your database username?');
246+
$this->databaseDetails->password = $this->secret('What is your database password?');
247+
$this->databaseDetails->charset = $this->ask('What is your database charset? [Hit enter to use utf8]', 'utf8');
248+
$this->databaseDetails->collation = $this->ask('What is your database collation? [Hit enter to use utf8_unicode_ci]', 'utf8_unicode_ci');
249+
$this->databaseDetails->prefix = $this->ask('What is your database prefix?');
250+
251+
$this->confirmConfig('database');
252+
}
253+
254+
protected function setUpSyntaxCore()
255+
{
256+
// Set up our syntax config
257+
$this->comment('Setting up syntax details...');
258+
$this->syntaxCoreDetails->controlRoomDetail = $this->ask('What is this site\'s control room name?');
259+
$this->syntaxCoreDetails->siteName = $this->ask('What is this name to display for this site?');
260+
$this->syntaxCoreDetails->siteIcon = $this->ask('What is this icon to display for this site? (Use tha last part of the font-awesome icon class)');
261+
$this->syntaxCoreDetails->menu = $this->ask('What is menu style should this site default to? (utopian or twitter)');
262+
263+
$this->confirmConfig('core');
264+
}
265+
266+
protected function setUpSyntax($package)
267+
{
268+
switch ($package) {
269+
case 'chat':
270+
return $this->setUpSyntaxChat();
271+
break;
272+
}
273+
}
274+
275+
protected function setUpSyntaxChat()
276+
{
277+
// Set up our syntax config
278+
$this->comment('Setting up syntax details...');
279+
$chatDetails = new stdClass();
280+
$chatDetails->debug = $this->confirm('Should the chats show debug info? [Hit enter to leave as true]', true) ? true : false;
281+
$chatDetails->port = $this->ask('What is the chat port? [Hit enter to leave as 1337]', 1337);
282+
$chatDetails->backLog = $this->ask('How much back log should the chats get? [Hit enter to leave as 100]', 100);
283+
$chatDetails->backFill = $this->ask('How much should the chats backfil on connect? [Hit enter to leave as 30]', 30);
284+
$chatDetails->apiEndPoint = $this->ask('What is the chat url? [Hit enter to leave as '. $this->siteUrl .']', $this->siteUrl);
285+
$chatDetails->connectionMessage = $this->confirm('Should the chats show a connection message? [Hit enter to leave as true]', true) ? true : false;
286+
287+
$chatConfig = json_encode($chatDetails, JSON_PRETTY_PRINT);
288+
289+
$this->line('Your chat configuration will be set to the following.');
290+
$this->line($chatConfig ."\n");
291+
if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) {
292+
return $this->setUpSyntaxChat();
293+
} else {
294+
return $this->configureSyntaxChat($chatConfig);
295+
}
296+
}
297+
298+
protected function setUpApp()
299+
{
300+
// Set up our app config
301+
$this->comment('Setting up app details...');
302+
$this->siteUrl = $this->ask('What is this site\'s url?');
303+
304+
list($path, $contents) = $this->getConfig('app');
305+
306+
$contents = str_replace($this->laravel['config']['app.url'], $this->siteUrl, $contents);
307+
308+
File::put($path, $contents);
309+
}
310+
311+
/********************************************************************
312+
* Configuration Methods
313+
*******************************************************************/
314+
protected function configureRemote()
315+
{
316+
list($path, $contents) = $this->getConfig('remote');
317+
318+
foreach ($this->remoteDetails as $key => $value) {
319+
$contents = str_replace($this->laravel['config']['remote.connections.default.'. $key], $value, $contents);
320+
}
321+
322+
File::put($path, $contents);
323+
}
324+
325+
protected function configureDatabase()
326+
{
327+
list($path, $contents) = $this->getConfig('database');
328+
329+
foreach ($this->databaseDetails as $key => $value) {
330+
$contents = str_replace($this->laravel['config']['database.connections.mysql.'. $key], $value, $contents);
331+
}
332+
333+
File::put($path, $contents);
334+
}
335+
336+
protected function configureSyntaxCore()
337+
{
338+
list($path, $contents) = $this->getConfig('packages/syntax/core/config');
339+
340+
foreach ($this->syntaxCoreDetails as $key => $value) {
341+
$contents = str_replace($this->laravel['config']['core::'. $key], $value, $contents);
342+
}
343+
344+
File::put($path, $contents);
345+
}
346+
347+
protected function configureSyntaxChat($config)
348+
{
349+
$path = $this->laravel['path'].'/config/packages/syntax/chat/chatConfig.json';
350+
File::put($path, $config);
351+
}
352+
353+
/********************************************************************
354+
* Extra Methods
355+
*******************************************************************/
356+
protected function getConfig($file)
357+
{
358+
$path = $this->laravel['path'].'/config/'. $file .'.php';
359+
360+
$contents = File::get($path);
361+
362+
return array($path, $contents);
363+
}
364+
365+
/**
366+
* Get the console command arguments.
367+
*
368+
* @return array
369+
*/
370+
protected function getArguments()
371+
{
372+
return array(
373+
// array('example', InputArgument::REQUIRED, 'An example argument.'),
374+
);
375+
}
376+
377+
/**
378+
* Get the console command options.
379+
*
380+
* @return array
381+
*/
382+
protected function getOptions()
383+
{
384+
return array(
385+
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
386+
);
387+
}
388+
389+
}

0 commit comments

Comments
 (0)