-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSelectPropertyKeyTask.php
143 lines (121 loc) · 3.22 KB
/
SelectPropertyKeyTask.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace TheBuild;
/**
* Interactively select an option from an array of property keys.
*/
class SelectPropertyKeyTask extends \Task {
/**
* Required. Prefix for properties to copy.
*
* @var string
*/
protected $prefix = '';
/**
* Required. Property to populate with the selected value.
*
* @var string
*/
protected $propertyName = '';
/**
* Message to display to the user when more than one key is available.
*
* @var string
*/
protected $message = 'Select one:';
/**
* Keys to ignore.
*
* @var array
*/
protected $omitKeys = [];
/**
* Copy properties.
*/
public function main() {
$this->validate();
$project = $this->getProject();
if ($existing_value = $this->project->getProperty($this->propertyName)) {
$this->log("Using {$this->propertyName} = '{$existing_value}' (existing value)", \Project::MSG_INFO);
return;
}
// Extract matching keys from the properties array.
$keys = [];
foreach ($project->getProperties() as $name => $value) {
if (strpos($name, $this->prefix) === 0) {
$property_children = substr($name, strlen($this->prefix));
// phpcs:ignore
[$key] = explode('.', $property_children, 2);
$keys[$key] = $key;
}
}
// Remove keys based on the 'omitKeys' attribute.
$keys = array_diff($keys, $this->omitKeys);
$value = NULL;
if (count($keys) > 1) {
// Prompt for input.
$request = new MenuInputRequest($this->message);
$request->setOptions($keys);
$this->project->getInputHandler()->handleInput($request);
$value = $request->getInput();
}
elseif (count($keys) == 1) {
$value = current($keys);
$this->log("Using {$this->propertyName} = '{$value}' (one value found)", \Project::MSG_INFO);
}
else {
$this->log("No properties found with prefix '{$this->prefix}'", \Project::MSG_WARN);
}
if ($value) {
$project->setNewProperty($this->propertyName, $value);
}
}
/**
* Verify that the required attributes are set.
*/
public function validate() {
foreach (['prefix', 'propertyName'] as $attribute) {
if (empty($this->$attribute)) {
throw new \BuildException("$attribute attribute is required.", $this->location);
}
}
}
/**
* Set the prefix for which options will be shown.
*
* @param string $value
* Keys with this prefix will be provided as options.
*/
public function setPrefix($value) {
if (!\StringHelper::endsWith(".", $value)) {
$value .= ".";
}
$this->prefix = $value;
}
/**
* Set the destination property.
*
* @param string $value
* Property name for the selection result.
*/
public function setPropertyName($value) {
$this->propertyName = $value;
}
/**
* Set the message.
*
* @param string $value
* Message to display with the options.
*/
public function setMessage($value) {
$this->message = $value;
}
/**
* Exclude some of the property keys from the options.
*
* @param string $value
* A comma-separated list of keys to exclude.
*/
public function setOmitKeys($value) {
$this->omitKeys = array_map('trim', explode(',', $value));
}
}