1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core\Robo\Plugin\Commands; |
6
|
|
|
|
7
|
|
|
use Consolidation\Config\Loader\ConfigProcessor; |
8
|
|
|
use PhpTaskman\Core\Contract\ComposerAwareInterface; |
9
|
|
|
use PhpTaskman\Core\Traits\ComposerAwareTrait; |
10
|
|
|
use Robo\Common\ConfigAwareTrait; |
11
|
|
|
use Robo\Common\IO; |
12
|
|
|
use Robo\Contract\ConfigAwareInterface; |
13
|
|
|
use Robo\Contract\IOAwareInterface; |
14
|
|
|
use Robo\Contract\BuilderAwareInterface; |
15
|
|
|
use Robo\LoadAllTasks; |
16
|
|
|
use Robo\Robo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AbstractCommands. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractCommands implements |
22
|
|
|
BuilderAwareInterface, |
23
|
|
|
IOAwareInterface, |
24
|
|
|
ConfigAwareInterface, |
25
|
|
|
ComposerAwareInterface |
26
|
|
|
{ |
27
|
|
|
use ComposerAwareTrait; |
28
|
|
|
use ConfigAwareTrait; |
29
|
|
|
use IO; |
30
|
|
|
use LoadAllTasks; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Path to YAML configuration file containing command defaults. |
34
|
|
|
* |
35
|
|
|
* Command classes should implement this method. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
abstract public function getConfigurationFile(): string; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Path to YAML configuration file containing command defaults. |
43
|
|
|
* |
44
|
|
|
* Command classes should implement this method. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
* The path of the default configuration file. |
48
|
|
|
*/ |
49
|
|
|
abstract public function getDefaultConfigurationFile(): string; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Load default configuration. |
53
|
|
|
* |
54
|
|
|
* PHP Tasks does not allow to provide default configuration for |
55
|
|
|
* commands. In this hook we load Toolkit default configuration and re-apply |
56
|
|
|
* it back. |
57
|
|
|
* |
58
|
|
|
* @hook pre-command-event * |
59
|
|
|
*/ |
60
|
|
|
public function loadDefaultConfig() |
61
|
|
|
{ |
62
|
|
|
// Get Tasks configuration. |
63
|
|
|
$config = $this->getConfig(); |
64
|
|
|
|
65
|
|
|
if (null === $config->get('taskman.bin_dir')) { |
66
|
|
|
if (null !== $composer = $this->getComposer()) { |
67
|
|
|
// The COMPOSER_BIN_DIR environment takes precedence over the value |
68
|
|
|
// defined in composer.json config, if any. Default to ./vendor/bin. |
69
|
|
|
if (!$composerBinDir = \getenv('COMPOSER_BIN_DIR')) { |
70
|
|
|
if (!$composerBinDir = $composer->getConfig('bin-dir')) { |
71
|
|
|
$composerBinDir = './vendor/bin'; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (false === \strpos($composerBinDir, './')) { |
76
|
|
|
$composerBinDir = './' . $composerBinDir; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$composerBinDir = \rtrim($composerBinDir, \DIRECTORY_SEPARATOR); |
80
|
|
|
$config->set('taskman.bin_dir', $composerBinDir); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$configurationFilePath = \realpath($this->getConfigurationFile()); |
85
|
|
|
|
86
|
|
|
if (false !== $config) { |
|
|
|
|
87
|
|
|
Robo::loadConfiguration([$configurationFilePath], $config); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$default_config = Robo::createConfiguration([$this->getDefaultConfigurationFile()]); |
91
|
|
|
|
92
|
|
|
// Re-build configuration. |
93
|
|
|
$processor = new ConfigProcessor(); |
94
|
|
|
$processor->add($default_config->export()); |
95
|
|
|
$processor->add($config->export()); |
96
|
|
|
|
97
|
|
|
// Import newly built configuration. |
98
|
|
|
$config->import($processor->export()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|