3 * Makefile for phpxmlrpc library.
4 * To be used with the Pake tool: https://github.com/indeyets/pake/wiki
6 * @copyright (c) 2015 G. Giunta
8 * @todo allow user to specify release number and tag/branch to use
9 * @todo !important allow user to specify location of docbook xslt instead of the one installed via composer
16 protected static $buildDir = 'build';
17 protected static $libVersion;
18 protected static $sourceBranch = 'master';
19 protected static $tools = array(
25 public static function libVersion()
27 return self::$libVersion;
30 public static function buildDir()
32 return self::$buildDir;
35 public static function workspaceDir()
37 return self::buildDir().'/workspace';
40 /// most likely things will break if this one is moved outside of BuildDir
41 public static function distDir()
43 return self::buildDir().'/xmlrpc-'.self::libVersion();
46 /// these will be generated in BuildDir
47 public static function distFiles()
50 'xmlrpc-'.self::libVersion().'.tar.gz',
51 'xmlrpc-'.self::libVersion().'.zip',
55 public static function sourceRepo()
57 return 'https://github.com/gggeek/phpxmlrpc';
60 /// @todo move git branch to be a named option?
61 public static function getOpts($args=array(), $cliOpts=array())
64 throw new \Exception('Missing library version argument');
65 self::$libVersion = $args[0];
67 self::$sourceBranch = $args[1];
69 foreach (self::$tools as $name => $binary) {
70 if (isset($cliOpts[$name])) {
71 self::$tools[$name] = $cliOpts[$name];
75 //pake_echo('---'.self::$libVersion.'---');
78 public static function tool($name)
80 return self::$tools[$name];
84 * @param string $inFile
85 * @param string $xssFile
86 * @param string $outFileOrDir
89 public static function applyXslt($inFile, $xssFile, $outFileOrDir)
92 if (!file_exists($inFile)) {
93 throw new \Exception("File $inFile cannot be found");
95 if (!file_exists($xssFile)) {
96 throw new \Exception("File $xssFile cannot be found");
99 // Load the XML source
100 $xml = new \DOMDocument();
102 $xsl = new \DOMDocument();
103 $xsl->load($xssFile);
105 // Configure the transformer
106 $processor = new \XSLTProcessor();
107 if (version_compare(PHP_VERSION, '5.4', "<")) {
108 if (defined('XSL_SECPREF_WRITE_FILE')) {
109 ini_set("xsl.security_prefs", XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
112 // the php online docs only mention setSecurityPrefs, but somehow some installs have setSecurityPreferences...
113 if (method_exists('XSLTProcessor', 'setSecurityPrefs')) {
114 $processor->setSecurityPrefs(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
116 $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
119 $processor->importStyleSheet($xsl); // attach the xsl rules
121 if (is_dir($outFileOrDir)) {
122 if (!$processor->setParameter('', 'base.dir', realpath($outFileOrDir))) {
123 echo "setting param base.dir KO\n";
127 $out = $processor->transformToXML($xml);
129 if (!is_dir($outFileOrDir)) {
130 file_put_contents($outFileOrDir, $out);
134 public static function highlightPhpInHtml($content)
136 $startTag = '<pre class="programlisting">';
139 //$content = file_get_contents($inFile);
142 while (($start = strpos($content, $startTag, $last)) !== false) {
143 $end = strpos($content, $endTag, $start);
144 $code = substr($content, $start + strlen($startTag), $end - $start - strlen($startTag));
145 if ($code[strlen($code) - 1] == "\n") {
146 $code = substr($code, 0, -1);
149 $code = str_replace(array('>', '<'), array('>', '<'), $code);
150 $code = highlight_string('<?php ' . $code, true);
151 $code = str_replace('<span style="color: #0000BB"><?php <br />', '<span style="color: #0000BB">', $code);
153 $out = $out . substr($content, $last, $start + strlen($startTag) - $last) . $code . $endTag;
154 $last = $end + strlen($endTag);
156 $out .= substr($content, $last, strlen($content));
166 use PhpXmlRpc\Builder;
168 function run_default($task=null, $args=array(), $cliOpts=array())
170 echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag] {\$task-options}\n";
172 echo " Run 'pake help' to list all pake options\n";
173 echo " Run 'pake -T' to list all available tasks\n";
174 echo " Task options:\n";
180 function run_getopts($task=null, $args=array(), $cliOpts=array())
182 Builder::getOpts($args, $cliOpts);
186 * Downloads source code in the build workspace directory, optionally checking out the given branch/tag
188 function run_init($task=null, $args=array(), $cliOpts=array())
190 // download the current version into the workspace
191 $targetDir = Builder::workspaceDir();
192 $targetBranch = 'php53';
194 // check if workspace exists and is not already set to the correct repo
195 if (is_dir($targetDir) && pakeGit::isRepository($targetDir)) {
196 $repo = new pakeGit($targetDir);
197 $remotes = $repo->remotes();
198 if (trim($remotes['origin']['fetch']) != Builder::sourceRepo()) {
199 throw new Exception("Directory '$targetDir' exists and is not linked to correct git repo");
202 /// @todo should we not just fetch instead?
205 pake_mkdirs(dirname($targetDir));
206 $repo = pakeGit::clone_repository(Builder::sourceRepo(), Builder::workspaceDir());
209 $repo->checkout($targetBranch);
213 * Runs all the build steps.
215 * (does nothing by itself, as all the steps are managed via task dependencies)
217 function run_build($task=null, $args=array(), $cliOpts=array())
221 function run_clean_doc()
223 pake_remove_dir(Builder::workspaceDir().'/doc/out');
224 pake_remove_dir(Builder::workspaceDir().'/doc/javadoc-out');
228 * Generates documentation in all formats
230 function run_doc($task=null, $args=array(), $cliOpts=array())
232 $docDir = Builder::workspaceDir().'/doc';
234 // API docs from phpdoc comments using phpdocumentor
235 $cmd = Builder::tool('php');
236 pake_sh("$cmd vendor/phpdocumentor/phpdocumentor/bin/phpdoc run -d ".Builder::workspaceDir().'/src'." -t ".Builder::workspaceDir().'/doc/javadoc-out --title PHP-XMLRPC');
238 # Jade cmd yet to be rebuilt, starting from xml file and putting output in ./out dir, e.g.
239 # jade -t xml -d custom.dsl xmlrpc_php.xml
241 # convertdoc command for xmlmind xxe editor
242 # convertdoc docb.toHTML xmlrpc_php.xml -u out
244 # saxon + xerces xml parser + saxon extensions + xslthl: adds a little syntax highligting
245 # (bold and italics only, no color) for php source examples...
247 # -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 \
248 # -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \
249 # -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \
250 # -Dxslthl.config=file:///c:/htdocs/xmlrpc_cvs/docbook-xsl/highlighting/xslthl-config.xml \
251 # com.icl.saxon.StyleSheet -o xmlrpc_php.fo.xml xmlrpc_php.xml custom.fo.xsl use.extensions=1
253 pake_mkdirs($docDir.'/out');
255 // HTML files from docbook
257 Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.xsl', $docDir.'/out/');
258 // post process html files to highlight php code samples
259 foreach(pakeFinder::type('file')->name('*.html')->in($docDir) as $file)
261 file_put_contents($file, Builder::highlightPhpInHtml(file_get_contents($file)));
264 // PDF file from docbook
266 // convert to fo and then to pdf using apache fop
267 Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.fo.xsl', $docDir.'/xmlrpc_php.fo.xml');
268 $cmd = Builder::tool('fop');
269 pake_sh("$cmd $docDir/xmlrpc_php.fo.xml $docDir/xmlrpc_php.pdf");
270 unlink($docDir.'/xmlrpc_php.fo.xml');
273 function run_clean_dist()
275 pake_remove_dir(Builder::distDir());
276 $finder = pakeFinder::type('file')->name(Builder::distFiles());
277 pake_remove($finder, Builder::buildDir());
281 * Creates the tarballs for a release
283 function run_dist($task=null, $args=array(), $cliOpts=array())
285 // copy workspace dir into dist dir, without git
286 pake_mkdirs(Builder::distDir());
287 $finder = pakeFinder::type('any')->ignore_version_control();
288 pake_mirror($finder, realpath(Builder::workspaceDir()), realpath(Builder::distDir()));
290 // remove unwanted files from dist dir
292 // also: do we still need to run dos2unix?
296 chdir(dirname(Builder::distDir()));
297 foreach(Builder::distFiles() as $distFile) {
298 // php can not really create good zip files via phar: they are not compressed!
299 if (substr($distFile, -4) == '.zip') {
300 $cmd = Builder::tool('zip');
302 pake_sh("$cmd $distFile $extra ".basename(Builder::distDir()));
305 $finder = pakeFinder::type('any')->pattern(basename(Builder::distDir()).'/**');
306 // see https://bugs.php.net/bug.php?id=58852
307 $pharFile = str_replace(Builder::libVersion(), '_LIBVERSION_', $distFile);
308 pakeArchive::createArchive($finder, '.', $pharFile);
309 rename($pharFile, $distFile);
316 * Cleans up the build directory
317 * @todo 'make clean' usually just removes the results of the build, distclean removes all but sources
319 function run_clean($task=null, $args=array(), $cliOpts=array())
321 pake_remove_dir(Builder::buildDir());
324 // helper task: display help text
325 pake_task( 'default' );
326 // internal task: parse cli options
327 pake_task('getopts');
328 pake_task('init', 'getopts');
329 pake_task('doc', 'getopts', 'init', 'clean-doc');
330 pake_task('build', 'getopts', 'init', 'doc');
331 pake_task('dist', 'getopts', 'init', 'build', 'clean-dist');
332 pake_task('clean-doc', 'getopts');
333 pake_task('clean-dist', 'getopts');
334 pake_task('clean', 'getopts');