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
15 protected static $buildDir = 'build';
16 protected static $libVersion;
17 protected static $sourceBranch = 'master';
18 protected static $tools = array(
24 public static function libVersion()
26 return self::$libVersion;
29 public static function buildDir()
31 return self::$buildDir;
34 public static function workspaceDir()
36 return self::buildDir().'/workspace';
39 /// most likely things will break if this one is moved outside of BuildDir
40 public static function distDir()
42 return self::buildDir().'/xmlrpc-'.self::libVersion();
45 /// these will be generated in BuildDir
46 public static function distFiles()
49 'xmlrpc-'.self::libVersion().'.tar.gz',
50 'xmlrpc-'.self::libVersion().'.zip',
54 public static function sourceRepo()
56 return 'https://github.com/gggeek/phpxmlrpc';
59 /// @todo move git branch to be a named option?
60 public static function getOpts($args=array(), $cliOpts=array())
63 throw new \Exception('Missing library version argument');
64 self::$libVersion = $args[0];
66 self::$sourceBranch = $args[1];
68 foreach (self::$tools as $name => $binary) {
69 if (isset($cliOpts[$name])) {
70 self::$tools[$name] = $cliOpts[$name];
74 //pake_echo('---'.self::$libVersion.'---');
77 public static function tool($name)
79 return self::$tools[$name];
83 * @param string $inFile
84 * @param string $xssFile
85 * @param string $outFileOrDir
88 public static function applyXslt($inFile, $xssFile, $outFileOrDir)
91 if (!file_exists($inFile)) {
92 throw new \Exception("File $inFile cannot be found");
94 if (!file_exists($xssFile)) {
95 throw new \Exception("File $xssFile cannot be found");
98 // Load the XML source
99 $xml = new \DOMDocument();
101 $xsl = new \DOMDocument();
102 $xsl->load($xssFile);
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);
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);
115 $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
118 $processor->importStyleSheet($xsl); // attach the xsl rules
120 if (is_dir($outFileOrDir)) {
121 if (!$processor->setParameter('', 'base.dir', realpath($outFileOrDir))) {
122 echo "setting param base.dir KO\n";
126 $out = $processor->transformToXML($xml);
128 if (!is_dir($outFileOrDir)) {
129 file_put_contents($outFileOrDir, $out);
133 public static function highlightPhpInHtml($content)
135 $startTag = '<pre class="programlisting">';
138 //$content = file_get_contents($inFile);
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);
148 $code = str_replace(array('>', '<'), array('>', '<'), $code);
149 $code = highlight_string('<?php ' . $code, true);
150 $code = str_replace('<span style="color: #0000BB"><?php <br />', '<span style="color: #0000BB">', $code);
152 $out = $out . substr($content, $last, $start + strlen($startTag) - $last) . $code . $endTag;
153 $last = $end + strlen($endTag);
155 $out .= substr($content, $last, strlen($content));
165 use PhpXmlRpc\Builder;
167 function run_default($task=null, $args=array(), $cliOpts=array())
169 echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag] {\$task-options}\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";
179 function run_getopts($task=null, $args=array(), $cliOpts=array())
181 Builder::getOpts($args, $cliOpts);
185 * Downloads source code in the build workspace directory, optionally checking out the given branch/tag
187 function run_init($task=null, $args=array(), $cliOpts=array())
189 // download the current version into the workspace
190 $targetDir = Builder::workspaceDir();
191 $targetBranch = 'php53';
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");
201 /// @todo should we not just fetch instead?
204 pake_mkdirs(dirname($targetDir));
205 $repo = pakeGit::clone_repository(Builder::sourceRepo(), Builder::workspaceDir());
208 $repo->checkout($targetBranch);
212 * Runs all the build steps.
214 * (does nothing by itself, as all the steps are managed via task dependencies)
216 function run_build($task=null, $args=array(), $cliOpts=array())
220 function run_clean_doc()
222 pake_remove_dir(Builder::workspaceDir().'/doc/out');
223 pake_remove_dir(Builder::workspaceDir().'/doc/javadoc-out');
227 * Generates documentation in all formats
229 function run_doc($task=null, $args=array(), $cliOpts=array())
231 $docDir = Builder::workspaceDir().'/doc';
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');
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
240 # convertdoc command for xmlmind xxe editor
241 # convertdoc docb.toHTML xmlrpc_php.xml -u out
243 # saxon + xerces xml parser + saxon extensions + xslthl: adds a little syntax highligting
244 # (bold and italics only, no color) for php source examples...
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
252 pake_mkdirs($docDir.'/out');
254 // HTML files from docbook
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)
260 file_put_contents($file, Builder::highlightPhpInHtml(file_get_contents($file)));
263 // PDF file from docbook
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');
272 function run_clean_dist()
274 pake_remove_dir(Builder::distDir());
275 $finder = pakeFinder::type('file')->name(Builder::distFiles());
276 pake_remove($finder, Builder::buildDir());
280 * Creates the tarballs for a release
282 function run_dist($task=null, $args=array(), $cliOpts=array())
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()));
289 // remove unwanted files from dist dir
291 // also: do we still need to run dos2unix?
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');
301 pake_sh("$cmd $distFile $extra ".basename(Builder::distDir()));
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);
315 * Cleans up the build directory
316 * @todo 'make clean' usually just removes the results of the build, distclean removes all but sources
318 function run_clean($task=null, $args=array(), $cliOpts=array())
320 pake_remove_dir(Builder::buildDir());
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');