-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathCopyBuild.php
More file actions
109 lines (92 loc) · 2.93 KB
/
Copy pathCopyBuild.php
File metadata and controls
109 lines (92 loc) · 2.93 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
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Copy Build Plugin - Copies the entire build to another directory.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class CopyBuild extends Plugin
{
protected $respectIgnore;
protected $wipe;
/**
* @return string
*/
public static function pluginName()
{
return 'copy_build';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->wipe = isset($options['wipe']) ? (bool)$options['wipe'] : false;
$this->respectIgnore = isset($options['respect_ignore']) ? (bool)$options['respect_ignore'] : false;
}
/**
* Copies files from the root of the build directory into the target folder
*
* @return bool
* @throws RuntimeException
*/
public function execute()
{
$buildPath = $this->builder->buildPath;
if ($this->directory === $buildPath) {
return false;
}
$this->wipeExistingDirectory();
if (\is_dir($this->directory)) {
throw new RuntimeException(
\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->builder->executeCommand($cmd, $buildPath, $this->directory, \rtrim($buildPath, '/'), $this->directory);
$this->deleteIgnoredFiles();
return $success;
}
/**
* Wipe the destination directory if it already exists.
*
* @throws RuntimeException
*/
protected function wipeExistingDirectory()
{
if ($this->wipe === true && $this->directory !== '/' && \is_dir($this->directory)) {
$cmd = 'cd "%s" && rm -Rf "%s"';
$success = $this->builder->executeCommand($cmd, $this->builder->buildPath, $this->directory);
if (!$success) {
throw new RuntimeException(
\sprintf('Failed to wipe existing directory "%s" before copy!', $this->directory)
);
}
\clearstatcache();
}
}
/**
* Delete any ignored files from the build prior to copying.
*/
protected function deleteIgnoredFiles()
{
if ($this->respectIgnore) {
foreach ($this->builder->ignore as $file) {
$cmd = 'cd "%s" && rm -Rf "%s/%s"';
$this->builder->executeCommand($cmd, $this->builder->buildPath, $this->directory, $file);
}
}
}
}