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