|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Process; |
|
4
|
|
|
|
|
5
|
|
|
use Nwidart\Modules\Module; |
|
6
|
|
|
|
|
7
|
|
|
class Updater extends Runner |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Update the dependencies for the specified module by given the module name. |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $module |
|
13
|
|
|
*/ |
|
14
|
|
|
public function update($module) |
|
15
|
|
|
{ |
|
16
|
|
|
$module = $this->module->findOrFail($module); |
|
17
|
|
|
|
|
18
|
|
|
chdir(base_path()); |
|
19
|
|
|
|
|
20
|
|
|
$this->installRequires($module); |
|
21
|
|
|
$this->installDevRequires($module); |
|
22
|
|
|
$this->copyScriptsToMainComposerJson($module); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Check if composer should output anything. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
private function isComposerSilenced() |
|
31
|
|
|
{ |
|
32
|
|
|
return config('modules.composer.composer-output') === false ? ' --quiet' : ''; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Module $module |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
private function installRequires(Module $module) |
|
39
|
|
|
{ |
|
40
|
|
|
$packages = $module->getComposerAttr('require', []); |
|
41
|
|
|
|
|
42
|
|
|
$concatenatedPackages = ''; |
|
43
|
|
|
foreach ($packages as $name => $version) { |
|
44
|
|
|
$concatenatedPackages .= "\"{$name}:{$version}\" "; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (!empty($concatenatedPackages)) { |
|
48
|
|
|
$this->run("composer require {$concatenatedPackages}{$this->isComposerSilenced()}"); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Module $module |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
private function installDevRequires(Module $module) |
|
56
|
|
|
{ |
|
57
|
|
|
$devPackages = $module->getComposerAttr('require-dev', []); |
|
58
|
|
|
|
|
59
|
|
|
$concatenatedPackages = ''; |
|
60
|
|
|
foreach ($devPackages as $name => $version) { |
|
61
|
|
|
$concatenatedPackages .= "\"{$name}:{$version}\" "; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($concatenatedPackages)) { |
|
65
|
|
|
$this->run("composer require --dev {$concatenatedPackages}{$this->isComposerSilenced()}"); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param Module $module |
|
71
|
|
|
*/ |
|
72
|
|
|
private function copyScriptsToMainComposerJson(Module $module) |
|
73
|
|
|
{ |
|
74
|
|
|
$scripts = $module->getComposerAttr('scripts', []); |
|
75
|
|
|
|
|
76
|
|
|
$composer = json_decode(file_get_contents(base_path('composer.json')), true); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($scripts as $key => $script) { |
|
79
|
|
|
if (array_key_exists($key, $composer['scripts'])) { |
|
80
|
|
|
$composer['scripts'][$key] = array_unique(array_merge($composer['scripts'][$key], $script)); |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
$composer['scripts'] = array_merge($composer['scripts'], [$key => $script]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
file_put_contents(base_path('composer.json'), json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|