summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/ext/PearPackage2Task.php
blob: f42231f715bd26684dc2549166100887e5439d18 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
 *  $Id: 1b20dbb6595bd4c41d1e5f1430900e3bf95de411 $
 *
 * 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/tasks/ext/PearPackageTask.php';

/**
 * A task to create a PEAR package.xml version 2.0 file.
 *
 * This class uses the PEAR_PackageFileManager2 class to perform the work.
 *
 * This class is designed to be very flexible -- i.e. account for changes to the package.xml w/o
 * requiring changes to this class.  We've accomplished this by having generic <option> and <mapping>
 * nested elements.  All options are set using PEAR_PackageFileManager2::setOptions().
 *
 * The <option> tag is used to set a simple option value.
 * <code>
 * <option name="option_name" value="option_value"/>
 * or <option name="option_name">option_value</option>
 * </code>
 *
 * The <mapping> tag represents a complex data type.  You can use nested <element> (and nested <element> with
 * <element> tags) to represent the full complexity of the structure.  Bear in mind that what you are creating
 * will be mapped to an associative array that will be passed in via PEAR_PackageFileManager2::setOptions().
 * <code>
 * <mapping name="option_name">
 *  <element key="key_name" value="key_val"/>
 *  <element key="key_name" value="key_val"/>
 * </mapping>
 * </code>
 *
 * Here's an over-simple example of how this could be used:
 * <code>
 * <pearpkg2 name="phing" dir="${build.src.dir}">
 *  <fileset dir="src">
 *      <include name="**"/>
 *  </fileset>
 *  <option name="outputdirectory" value="./build"/>
 *  <option name="packagefile" value="package2.xml"/>
 *  <option name="packagedirectory" value="./${build.dist.dir}"/>
 *  <option name="baseinstalldir" value="${pkg.prefix}"/>
 *  <option name="channel" value="my.pear-channel.com"/>
 *  <option name="summary" value="${pkg.summary}"/>
 *  <option name="description" value="${pkg.description}"/>
 *  <option name="apiversion" value="${pkg.version}"/>
 *  <option name="apistability" value="beta"/>
 *  <option name="releaseversion" value="${pkg.version}"/>
 *  <option name="releasestability" value="beta"/>
 *  <option name="license" value="none"/>
 *  <option name="phpdep" value="5.0.0"/>
 *  <option name="pearinstallerdep" value="1.4.6"/>
 *  <option name="packagetype" value="php"/>
 *  <option name="notes" value="${pkg.relnotes}"/>
 *  <mapping name="maintainers">
 *   <element>
 *    <element key="handle" value="hlellelid"/>
 *    <element key="name" value="Hans"/>
 *    <element key="email" value="hans@xmpl.org"/>
 *    <element key="role" value="lead"/>
 *    <element key="active" value="yes"/>
 *   </element>
 *  </mapping>
 * </pearpkg2>
 * </code>
 *
 * Look at the build.xml in the Phing base directory (assuming you have the full distro / CVS version of Phing) to
 * see a more complete example of how to call this script.
 *
 * @author   Stuart Binge <stuart.binge@complinet.com>
 * @author   Hans Lellelid <hans@xmpl.org>
 * @package  phing.tasks.ext
 * @version  $Id: 1b20dbb6595bd4c41d1e5f1430900e3bf95de411 $
 */
class PearPackage2Task extends PearPackageTask {

    public function init() {
        include_once 'PEAR/PackageFileManager2.php';
        if (!class_exists('PEAR_PackageFileManager2')) {
            throw new BuildException("You must have installed PEAR_PackageFileManager in order to create a PEAR package.xml version 2.0 file.");
        }
    }

    protected function setVersion2Options()
    {
        $this->pkg->setPackage($this->package);
        $this->pkg->setDate(strftime('%Y-%m-%d'));
        $this->pkg->setTime(strftime('%H:%M:%S')); 

        $newopts = array();
        foreach ($this->options as $opt) {
            switch ($opt->getName()) {
                case 'summary':
                    $this->pkg->setSummary($opt->getValue());
                    break;

                case 'description':
                    $this->pkg->setDescription($opt->getValue());
                    break;

                case 'uri':
                    $this->pkg->setUri($opt->getValue());
                    break;

                case 'license':
                    $this->pkg->setLicense($opt->getValue());
                    break;

                case 'channel':
                    $this->pkg->setChannel($opt->getValue());
                    break;

                case 'apiversion':
                    $this->pkg->setAPIVersion($opt->getValue());
                    break;

                case 'releaseversion':
                    $this->pkg->setReleaseVersion($opt->getValue());
                    break;

                case 'releasestability':
                    $this->pkg->setReleaseStability($opt->getValue());
                    break;

                case 'apistability':
                    $this->pkg->setAPIStability($opt->getValue());
                    break;

                case 'notes':
                    $this->pkg->setNotes($opt->getValue());
                    break;

                case 'packagetype':
                    $this->pkg->setPackageType($opt->getValue());
                    break;

                case 'phpdep':
                    $this->pkg->setPhpDep($opt->getValue());
                    break;

                case 'pearinstallerdep':
                    $this->pkg->setPearinstallerDep($opt->getValue());
                    break;

                default:
                    $newopts[] = $opt;
                    break;
            }
        }
        $this->options = $newopts;

        $newmaps = array();
        foreach ($this->mappings as $map) {
            switch ($map->getName()) {
                case 'deps':
                    $deps = $map->getValue();
                    foreach ($deps as $dep) {
                        $type = isset($dep['optional']) ? 'optional' : 'required';
                        $min = isset($dep['min']) ? $dep['min'] : $dep['version'];
                        $max = isset($dep['max']) ? $dep['max'] : null;
                        $rec = isset($dep['recommended']) ? $dep['recommended'] : null;
                        $channel = isset($dep['channel']) ? $dep['channel'] : false;
                        $uri = isset($dep['uri']) ? $dep['uri'] : false;

                        if (!empty($channel)) {
                            $this->pkg->addPackageDepWithChannel(
                                $type, $dep['name'], $channel, $min, $max, $rec
                            );
                        } elseif (!empty($uri)) {
                            $this->pkg->addPackageDepWithUri(
                                $type, $dep['name'], $uri
                            );
                        }
                    };
                    break;

                case 'extdeps':
                    $deps = $map->getValue();
                    foreach ($deps as $dep) {
                        $type = isset($dep['optional']) ? 'optional' : 'required';
                        $min = isset($dep['min']) ? $dep['min'] : $dep['version'];
                        $max = isset($dep['max']) ? $dep['max'] : $dep['version'];
                        $rec = isset($dep['recommended']) ? $dep['recommended'] : $dep['version'];

                        $this->pkg->addExtensionDep(
                            $type, $dep['name'], $min, $max, $rec
                        );
                    };
                    break;

                case 'maintainers':
                    $maintainers = $map->getValue();

                    foreach ($maintainers as $maintainer) {
                        if (!isset($maintainer['active'])) {
                            $maintainer['active'] = 'yes';
                        } else {
                            $maintainer['active'] = $maintainer['active'] === false ? 'no' : 'yes';
                        }
                        $this->pkg->addMaintainer(
                            $maintainer['role'],
                            $maintainer['handle'],
                            $maintainer['name'],
                            $maintainer['email'],
                            $maintainer['active']
                        );
                    }
                    break;

                case 'replacements':
                    $replacements = $map->getValue();

                    foreach($replacements as $replacement) { 
                        $this->pkg->addReplacement(
                            $replacement['path'], 
                            $replacement['type'], 
                            $replacement['from'], 
                            $replacement['to']
                        );
                    }
                    break;

				case 'role':
					foreach ($map->getValue() as $role) {
						$this->pkg->addRole($role['extension'], $role['role']);
					}
					break;

                default:
                    $newmaps[] = $map;
            }
        }
        $this->mappings = $newmaps;
    }

    /**
     * Main entry point.
     * @return void
     */
    public function main()
    {
        if ($this->dir === null) {
            throw new BuildException("You must specify the \"dir\" attribute for PEAR package 2 task.");
        }

        if ($this->package === null) {
            throw new BuildException("You must specify the \"name\" attribute for PEAR package 2 task.");
        }

        $this->pkg = new PEAR_PackageFileManager2();

        $this->setVersion2Options();
        $this->setOptions();

        $this->pkg->addRelease();
        $this->pkg->generateContents();
        $e = $this->pkg->writePackageFile();
        if (PEAR::isError($e)) {
            throw new BuildException("Unable to write package file.", new Exception($e->getMessage()));
        }
    }

}