1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\TestingSuite\Composer\Installer; |
8
|
|
|
|
9
|
|
|
use Composer\Factory; |
10
|
|
|
use Composer\IO\IOInterface; |
11
|
|
|
use Composer\Json\JsonFile; |
12
|
|
|
use Mediact\FileMapping\FileMappingInterface; |
13
|
|
|
use Mediact\TestingSuite\Composer\MappingResolver; |
14
|
|
|
|
15
|
|
|
class ArchiveExcludeInstaller implements InstallerInterface |
16
|
|
|
{ |
17
|
|
|
/** @var JsonFile */ |
18
|
|
|
private $file; |
19
|
|
|
|
20
|
|
|
/** @var MappingResolver */ |
21
|
|
|
private $resolver; |
22
|
|
|
|
23
|
|
|
/** @var IOInterface */ |
24
|
|
|
private $io; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $destination; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $defaults = [ |
31
|
|
|
'/bitbucket-pipelines.yml', |
32
|
|
|
'/.gitignore', |
33
|
|
|
'/tests' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param MappingResolver $resolver |
40
|
|
|
* @param IOInterface $io |
41
|
|
|
* @param JsonFile|null $file |
42
|
|
|
* @param string $destination |
43
|
|
|
* @param array|null $defaults |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
MappingResolver $resolver, |
47
|
|
|
IOInterface $io, |
48
|
|
|
JsonFile $file = null, |
49
|
|
|
string $destination = null, |
50
|
|
|
array $defaults = null |
51
|
|
|
) { |
52
|
|
|
$this->resolver = $resolver; |
53
|
|
|
$this->io = $io; |
54
|
|
|
$this->file = $file ?: new JsonFile(Factory::getComposerFile()); |
55
|
|
|
$this->destination = $destination ?: getcwd(); |
56
|
|
|
|
57
|
|
|
$defaults !== null && $this->defaults = $defaults; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Install. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function install() |
66
|
|
|
{ |
67
|
|
|
$definition = $this->file->read(); |
68
|
|
|
$excluded = isset($definition['archive']['exclude']) |
69
|
|
|
? $definition['archive']['exclude'] |
70
|
|
|
: []; |
71
|
|
|
|
72
|
|
|
$excluded = array_map( |
73
|
|
|
function (string $exclude): string { |
74
|
|
|
return substr($exclude, 0, 1) !== '/' |
75
|
|
|
? '/' . $exclude |
76
|
|
|
: $exclude; |
77
|
|
|
}, |
78
|
|
|
$excluded |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$files = array_merge( |
82
|
|
|
$this->defaults, |
83
|
|
|
array_map( |
84
|
|
|
function (FileMappingInterface $mapping): string { |
85
|
|
|
return '/' . $mapping->getRelativeDestination(); |
86
|
|
|
}, |
87
|
|
|
iterator_to_array( |
88
|
|
|
$this->resolver->resolve() |
89
|
|
|
) |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
foreach ($files as $file) { |
94
|
|
|
if (!in_array($file, $excluded) |
95
|
|
|
&& file_exists($this->destination . $file) |
96
|
|
|
) { |
97
|
|
|
$excluded[] = $file; |
98
|
|
|
$this->io->write( |
99
|
|
|
sprintf( |
100
|
|
|
'<info>Added:</info> %s to archive exclude in composer.json', |
101
|
|
|
$file |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$definition['archive']['exclude'] = $excluded; |
108
|
|
|
$this->file->write($definition); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|