-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy.php
More file actions
95 lines (79 loc) · 2.26 KB
/
Copy.php
File metadata and controls
95 lines (79 loc) · 2.26 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Phug\Split\Command;
use Exception;
use Phug\Split;
use Phug\Split\Command\Options\GitProgram;
use Phug\Split\Command\Options\HashPrefix;
use SimpleCli\SimpleCli;
/**
* Copy files from the mono-repository to a sub-package at the current linked revision.
*
* @psalm-suppress MissingConstructor
*/
class Copy extends CommandBase
{
use HashPrefix, GitProgram;
/**
* @argument
*
* Source mono-repository URL.
*
* @var string
*/
public $repository;
/**
* @argument
*
* Destination directory.
*
* @var string
*/
public $destination = '.';
/**
* @option
*
* Glob filters separated by commas to select the files to copy.
*
* @var string
*/
public $filters = 'composer.json';
/**
* @param Split|SimpleCli $cli
*
* @throws Exception
*
* @return bool
*
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
public function run(SimpleCli $cli): bool
{
$cli = $this->getSplitCli($cli);
if (!$this->repository) {
return $cli->error('Please provide an input repository URL.');
}
$hash = $this->getCurrentLinkedCommitHash();
if (!$hash) {
return $cli->error('Last commit must be linked to a mono-repository commit.');
}
/** @psalm-var truthy-string|false $destination */
$destination = @realpath($this->destination);
if (!$destination) {
return $cli->error('Destination directory "'.$this->destination.'" does not seem to exist.');
}
$workDirectory = sys_get_temp_dir().'/split-copy-'.mt_rand(0, 9999999);
mkdir($workDirectory, 0777, true);
chdir($workDirectory);
$this->git('clone '.$this->repository.' .', [], '2>&1');
$this->git("reset --hard $hash", [], '2>&1');
foreach (explode(',', $this->filters) as $filter) {
shell_exec(
'cp -r '.$filter.' '.escapeshellarg($destination.DIRECTORY_SEPARATOR).' || '.
'copy /Y '.$filter.' '.escapeshellarg($destination.DIRECTORY_SEPARATOR).' 2>&1'
);
}
$cli->writeLine('Copy ended.');
$this->remove($workDirectory);
return true;
}
}