|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the dotfiles project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Anthonius Munthi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Dotfiles\Plugins\Bash; |
|
15
|
|
|
|
|
16
|
|
|
use Dotfiles\Core\Config\Config; |
|
17
|
|
|
use Dotfiles\Core\Constant; |
|
18
|
|
|
use Dotfiles\Core\Event\Dispatcher; |
|
19
|
|
|
use Dotfiles\Core\Event\PatchEvent; |
|
20
|
|
|
use Dotfiles\Core\Util\Toolkit; |
|
21
|
|
|
use Dotfiles\Plugins\Bash\Event\ReloadBashConfigEvent; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
24
|
|
|
|
|
25
|
|
|
class EventSubscriber implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Config |
|
29
|
|
|
*/ |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Dispatcher |
|
34
|
|
|
*/ |
|
35
|
|
|
private $dispatcher; |
|
36
|
|
|
|
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(Dispatcher $dispatcher, Config $config, LoggerInterface $logger) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->dispatcher = $dispatcher; |
|
42
|
|
|
$this->config = $config; |
|
43
|
|
|
$this->logger = $logger; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function getSubscribedEvents() |
|
47
|
|
|
{ |
|
48
|
|
|
return array( |
|
49
|
|
|
Constant::EVENT_PRE_PATCH => array( |
|
50
|
|
|
array('onPrePatchEvent', -999), |
|
51
|
|
|
), |
|
52
|
|
|
Constant::EVENT_POST_RESTORE => array( |
|
53
|
|
|
array('onPostRestore', -999), |
|
54
|
|
|
), |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param PatchEvent $event |
|
60
|
|
|
*/ |
|
61
|
|
|
public function onPrePatchEvent(PatchEvent $event): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->doBashPatch($event); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function doBashPatch(PatchEvent $event): void |
|
67
|
|
|
{ |
|
68
|
|
|
$currentPatches = $event->getPatches(); |
|
69
|
|
|
$bashPatch = array(); |
|
70
|
|
|
if (array_key_exists('.bashrc', $currentPatches)) { |
|
71
|
|
|
$bashPatch = $currentPatches['.bashrc']; |
|
72
|
|
|
} |
|
73
|
|
|
$reloadEvent = new ReloadBashConfigEvent($this->logger); |
|
74
|
|
|
$reloadEvent->addFooterConfig(implode("\n", $bashPatch)); |
|
75
|
|
|
|
|
76
|
|
|
$this->dispatcher->dispatch(ReloadBashConfigEvent::NAME, $reloadEvent); |
|
77
|
|
|
$this->generateDotfilesConfig($reloadEvent->getBashConfig()); |
|
78
|
|
|
|
|
79
|
|
|
$installDir = $this->config->get('dotfiles.install_dir'); |
|
80
|
|
|
$target = '.bashrc'; |
|
81
|
|
|
|
|
82
|
|
|
$event->setPatch($target, array("source \"${installDir}/bashrc\"")); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function generateDotfilesConfig($bashConfig): void |
|
86
|
|
|
{ |
|
87
|
|
|
$installDir = $this->config->get('dotfiles.install_dir'); |
|
88
|
|
|
|
|
89
|
|
|
$uname = php_uname(); |
|
90
|
|
|
if (false !== strpos('darwin', $uname)) { |
|
91
|
|
|
$fileName = 'bash_profile'; |
|
92
|
|
|
} else { |
|
93
|
|
|
$fileName = 'bashrc'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// write config into dotfiles location |
|
97
|
|
|
$contents = <<<EOC |
|
98
|
|
|
|
|
99
|
|
|
# WARNING!!! |
|
100
|
|
|
# This file is generated automatically by DOTFILES installer |
|
101
|
|
|
# All changes in this file will be overwrite later with DOTFILES |
|
102
|
|
|
|
|
103
|
|
|
export PATH="{$installDir}/bin:\$PATH" |
|
104
|
|
|
$bashConfig |
|
105
|
|
|
|
|
106
|
|
|
# END DOTFILES CONFIG |
|
107
|
|
|
|
|
108
|
|
|
EOC; |
|
109
|
|
|
Toolkit::ensureFileDir($file = $installDir.DIRECTORY_SEPARATOR.$fileName); |
|
110
|
|
|
file_put_contents($file, $contents, LOCK_EX); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|