-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCopyPropertiesTask.php
108 lines (91 loc) · 2.27 KB
/
CopyPropertiesTask.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
<?php
namespace TheBuild;
/**
* Copy properties matching a prefix to properties with a different prefix.
*/
class CopyPropertiesTask extends \Task {
/**
* Prefix for properties to copy.
*
* @var string
*/
protected $fromPrefix = '';
/**
* Prefix for properties to create/update.
*
* @var string
*/
protected $toPrefix = '';
/**
* Whether to overwrite the existing properties.
*
* @var bool
*/
protected $override = TRUE;
/**
* Internal method to use for creating/updating properties.
*
* @var string
*/
protected $propertyMethod = 'setProperty';
/**
* Copy properties.
*/
public function main() {
$this->validate();
// Use either Project::setProperty() or Project::setNewProperty() based on
// whether we're overriding values or not.
$this->propertyMethod = $this->override ? 'setProperty' : 'setNewProperty';
$project = $this->getProject();
foreach ($project->getProperties() as $name => $value) {
if (strpos($name, $this->fromPrefix) === 0) {
$new_name = $this->toPrefix . substr($name, strlen($this->fromPrefix));
$project->{$this->propertyMethod}($new_name, $value);
}
}
}
/**
* Verify that the required attributes are set.
*/
public function validate() {
if (empty($this->fromPrefix)) {
throw new \BuildException("fromPrefix attribute is required.", $this->location);
}
if (empty($this->toPrefix)) {
throw new \BuildException("toPrefix attribute is required.", $this->location);
}
}
/**
* Set the source property prefix.
*
* @param string $prefix
* Prefix to copy properties from.
*/
public function setFromPrefix($prefix) {
if (!\StringHelper::endsWith(".", $prefix)) {
$prefix .= ".";
}
$this->fromPrefix = $prefix;
}
/**
* Set the destination property prefix.
*
* @param string $prefix
* Prefix to copy properties into.
*/
public function setToPrefix($prefix) {
if (!\StringHelper::endsWith(".", $prefix)) {
$prefix .= ".";
}
$this->toPrefix = $prefix;
}
/**
* Set override.
*
* @param bool $override
* Whether to override existing values.
*/
public function setOverride($override) {
$this->override = $override;
}
}