4 use splitbrain\phpcli\CLI
;
5 use splitbrain\phpcli\Options
;
7 if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__
) . '/../') . '/');
8 define('NOSESSION', 1);
9 require_once(DOKU_INC
. 'inc/init.php');
12 * Easily manage DokuWiki git repositories
14 * @author Andreas Gohr <andi@splitbrain.org>
16 class GitToolCLI
extends CLI
{
19 * Register options and arguments on the given $options object
21 * @param Options $options
24 protected function setup(Options
$options) {
26 "Manage git repositories for DokuWiki and its plugins and templates.\n\n" .
27 "$> ./bin/gittool.php clone gallery template:ach\n" .
28 "$> ./bin/gittool.php repos\n" .
29 "$> ./bin/gittool.php origin -v"
32 $options->registerArgument(
34 'Command to execute. See below',
38 $options->registerCommand(
40 'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org ' .
41 'plugin repository to find the proper git repository. Multiple extensions can be given as parameters'
43 $options->registerArgument(
45 'name of the extension to install, prefix with \'template:\' for templates',
50 $options->registerCommand(
52 'The same as clone, but when no git source repository can be found, the extension is installed via ' .
55 $options->registerArgument(
57 'name of the extension to install, prefix with \'template:\' for templates',
62 $options->registerCommand(
64 'Lists all git repositories found in this DokuWiki installation'
67 $options->registerCommand(
69 'Any unknown commands are assumed to be arguments to git and will be executed in all repositories ' .
70 'found within this DokuWiki installation'
77 * Arguments and options have been parsed when this is run
79 * @param Options $options
82 protected function main(Options
$options) {
83 $command = $options->getCmd();
84 $args = $options->getArgs();
85 if(!$command) $command = array_shift($args);
89 echo $options->help();
92 $this->cmd_clone($args);
95 $this->cmd_install($args);
102 $this->cmd_git($command, $args);
107 * Tries to install the given extensions using git clone
109 * @param array $extensions
111 public function cmd_clone($extensions) {
113 $succeeded = array();
115 foreach($extensions as $ext) {
116 $repo = $this->getSourceRepo($ext);
119 $this->error("could not find a repository for $ext");
122 if($this->cloneExtension($ext, $repo)) {
131 if($succeeded) $this->success('successfully cloned the following extensions: ' . join(', ', $succeeded));
132 if($errors) $this->error('failed to clone the following extensions: ' . join(', ', $errors));
136 * Tries to install the given extensions using git clone with fallback to install
138 * @param array $extensions
140 public function cmd_install($extensions) {
142 $succeeded = array();
144 foreach($extensions as $ext) {
145 $repo = $this->getSourceRepo($ext);
148 $this->info("could not find a repository for $ext");
149 if($this->downloadExtension($ext)) {
155 if($this->cloneExtension($ext, $repo)) {
164 if($succeeded) $this->success('successfully installed the following extensions: ' . join(', ', $succeeded));
165 if($errors) $this->error('failed to install the following extensions: ' . join(', ', $errors));
169 * Executes the given git command in every repository
174 public function cmd_git($cmd, $arg) {
175 $repos = $this->findRepos();
177 $shell = array_merge(array('git', $cmd), $arg);
178 $shell = array_map('escapeshellarg', $shell);
179 $shell = join(' ', $shell);
181 foreach($repos as $repo) {
183 $this->error("Could not change into $repo");
187 $this->info("executing $shell in $repo");
189 system($shell, $ret);
192 $this->success("git succeeded in $repo");
194 $this->error("git failed in $repo");
200 * Simply lists the repositories
202 public function cmd_repos() {
203 $repos = $this->findRepos();
204 foreach($repos as $repo) {
210 * Install extension from the given download URL
215 private function downloadExtension($ext) {
216 /** @var helper_plugin_extension_extension $plugin */
217 $plugin = plugin_load('helper', 'extension_extension');
218 if(!$ext) die("extension plugin not available, can't continue");
220 $plugin->setExtension($ext);
222 $url = $plugin->getDownloadURL();
224 $this->error("no download URL for $ext");
230 $this->info("installing $ext via download from $url");
231 $ok = $plugin->installFromURL($url);
232 } catch(Exception
$e) {
233 $this->error($e->getMessage());
237 $this->success("installed $ext via download");
240 $this->success("failed to install $ext via download");
246 * Clones the extension from the given repository
249 * @param string $repo
252 private function cloneExtension($ext, $repo) {
253 if(substr($ext, 0, 9) == 'template:') {
254 $target = fullpath(tpl_incdir() . '../' . substr($ext, 9));
256 $target = DOKU_PLUGIN
. $ext;
259 $this->info("cloning $ext from $repo to $target");
261 system("git clone $repo $target", $ret);
263 $this->success("cloning of $ext succeeded");
266 $this->error("cloning of $ext failed");
272 * Returns all git repositories in this DokuWiki install
274 * Looks in root, template and plugin directories only.
278 private function findRepos() {
279 $this->info('Looking for .git directories');
281 glob(DOKU_INC
. '.git', GLOB_ONLYDIR
),
282 glob(DOKU_PLUGIN
. '*/.git', GLOB_ONLYDIR
),
283 glob(fullpath(tpl_incdir() . '../') . '/*/.git', GLOB_ONLYDIR
)
287 $this->error('Found no .git directories');
289 $this->success('Found ' . count($data) . ' .git directories');
291 $data = array_map('fullpath', array_map('dirname', $data));
296 * Returns the repository for the given extension
299 * @return false|string
301 private function getSourceRepo($extension) {
302 /** @var helper_plugin_extension_extension $ext */
303 $ext = plugin_load('helper', 'extension_extension');
304 if(!$ext) die("extension plugin not available, can't continue");
306 $ext->setExtension($extension);
308 $repourl = $ext->getSourcerepoURL();
309 if(!$repourl) return false;
311 // match github repos
312 if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
315 return 'https://github.com/' . $user . '/' . $repo . '.git';
318 // match gitorious repos
319 if(preg_match('/gitorious.org\/([^\/]+)\/([^\/]+)?/i', $repourl, $m)) {
322 if(!$repo) $repo = $user;
324 return 'https://git.gitorious.org/' . $user . '/' . $repo . '.git';
327 // match bitbucket repos - most people seem to use mercurial there though
328 if(preg_match('/bitbucket\.org\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
331 return 'https://bitbucket.org/' . $user . '/' . $repo . '.git';
339 $cli = new GitToolCLI();