Ticket #193: ManifestTask.php

File ManifestTask.php, 2.5 kB (added by davidpersson@qeweurope.org, 11 months ago)

php source code of the task

Line 
1 <?php
2 /**
3  * ManifestTask
4  *
5  * Generates a simple MANIFEST file with optional checksums
6  *
7  * @author David Persson <davidpersson at qeweurope dot org>
8  *
9  */
10 require_once "phing/Task.php";
11 require_once 'phing/system/io/PhingFile.php';
12
13 class ManifestTask extends Task {
14
15     var $taskname = 'manifest';
16     
17     /**
18      * The target file passed in the buildfile.
19      */
20     private $file = null;
21     
22     private $filesets = array();
23
24     /**
25      * Enable/Disable checksuming (md5)
26      */
27     private $checksum = false;
28     
29
30     /**
31      * The setter for the attribute "file"
32      */
33     public function setFile(PhingFile $file) {
34         $this->file = $file;
35     }
36
37     /**
38      * The setter for the attribute "checksum"
39      */
40     public function setChecksum($bool) {
41         $this->checksum = $bool;
42     }
43     
44     /**
45      * Nested creator, creates a FileSet for this task
46      *
47      * @access  public
48      * @return  object  The created fileset object
49      */
50     function createFileSet() {
51         $num = array_push($this->filesets, new FileSet());
52         return $this->filesets[$num-1];
53     }
54
55     /**
56      * The init method: Do init steps.
57      */
58     public function init() {
59       // nothing to do here
60     }
61
62     /**
63      * do the work
64      */
65     public function main() {
66           $project = $this->getProject();
67         $this->validateAttributes();
68         
69         $this->log("Building Manifest: " . $this->file->__toString(), Project::MSG_INFO);
70         
71         foreach($this->filesets as $fs) {
72             
73             $dir = $fs->getDir($this->project);
74
75             $ds = $fs->getDirectoryScanner($project);
76             $fromDir  = $fs->getDir($project);
77             $srcFiles = $ds->getIncludedFiles();
78             $srcDirs  = $ds->getIncludedDirectories();           
79
80             foreach($ds->getIncludedFiles() as $file_path) {
81                 $line = $file_path;
82                 if($this->checksum) {
83                     $line .= "\t".md5_file($dir.'/'.$file_path);
84                 }
85                 $line .= "\n";
86                 $manifest[] = $line;
87             }
88             
89         }
90         
91         file_put_contents($this->file,$manifest);
92         
93     }
94
95     /**
96      * Validates attributes coming in from XML
97      *
98      * @access  private
99      * @return  void
100      * @throws  BuildException
101      */
102     protected function validateAttributes() {
103     
104         if ($this->file === null && count($this->filesets) === 0) {
105             throw new BuildException("Specify at least sources and destination - a file or a fileset.");
106         }
107
108         if ($this->file !== null && $this->file->exists() && $this->file->isDirectory()) {
109             throw new BuildException("Destination file cannot be a directory.");
110         }
111         
112     }     
113 }
114 ?>
115