Add generation of docs to pakefile
[plcapi.git] / pakefile.php
1 <?php
2 /**
3  * Makefile for phpxmlrpc library.
4  * To be used with the Pake tool: https://github.com/indeyets/pake/wiki
5  *
6  * @copyright (c) 2015 G. Giunta
7  *
8  * @todo allow user to specify release number and tag/branch to use
9  */
10
11 namespace PhpXmlRpc {
12
13 class Builder
14 {
15     protected static $buildDir = 'build';
16     protected static $libVersion;
17     protected static $sourceBranch = 'master';
18     protected static $tools = array(
19         'zip' => 'zip',
20         'fop' => 'fop',
21         'php' => 'php'
22     );
23
24     public static function libVersion()
25     {
26         return self::$libVersion;
27     }
28
29     public static function buildDir()
30     {
31         return self::$buildDir;
32     }
33
34     public static function workspaceDir()
35     {
36         return self::buildDir().'/workspace';
37     }
38
39     /// most likely things will break if this one is moved outside of BuildDir
40     public static function distDir()
41     {
42         return self::buildDir().'/xmlrpc-'.self::libVersion();
43     }
44
45     /// these will be generated in BuildDir
46     public static function distFiles()
47     {
48         return array(
49             'xmlrpc-'.self::libVersion().'.tar.gz',
50             'xmlrpc-'.self::libVersion().'.zip',
51         );
52     }
53
54     public static function sourceRepo()
55     {
56         return 'https://github.com/gggeek/phpxmlrpc';
57     }
58
59     /// @todo move git branch to be a named option?
60     public static function getOpts($args=array(), $cliOpts=array())
61     {
62         if (count($args) < 1)
63             throw new \Exception('Missing library version argument');
64         self::$libVersion = $args[0];
65         if (count($args) > 1)
66             self::$sourceBranch = $args[1];
67
68         foreach (self::$tools as $name => $binary) {
69             if (isset($cliOpts[$name])) {
70                 self::$tools[$name] = $cliOpts[$name];
71             }
72         }
73
74         //pake_echo('---'.self::$libVersion.'---');
75     }
76
77     public static function tool($name)
78     {
79         return self::$tools[$name];
80     }
81
82     /**
83      * @param string $inFile
84      * @param string $xssFile
85      * @param string $outFileOrDir
86      * @throws \Exception
87      */
88     public static function applyXslt($inFile, $xssFile, $outFileOrDir)
89     {
90
91         if (!file_exists($inFile)) {
92             throw new \Exception("File $inFile cannot be found");
93         }
94         if (!file_exists($xssFile)) {
95             throw new \Exception("File $xssFile cannot be found");
96         }
97
98         // Load the XML source
99         $xml = new \DOMDocument();
100         $xml->load($inFile);
101         $xsl = new \DOMDocument();
102         $xsl->load($xssFile);
103
104         // Configure the transformer
105         $processor = new \XSLTProcessor();
106         if (version_compare(PHP_VERSION, '5.4', "<")) {
107             if (defined('XSL_SECPREF_WRITE_FILE')) {
108                 ini_set("xsl.security_prefs", XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
109             }
110         } else {
111             // the php online docs only mention setSecurityPrefs, but somehow some installs have setSecurityPreferences...
112             if (method_exists('XSLTProcessor', 'setSecurityPrefs')) {
113                 $processor->setSecurityPrefs(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
114             } else {
115                 $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
116             }
117         }
118         $processor->importStyleSheet($xsl); // attach the xsl rules
119
120         if (is_dir($outFileOrDir)) {
121             if (!$processor->setParameter('', 'base.dir', realpath($outFileOrDir))) {
122                 echo "setting param base.dir KO\n";
123             }
124         }
125
126         $out = $processor->transformToXML($xml);
127
128         if (!is_dir($outFileOrDir)) {
129             file_put_contents($outFileOrDir, $out);
130         }
131     }
132
133     public static function highlightPhpInHtml($content)
134     {
135         $startTag = '<pre class="programlisting">';
136         $endTag = '</pre>';
137
138         //$content = file_get_contents($inFile);
139         $last = 0;
140         $out = '';
141         while (($start = strpos($content, $startTag, $last)) !== false) {
142             $end = strpos($content, $endTag, $start);
143             $code = substr($content, $start + strlen($startTag), $end - $start - strlen($startTag));
144             if ($code[strlen($code) - 1] == "\n") {
145                 $code = substr($code, 0, -1);
146             }
147
148             $code = str_replace(array('&gt;', '&lt;'), array('>', '<'), $code);
149             $code = highlight_string('<?php ' . $code, true);
150             $code = str_replace('<span style="color: #0000BB">&lt;?php&nbsp;<br />', '<span style="color: #0000BB">', $code);
151
152             $out = $out . substr($content, $last, $start + strlen($startTag) - $last) . $code . $endTag;
153             $last = $end + strlen($endTag);
154         }
155         $out .= substr($content, $last, strlen($content));
156
157         return $out;
158     }
159 }
160
161 }
162
163 namespace {
164
165 use PhpXmlRpc\Builder;
166
167 function run_default($task=null, $args=array(), $cliOpts=array())
168 {
169     echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag] {\$task-options}\n";
170     echo "\n";
171     echo "  Run 'pake help' to list all pake options\n";
172     echo "  Run 'pake -T' to list all available tasks\n";
173     echo "  Task options:\n";
174     echo "      --php=\$php";
175     echo "      --fop=\$fop";
176     echo "      --zip=\$zip";
177 }
178
179 function run_getopts($task=null, $args=array(), $cliOpts=array())
180 {
181     Builder::getOpts($args, $cliOpts);
182 }
183
184 /**
185  * Downloads source code in the build workspace directory, optionally checking out the given branch/tag
186  */
187 function run_init($task=null, $args=array(), $cliOpts=array())
188 {
189     // download the current version into the workspace
190     $targetDir = Builder::workspaceDir();
191     $targetBranch = 'php53';
192
193     // check if workspace exists and is not already set to the correct repo
194     if (is_dir($targetDir) && pakeGit::isRepository($targetDir)) {
195         $repo = new pakeGit($targetDir);
196         $remotes = $repo->remotes();
197         if (trim($remotes['origin']['fetch']) != Builder::sourceRepo()) {
198             throw new Exception("Directory '$targetDir' exists and is not linked to correct git repo");
199         }
200
201         /// @todo should we not just fetch instead?
202         $repo->pull();
203     } else {
204         pake_mkdirs(dirname($targetDir));
205         $repo = pakeGit::clone_repository(Builder::sourceRepo(), Builder::workspaceDir());
206     }
207
208     $repo->checkout($targetBranch);
209 }
210
211 /**
212  * Runs all the build steps.
213  *
214  * (does nothing by itself, as all the steps are managed via task dependencies)
215  */
216 function run_build($task=null, $args=array(), $cliOpts=array())
217 {
218 }
219
220 function run_clean_doc()
221 {
222     pake_remove_dir(Builder::workspaceDir().'/doc/out');
223     pake_remove_dir(Builder::workspaceDir().'/doc/javadoc-out');
224 }
225
226 /**
227  * Generates documentation in all formats
228  */
229 function run_doc($task=null, $args=array(), $cliOpts=array())
230 {
231     $docDir = Builder::workspaceDir().'/doc';
232
233     // API docs from phpdoc comments using phpdocumentor
234     $cmd = Builder::tool('php');
235     pake_sh("$cmd vendor/phpdocumentor/phpdocumentor/bin/phpdoc run -d ".Builder::workspaceDir().'/src'." -t ".Builder::workspaceDir().'/doc/javadoc-out --title PHP-XMLRPC');
236
237     # Jade cmd yet to be rebuilt, starting from xml file and putting output in ./out dir, e.g.
238     #   jade -t xml -d custom.dsl xmlrpc_php.xml
239     #
240     # convertdoc command for xmlmind xxe editor
241     #   convertdoc docb.toHTML xmlrpc_php.xml -u out
242     #
243     # saxon + xerces xml parser + saxon extensions + xslthl: adds a little syntax highligting
244     # (bold and italics only, no color) for php source examples...
245     #   java \
246     #   -classpath c:\programmi\saxon\saxon.jar\;c:\programmi\saxon\xslthl.jar\;c:\programmi\xerces\xercesImpl.jar\;C:\htdocs\xmlrpc_cvs\docbook-xsl\extensions\saxon65.jar \
247     #   -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \
248     #   -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \
249     #   -Dxslthl.config=file:///c:/htdocs/xmlrpc_cvs/docbook-xsl/highlighting/xslthl-config.xml \
250     #   com.icl.saxon.StyleSheet -o xmlrpc_php.fo.xml xmlrpc_php.xml custom.fo.xsl use.extensions=1
251
252     pake_mkdirs($docDir.'/out');
253
254     // HTML files from docbook
255
256     Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.xsl', $docDir.'/out/');
257     // post process html files to highlight php code samples
258     foreach(pakeFinder::type('file')->name('*.html')->in($docDir) as $file)
259     {
260         file_put_contents($file, Builder::highlightPhpInHtml(file_get_contents($file)));
261     }
262
263     // PDF file from docbook
264
265     // convert to fo and then to pdf using apache fop
266     Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.fo.xsl', $docDir.'/xmlrpc_php.fo.xml');
267     $cmd = Builder::tool('fop');
268     pake_sh("$cmd $docDir/xmlrpc_php.fo.xml $docDir/xmlrpc_php.pdf");
269     unlink($docDir.'/xmlrpc_php.fo.xml');
270 }
271
272 function run_clean_dist()
273 {
274     pake_remove_dir(Builder::distDir());
275     $finder = pakeFinder::type('file')->name(Builder::distFiles());
276     pake_remove($finder, Builder::buildDir());
277 }
278
279 /**
280  * Creates the tarballs for a release
281  */
282 function run_dist($task=null, $args=array(), $cliOpts=array())
283 {
284     // copy workspace dir into dist dir, without git
285     pake_mkdirs(Builder::distDir());
286     $finder = pakeFinder::type('any')->ignore_version_control();
287     pake_mirror($finder, realpath(Builder::workspaceDir()), realpath(Builder::distDir()));
288
289     // remove unwanted files from dist dir
290
291     // also: do we still need to run dos2unix?
292
293     // create tarballs
294     $cwd = getcwd();
295     chdir(dirname(Builder::distDir()));
296     foreach(Builder::distFiles() as $distFile) {
297         // php can not really create good zip files via phar: they are not compressed!
298         if (substr($distFile, -4) == '.zip') {
299             $cmd = Builder::tool('zip');
300             $extra = '-9 -r';
301             pake_sh("$cmd $distFile $extra ".basename(Builder::distDir()));
302         }
303         else {
304             $finder = pakeFinder::type('any')->pattern(basename(Builder::distDir()).'/**');
305             // see https://bugs.php.net/bug.php?id=58852
306             $pharFile = str_replace(Builder::libVersion(), '_LIBVERSION_', $distFile);
307             pakeArchive::createArchive($finder, '.', $pharFile);
308             rename($pharFile, $distFile);
309         }
310     }
311     chdir($cwd);
312 }
313
314 /**
315  * Cleans up the build directory
316  * @todo 'make clean' usually just removes the results of the build, distclean removes all but sources
317  */
318 function run_clean($task=null, $args=array(), $cliOpts=array())
319 {
320     pake_remove_dir(Builder::buildDir());
321 }
322
323 // helper task: display help text
324 pake_task( 'default' );
325 // internal task: parse cli options
326 pake_task('getopts');
327 pake_task('init', 'getopts');
328 pake_task('doc', 'getopts', 'init', 'clean-doc');
329 pake_task('build', 'getopts', 'init', 'doc');
330 pake_task('dist', 'getopts', 'init', 'build', 'clean-dist');
331 pake_task('clean-doc', 'getopts');
332 pake_task('clean-dist', 'getopts');
333 pake_task('clean', 'getopts');
334
335 }