Skip to content

Commit

Permalink
Merge pull request #2168 from splitbrain/cli
Browse files Browse the repository at this point in the history
New CLI Plugin Type
  • Loading branch information
splitbrain committed Apr 5, 2018
2 parents 5c0b2e6 + 7ba2877 commit 874fc8d
Show file tree
Hide file tree
Showing 33 changed files with 2,315 additions and 421 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -85,4 +85,8 @@ vendor/marcusschwarz/lesserphp/package.sh
vendor/marcusschwarz/lesserphp/lessify*
vendor/marcusschwarz/lesserphp/Makefile
vendor/marcusschwarz/lesserphp/plessc
vendor/splitbrain/php-cli/examples/*
vendor/splitbrain/php-cli/screenshot*
vendor/splitbrain/php-cli/generate-api.sh
vendor/splitbrain/php-cli/apigen.neon

54 changes: 29 additions & 25 deletions bin/dwpage.php
@@ -1,24 +1,28 @@
#!/usr/bin/php
<?php
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC . 'inc/init.php');

/**
* Checkout and commit pages from the command line while maintaining the history
*/
class PageCLI extends DokuCLI {
class PageCLI extends CLI {

protected $force = false;
protected $username = '';

/**
* Register options and arguments on the given $options object
*
* @param DokuCLI_Options $options
* @param Options $options
* @return void
*/
protected function setup(DokuCLI_Options $options) {
protected function setup(Options $options) {
/* global */
$options->registerOption(
'force',
Expand All @@ -32,17 +36,17 @@ protected function setup(DokuCLI_Options $options) {
'username'
);
$options->setHelp(
'Utility to help command line Dokuwiki page editing, allow '.
'Utility to help command line Dokuwiki page editing, allow ' .
'pages to be checked out for editing then committed after changes'
);

/* checkout command */
$options->registerCommand(
'checkout',
'Checks out a file from the repository, using the wiki id and obtaining '.
'a lock for the page. '."\n".
'If a working_file is specified, this is where the page is copied to. '.
'Otherwise defaults to the same as the wiki page in the current '.
'Checks out a file from the repository, using the wiki id and obtaining ' .
'a lock for the page. ' . "\n" .
'If a working_file is specified, this is where the page is copied to. ' .
'Otherwise defaults to the same as the wiki page in the current ' .
'working directory.'
);
$options->registerArgument(
Expand All @@ -61,7 +65,7 @@ protected function setup(DokuCLI_Options $options) {
/* commit command */
$options->registerCommand(
'commit',
'Checks in the working_file into the repository using the specified '.
'Checks in the working_file into the repository using the specified ' .
'wiki id, archiving the previous version.'
);
$options->registerArgument(
Expand Down Expand Up @@ -121,23 +125,24 @@ protected function setup(DokuCLI_Options $options) {
*
* Arguments and options have been parsed when this is run
*
* @param DokuCLI_Options $options
* @param Options $options
* @return void
*/
protected function main(DokuCLI_Options $options) {
$this->force = $options->getOpt('force', false);
protected function main(Options $options) {
$this->force = $options->getOpt('force', false);
$this->username = $options->getOpt('user', $this->getUser());

$command = $options->getCmd();
$args = $options->getArgs();
switch($command) {
case 'checkout':
$wiki_id = array_shift($options->args);
$localfile = array_shift($options->args);
$wiki_id = array_shift($args);
$localfile = array_shift($args);
$this->commandCheckout($wiki_id, $localfile);
break;
case 'commit':
$localfile = array_shift($options->args);
$wiki_id = array_shift($options->args);
$localfile = array_shift($args);
$wiki_id = array_shift($args);
$this->commandCommit(
$localfile,
$wiki_id,
Expand All @@ -146,12 +151,12 @@ protected function main(DokuCLI_Options $options) {
);
break;
case 'lock':
$wiki_id = array_shift($options->args);
$wiki_id = array_shift($args);
$this->obtainLock($wiki_id);
$this->success("$wiki_id locked");
break;
case 'unlock':
$wiki_id = array_shift($options->args);
$wiki_id = array_shift($args);
$this->clearLock($wiki_id);
$this->success("$wiki_id unlocked");
break;
Expand All @@ -177,11 +182,11 @@ protected function commandCheckout($wiki_id, $localfile) {
}

if(empty($localfile)) {
$localfile = getcwd().'/'.utf8_basename($wiki_fn);
$localfile = getcwd() . '/' . utf8_basename($wiki_fn);
}

if(!file_exists(dirname($localfile))) {
$this->fatal("Directory ".dirname($localfile)." does not exist");
$this->fatal("Directory " . dirname($localfile) . " does not exist");
}

if(stristr(realpath(dirname($localfile)), realpath($conf['datadir'])) !== false) {
Expand All @@ -204,7 +209,7 @@ protected function commandCheckout($wiki_id, $localfile) {
* @param string $localfile
* @param string $wiki_id
* @param string $message
* @param bool $minor
* @param bool $minor
*/
protected function commandCommit($localfile, $wiki_id, $message, $minor) {
$wiki_id = cleanID($wiki_id);
Expand Down Expand Up @@ -312,7 +317,6 @@ protected function getUser() {
}
}


// Main
$cli = new PageCLI();
$cli->run();
$cli->run();
76 changes: 40 additions & 36 deletions bin/gittool.php
@@ -1,27 +1,31 @@
#!/usr/bin/php
<?php
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC . 'inc/init.php');

/**
* Easily manage DokuWiki git repositories
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
class GitToolCLI extends DokuCLI {
class GitToolCLI extends CLI {

/**
* Register options and arguments on the given $options object
*
* @param DokuCLI_Options $options
* @param Options $options
* @return void
*/
protected function setup(DokuCLI_Options $options) {
protected function setup(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".
"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"
);

Expand All @@ -33,7 +37,7 @@ protected function setup(DokuCLI_Options $options) {

$options->registerCommand(
'clone',
'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org '.
'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(
Expand All @@ -45,7 +49,7 @@ protected function setup(DokuCLI_Options $options) {

$options->registerCommand(
'install',
'The same as clone, but when no git source repository can be found, the extension is installed via '.
'The same as clone, but when no git source repository can be found, the extension is installed via ' .
'download'
);
$options->registerArgument(
Expand All @@ -62,7 +66,7 @@ protected function setup(DokuCLI_Options $options) {

$options->registerCommand(
'*',
'Any unknown commands are assumed to be arguments to git and will be executed in all repositories '.
'Any unknown commands are assumed to be arguments to git and will be executed in all repositories ' .
'found within this DokuWiki installation'
);
}
Expand All @@ -72,39 +76,40 @@ protected function setup(DokuCLI_Options $options) {
*
* Arguments and options have been parsed when this is run
*
* @param DokuCLI_Options $options
* @param Options $options
* @return void
*/
protected function main(DokuCLI_Options $options) {
protected function main(Options $options) {
$command = $options->getCmd();
if(!$command) $command = array_shift($options->args);
$args = $options->getArgs();
if(!$command) $command = array_shift($args);

switch($command) {
case '':
echo $options->help();
break;
case 'clone':
$this->cmd_clone($options->args);
$this->cmd_clone($args);
break;
case 'install':
$this->cmd_install($options->args);
$this->cmd_install($args);
break;
case 'repo':
case 'repos':
$this->cmd_repos();
break;
default:
$this->cmd_git($command, $options->args);
$this->cmd_git($command, $args);
}
}

/**
* Tries to install the given extensions using git clone
*
* @param array $extensions
* @param array $extensions
*/
public function cmd_clone($extensions) {
$errors = array();
$errors = array();
$succeeded = array();

foreach($extensions as $ext) {
Expand All @@ -123,17 +128,17 @@ public function cmd_clone($extensions) {
}

echo "\n";
if($succeeded) $this->success('successfully cloned the following extensions: '.join(', ', $succeeded));
if($errors) $this->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));
}

/**
* Tries to install the given extensions using git clone with fallback to install
*
* @param array $extensions
* @param array $extensions
*/
public function cmd_install($extensions) {
$errors = array();
$errors = array();
$succeeded = array();

foreach($extensions as $ext) {
Expand All @@ -156,8 +161,8 @@ public function cmd_install($extensions) {
}

echo "\n";
if($succeeded) $this->success('successfully installed the following extensions: '.join(', ', $succeeded));
if($errors) $this->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));
}

/**
Expand All @@ -179,7 +184,6 @@ public function cmd_git($cmd, $arg) {
continue;
}

echo "\n";
$this->info("executing $shell in $repo");
$ret = 0;
system($shell, $ret);
Expand Down Expand Up @@ -247,9 +251,9 @@ private function downloadExtension($ext) {
*/
private function cloneExtension($ext, $repo) {
if(substr($ext, 0, 9) == 'template:') {
$target = fullpath(tpl_incdir().'../'.substr($ext, 9));
$target = fullpath(tpl_incdir() . '../' . substr($ext, 9));
} else {
$target = DOKU_PLUGIN.$ext;
$target = DOKU_PLUGIN . $ext;
}

$this->info("cloning $ext from $repo to $target");
Expand All @@ -274,15 +278,15 @@ private function cloneExtension($ext, $repo) {
private function findRepos() {
$this->info('Looking for .git directories');
$data = array_merge(
glob(DOKU_INC.'.git', GLOB_ONLYDIR),
glob(DOKU_PLUGIN.'*/.git', GLOB_ONLYDIR),
glob(fullpath(tpl_incdir().'../').'/*/.git', GLOB_ONLYDIR)
glob(DOKU_INC . '.git', GLOB_ONLYDIR),
glob(DOKU_PLUGIN . '*/.git', GLOB_ONLYDIR),
glob(fullpath(tpl_incdir() . '../') . '/*/.git', GLOB_ONLYDIR)
);

if(!$data) {
$this->error('Found no .git directories');
} else {
$this->success('Found '.count($data).' .git directories');
$this->success('Found ' . count($data) . ' .git directories');
}
$data = array_map('fullpath', array_map('dirname', $data));
return $data;
Expand All @@ -308,7 +312,7 @@ private function getSourceRepo($extension) {
if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
$user = $m[1];
$repo = $m[2];
return 'https://github.com/'.$user.'/'.$repo.'.git';
return 'https://github.com/' . $user . '/' . $repo . '.git';
}

// match gitorious repos
Expand All @@ -317,14 +321,14 @@ private function getSourceRepo($extension) {
$repo = $m[2];
if(!$repo) $repo = $user;

return 'https://git.gitorious.org/'.$user.'/'.$repo.'.git';
return 'https://git.gitorious.org/' . $user . '/' . $repo . '.git';
}

// match bitbucket repos - most people seem to use mercurial there though
if(preg_match('/bitbucket\.org\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
$user = $m[1];
$repo = $m[2];
return 'https://bitbucket.org/'.$user.'/'.$repo.'.git';
return 'https://bitbucket.org/' . $user . '/' . $repo . '.git';
}

return false;
Expand All @@ -333,4 +337,4 @@ private function getSourceRepo($extension) {

// Main
$cli = new GitToolCLI();
$cli->run();
$cli->run();

0 comments on commit 874fc8d

Please sign in to comment.