forked from LastCallMedia/ComposerExtraFiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGitHandler.php
66 lines (59 loc) · 1.93 KB
/
GitHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace LastCall\DownloadsPlugin\Handler;
use Composer\Composer;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Util\Git;
use Composer\Util\ProcessExecutor;
use Composer\Util\Filesystem;
use React\Promise\PromiseInterface;
class GitHandler extends BaseHandler
{
const TMP_PREFIX = '.composer-extra-tmp-';
public function createSubpackage()
{
$pkg = parent::createSubpackage();
$pkg->setDistType('git');
return $pkg;
}
public function getTrackingFile()
{
$file = basename($this->extraFile['id']) . '-' . md5($this->extraFile['id']) . '.json';
return
dirname($this->getTargetPath()) .
DIRECTORY_SEPARATOR . self::DOT_DIR .
DIRECTORY_SEPARATOR . $file;
}
/**
* @param Composer $composer
* @param IOInterface $io
*/
public function download(Composer $composer, IOInterface $io) {
$urlAndVersion = $this->getSubpackage()->getDistUrl();
$config = $composer->getConfig();
$wd = $this->getTargetPath();
$process = new ProcessExecutor($io);
$cfs = new Filesystem();
$git = new Git($io, $config, $process, $cfs);
if (file_exists($wd)) {
$cfs->removeDirectory($wd);
}
// Make the directory recursively.
$cfs->ensureDirectoryExists($wd);
$gitCallable = static function ($urlAndVersion): string {
$parts = explode('@', $urlAndVersion);
$url = $parts[0];
if (count($parts) > 1) {
$version = $parts[1];
}
else {
$version = 'master';
}
return sprintf('git init && git fetch %s "+refs/heads/*:refs/remotes/origin/*" && git checkout %s',
ProcessExecutor::escape($url),
ProcessExecutor::escape($version)
);
};
$git->runCommand($gitCallable, $urlAndVersion, $wd);
}
}