|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webcreate\Conveyor\Task; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
6
|
|
|
use Symfony\Component\Finder\Finder; |
|
7
|
|
|
use Webcreate\Conveyor\IO\IOInterface; |
|
8
|
|
|
use Webcreate\Conveyor\Repository\Version; |
|
9
|
|
|
use Webcreate\Conveyor\Task\Result\ExecuteResult; |
|
10
|
|
|
use Webcreate\Conveyor\Util\FileCollection; |
|
11
|
|
|
|
|
12
|
|
|
class RemoveTask extends Task |
|
13
|
|
|
{ |
|
14
|
|
|
protected $cwd; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var IOInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $io; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $cwd |
|
23
|
|
|
*/ |
|
24
|
|
|
function __construct($cwd) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$this->cwd = $cwd; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param IOInterface $io |
|
31
|
|
|
* |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setIo(IOInterface $io) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$this->io = $io; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $target |
|
43
|
|
|
* @param Version $version |
|
44
|
|
|
* |
|
45
|
|
|
* @return ExecuteResult |
|
46
|
|
|
*/ |
|
47
|
|
|
public function execute($target, Version $version) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$filesystem = new Filesystem(); |
|
50
|
|
|
|
|
51
|
|
|
if ($this->io && !$this->io->isVerbose()) { |
|
52
|
|
|
$this->output(sprintf('Removing: <comment>0%%</comment>')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$collection = new FileCollection($this->cwd); |
|
56
|
|
|
|
|
57
|
|
|
if (!$this->options['files'] && $this->options['exclude']) { |
|
58
|
|
|
$collection->add('*'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach ($this->options['files'] as $file) { |
|
62
|
|
|
$collection->add($file); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($this->options['exclude'] as $file) { |
|
66
|
|
|
$collection->remove($file); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$files = $collection->toArray(); |
|
70
|
|
|
|
|
71
|
|
|
$total = count($files); |
|
72
|
|
|
$i = 0; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($files as $file) { |
|
75
|
|
|
$filename = sprintf('%s/%s', rtrim($this->cwd, DIRECTORY_SEPARATOR), $file); |
|
76
|
|
|
|
|
77
|
|
|
$filesystem->remove($filename); |
|
78
|
|
|
|
|
79
|
|
|
if ($this->io && !$this->io->isVerbose()) { |
|
80
|
|
|
$this->output(sprintf('Removing: <comment>%d%%</comment>', round(++$i * 100 / $total))); |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->output(sprintf("Removed %s", $file)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// remove empty directories |
|
86
|
|
|
$this->removeEmptyDirectories($this->cwd, $file); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->io && !$this->io->isVerbose()) { |
|
90
|
|
|
$this->output(sprintf('Removing: <comment>%d%%</comment>', 100)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new ExecuteResult( |
|
94
|
|
|
array(), |
|
95
|
|
|
$files |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Removes empty directories |
|
101
|
|
|
* |
|
102
|
|
|
* @param $cwd |
|
103
|
|
|
* @param $file |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function removeEmptyDirectories($cwd, $file) |
|
106
|
|
|
{ |
|
107
|
|
|
$filesystem = new Filesystem(); |
|
108
|
|
|
|
|
109
|
|
|
$filename = sprintf('%s/%s', rtrim($cwd, DIRECTORY_SEPARATOR), $file); |
|
110
|
|
|
|
|
111
|
|
|
while ('.' !== dirname($file)) { |
|
112
|
|
|
$file = dirname($file); |
|
|
|
|
|
|
113
|
|
|
$filename = dirname($filename); |
|
114
|
|
|
|
|
115
|
|
|
$finder = new Finder(); |
|
116
|
|
|
$countFiles = $finder |
|
117
|
|
|
->files() |
|
118
|
|
|
->in($filename) |
|
119
|
|
|
->ignoreDotFiles(false) |
|
120
|
|
|
->ignoreVCS(false) |
|
121
|
|
|
->count(); |
|
122
|
|
|
|
|
123
|
|
|
if ($countFiles === 0) { |
|
124
|
|
|
$filesystem->remove($filename); |
|
125
|
|
|
|
|
126
|
|
|
if ($this->io && $this->io->isVerbose()) { |
|
127
|
|
|
$this->output(sprintf("Removed %s", $file)); |
|
128
|
|
|
} |
|
129
|
|
|
} else { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.