Add generation of docs to pakefile
[plcapi.git] / pakefile.php
index 2ae9e35..5b7386b 100644 (file)
@@ -3,7 +3,8 @@
  * Makefile for phpxmlrpc library.
  * To be used with the Pake tool: https://github.com/indeyets/pake/wiki
  *
- * @todo allow user to specify location for zip command
+ * @copyright (c) 2015 G. Giunta
+ *
  * @todo allow user to specify release number and tag/branch to use
  */
 
@@ -11,9 +12,14 @@ namespace PhpXmlRpc {
 
 class Builder
 {
-    protected static $buildDir = 'build/';
+    protected static $buildDir = 'build';
     protected static $libVersion;
     protected static $sourceBranch = 'master';
+    protected static $tools = array(
+        'zip' => 'zip',
+        'fop' => 'fop',
+        'php' => 'php'
+    );
 
     public static function libVersion()
     {
@@ -27,13 +33,13 @@ class Builder
 
     public static function workspaceDir()
     {
-        return self::buildDir().'workspace';
+        return self::buildDir().'/workspace';
     }
 
     /// most likely things will break if this one is moved outside of BuildDir
     public static function distDir()
     {
-        return self::buildDir().'xmlrpc-'.self::libVersion();
+        return self::buildDir().'/xmlrpc-'.self::libVersion();
     }
 
     /// these will be generated in BuildDir
@@ -59,7 +65,96 @@ class Builder
         if (count($args) > 1)
             self::$sourceBranch = $args[1];
 
-        pake_echo('---'.self::$libVersion.'---');
+        foreach (self::$tools as $name => $binary) {
+            if (isset($cliOpts[$name])) {
+                self::$tools[$name] = $cliOpts[$name];
+            }
+        }
+
+        //pake_echo('---'.self::$libVersion.'---');
+    }
+
+    public static function tool($name)
+    {
+        return self::$tools[$name];
+    }
+
+    /**
+     * @param string $inFile
+     * @param string $xssFile
+     * @param string $outFileOrDir
+     * @throws \Exception
+     */
+    public static function applyXslt($inFile, $xssFile, $outFileOrDir)
+    {
+
+        if (!file_exists($inFile)) {
+            throw new \Exception("File $inFile cannot be found");
+        }
+        if (!file_exists($xssFile)) {
+            throw new \Exception("File $xssFile cannot be found");
+        }
+
+        // Load the XML source
+        $xml = new \DOMDocument();
+        $xml->load($inFile);
+        $xsl = new \DOMDocument();
+        $xsl->load($xssFile);
+
+        // Configure the transformer
+        $processor = new \XSLTProcessor();
+        if (version_compare(PHP_VERSION, '5.4', "<")) {
+            if (defined('XSL_SECPREF_WRITE_FILE')) {
+                ini_set("xsl.security_prefs", XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
+            }
+        } else {
+            // the php online docs only mention setSecurityPrefs, but somehow some installs have setSecurityPreferences...
+            if (method_exists('XSLTProcessor', 'setSecurityPrefs')) {
+                $processor->setSecurityPrefs(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
+            } else {
+                $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE);
+            }
+        }
+        $processor->importStyleSheet($xsl); // attach the xsl rules
+
+        if (is_dir($outFileOrDir)) {
+            if (!$processor->setParameter('', 'base.dir', realpath($outFileOrDir))) {
+                echo "setting param base.dir KO\n";
+            }
+        }
+
+        $out = $processor->transformToXML($xml);
+
+        if (!is_dir($outFileOrDir)) {
+            file_put_contents($outFileOrDir, $out);
+        }
+    }
+
+    public static function highlightPhpInHtml($content)
+    {
+        $startTag = '<pre class="programlisting">';
+        $endTag = '</pre>';
+
+        //$content = file_get_contents($inFile);
+        $last = 0;
+        $out = '';
+        while (($start = strpos($content, $startTag, $last)) !== false) {
+            $end = strpos($content, $endTag, $start);
+            $code = substr($content, $start + strlen($startTag), $end - $start - strlen($startTag));
+            if ($code[strlen($code) - 1] == "\n") {
+                $code = substr($code, 0, -1);
+            }
+
+            $code = str_replace(array('&gt;', '&lt;'), array('>', '<'), $code);
+            $code = highlight_string('<?php ' . $code, true);
+            $code = str_replace('<span style="color: #0000BB">&lt;?php&nbsp;<br />', '<span style="color: #0000BB">', $code);
+
+            $out = $out . substr($content, $last, $start + strlen($startTag) - $last) . $code . $endTag;
+            $last = $end + strlen($endTag);
+        }
+        $out .= substr($content, $last, strlen($content));
+
+        return $out;
     }
 }
 
@@ -71,10 +166,14 @@ use PhpXmlRpc\Builder;
 
 function run_default($task=null, $args=array(), $cliOpts=array())
 {
-    echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag]\n";
+    echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag] {\$task-options}\n";
     echo "\n";
     echo "  Run 'pake help' to list all pake options\n";
     echo "  Run 'pake -T' to list all available tasks\n";
+    echo "  Task options:\n";
+    echo "      --php=\$php";
+    echo "      --fop=\$fop";
+    echo "      --zip=\$zip";
 }
 
 function run_getopts($task=null, $args=array(), $cliOpts=array())
@@ -118,12 +217,56 @@ function run_build($task=null, $args=array(), $cliOpts=array())
 {
 }
 
+function run_clean_doc()
+{
+    pake_remove_dir(Builder::workspaceDir().'/doc/out');
+    pake_remove_dir(Builder::workspaceDir().'/doc/javadoc-out');
+}
+
 /**
  * Generates documentation in all formats
  */
 function run_doc($task=null, $args=array(), $cliOpts=array())
 {
-    pake_echo('TBD...');
+    $docDir = Builder::workspaceDir().'/doc';
+
+    // API docs from phpdoc comments using phpdocumentor
+    $cmd = Builder::tool('php');
+    pake_sh("$cmd vendor/phpdocumentor/phpdocumentor/bin/phpdoc run -d ".Builder::workspaceDir().'/src'." -t ".Builder::workspaceDir().'/doc/javadoc-out --title PHP-XMLRPC');
+
+    # Jade cmd yet to be rebuilt, starting from xml file and putting output in ./out dir, e.g.
+    #  jade -t xml -d custom.dsl xmlrpc_php.xml
+    #
+    # convertdoc command for xmlmind xxe editor
+    #  convertdoc docb.toHTML xmlrpc_php.xml -u out
+    #
+    # saxon + xerces xml parser + saxon extensions + xslthl: adds a little syntax highligting
+    # (bold and italics only, no color) for php source examples...
+    #  java \
+    #  -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 \
+    #  -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \
+    #  -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \
+    #  -Dxslthl.config=file:///c:/htdocs/xmlrpc_cvs/docbook-xsl/highlighting/xslthl-config.xml \
+    #  com.icl.saxon.StyleSheet -o xmlrpc_php.fo.xml xmlrpc_php.xml custom.fo.xsl use.extensions=1
+
+    pake_mkdirs($docDir.'/out');
+
+    // HTML files from docbook
+
+    Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.xsl', $docDir.'/out/');
+    // post process html files to highlight php code samples
+    foreach(pakeFinder::type('file')->name('*.html')->in($docDir) as $file)
+    {
+        file_put_contents($file, Builder::highlightPhpInHtml(file_get_contents($file)));
+    }
+
+    // PDF file from docbook
+
+    // convert to fo and then to pdf using apache fop
+    Builder::applyXslt($docDir.'/xmlrpc_php.xml', $docDir.'/custom.fo.xsl', $docDir.'/xmlrpc_php.fo.xml');
+    $cmd = Builder::tool('fop');
+    pake_sh("$cmd $docDir/xmlrpc_php.fo.xml $docDir/xmlrpc_php.pdf");
+    unlink($docDir.'/xmlrpc_php.fo.xml');
 }
 
 function run_clean_dist()
@@ -148,11 +291,12 @@ function run_dist($task=null, $args=array(), $cliOpts=array())
     // also: do we still need to run dos2unix?
 
     // create tarballs
+    $cwd = getcwd();
     chdir(dirname(Builder::distDir()));
     foreach(Builder::distFiles() as $distFile) {
         // php can not really create good zip files via phar: they are not compressed!
         if (substr($distFile, -4) == '.zip') {
-            $cmd = 'zip';
+            $cmd = Builder::tool('zip');
             $extra = '-9 -r';
             pake_sh("$cmd $distFile $extra ".basename(Builder::distDir()));
         }
@@ -164,6 +308,7 @@ function run_dist($task=null, $args=array(), $cliOpts=array())
             rename($pharFile, $distFile);
         }
     }
+    chdir($cwd);
 }
 
 /**
@@ -180,9 +325,10 @@ pake_task( 'default' );
 // internal task: parse cli options
 pake_task('getopts');
 pake_task('init', 'getopts');
-pake_task('doc', 'getopts', 'init');
+pake_task('doc', 'getopts', 'init', 'clean-doc');
 pake_task('build', 'getopts', 'init', 'doc');
 pake_task('dist', 'getopts', 'init', 'build', 'clean-dist');
+pake_task('clean-doc', 'getopts');
 pake_task('clean-dist', 'getopts');
 pake_task('clean', 'getopts');