diff options
Diffstat (limited to 'bin/gittool.php')
-rwxr-xr-x | bin/gittool.php | 216 |
1 files changed, 105 insertions, 111 deletions
diff --git a/bin/gittool.php b/bin/gittool.php index f9f68ac94..6944dde57 100755 --- a/bin/gittool.php +++ b/bin/gittool.php @@ -1,78 +1,101 @@ #!/usr/bin/php <?php - -if('cli' != php_sapi_name()) die(); -ini_set('memory_limit', '128M'); if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); define('NOSESSION', 1); require_once(DOKU_INC.'inc/init.php'); -$GitToolCLI = new GitToolCLI(); - -array_shift($argv); -$command = array_shift($argv); - -switch($command) { - case '': - case 'help': - $GitToolCLI->cmd_help(); - break; - case 'clone': - $GitToolCLI->cmd_clone($argv); - break; - case 'install': - $GitToolCLI->cmd_install($argv); - break; - case 'repo': - case 'repos': - $GitToolCLI->cmd_repos(); - break; - default: - $GitToolCLI->cmd_git($command, $argv); -} - /** * Easily manage DokuWiki git repositories * * @author Andreas Gohr <andi@splitbrain.org> */ -class GitToolCLI { - private $color = true; - - public function cmd_help() { - echo <<<EOF -Usage: gittool.php <command> [parameters] - -Manage git repositories for DokuWiki and its plugins and templates. +class GitToolCLI extends DokuCLI { -EXAMPLE - -$> ./bin/gittool.php clone gallery template:ach -$> ./bin/gittool.php repos -$> ./bin/gittool.php origin -v - -COMMANDS + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + "Manage git repositories for DokuWiki and its plugins and templates.\n\n". + "$> ./bin/gittool.php clone gallery template:ach\n". + "$> ./bin/gittool.php repos\n". + "$> ./bin/gittool.php origin -v" + ); -help - This help screen + $options->registerArgument( + 'command', + 'Command to execute. See below', + true + ); -clone <extensions> - Tries to install a known plugin or template (prefix with template:) via - git. Uses the DokuWiki.org plugin repository to find the proper git - repository. Multiple extensions can be given as parameters + $options->registerCommand( + 'clone', + 'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org '. + 'plugin repository to find the proper git repository. Multiple extensions can be given as parameters' + ); + $options->registerArgument( + 'extension', + 'name of the extension to install, prefix with \'template:\' for templates', + true, + 'clone' + ); -install <extensions> - The same as clone, but when no git source repository can be found, the - extension is installed via download + $options->registerCommand( + 'install', + 'The same as clone, but when no git source repository can be found, the extension is installed via '. + 'download' + ); + $options->registerArgument( + 'extension', + 'name of the extension to install, prefix with \'template:\' for templates', + true, + 'install' + ); -repos - Lists all git repositories found in this DokuWiki installation + $options->registerCommand( + 'repos', + 'Lists all git repositories found in this DokuWiki installation' + ); -<any> - Any unknown commands are assumed to be arguments to git and will be - executed in all repositories found within this DokuWiki installation + $options->registerCommand( + '*', + 'Any unknown commands are assumed to be arguments to git and will be executed in all repositories '. + 'found within this DokuWiki installation' + ); + } -EOF; + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param DokuCLI_Options $options + * @return void + */ + protected function main(DokuCLI_Options $options) { + $command = $options->getCmd(); + if(!$command) $command = array_shift($options->args); + + switch($command) { + case '': + echo $options->help(); + break; + case 'clone': + $this->cmd_clone($options->args); + break; + case 'install': + $this->cmd_install($options->args); + break; + case 'repo': + case 'repos': + $this->cmd_repos(); + break; + default: + $this->cmd_git($command, $options->args); + } } /** @@ -88,7 +111,7 @@ EOF; $repo = $this->getSourceRepo($ext); if(!$repo) { - $this->msg_error("could not find a repository for $ext"); + $this->error("could not find a repository for $ext"); $errors[] = $ext; } else { if($this->cloneExtension($ext, $repo)) { @@ -100,8 +123,8 @@ EOF; } echo "\n"; - if($succeeded) $this->msg_success('successfully cloned the following extensions: '.join(', ', $succeeded)); - if($errors) $this->msg_error('failed to clone the following extensions: '.join(', ', $errors)); + if($succeeded) $this->success('successfully cloned the following extensions: '.join(', ', $succeeded)); + if($errors) $this->error('failed to clone the following extensions: '.join(', ', $errors)); } /** @@ -117,7 +140,7 @@ EOF; $repo = $this->getSourceRepo($ext); if(!$repo) { - $this->msg_info("could not find a repository for $ext"); + $this->info("could not find a repository for $ext"); if($this->downloadExtension($ext)) { $succeeded[] = $ext; } else { @@ -133,8 +156,8 @@ EOF; } echo "\n"; - if($succeeded) $this->msg_success('successfully installed the following extensions: '.join(', ', $succeeded)); - if($errors) $this->msg_error('failed to install the following extensions: '.join(', ', $errors)); + if($succeeded) $this->success('successfully installed the following extensions: '.join(', ', $succeeded)); + if($errors) $this->error('failed to install the following extensions: '.join(', ', $errors)); } /** @@ -152,19 +175,19 @@ EOF; foreach($repos as $repo) { if(!@chdir($repo)) { - $this->msg_error("Could not change into $repo"); + $this->error("Could not change into $repo"); continue; } echo "\n"; - $this->msg_info("executing $shell in $repo"); + $this->info("executing $shell in $repo"); $ret = 0; system($shell, $ret); if($ret == 0) { - $this->msg_success("git succeeded in $repo"); + $this->success("git succeeded in $repo"); } else { - $this->msg_error("git failed in $repo"); + $this->error("git failed in $repo"); } } } @@ -193,23 +216,23 @@ EOF; $url = $plugin->getDownloadURL(); if(!$url) { - $this->msg_error("no download URL for $ext"); + $this->error("no download URL for $ext"); return false; } $ok = false; try { - $this->msg_info("installing $ext via download from $url"); + $this->info("installing $ext via download from $url"); $ok = $plugin->installFromURL($url); } catch(Exception $e) { - $this->msg_error($e->getMessage()); + $this->error($e->getMessage()); } if($ok) { - $this->msg_success("installed $ext via download"); + $this->success("installed $ext via download"); return true; } else { - $this->msg_success("failed to install $ext via download"); + $this->success("failed to install $ext via download"); return false; } } @@ -228,14 +251,14 @@ EOF; $target = DOKU_PLUGIN.$ext; } - $this->msg_info("cloning $ext from $repo to $target"); + $this->info("cloning $ext from $repo to $target"); $ret = 0; system("git clone $repo $target", $ret); if($ret === 0) { - $this->msg_success("cloning of $ext succeeded"); + $this->success("cloning of $ext succeeded"); return true; } else { - $this->msg_error("cloning of $ext failed"); + $this->error("cloning of $ext failed"); return false; } } @@ -248,7 +271,7 @@ EOF; * @return array */ private function findRepos() { - $this->msg_info('Looking for .git directories'); + $this->info('Looking for .git directories'); $data = array_merge( glob(DOKU_INC.'.git', GLOB_ONLYDIR), glob(DOKU_PLUGIN.'*/.git', GLOB_ONLYDIR), @@ -256,9 +279,9 @@ EOF; ); if(!$data) { - $this->msg_error('Found no .git directories'); + $this->error('Found no .git directories'); } else { - $this->msg_success('Found '.count($data).' .git directories'); + $this->success('Found '.count($data).' .git directories'); } $data = array_map('fullpath', array_map('dirname', $data)); return $data; @@ -304,37 +327,8 @@ EOF; return false; } +} - /** - * Print an error message - * - * @param $string - */ - private function msg_error($string) { - if($this->color) echo "\033[31m"; // red - echo "E: $string\n"; - if($this->color) echo "\033[37m"; // reset - } - - /** - * Print a success message - * - * @param $string - */ - private function msg_success($string) { - if($this->color) echo "\033[32m"; // green - echo "S: $string\n"; - if($this->color) echo "\033[37m"; // reset - } - - /** - * Print an info message - * - * @param $string - */ - private function msg_info($string) { - if($this->color) echo "\033[36m"; // cyan - echo "I: $string\n"; - if($this->color) echo "\033[37m"; // reset - } -}
\ No newline at end of file +// Main +$cli = new GitToolCLI(); +$cli->run();
\ No newline at end of file |