forked from SonsOfPHP/sonsofphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyCommand.php
More file actions
79 lines (64 loc) · 2.78 KB
/
Copy pathCopyCommand.php
File metadata and controls
79 lines (64 loc) · 2.78 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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Bard\Console\Command;
use SonsOfPHP\Bard\JsonFile;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;
/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
final class CopyCommand extends AbstractCommand
{
protected static $defaultName = 'copy';
protected function configure(): void
{
$this
->setDescription('Copies a file to each package')
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry Run (Do not make any changes)')
->addArgument('source', InputArgument::REQUIRED, 'Source file to copy')
->addArgument('package', InputArgument::OPTIONAL, 'Which package?')
->setHelp(
<<<'HELP'
The <info>copy</info> command will copy whatever file you give it to all the other
repositories it is managing. This is useful for LICENSE files.
Examples:
<comment>%command.full_name% LICENSE</comment>
Read more at https://docs.sonsofphp.com/bard/
HELP
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$isDryRun = $input->getOption('dry-run');
// ---
$sourceFile = $input->getOption('working-dir') . '/' . $input->getArgument('source');
if (!is_file($sourceFile)) {
throw new \RuntimeException(sprintf('The file "%s" is an invalid file.', $sourceFile));
}
// ---
// ---
$bardJsonFile = new JsonFile($input->getOption('working-dir') . '/bard.json');
foreach ($bardJsonFile->getSection('packages') as $pkg) {
$pkgComposerFile = realpath($input->getOption('working-dir') . '/' . $pkg['path'] . '/composer.json');
$pkgComposerJsonFile = new JsonFile($pkgComposerFile);
$pkgName = $pkgComposerJsonFile->getSection('name');
if (null !== $input->getArgument('package') && $pkgName !== $input->getArgument('package')) {
continue;
}
$process = new Process(['cp', $sourceFile, $pkg['path']]);
$io->text($process->getCommandLine());
if (!$isDryRun) {
$this->getHelper('process')->run($output, $process);
}
}
// ---
$io->success(sprintf('File "%s" has been copied to all managed repos.', $sourceFile));
return self::SUCCESS;
}
}