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

Installer::doInstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 12
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\DI\Parameters;
15
use Dotfiles\Core\Processor\Patcher;
16
use Dotfiles\Core\Processor\ProcessRunner;
17
use Dotfiles\Core\Util\Downloader;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class Installer.
22
 */
23
class Installer
24
{
25
    /**
26
     * @var Downloader
27
     */
28
    private $downloader;
29
30
    /**
31
     * @var string
32
     */
33
    private $installScript;
34
35
    /**
36
     * @var OutputInterface
37
     */
38
    private $output;
39
40
    /**
41
     * @var Parameters
42
     */
43
    private $parameters;
44
45
    /**
46
     * @var Patcher
47
     */
48
    private $patcher;
49
50
    /**
51
     * @var ProcessRunner
52
     */
53
    private $processor;
54
55
    public function __construct(
56
        Parameters $parameters,
57
        Patcher $patcher,
58
        Downloader $downloader,
59
        ProcessRunner $processor,
60
        OutputInterface $output
61
    ) {
62
        $this->parameters = $parameters;
63
        $this->patcher = $patcher;
64
        $this->downloader = $downloader;
65
        $this->processor = $processor;
66
        $this->output = $output;
67
        $this->installScript = $parameters->get('dotfiles.temp_dir').'/nvm/installer.sh';
68
    }
69
70
    public function getBashPatch()
71
    {
72
        $installDir = $this->parameters->get('nvm.install_dir');
73
74
        return <<<EOC
75
# > NVM patch
76
 export NVM_DIR="$installDir/.nvm"
77
 [ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
78
# < NVM patch
79
EOC;
80
    }
81
82
    public function install()
83
    {
84
        if ($this->downloadInstallScript()) {
85
            $this->doInstall();
86
        }
87
    }
88
89
    private function doInstall()
90
    {
91
        $temp = $this->parameters->get('dotfiles.temp_dir');
92
        $home = $temp.'/nvm';
93
        $env = array(
94
            // temporary home directory
95
            'HOME' => $home,
96
97
            // nvm install location
98
            'NVM_DIR' => $this->parameters->get('nvm.install_dir'),
99
        );
100
101
        $runner = $this->processor;
102
        $runner->run(
103
            'bash '.$this->installScript,
104
            null,//callback
105
            null, //cwd
106
            $env
107
        );
108
109
        // ask patcher to run, to add nvm bash config
110
        $this->patcher->run();
111
    }
112
113
    private function downloadInstallScript()
114
    {
115
        $downloader = $this->downloader;
116
117
        $url = $this->getInstallScriptUrl();
118
119
        try {
120
            $downloader->run($url, $this->installScript);
121
122
            return true;
123
        } catch (\Exception $exception) {
124
            $this->output->writeln($exception->getMessage());
125
            $this->output->writeln('<error>Aborting installation, download error');
126
127
            return false;
128
        }
129
    }
130
131
    private function getInstallScriptUrl()
132
    {
133
        $installUrl = 'https://raw.githubusercontent.com/creationix/nvm/{VERSION}/install.sh';
134
        $command = 'git ls-remote --tags git://github.com/creationix/nvm.git | sort -t \'/\' -k 3 -V';
135
136
        $process = $this
137
            ->processor
138
            ->run($command)
139
        ;
140
        $output = $process->getOutput();
141
        $pattern = '/v[0-9\.]+/im';
142
        preg_match_all($pattern, $output, $matches);
143
        $versions = $matches[0];
144
        $current = $versions[count($versions) - 1];
145
        $url = str_replace('{VERSION}', $current, $installUrl);
146
147
        return $url;
148
    }
149
}
150