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