-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyBuild.php
More file actions
131 lines (111 loc) · 3.34 KB
/
Copy pathCopyBuild.php
File metadata and controls
131 lines (111 loc) · 3.34 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
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Common;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
/**
* Copy Build Plugin - Copies the entire build to another directory.
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Dan Cryer <dan@block8.co.uk>
*/
class CopyBuild extends Plugin
{
private bool $respectIgnore = false;
private bool $wipe = false;
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'copy_build';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$buildPath = $this->build->getBuildPath();
if ($this->directory === $buildPath) {
return false;
}
$this->wipeExistingDirectory();
if (\is_dir($this->directory)) {
throw new Exception(
\sprintf(
'Directory "%s" already exists! Use "wipe" option if you want to delete directory before copy.',
$this->directory
)
);
}
$cmd = 'cd "%s" && mkdir -p "%s" && cp -R %s/. "%s"';
$success = $this->commandExecutor->executeCommand(
$cmd,
$buildPath,
$this->directory,
\rtrim((string)$buildPath, '/'),
$this->directory
);
$this->deleteIgnoredFiles();
return $success;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
BuildInterface::STAGE_DEPLOY,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
$this->wipe = (bool)$this->options->get('wipe', $this->wipe);
$this->respectIgnore = (bool)$this->options->get('respect_ignore', $this->respectIgnore);
}
/**
* Wipe the destination directory if it already exists.
*
* @throws Exception
*/
private function wipeExistingDirectory(): void
{
if ($this->wipe === true && $this->directory !== '/' && \is_dir($this->directory)) {
$cmd = 'cd "%s" && rm -Rf "%s"';
$success = $this->commandExecutor->executeCommand($cmd, $this->build->getBuildPath(), $this->directory);
if (!$success) {
throw new Exception(
\sprintf('Failed to wipe existing directory "%s" before copy!', $this->directory)
);
}
\clearstatcache();
}
}
/**
* Delete any ignored files from the build prior to copying.
*/
private function deleteIgnoredFiles(): void
{
if ($this->respectIgnore) {
foreach ($this->ignores as $file) {
$cmd = 'cd "%s" && rm -Rf "%s/%s"';
$this->commandExecutor->executeCommand($cmd, $this->build->getBuildPath(), $this->directory, $file);
}
}
}
}