Passed
Pull Request — master (#9)
by ANTHONIUS
02:18
created

Installer::doInstall()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the dotfiles project.
5
 *
6
 *     (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Dotfiles\Plugins\NVM;
13
14
use Dotfiles\Core\Constant;
15
use Dotfiles\Core\DI\Parameters;
16
use Dotfiles\Core\Event\Dispatcher;
17
use Dotfiles\Core\Processor\ProcessRunner;
18
use Dotfiles\Core\Util\Downloader;
19
use Dotfiles\Core\Util\Toolkit;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Class Installer.
24
 */
25
class Installer
26
{
27
    /**
28
     * @var Dispatcher
29
     */
30
    private $dispatcher;
31
32
    /**
33
     * @var Downloader
34
     */
35
    private $downloader;
36
37
    /**
38
     * @var string
39
     */
40
    private $installScript;
41
42
    /**
43
     * @var OutputInterface
44
     */
45
    private $output;
46
47
    /**
48
     * @var Parameters
49
     */
50
    private $parameters;
51
52
    /**
53
     * @var ProcessRunner
54
     */
55
    private $runner;
56
57
    public function __construct(
58
        Parameters $parameters,
59
        Dispatcher $dispatcher,
60
        Downloader $downloader,
61
        ProcessRunner $processor,
62
        OutputInterface $output
63
    ) {
64
        $this->parameters = $parameters;
65
        $this->dispatcher = $dispatcher;
66
        $this->downloader = $downloader;
67
        $this->runner = $processor;
68
        $this->output = $output;
69
        $this->installScript = $parameters->get('dotfiles.temp_dir').'/nvm/installer.sh';
70
    }
71
72
    public function getBashPatch()
73
    {
74
        $installDir = $this->parameters->get('nvm.install_dir');
75
76
        return <<<EOC
77
# > NVM patch
78
 export NVM_DIR="$installDir/.nvm"
79
 [ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
80
# < NVM patch
81
EOC;
82
    }
83
84
    public function install()
85
    {
86
        if ($this->downloadInstallScript()) {
87
            $this->doInstall();
88
        }
89
    }
90
91
    private function doInstall()
92
    {
93
        $installDir = $this->parameters->get('nvm.install_dir');
94
        $temp = $this->parameters->get('dotfiles.temp_dir');
95
        $home = $temp.'/nvm';
96
        $env = array(
97
            // temporary home directory
98
            'HOME' => $home,
99
100
            // nvm install location
101
            'NVM_DIR' => $installDir,
102
        );
103
        // create fake .bashrc file to disable error
104
        touch($home.'/.bashrc');
105
        Toolkit::ensureDir($installDir);
106
        $runner = $this->runner;
107
        $runner->run(
108
            'bash '.$this->installScript,
109
            null,//callback
110
            null, //cwd
111
            $env
112
        );
113
114
        // ask patcher to run, to add nvm bash config
115
        $this->dispatcher->dispatch(Constant::EVENT_PATCH);
116
    }
117
118
    private function downloadInstallScript()
119
    {
120
        $downloader = $this->downloader;
121
122
        $url = $this->getInstallScriptUrl();
123
124
        try {
125
            $downloader->run($url, $this->installScript);
126
127
            return true;
128
        } catch (\Exception $exception) {
129
            $this->output->writeln($exception->getMessage());
130
            $this->output->writeln('<error>Aborting installation, download error');
131
132
            return false;
133
        }
134
    }
135
136
    private function getInstallScriptUrl()
137
    {
138
        $tempFile = $this->parameters->get('nvm.temp_dir').'/versions.txt';
139
        Toolkit::ensureFileDir($tempFile);
140
        $installUrl = 'https://raw.githubusercontent.com/creationix/nvm/{VERSION}/install.sh';
141
        $command = 'git ls-remote --tags git://github.com/creationix/nvm.git > '.$tempFile;
142
143
        $this
144
            ->runner
145
            ->run($command)
146
        ;
147
        $output = file_get_contents($tempFile);
148
        $pattern = '/v[0-9\.]+/im';
149
        preg_match_all($pattern, $output, $matches);
150
        $versions = $matches[0];
151
        sort($versions, SORT_NATURAL);
152
153
        $current = $versions[count($versions) - 1];
154
        $url = str_replace('{VERSION}', $current, $installUrl);
155
156
        return $url;
157
    }
158
}
159