-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Update.php
191 lines (136 loc) · 4.36 KB
/
Update.php
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
<?php
namespace App\Console\Commands;
use App\Events\Install\UpdateCopied;
use App\Events\Install\UpdateDownloaded;
use App\Events\Install\UpdateFailed;
use App\Events\Install\UpdateUnzipped;
use App\Jobs\Install\CopyFiles;
use App\Jobs\Install\DownloadFile;
use App\Jobs\Install\FinishUpdate;
use App\Jobs\Install\UnzipFile;
use App\Traits\Jobs;
use App\Utilities\Versions;
use Illuminate\Console\Command;
class Update extends Command
{
use Jobs;
const CMD_SUCCESS = 0;
const CMD_ERROR = 1;
public $alias;
public $company;
public $new;
public $old;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update {alias} {company} {new=latest}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Allows to update Akaunting and modules directly through CLI';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
set_time_limit(3600); // 1 hour
$this->alias = $this->argument('alias');
$this->company = $this->argument('company');
if (false === $this->new = $this->getNewVersion()) {
$message = 'Not able to get the latest version of ' . $this->alias . '!';
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Version', $message));
return self::CMD_ERROR;
}
$this->old = $this->getOldVersion();
company($this->company)->makeCurrent();
if (!$path = $this->download()) {
return self::CMD_ERROR;
}
if (!$this->unzip($path)) {
return self::CMD_ERROR;
}
if (!$this->copyFiles($path)) {
return self::CMD_ERROR;
}
if (!$this->finish()) {
return self::CMD_ERROR;
}
return self::CMD_SUCCESS;
}
public function getNewVersion()
{
return ($this->argument('new') == 'latest') ? Versions::latest($this->alias)?->latest : $this->argument('new');
}
public function getOldVersion()
{
if ($this->alias == 'core') {
return version('short');
}
if ($module = module($this->alias)) {
return $module->get('version');
}
return '1.0.0';
}
public function download()
{
$this->info('Downloading update...');
try {
$path = $this->dispatch(new DownloadFile($this->alias, $this->new));
event(new UpdateDownloaded($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Download', $message));
return false;
}
return $path;
}
public function unzip($path)
{
$this->info('Unzipping update...');
try {
$this->dispatch(new UnzipFile($this->alias, $path));
event(new UpdateUnzipped($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Unzip', $message));
return false;
}
return true;
}
public function copyFiles($path)
{
$this->info('Copying update files...');
try {
$this->dispatch(new CopyFiles($this->alias, $path));
event(new UpdateCopied($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Copy Files', $message));
return false;
}
return true;
}
public function finish()
{
$this->info('Finishing update...');
try {
$this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old, $this->company));
} catch (\Exception $e) {
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Finish', $message));
return false;
}
return true;
}
}