* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage install */ // Don't use the factory in this stage! require_once 'Console/Getargs.php'; /** * Console interface for make * * * @todo add colored output * * @version 1.0.0 * @package wombatSite * @subpackage install */ class wbMake_Console extends wbMake { /** * console args * @access protected * @var array $_consoleArgs */ var $_consoleArgs = array( 'basedir' => array( 'short' => 'b', 'min' => 1, 'max' => 1, 'desc' => 'Set basic site directory. Uses current directory if not specified.', 'default' => '.' ), 'list' => array( 'short' => 'l', 'min' => 0, 'max' => 0, 'desc' => 'List available targets and exit. Try with "verbose".', 'default' => 1 ), 'quiet' => array( 'short' => 'q', 'min' => 0, 'max' => 0, 'desc' => 'Run silently if no error occurs', 'default' => 1 ), 'verbose' => array( 'short' => 'v', 'min' => 0, 'max' => 0, 'desc' => 'Switch on verbosity' ), 'debug' => array( 'short' => 'd', 'min' => 0, 'max' => 0, 'desc' => 'Switch to debug mode' ), 'help' => array( 'short' => 'h', 'min' => 0, 'max' => 0, 'desc' => 'Display command line help and exit' ), CONSOLE_GETARGS_PARAMS => array( 'min' => 1, 'max' => -1, 'desc' => 'Make targets', ) ); /** * console arg object * @access private * @var object $_args */ var $_args; /** * verbosity level * @access private * @var int $_verbose */ var $_verbose = 0; /** * be quiet * @access private * @var int $_quiet */ var $_quiet = 0; /** * debug mode * @access private * @var int $_debug */ var $_debug = 0; /** * bring birth! * * @access public * @return boolean $result true on success */ function __construct() { } /** * php4 constructor wrapper * * @access public * @see __construct() */ function wbMake_Console() { $this->__construct(); } /** * Display user interface * * @access public * @return int $return 0 on success */ function display( ) { if( PEAR::isError( $this->_args ) ) { return $this->displayHelp( 1 ); } // display help? if( $this->_args === CONSOLE_GETARGS_HELP ) { return $this->displayHelp( 0 ); } // be quiet $this->_quiet = $this->_args->getValue( 'quiet' ); if( is_array( $this->_quiet ) && !empty( $this->_quiet ) ) { $this->_quiet = 1; } // be verbose $this->_verbose = $this->_args->getValue( 'verbose' ); if( is_array( $this->_verbose ) && !empty( $this->_verbose ) ) { $this->_verbose = 1; } // resolve quiet and verbose conflict! if( $this->_verbose && $this->_quiet ) { $this->_quiet = 0; $this->_pvl( "Try to be quiet and verbose. Pooh, that's gonna be hard! Especially if you love to chat, like I do!. Anyway, I'll give it a go! Well, maybe I could tell you about all the important stuff in detail and remain silent otherwise. Sounds great, eh? Still, what is important? And - even more difficult - what if my opinion on what's important differs from yours. Why the heck everyone leaves me with those brain cracking tasks by myself? That's not fair! I'll tell you what: I go ahead in verbosity and ignore the quiet-switch! Beeing quite is not my cup of tea anyway. Actually, what am I guilty about? It is not my fault that you want silence and loud talking at the same time. ...continue normal operation in verbose-mode. Don't do this again! :-) " ); } // run in debug mode? $this->_debug = $this->_args->getValue( 'debug' ); if( is_array( $this->_debug ) && !empty( $this->_debug ) ) { $this->_debug = 1; } wbFactory::singleton( 'wbDebugger' ); if( $this->_debug ) { wbDebugger::setActive( true ); $this->_pvl( 'Debug mode is ON' ); } // display target list if( $this->_args->getValue( 'list' ) ) { $this->_displayTargetList(); return 0; } // check parameter basedir $baseDir = realpath( $this->_args->getValue( 'basedir' ) ); if( !$baseDir ) { $this->_pl( 'Basedir not found!', STDERR ); return 1; } // get factory parameter from environment variables foreach( $this->_parameter as $confKey ) { $confEnv = getenv( 'WOMBAT_' . strtoupper( $confKey ) ); if( !empty( $confEnv ) ) { $this->_pvl( "Set parameter: $confKey=$confEnv" ); wbFactory::setParam( $confKey, $confEnv ); } } // check whether config file exists $confFile = $baseDir . '/' . wbFactory::getParam( 'configDir' ) . '/' . wbFactory::getParam( 'configFile' ); if( !is_file( $confFile ) ) { $this->_pl( 'Configuration file '. $confFile .' could not be found!', STDERR ); return 1; } // check target(s) $tar = $this->_args->getValue( CONSOLE_GETARGS_PARAMS ); if( empty( $tar ) ) { $this->_pl( 'No target(s) specified', STDERR ); return 1; } if( !is_array( $tar ) ) { $tar = array( $tar ); } // collect targets, but remain order as defined in $_targets $availables = array_keys( $this->_targets ); $targets = array(); foreach( $tar as $t ) { $t = strtolower( $t ); $pos = array_search( $t, $availables ); // invalid target if( $pos === false ) { $this->_pl( 'Unknown target: ' . $t, STDERR ); $this->_pl( 'Use option "--list" to recieve a list of available targets' ); return 1; } $targets[$pos] = $t; } ksort( $targets ); $targets = array_values( array_unique( $targets ) ); // actually load config and set up factory $this->_pvl( 'Load Configuration ' . $confFile ); wbFactory::setParam( 'baseDir', $baseDir ); wbFactory::setParam( 'useCache', 0 ); wbFactory::loadSiteConfig(); $res = $this->_processTargets( $targets ); if( patErrorManager::isError( $res ) ) { $this->_pl( 'Stoped after fatal error!', STDERR ); return 127; } if( $res ) { $this->_pvl( 'Done and done!' ); $result = 0; } else { $this->_pl( 'Stoped processing after error', STDERR ); $result = 1; } if( $this->_debug ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Total processing time: ' . wbDebugger::getUtime( 'genesis', 1000, 6 ) . ' ms', 'Stopwatch' ); wbDebugger::printMsg(); } return $result; } /** * process all selected targets * * @access privat * @param array $targets * @return boolean $result true on success, false otherwise */ function _processTargets( $targets ) { $this->_pl( 'Processing targets' ); $done = array(); foreach( $targets as $t ) { $this->_pl( ' ' . $t . ':' ); $maker = $this->_targets[$t]; foreach( $maker as $m ) { if( in_array( $m, $done ) ) { $this->_pl( ' - ' . $m . ' done'); continue; } $this->_p( ' - ' . $m ); $proc =& wbFactory::singleton( 'wbMaker_' . $m ); if( patErrorManager::isError( $proc ) ) { return $proc; } $this->_pvl( '' ); $this->_pvl( ' Info: ' . $proc->getInfo( 'brief' ) ); $res = $proc->make(); if( patErrorManager::isError( $res ) ) { return $res; } $this->_pvl( ' Message: ' . $proc->getInfo( 'msg' ) ); $this->_pv( ' Result:' ); if( !$res ) { $this->_pl( ' FAILED'); return false; } // process $this->_pl( ' OK'); array_push( $done, $m ); } } return true; } /** * print text * * @access privat * @param string $txt * @param mixed $to target either STDOUT or STDERR * @return boolean $result true if text was written to output */ function _p( $txt, $to = STDOUT ) { if( $this->_quiet && ( $to == STDOUT ) ) { return false; } fwrite( $to, $txt ); return true; } /** * print text and "\n" * * @access privat * @param string $txt * @param mixed $to target either STDOUT or STDERR * @return boolean $result true if text was written to output */ function _pl( $txt, $to = STDOUT ) { return $this->_p( $txt . "\n", $to ); } /** * print text when verbose * * @access privat * @param string $txt * @return boolean $result true if text was written to output */ function _pv( $txt ) { if( $this->_verbose ) { return $this->_p( $txt, STDOUT ); } return false; } /** * print text and "\n" when verbose * * @access privat * @param string $txt * @return boolean $result true if text was written to output */ function _pvl( $txt ) { if( $this->_verbose ) { return $this->_p( $txt . "\n", STDOUT ); } return false; } /** * Display target list * * @access public * @return boolean $result true on success * @todo make nice output */ function _displayTargetList() { if( $this->_quiet ) { $this->_quiet = 0; $this->_pl( 'Switch off quiet mode!' ); } $this->_pl( 'Available targets:' ); foreach( $this->_targets as $key => $value ) { $this->_pl( ' ' . $key ); $this->_pvl( ' maker:' ); foreach( $value as $v ) { $this->_pvl( ' - ' . $v ); } $this->_pvl( '' ); } return true; } /** * Display help * * @access public * @param int $return return code * @return int $return code from callee */ function displayHelp( $return = 0 ) { $head = array(); array_push( $head, 'Wombat Make' ); array_push( $head, 'Installation tool for the wombat framework' ); array_push( $head, '' ); array_push( $head, 'Usage: ' . basename( $_SERVER['_'] ) . ' [OPTION]... TARGET... ' ); array_push( $head, 'Options:' ); array_push( $head, '' ); $foot = array( '' ); array_push( $foot, 'Environment variables:' ); array_push( $foot, ' Also you may want to overwrite factory parameter with environment variables.' ); array_push( $foot, ' Example: WOMBAT_' . strtoupper( $this->_parameter[0] ) . '=your_new_value' ); array_push( $foot, ' Overwriteable parameters are:' ); array_push( $foot, ' "'. implode( '", "', $this->_parameter ) . '"' ); array_push( $foot, '' ); echo Console_Getargs::getHelp( $this->_consoleArgs, implode( "\n", $head ), implode( "\n", $foot ) ); return $return; } /** * pass arguments from console * * This is usually just a copy from $_SERVER['argv'] * * @access public * @param array $argv */ function setArgv( $argv ) { $this->_args =& Console_GetArgs::factory( $this->_consoleArgs, $argv ); return true; } } ?>