1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Task\System; |
22
|
|
|
|
23
|
|
|
use Exception; |
24
|
|
|
use Phing\Exception\BuildException; |
25
|
|
|
use Phing\Filter\ReplaceRegexp; |
26
|
|
|
use Phing\Io\File; |
27
|
|
|
use Phing\Io\FileReader; |
28
|
|
|
use Phing\Io\FileUtils; |
29
|
|
|
use Phing\Io\FileWriter; |
30
|
|
|
use Phing\Project; |
31
|
|
|
use Phing\Task; |
32
|
|
|
use Phing\Type\Element\FileSetAware; |
33
|
|
|
use Phing\Type\FilterChain; |
34
|
|
|
use Phing\Type\RegularExpression; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ReplaceRegExp is a directory based task for replacing the occurrence of a |
38
|
|
|
* given regular expression with a substitution pattern in a selected file or |
39
|
|
|
* set of files. |
40
|
|
|
* |
41
|
|
|
* <code> |
42
|
|
|
* <replaceregexp file="${src}/build.properties" |
43
|
|
|
* match="OldProperty=(.*)" |
44
|
|
|
* replace="NewProperty=\1" |
45
|
|
|
* byline="true"/> |
46
|
|
|
* </code> |
47
|
|
|
* |
48
|
|
|
* @author Jonathan Bond-Caron <[email protected]> |
49
|
|
|
* |
50
|
|
|
* @see http://ant.apache.org/manual/OptionalTasks/replaceregexp.html |
51
|
|
|
*/ |
52
|
|
|
class ReplaceRegexpTask extends Task |
53
|
|
|
{ |
54
|
|
|
use FileSetAware; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Single file to process. |
58
|
|
|
*/ |
59
|
|
|
private $file; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Regular expression. |
63
|
|
|
* |
64
|
|
|
* @var RegularExpression |
65
|
|
|
*/ |
66
|
|
|
private $regexp; |
67
|
|
|
|
68
|
|
|
private $failonerror = false; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* If false, note errors but continue. |
72
|
|
|
*/ |
73
|
1 |
|
public function setFailOnError($failonerror) |
74
|
|
|
{ |
75
|
1 |
|
$this->failonerror = $failonerror; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* File to apply regexp on. |
80
|
|
|
*/ |
81
|
3 |
|
public function setFile(File $path) |
82
|
|
|
{ |
83
|
3 |
|
$this->file = $path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the regexp match pattern. |
88
|
|
|
* |
89
|
|
|
* @param string $regexp |
90
|
|
|
*/ |
91
|
3 |
|
public function setMatch($regexp) |
92
|
|
|
{ |
93
|
3 |
|
$this->regexp->setPattern($regexp); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $regexp |
98
|
|
|
* |
99
|
|
|
* @see setMatch() |
100
|
|
|
*/ |
101
|
|
|
public function setPattern($regexp) |
102
|
|
|
{ |
103
|
|
|
$this->setMatch($regexp); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets the replacement string. |
108
|
|
|
* |
109
|
|
|
* @param string $string |
110
|
|
|
*/ |
111
|
3 |
|
public function setReplace($string) |
112
|
|
|
{ |
113
|
3 |
|
$this->regexp->setReplace($string); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets the regexp flags. |
118
|
|
|
* |
119
|
|
|
* @param string $flags |
120
|
|
|
*/ |
121
|
1 |
|
public function setFlags($flags) |
122
|
|
|
{ |
123
|
1 |
|
$this->regexp->setModifiers($flags); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Match only per line. |
128
|
|
|
* |
129
|
|
|
* @param bool $yesNo |
130
|
|
|
*/ |
131
|
|
|
public function setByline($yesNo) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
// TODO... $this->_regexp-> |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
3 |
|
public function init() |
140
|
|
|
{ |
141
|
3 |
|
$this->regexp = new RegularExpression(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
* |
147
|
|
|
* @throws BuildException |
148
|
|
|
*/ |
149
|
3 |
|
public function main() |
150
|
|
|
{ |
151
|
3 |
|
if (null === $this->file && empty($this->filesets)) { |
152
|
|
|
throw new BuildException('You must specify a file or fileset(s) for the <ReplaceRegexp> task.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// compile a list of all files to modify, both file attrib and fileset elements |
156
|
|
|
// can be used. |
157
|
3 |
|
$files = []; |
158
|
|
|
|
159
|
3 |
|
if (null !== $this->file) { |
160
|
3 |
|
$files[] = $this->file; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
if (!empty($this->filesets)) { |
164
|
|
|
$filenames = []; |
|
|
|
|
165
|
|
|
foreach ($this->filesets as $fs) { |
166
|
|
|
try { |
167
|
|
|
$ds = $fs->getDirectoryScanner($this->project); |
168
|
|
|
$filenames = $ds->getIncludedFiles(); // get included filenames |
169
|
|
|
$dir = $fs->getDir($this->project); |
170
|
|
|
foreach ($filenames as $fname) { |
171
|
|
|
$files[] = new File($dir, $fname); |
172
|
|
|
} |
173
|
|
|
} catch (BuildException $be) { |
174
|
|
|
$this->log($be->getMessage(), Project::MSG_WARN); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
$this->log('Applying Regexp processing to ' . count($files) . ' files.'); |
180
|
|
|
|
181
|
|
|
// These "slots" allow filters to retrieve information about the currently-being-process files |
182
|
3 |
|
$slot = $this->getRegisterSlot('currentFile'); |
183
|
3 |
|
$basenameSlot = $this->getRegisterSlot('currentFile.basename'); |
184
|
|
|
|
185
|
3 |
|
$filter = new FilterChain($this->project); |
186
|
|
|
|
187
|
3 |
|
$r = new ReplaceRegexp(); |
188
|
3 |
|
$r->setRegexps([$this->regexp]); |
189
|
|
|
|
190
|
3 |
|
$filter->addReplaceRegexp($r); |
191
|
3 |
|
$filters = [$filter]; |
192
|
|
|
|
193
|
3 |
|
foreach ($files as $file) { |
194
|
|
|
// set the register slots |
195
|
|
|
|
196
|
3 |
|
$slot->setValue($file->getPath()); |
197
|
3 |
|
$basenameSlot->setValue($file->getName()); |
198
|
|
|
|
199
|
|
|
// 1) read contents of file, pulling through any filters |
200
|
3 |
|
$in = null; |
|
|
|
|
201
|
3 |
|
$out = null; |
|
|
|
|
202
|
3 |
|
$contents = ''; |
203
|
|
|
|
204
|
|
|
try { |
205
|
3 |
|
$in = FileUtils::getChainedReader(new FileReader($file), $filters, $this->project); |
206
|
2 |
|
while (-1 !== ($buffer = $in->read())) { |
207
|
2 |
|
$contents .= $buffer; |
208
|
|
|
} |
209
|
2 |
|
$in->close(); |
210
|
1 |
|
} catch (Exception $e) { |
211
|
1 |
|
if ($in) { |
212
|
|
|
$in->close(); |
213
|
|
|
} |
214
|
1 |
|
$this->log('Error reading file: ' . $e->getMessage(), Project::MSG_ERR); |
215
|
1 |
|
if ($this->failonerror) { |
216
|
1 |
|
throw new BuildException("Error reading file: '" . $file->getAbsolutePath() . "'", $e); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
try { |
221
|
|
|
// now create a FileWriter w/ the same file, and write to the file |
222
|
2 |
|
$out = new FileWriter($file); |
223
|
2 |
|
$out->write($contents); |
224
|
2 |
|
$out->close(); |
225
|
2 |
|
$this->log('Applying regexp processing to ' . $file->getPath(), Project::MSG_VERBOSE); |
226
|
|
|
} catch (Exception $e) { |
227
|
|
|
if ($out) { |
228
|
|
|
$out->close(); |
229
|
|
|
} |
230
|
|
|
$this->log('Error writing file back: ' . $e->getMessage(), Project::MSG_ERR); |
231
|
|
|
if ($this->failonerror) { |
232
|
|
|
throw new BuildException("Error writing file back: '" . $file->getAbsolutePath() . "'", $e); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.