-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBase.php
More file actions
154 lines (139 loc) · 3.96 KB
/
CommandBase.php
File metadata and controls
154 lines (139 loc) · 3.96 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Phug\Split\Command;
use Exception;
use Phug\Split;
use Phug\Split\Git\Commit;
use Phug\Split\Git\Log;
use Phug\Split\InvalidCli;
use SimpleCli\CommandBase as Command;
use SimpleCli\SimpleCli;
/**
* @property-read string $gitProgram Git program path.
* @property-read string $hashPrefix Prefix for split linked commit hashes.
*/
abstract class CommandBase extends Command
{
/**
* Assert the given CLI instance is a Split CLI instance.
*
* @param SimpleCli $cli
*
* @return Split
*/
protected function getSplitCli(SimpleCli $cli): Split
{
if ($cli instanceof Split) {
return $cli;
}
throw new InvalidCli($cli);
}
/**
* Escape a value for git command argument or option.
*
* @param string $value
*
* @return string
*/
protected function gitEscape(string $value): string
{
return '"'.addcslashes($value, '"').'"';
}
/**
* Get a command using git program.
*
* @param string $command Git command and mandatory arguments
* @param array $options CLI git command options
* @param string|null $redirect redirection suffix (like '2>&1')
*
* @psalm-param truthy-string|null $redirect
*
* @return string
*
* @psalm-suppress UndefinedThisPropertyFetch
*/
protected function getGitCommand(string $command, array $options = [], ?string $redirect = null): string
{
foreach ($options as $name => $value) {
$command .= ' --'.$name.'='.$this->gitEscape($value);
}
return $this->gitProgram.' '.$command.($redirect ? ' '.$redirect : '');
}
/**
* Execute a command using git program.
*
* @param string $command Git command and mandatory arguments
* @param array $options CLI git command options
* @param string|null $redirect redirection suffix (like '2>&1')
*
* @psalm-param truthy-string|null $redirect
*
* @return string
*/
protected function git(string $command, array $options = [], ?string $redirect = null): string
{
$command = $this->getGitCommand($command, $options, $redirect);
return (string) shell_exec($command);
}
/**
* Return given count of latest commits as Log instance (collection of Commit instances).
*
* @param int $count
* @param string $directory
*
* @throws Exception
*
* @suppressWarnings(PHPMD.StaticAccess)
*
* @return Log
*/
protected function latest(int $count = 1, string $directory = ''): Log
{
return Log::fromGitLogString($this->git("log --pretty=fuller --max-count=$count $directory") ?: '');
}
/**
* Return the last commit.
*
* @param string $directory
*
* @throws Exception
*
* @return Commit
*/
protected function last(string $directory = ''): Commit
{
return $this->latest(1, $directory)[0];
}
/**
* Get the hash of the linked commit in the mono-repository for the last sub-package commit.
*
* @throws Exception
*
* @return string|null
*
* @psalm-return truthy-string|null
*
* @psalm-suppress UndefinedThisPropertyFetch
*/
protected function getCurrentLinkedCommitHash(): ?string
{
return $this->last()->findInMessage('/^'.preg_quote($this->hashPrefix).'(.+)$/m');
}
/**
* Remove a file or a directory even if not empty.
*
* @param string $fileOrDirectory
*
* @return bool
*
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
protected function remove(string $fileOrDirectory): bool
{
if (!is_writable($fileOrDirectory)) {
return false;
}
@shell_exec('rm -rf '.escapeshellarg($fileOrDirectory).' 2>&1');
file_exists($fileOrDirectory) && @shell_exec('rmdir /S /Q '.escapeshellarg($fileOrDirectory).' 2>&1');
return true;
}
}