Passed
Pull Request — master (#9)
by ANTHONIUS
02:22
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
     * @var Parameters
41
     */
42
    private $parameters;
43
44
    /**
45
     * @var Patcher
46
     */
47
    private $patcher;
48
49
    /**
50
     * @var ProcessRunner
51
     */
52
    private $processor;
53
54
    public function __construct(
55
        Parameters $parameters,
56
        Patcher $patcher,
57
        Downloader $downloader,
58
        ProcessRunner $processor,
59
        OutputInterface $output
60
    ) {
61
        $this->parameters = $parameters;
62
        $this->patcher = $patcher;
63
        $this->downloader = $downloader;
64
        $this->processor = $processor;
65
        $this->output = $output;
66
        $this->installScript = $parameters->get('dotfiles.temp_dir').'/nvm/installer.sh';
67
    }
68
69
    public function getBashPatch()
70
    {
71
        $installDir = $this->parameters->get('nvm.install_dir');
72
73
        return <<<EOC
74
# > NVM patch
75
 export NVM_DIR="$installDir/.nvm"
76
 [ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
77
# < NVM patch
78
EOC;
79
    }
80
81
    public function install()
82
    {
83
        if ($this->downloadInstallScript()) {
84
            $this->doInstall();
85
        }
86
    }
87
88
    private function doInstall()
89
    {
90
        $temp = $this->parameters->get('dotfiles.temp_dir');
91
        $home = $temp.'/nvm';
92
        $env = array(
93
            // temporary home directory
94
            'HOME' => $home,
95
96
            // nvm install location
97
            'NVM_DIR' => $this->parameters->get('nvm.install_dir'),
98
        );
99
100
        $runner = $this->processor;
101
        $runner->run(
102
            'bash '.$this->installScript,
103
            null,//callback
104
            null, //cwd
105
            $env
106
        );
107
108
        // ask patcher to run, to add nvm bash config
109
        $this->patcher->run();
110
    }
111
112
    private function downloadInstallScript()
113
    {
114
        $downloader = $this->downloader;
115
116
        $url = $this->getInstallScriptUrl();
117
118
        try {
119
            $downloader->run($url, $this->installScript);
120
121
            return true;
122
        } catch (\Exception $exception) {
123
            $this->output->writeln($exception->getMessage());
124
            $this->output->writeln('<error>Aborting installation, download error');
125
126
            return false;
127
        }
128
    }
129
130
    private function getInstallScriptUrl()
131
    {
132
        $installUrl = 'https://raw.githubusercontent.com/creationix/nvm/{VERSION}/install.sh';
133
        $command = 'git ls-remote --tags git://github.com/creationix/nvm.git | sort -t \'/\' -k 3 -V';
134
135
        $process = $this
136
            ->processor
137
            ->run($command)
138
        ;
139
        $output = $process->getOutput();
140
        $pattern = '/v[0-9\.]+/im';
141
        preg_match_all($pattern, $output, $matches);
142
        $versions = $matches[0];
143
        $current = $versions[count($versions) - 1];
144
        $url = str_replace('{VERSION}', $current, $installUrl);
145
146
        return $url;
147
    }
148
}
149