Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 * $Id: UpToDateTask.php 144 2007-02-05 15:19:00Z hans $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
 
22
require_once 'phing/Task.php';
23
include_once 'phing/tasks/system/condition/Condition.php';
24
include_once 'phing/util/DirectoryScanner.php';
25
include_once 'phing/util/SourceFileScanner.php';
26
include_once 'phing/mappers/MergeMapper.php';
27
 
28
/**
29
 * Sets the given property if the specified target has a timestamp
30
 * greater than all of the source files.
31
 *
32
 * @author    Hans Lellelid <hans@xmpl.org> (Phing)
33
 * @author    William Ferguson <williamf@mincom.com> (Ant)
34
 * @author    Hiroaki Nakamura <hnakamur@mc.neweb.ne.jp> (Ant)
35
 * @author    Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
36
 * @version   $Revision: 1.6 $
37
 * @package   phing.tasks.system
38
 */
39
class UpToDateTask extends Task implements Condition {
40
 
41
    private $_property;
42
    private $_value;
43
    private $_sourceFile;
44
    private $_targetFile;
45
    private $sourceFileSets = array();
46
 
47
    protected $mapperElement = null;
48
 
49
    /**
50
     * The property to set if the target file is more up-to-date than
51
     * (each of) the source file(s).
52
     *
53
     * @param property the name of the property to set if Target is up-to-date.
54
     */
55
    public function setProperty($property) {
56
        $this->_property = $property;
57
    }
58
 
59
    /**
60
     * The value to set the named property to if the target file is more
61
     * up-to-date than (each of) the source file(s). Defaults to 'true'.
62
     *
63
     * @param value the value to set the property to if Target is up-to-date
64
     */
65
    public function setValue($value) {
66
        $this->_value = $value;
67
    }
68
 
69
    /**
70
     * Returns the value, or "true" if a specific value wasn't provided.
71
     */
72
    private function getValue() {
73
        return ($this->_value !== null) ? $this->_value : "true";
74
    }
75
 
76
    /**
77
     * The file which must be more up-to-date than (each of) the source file(s)
78
     * if the property is to be set.
79
     *
80
     * @param file the file we are checking against.
81
     */
82
    public function setTargetFile($file) {
83
        if (is_string($file)) {
84
            $file = new PhingFile($file);
85
        }
86
        $this->_targetFile = $file;
87
    }
88
 
89
    /**
90
     * The file that must be older than the target file
91
     * if the property is to be set.
92
     *
93
     * @param file the file we are checking against the target file.
94
     */
95
    public function setSrcfile($file) {
96
        if (is_string($file)) {
97
            $file = new PhingFile($file);
98
        }
99
        $this->_sourceFile = $file;
100
    }
101
 
102
    /**
103
     * Nested <srcfiles> element.
104
     */
105
    public function createSrcfiles() {
106
        $fs = new FileSet();
107
        $this->sourceFileSets[] = $fs;
108
        return $fs;
109
    }
110
 
111
    /**
112
     * Defines the FileNameMapper to use (nested mapper element).
113
     */
114
    public function createMapper() {
115
        if ($this->mapperElement !== null) {
116
            throw new BuildException("Cannot define more than one mapper",
117
                                     $this->location);
118
        }
119
        $this->mapperElement = new Mapper($this->getProject());
120
        return $this->mapperElement;
121
    }
122
 
123
    /**
124
     * Evaluate (all) target and source file(s) to
125
     * see if the target(s) is/are up-to-date.
126
     * @return boolean
127
     */
128
    public function evaluate() {
129
        if (count($this->sourceFileSets) === 0 && $this->_sourceFile === null) {
130
            throw new BuildException("At least one srcfile or a nested "
131
                                     . "<srcfiles> element must be set.");
132
        }
133
 
134
        if (count($this->sourceFileSets) > 0 && $this->_sourceFile !== null) {
135
            throw new BuildException("Cannot specify both the srcfile "
136
                                     . "attribute and a nested <srcfiles> "
137
                                     . "element.");
138
        }
139
 
140
        if ($this->_targetFile === null && $this->mapperElement === null) {
141
            throw new BuildException("The targetfile attribute or a nested "
142
                                     . "mapper element must be set.");
143
        }
144
 
145
        // if the target file is not there, then it can't be up-to-date
146
        if ($this->_targetFile !== null && !$this->_targetFile->exists()) {
147
            return false;
148
        }
149
 
150
        // if the source file isn't there, throw an exception
151
        if ($this->_sourceFile !== null && !$this->_sourceFile->exists()) {
152
            throw new BuildException($this->_sourceFile->getAbsolutePath()
153
                                     . " not found.");
154
        }
155
 
156
        $upToDate = true;
157
        for($i=0,$size=count($this->sourceFileSets); $i < $size && $upToDate; $i++) {
158
            $fs = $this->sourceFileSets[$i];
159
            $ds = $fs->getDirectoryScanner($this->project);
160
            $upToDate = $upToDate && $this->scanDir($fs->getDir($this->project),
161
                                           $ds->getIncludedFiles());
162
        }
163
 
164
        if ($this->_sourceFile !== null) {
165
            if ($this->mapperElement === null) {
166
                $upToDate = $upToDate &&
167
                    ($this->_targetFile->lastModified() >= $this->_sourceFile->lastModified());
168
            } else {
169
                $sfs = new SourceFileScanner($this);
170
                $upToDate = $upToDate &&
171
                    count($sfs->restrict($this->_sourceFile->getAbsolutePath(),
172
                                  null, null,
173
                                  $this->mapperElement->getImplementation())) === 0;
174
            }
175
        }
176
        return $upToDate;
177
    }
178
 
179
 
180
    /**
181
     * Sets property to true if target file(s) have a more recent timestamp
182
     * than (each of) the corresponding source file(s).
183
     * @throws BuildException
184
     */
185
    public function main() {
186
        if ($this->_property === null) {
187
            throw new BuildException("property attribute is required.",
188
                                     $this->location);
189
        }
190
        $upToDate = $this->evaluate();
191
        if ($upToDate) {
192
            $this->project->setNewProperty($this->_property, $this->getValue());
193
            if ($this->mapperElement === null) {
194
                $this->log("File \"" . $this->_targetFile->getAbsolutePath()
195
                    . "\" is up-to-date.", Project::MSG_VERBOSE);
196
            } else {
197
                $this->log("All target files are up-to-date.",
198
                    Project::MSG_VERBOSE);
199
            }
200
        }
201
    }
202
 
203
    protected function scanDir(PhingFile $srcDir, $files) {
204
        $sfs = new SourceFileScanner($this);
205
        $mapper = null;
206
        $dir = $srcDir;
207
        if ($this->mapperElement === null) {
208
            $mm = new MergeMapper();
209
            $mm->setTo($this->_targetFile->getAbsolutePath());
210
            $mapper = $mm;
211
            $dir = null;
212
        } else {
213
            $mapper = $this->mapperElement->getImplementation();
214
        }
215
        return (count($sfs->restrict($files, $srcDir, $dir, $mapper)) === 0);
216
    }
217
}