1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Process; |
4
|
|
|
|
5
|
|
|
use Rawilk\LaravelModules\Module; |
6
|
|
|
|
7
|
|
|
class Updater extends Runner |
8
|
|
|
{ |
9
|
|
|
public function update(string $name): void |
10
|
|
|
{ |
11
|
|
|
$module = $this->module->findOrFail($name); |
12
|
|
|
|
13
|
|
|
chdir(base_path()); |
14
|
|
|
|
15
|
|
|
$this->installRequires($module) |
16
|
|
|
->installDevRequires($module) |
17
|
|
|
->copyScriptsToMainComposerJson($module); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function copyScriptsToMainComposerJson(Module $module): self |
21
|
|
|
{ |
22
|
|
|
$scripts = $module->getComposerAttr('scripts', []); |
23
|
|
|
|
24
|
|
|
$composer = json_decode(file_get_contents(base_path('composer.json')), true); |
25
|
|
|
|
26
|
|
|
foreach ($scripts as $key => $script) { |
27
|
|
|
if (array_key_exists($key, $composer['scripts'])) { |
28
|
|
|
$composer['scripts'][$key] = array_unique(array_merge($composer['scripts'][$key], $script)); |
29
|
|
|
|
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$composer['scripts'] = array_merge($composer['scripts'], [$key => $script]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
file_put_contents(base_path('composer.json'), json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function installDevRequires(Module $module): self |
42
|
|
|
{ |
43
|
|
|
$devPackages = $module->getComposerAttr('require-dev', []); |
44
|
|
|
|
45
|
|
|
$concatenatedPackages = ''; |
46
|
|
|
foreach ($devPackages as $name => $version) { |
47
|
|
|
$concatenatedPackages .= "\"{$name}:{$version}\" "; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (! empty($concatenatedPackages)) { |
51
|
|
|
$this->run("composer require --dev {$concatenatedPackages}"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function installRequires(Module $module): self |
58
|
|
|
{ |
59
|
|
|
$packages = $module->getComposerAttr('require', []); |
60
|
|
|
|
61
|
|
|
$concatenatedPackages = ''; |
62
|
|
|
foreach ($packages as $name => $version) { |
63
|
|
|
$concatenatedPackages .= "\"{$name}:{$version}\" "; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (! empty($concatenatedPackages)) { |
67
|
|
|
$this->run("composer require {$concatenatedPackages}"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|