|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite\Workflow |
|
9
|
|
|
* @subpackage Composer |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Workflow\Composer; |
|
16
|
|
|
use Netresearch\Kite\Exception; |
|
17
|
|
|
use Netresearch\Kite\Service\Composer\Package; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Checkout a branch and eventually merge it with the previously checked out branch |
|
21
|
|
|
* |
|
22
|
|
|
* @category Netresearch |
|
23
|
|
|
* @package Netresearch\Kite\Workflow |
|
24
|
|
|
* @subpackage Composer |
|
25
|
|
|
* @author Christian Opitz <[email protected]> |
|
26
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
27
|
|
|
* @link http://www.netresearch.de |
|
28
|
|
|
*/ |
|
29
|
|
|
class Checkout extends Base |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Configures the arguments/options |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configureVariables() |
|
37
|
|
|
{ |
|
38
|
|
|
return array( |
|
39
|
|
|
'branch' => array( |
|
40
|
|
|
'type' => 'string|array', |
|
41
|
|
|
'label' => 'The branch(es) to check out (fallback is always master)', |
|
42
|
|
|
'argument' => true, |
|
43
|
|
|
'required' => true |
|
44
|
|
|
), |
|
45
|
|
|
'merge' => array( |
|
46
|
|
|
'type' => 'bool', |
|
47
|
|
|
'label' => 'Whether to merge the checked out branch with the previously checked out branch', |
|
48
|
|
|
'option' => true, |
|
49
|
|
|
'shortcut' => 'm' |
|
50
|
|
|
), |
|
51
|
|
|
'create' => array( |
|
52
|
|
|
'type' => 'bool', |
|
53
|
|
|
'label' => 'Create branch if not exists', |
|
54
|
|
|
'option' => true, |
|
55
|
|
|
'shortcut' => 'c' |
|
56
|
|
|
), |
|
57
|
|
|
'--' |
|
58
|
|
|
) + parent::configureVariables(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Override to assemble the tasks |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function assemble() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->callback( |
|
69
|
|
|
function () { |
|
70
|
|
|
$this->checkoutPackages( |
|
71
|
|
|
array_unique(array_merge((array) $this->get('branch'), ['master'])), |
|
72
|
|
|
$this->get('merge'), |
|
73
|
|
|
$this->get('create') |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Go through all packages and check it out in the first matching branch |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $branches The branches to try |
|
83
|
|
|
* @param bool $merge Whether to merge the new branch with the previously |
|
84
|
|
|
* checked out branch |
|
85
|
|
|
* @param bool $create Create branch if not exists |
|
86
|
|
|
* |
|
87
|
|
|
* @throws Exception\MissingVariableException |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function checkoutPackages(array $branches, $merge = false, $create = false) |
|
92
|
|
|
{ |
|
93
|
|
|
/* @var $packages \Netresearch\Kite\Service\Composer\Package[] */ |
|
94
|
|
|
$packages = array(); |
|
95
|
|
|
foreach ($this->getPackages() as $package) { |
|
96
|
|
|
foreach ($branches as $branch) { |
|
97
|
|
|
$oldBranch = $package->branch; |
|
98
|
|
|
if ($this->checkoutPackage($package, $branch, $create) !== false) { |
|
99
|
|
|
$packages[$package->name] = $package; |
|
100
|
|
|
if ($merge && $oldBranch !== $branch) { |
|
101
|
|
|
$this->mergePackage($package, $oldBranch); |
|
102
|
|
|
} |
|
103
|
|
|
continue 2; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!$packages) { |
|
|
|
|
|
|
109
|
|
|
$lastBranch = array_pop($branches); |
|
110
|
|
|
$message = 'Could not find branch '; |
|
111
|
|
|
if ($branches) { |
|
|
|
|
|
|
112
|
|
|
$message .= implode(', ', $branches) . ' or '; |
|
113
|
|
|
} |
|
114
|
|
|
$message .= $lastBranch . ' in any installed package'; |
|
115
|
|
|
$this->console->output("<warning>$message</warning>"); |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->rewriteRequirements($packages, $merge); |
|
120
|
|
|
$this->pushPackages(); |
|
121
|
|
|
|
|
122
|
|
|
// Each package containing one of the branches should now be checked out in |
|
123
|
|
|
// this branch. Anyway we do a composer update in order to update lock file |
|
124
|
|
|
// and eventually changed dependencies |
|
125
|
|
|
$this->doComposerUpdate(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
?> |
|
130
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.