Skip to content

Commit b4f57fe

Browse files
committed
Adding version control and commands
1 parent c621291 commit b4f57fe

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"autoload": {
2020
"classmap": [
21+
"src/commands",
2122
"src/controllers",
2223
"src/database/migrations",
2324
"src/database/seeds",

src/Syntax/Forum/ForumServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class ForumServiceProvider extends ServiceProvider {
1111
*/
1212
protected $defer = false;
1313

14+
const version = '1.0.0';
15+
1416
/**
1517
* Bootstrap the application events.
1618
*

src/commands/UpdateForumCommand.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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 UpdateForumCommand extends Command {
9+
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'syntax:update-forum';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Perform any steps needed for syntax\chat updates.';
23+
24+
/**
25+
* The output stream for any artisan commands
26+
*
27+
* @var string
28+
*/
29+
protected $stream;
30+
31+
/**
32+
* Create a new command instance.
33+
*
34+
* @return void
35+
*/
36+
public function __construct()
37+
{
38+
parent::__construct();
39+
40+
$this->stream = fopen('php://output', 'w');
41+
}
42+
43+
/**
44+
* Execute the console command.
45+
*
46+
* @return mixed
47+
*/
48+
public function fire()
49+
{
50+
// Handle Forum
51+
if ($this->confirm('Do you want to update Syntax\Forum? [yes|no]')) {
52+
$requestedVersion = $this->ask('What version of forum do you require? [Hit enter to leave as dev-master]', 'dev-master');
53+
54+
$this->comment('Running composer update...');
55+
$this->composerUpdate($requestedVersion);
56+
57+
$currentVersion = Config::get('forum::version');
58+
$newVersion = Syntax\Forum\ForumServiceProvider::version;
59+
60+
$this->comment('Checking for update steps to perform...');
61+
$this->update($currentVersion, $newVersion);
62+
63+
$this->comment('Updating the version in the config...');
64+
$this->updateVersion($newVersion);
65+
}
66+
67+
$this->comment('Done!');
68+
}
69+
70+
/********************************************************************
71+
* Update Methods
72+
*******************************************************************/
73+
public function update($start, $end)
74+
{
75+
$versionRange = range($this->cleanVersion($start), $this->cleanVersion($end));
76+
77+
if (count($versionRange) == 1) {
78+
$this->comment('Already on the latest version: '. $end);
79+
return true;
80+
}
81+
82+
$performedUpdate = false;
83+
84+
foreach ($versionRange as $version) {
85+
if ($version == $start) {
86+
$previousVersion = $version;
87+
continue;
88+
}
89+
90+
$method = 'update'. $previousVersion .'To'. $version;
91+
92+
if (method_exists($this, $method)) {
93+
$performedUpdate = true;
94+
95+
$this->comment('Updating from version '. $previousVersion .' to '. $version .'...');
96+
}
97+
98+
$previousVersion = $version;
99+
}
100+
101+
if ($performedUpdate === false) {
102+
$this->comment('No updates needed...');
103+
}
104+
}
105+
106+
/********************************************************************
107+
* Helper Methods
108+
*******************************************************************/
109+
protected function composerUpdate($version)
110+
{
111+
$commands = [
112+
'cd '. base_path(),
113+
'composer update syntax/forum:'. $version,
114+
];
115+
116+
SSH::run($commands, function ($line) {
117+
echo $line.PHP_EOL;
118+
});
119+
}
120+
121+
protected function cleanVersion($version)
122+
{
123+
return str_replace('.', '', $version);
124+
}
125+
126+
protected function updateVersion($version)
127+
{
128+
list($path, $contents) = $this->getConfig('packages/syntax/forum/config.php');
129+
130+
$contents = str_replace($this->laravel['config']['forum::version'], $version, $contents);
131+
132+
File::put($path, $contents);
133+
}
134+
135+
/********************************************************************
136+
* Extra Methods
137+
*******************************************************************/
138+
protected function getConfig($file)
139+
{
140+
$path = $this->laravel['path'].'/config/'. $file;
141+
142+
$contents = File::get($path);
143+
144+
return array($path, $contents);
145+
}
146+
147+
/**
148+
* Get the console command arguments.
149+
*
150+
* @return array
151+
*/
152+
protected function getArguments()
153+
{
154+
return array(
155+
// array('example', InputArgument::REQUIRED, 'An example argument.'),
156+
);
157+
}
158+
159+
/**
160+
* Get the console command options.
161+
*
162+
* @return array
163+
*/
164+
protected function getOptions()
165+
{
166+
return array(
167+
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
168+
);
169+
}
170+
171+
}

src/config/config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
return array(
44

5+
/*
6+
|--------------------------------------------------------------------------
7+
| Current version
8+
|--------------------------------------------------------------------------
9+
|
10+
| The current version of Syntax\Forum the site is using
11+
*/
12+
'version' => '1.0.0',
13+
514
/*
615
|--------------------------------------------------------------------------
716
| Application Forum for News

0 commit comments

Comments
 (0)