summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/ext/phk/PhkPackageTask.php
blob: 80da93fa26ebaf34fc074c7c25ec9341a9ce27f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
 * $Id: 5029881915d6a9e95320ba6e7f463f38af66cd93 $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */

require_once 'phing/Task.php';
require_once 'phing/tasks/ext/phk/PhkPackageWebAccess.php';

/**
 * See {@link http://phk.tekwire.net/} for more information about PHK.
 *
 * @author Alexey Shockov <alexey@shockov.com>
 * @package phing.tasks.ext.phk
 */
class PhkPackageTask extends Task
{
    /**
     * @var string
     */
    private $outputFile;
    /**
     * @var string
     */
    private $inputDirectory;
    /**
     * @var string
     */
    private $phkCreatorPath;
    /**
     * @var PhkPackageWebAccess
     */
    private $webAccess;
    /**
     * @var array
     */
    private $modifiers = array();
    /**
     * @var array
     */
    private $options = array();
    /**
     * @return PhkPackageWebAccess
     */
    public function createWebAccess()
    {
        return ($this->webAccess = new PhkPackageWebAccess());
    }
    /**
     * @param string $crcCheck
     */
    public function setCrcCheck($crcCheck)
    {
        $this->options['crc_check'] = ('true' == $crcCheck ? true : false);
    }
    /**
     * @param string $webRunScript
     */
    public function setWebRunScript($webRunScript)
    {
        $this->options['web_run_script'] = $webRunScript;
    }
    /**
     * @param string $cliRunScript
     */
    public function setCliRunScript($cliRunScript)
    {
        $this->options['cli_run_script'] = $cliRunScript;
    }
    /**
     * @param string $libRunScript
     */
    public function setLibRunScript($libRunScript)
    {
        $this->options['lib_run_script'] = $libRunScript;
    }
    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->options['name'] =  $name;
    }
    /**
     * @param string $webMainRedirect
     */
    public function setWebMainRedirect($webMainRedirect)
    {
        $this->options['web_main_redirect'] = ('true' == $webMainRedirect ? true : false);
    }
    /**
     * @param string $pluginClass
     */
    public function setPluginClass($pluginClass)
    {
        $this->options['plugin_class'] = $pluginClass;
    }
    /**
     * @param string $version
     */
    public function setVersion($version)
    {
        $this->options['version'] = $version;
    }
    /**
     * @param string $summary
     */
    public function setSummary($summary)
    {
        $this->options['summary'] = $summary;
    }
    /**
     * @param string $inputDirectory
     */
    public function setInputDirectory($inputDirectory)
    {
        $this->inputDirectory = $inputDirectory;
    }
    /**
     * @param string $outputFile
     */
    public function setOutputFile($outputFile)
    {
        $this->outputFile = $outputFile;
    }
    /**
     * May be none, gzip or bzip2.
     *
     * @param string $compress
     */
    public function setCompress($compress)
    {
        $this->modifiers['compress'] = $compress;
    }
    /**
     * True or false.
     *
     * @param srting $strip
     */
    public function setStrip($strip)
    {
        $this->modifiers['strip'] = $strip;
    }
    /**
     * Path to PHK_Creator.phk file.
     *
     * @param srting $path
     */
    public function setPhkCreatorPath($path)
    {
        $this->phkCreatorPath = $path;
    }
    /**
     *
     */
    public function init()
    {

    }
    /**
     * Main method...
     */
    public function main()
    {
        /*
         * Check for empty first - speed ;)
         */
        if (!is_file($this->phkCreatorPath)) {
            throw new BuildException('You must specify the "phkcreatorpath" attribute for PHK task.');
        }
        if (empty($this->inputDirectory)) {
            throw new BuildException('You must specify the "inputdirectory" attribute for PHK task.');
        }
        if (empty($this->outputFile)) {
            throw new BuildException('You must specify the "outputfile" attribute for PHK task.');
        }

        require_once $this->phkCreatorPath;

        $mountPoint = PHK_Mgr::mount($this->outputFile, PHK::F_CREATOR);
        $phkManager = PHK_Mgr::instance($mountPoint);

        /*
         * Add files.
         */
        $phkManager->ftree()->merge_file_tree('/', $this->inputDirectory, $this->modifiers);

        /*
         * Add web_access to options, if present.
         */
        if (!is_null($this->webAccess)) {
            $webAccessPaths = $this->webAccess->getPaths();
            if (!empty($webAccessPaths)) {
                $this->options['web_access'] = $webAccessPaths;
            }
        }

        $phkManager->set_options($this->options);

        /*
         * Intercept output (in PHP we can't intercept stream).
         */
        ob_start();
        /*
         * Create file...
         */
        $phkManager->dump();
        /*
         * Print with Phing log...
         */
        $output = trim(ob_get_clean());
        $output = explode("\n", $output);
        foreach ($output as $line) {
            /*
             * Delete all '--- *' lines. Bluh!
             */
            /*
             * TODO Change preg_math to more faster alternative.
             */
            if (preg_match('/^---/', $line)) {
                continue;
            }

            $this->log($line);
        }

        /*
         * Set rights for generated file... Don't use umask() - see
         * notes in official documentation for this function.
         */
        chmod($this->outputFile, 0644);
    }
}