-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSelectPropertyKeyTask.php
146 lines (120 loc) · 3.41 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
144
145
146
<?php
/**
* @file SelectPropertyKeyTask.php
*
* Interactively select a key from available property keys.
*
* - If the propertyName property is already set, the task does nothing
* - If there is only one key available, that key is used and the user is not
* prompted
* - If no keys are available, the propertyName property is not set
* - If there are multiple keys available, the user will be prompted to select
* one using a multiple choice menu
*
* @code
* <selectpropertykey prefix="drupal.sites." omitKeys="_defaults" propertyName="build.site" message="Select a site to build:" />
* @endcode
*
* @copyright 2018 Palantir.net, Inc.
*/
namespace TheBuild;
use BuildException;
use StringHelper;
use Project;
class SelectPropertyKeyTask extends \Task {
/**
* @var string
* Required. Prefix for properties to copy.
*/
protected $prefix = '';
/**
* @var string
* Required. Property to populate with the selected value.
*/
protected $propertyName = '';
/**
* @var string
* Message to display to the user when more than one key is available.
*/
protected $message = 'Select one:';
/**
* @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));
list($key, $property_grandchildren) = explode('.', $property_children, 2);
$keys[$key] = $key;
}
}
// Remove keys based on the 'omitKeys' attribute.
$keys = array_diff($keys, $this->omitKeys);
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);
}
}
}
/**
* @param string $value
*/
public function setPrefix($value) {
if (!StringHelper::endsWith(".", $value)) {
$value .= ".";
}
$this->prefix = $value;
}
/**
* @param string $value
*/
public function setPropertyName($value) {
$this->propertyName = $value;
}
/**
* @param string $value
*/
public function setMessage($value) {
$this->message = $value;
}
/**
* @param string $value
*/
public function setOmitKeys($value) {
$this->omitKeys = array_map('trim', explode(',', $value));
}
}