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
|
|
|
namespace Phing\Tasks\System; |
21
|
|
|
|
22
|
|
|
use Phing\Exception\BuildException; |
23
|
|
|
use Phing\Io\FileReader; |
24
|
|
|
use Phing\Io\FileUtils; |
25
|
|
|
use Phing\Io\FileWriter; |
26
|
|
|
use Phing\Io\File; |
27
|
|
|
use Phing\Project; |
28
|
|
|
use Phing\Task; |
29
|
|
|
use Phing\Type\Element\FileSetAware; |
30
|
|
|
use Phing\Type\Element\FilterChainAware; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This task is for using filter chains to make changes to files and overwrite the original files. |
34
|
|
|
* |
35
|
|
|
* This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task |
36
|
|
|
* being used to modify files and then overwrite the modified files. In many (most?) cases you probably |
37
|
|
|
* should just use a copy task to preserve the original source files, but this task supports situations |
38
|
|
|
* where there is no src vs. build directory, and modifying source files is actually desired. |
39
|
|
|
* |
40
|
|
|
* <code> |
41
|
|
|
* <reflexive> |
42
|
|
|
* <fileset dir="."> |
43
|
|
|
* <include pattern="*.html"> |
44
|
|
|
* </fileset> |
45
|
|
|
* <filterchain> |
46
|
|
|
* <replaceregexp> |
47
|
|
|
* <regexp pattern="\n\r" replace="\n"/> |
48
|
|
|
* </replaceregexp> |
49
|
|
|
* </filterchain> |
50
|
|
|
* </reflexive> |
51
|
|
|
* </code> |
52
|
|
|
* |
53
|
|
|
* @author Hans Lellelid <[email protected]> |
54
|
|
|
* |
55
|
|
|
* @package phing.tasks.system |
56
|
|
|
*/ |
57
|
|
|
class ReflexiveTask extends Task |
58
|
|
|
{ |
59
|
|
|
use FileSetAware; |
60
|
|
|
use FilterChainAware; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Single file to process. |
64
|
|
|
*/ |
65
|
|
|
private $file; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Alias for setFrom() |
69
|
|
|
* |
70
|
|
|
* @param File $f |
71
|
|
|
*/ |
72
|
|
|
public function setFile(File $f) |
73
|
|
|
{ |
74
|
|
|
$this->file = $f; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Append the file(s). |
79
|
|
|
*/ |
80
|
|
|
public function main() |
81
|
|
|
{ |
82
|
|
|
if ($this->file === null && empty($this->filesets)) { |
83
|
|
|
throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task."); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// compile a list of all files to modify, both file attrib and fileset elements |
87
|
|
|
// can be used. |
88
|
|
|
|
89
|
|
|
$files = []; |
90
|
|
|
|
91
|
|
|
if ($this->file !== null) { |
92
|
|
|
$files[] = $this->file; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!empty($this->filesets)) { |
96
|
|
|
foreach ($this->filesets as $fs) { |
97
|
|
|
try { |
98
|
|
|
$ds = $fs->getDirectoryScanner($this->project); |
99
|
|
|
$filenames = $ds->getIncludedFiles(); // get included filenames |
100
|
|
|
$dir = $fs->getDir($this->project); |
101
|
|
|
foreach ($filenames as $fname) { |
102
|
|
|
$files[] = new File($dir, $fname); |
103
|
|
|
} |
104
|
|
|
} catch (BuildException $be) { |
105
|
|
|
$this->log($be->getMessage(), Project::MSG_WARN); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->log("Applying reflexive processing to " . count($files) . " files."); |
111
|
|
|
|
112
|
|
|
// These "slots" allow filters to retrieve information about the currently-being-process files |
113
|
|
|
$slot = $this->getRegisterSlot("currentFile"); |
114
|
|
|
$basenameSlot = $this->getRegisterSlot("currentFile.basename"); |
115
|
|
|
|
116
|
|
|
foreach ($files as $file) { |
117
|
|
|
// set the register slots |
118
|
|
|
|
119
|
|
|
$slot->setValue($file->getPath()); |
120
|
|
|
$basenameSlot->setValue($file->getName()); |
121
|
|
|
|
122
|
|
|
// 1) read contents of file, pulling through any filters |
123
|
|
|
$in = null; |
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$contents = ""; |
126
|
|
|
$in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project); |
127
|
|
|
while (-1 !== ($buffer = $in->read())) { |
128
|
|
|
$contents .= $buffer; |
129
|
|
|
} |
130
|
|
|
$in->close(); |
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
if ($in) { |
133
|
|
|
$in->close(); |
134
|
|
|
} |
135
|
|
|
$this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
try { |
139
|
|
|
// now create a FileWriter w/ the same file, and write to the file |
140
|
|
|
$out = new FileWriter($file); |
141
|
|
|
$out->write($contents); |
142
|
|
|
$out->close(); |
143
|
|
|
$this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
if ($out) { |
146
|
|
|
$out->close(); |
147
|
|
|
} |
148
|
|
|
$this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|