|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the composer-link plugin. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2021-2022 Sander Visser <[email protected]>. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/SanderSander/composer-link |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace ComposerLink\Commands; |
|
17
|
|
|
|
|
18
|
|
|
use ComposerLink\PathHelper; |
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
|
|
24
|
|
|
class LinkCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
// TODO We need to add a flag, to skip packages that are not installed (For when a wildcard is used) |
|
27
|
|
|
protected function configure(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setName('link'); |
|
30
|
|
|
$this->setDescription('Link a package to a local directory'); |
|
31
|
|
|
$this->addArgument('path', InputArgument::REQUIRED, 'The path of the package'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
38
|
|
|
{ |
|
39
|
|
|
$helper = new PathHelper($input->getArgument('path')); |
|
40
|
|
|
|
|
41
|
|
|
// When run in global we should transform path to absolute path |
|
42
|
|
|
if ($this->plugin->isGlobal()) { |
|
43
|
|
|
/** @var string $working */ |
|
44
|
|
|
$working = $this->getApplication()->getInitialWorkingDirectory(); |
|
45
|
|
|
$helper = $helper->toAbsolutePath($working); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$paths = $helper->isWildCard() ? $helper->getPathsFromWildcard() : [$helper]; |
|
49
|
|
|
foreach ($paths as $path) { |
|
50
|
|
|
$this->linkPackage($path); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return 0; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// TODO instead of throwing exception, we should show a warning and continue, |
|
57
|
|
|
// this is needed when use wildcards so we can continue the process |
|
58
|
|
|
protected function linkPackage(PathHelper $helper): void |
|
59
|
|
|
{ |
|
60
|
|
|
$linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath()); |
|
61
|
|
|
|
|
62
|
|
|
if (!is_null($this->plugin->getRepository()->findByPath($helper->getNormalizedPath()))) { |
|
63
|
|
|
throw new InvalidArgumentException( |
|
64
|
|
|
sprintf('Package in path "%s" already linked', $helper->getNormalizedPath()) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$currentLinked = $this->plugin->getRepository()->findByName($linkedPackage->getName()); |
|
69
|
|
|
if (!is_null($currentLinked)) { |
|
70
|
|
|
throw new InvalidArgumentException( |
|
71
|
|
|
sprintf( |
|
72
|
|
|
'Package "%s" already linked from path "%s"', |
|
73
|
|
|
$linkedPackage->getName(), |
|
74
|
|
|
$currentLinked->getPath() |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->plugin->getRepository()->store($linkedPackage); |
|
80
|
|
|
$this->plugin->getRepository()->persist(); |
|
81
|
|
|
$this->plugin->getLinkManager()->linkPackage($linkedPackage); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|