Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
130 lines (107 loc) · 4.45 KB
/
Copy pathPlugin.php
File metadata and controls
130 lines (107 loc) · 4.45 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
<?php
declare(strict_types=1);
namespace TEN7\CodeTests;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
class Plugin implements PluginInterface, EventSubscriberInterface {
protected Composer $composer;
protected IOInterface $io;
public function activate(Composer $composer, IOInterface $io): void {
$this->composer = $composer;
$this->io = $io;
}
public function deactivate(Composer $composer, IOInterface $io): void {}
public function uninstall(Composer $composer, IOInterface $io): void {}
public static function getSubscribedEvents(): array {
// Use a low priority so we run after other plugins (e.g. lullabot/drainpipe),
// which may remove .github/actions/drainpipe before we copy it back.
return [
ScriptEvents::POST_INSTALL_CMD => ['scaffold', -100],
ScriptEvents::POST_UPDATE_CMD => ['scaffold', -100],
];
}
public function scaffold(Event $event): void {
$scaffoldDir = dirname(__DIR__) . '/scaffold/.github';
$projectDir = dirname($this->composer->getConfig()->get('vendor-dir'));
$targetDir = $projectDir . '/.github';
// Always copy all actions — they are lightweight and only invoked when referenced.
$this->copyDirectory($scaffoldDir . '/actions', $targetDir . '/actions');
$extra = $this->composer->getPackage()->getExtra();
$config = $extra['code_testing']['github'] ?? [];
if (empty($config)) {
$this->io->write('<info>ten7/code-testing: no code_testing config found, skipping scaffold.</info>');
return;
}
// Copy only the workflows declared in extra.code_testing.github.
// Each context key becomes a filename prefix:
// "drainpipe": ["PlaywrightTests"] → drainpipePlaywrightTests.yml
// "deployment": ["PlaywrightTests"] → deploymentPlaywrightTests.yml
// "pantheon": ["ReviewApps"] → pantheonReviewApps.yml
$hasDrainpipe = FALSE;
foreach ($config as $context => $items) {
if ($context === 'drainpipe') {
$hasDrainpipe = TRUE;
}
foreach ($items as $item) {
$filename = $context . $item . '.yml';
$source = $scaffoldDir . '/workflows/' . $filename;
$target = $targetDir . '/workflows/' . $filename;
if (!file_exists($source)) {
$this->io->writeError("<warning>ten7/code-testing: scaffold file not found: $filename</warning>");
continue;
}
if (!is_dir(dirname($target))) {
mkdir(dirname($target), 0755, TRUE);
}
copy($source, $target);
$this->io->write("<info>ten7/code-testing: scaffolded $filename</info>");
}
}
// Drainpipe workflows rely on actions from lullabot/drainpipe. Copy them
// automatically so the user doesn't have to do it manually.
if ($hasDrainpipe) {
$actionsSrc = dirname($this->composer->getConfig()->get('vendor-dir')) . '/vendor/lullabot/drainpipe/scaffold/github/actions';
$actions = [
$actionsSrc . '/common' => $targetDir . '/actions/drainpipe',
$actionsSrc . '/pantheon' => $targetDir . '/actions/drainpipe/pantheon',
];
foreach ($actions as $actionSrc => $actionDir) {
if (is_dir($actionSrc)) {
$this->copyDirectory($actionSrc, $actionDir);
$this->io->write('<info>ten7/code-testing: scaffolded drainpipe actions</info>');
}
else {
$this->io->writeError('<warning>ten7/code-testing: drainpipe workflows configured but lullabot/drainpipe not found in vendor — run composer require lullabot/drainpipe</warning>');
}
}
}
}
private function copyDirectory(string $source, string $target): void {
if (!is_dir($source)) {
return;
}
if (!is_dir($target)) {
mkdir($target, 0755, TRUE);
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$targetPath = $target . '/' . $iterator->getSubPathname();
if ($item->isDir()) {
if (!is_dir($targetPath)) {
mkdir($targetPath, 0755, TRUE);
}
}
else {
copy($item->getPathname(), $targetPath);
$this->io->write('<info>ten7/code-testing: scaffolded action ' . $iterator->getSubPathname() . '</info>');
}
}
}
}