From: Michael Wallner Date: Thu, 5 Mar 2015 14:27:09 +0000 (+0100) Subject: refactor some commonly used code into a trait X-Git-Tag: v1.1.0~12 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=4e1839950dd39fd793346e724e4157bce8a169bf;p=pharext%2Fpharext refactor some commonly used code into a trait --- diff --git a/Makefile b/Makefile index 3f00b50..cf19520 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,16 @@ all: bin/pharext bin/pharext: src/* src/pharext/* + @for file in $?; do php -l $$file | sed -ne '/^No syntax errors/!p' && exit $${PIPESTATUS[0]}; done + phpunit tests php -d phar.readonly=0 build/create-phar.php chmod +x $@ +test: + phpunit tests + clean: rm bin/pharext* -.PHONY: all clean - +.PHONY: all clean test +.SUFFIXES: .php \ No newline at end of file diff --git a/bin/pharext b/bin/pharext index 1d0ed19..222569d 100755 Binary files a/bin/pharext and b/bin/pharext differ diff --git a/src/pharext/CliArgs.php b/src/pharext/CliArgs.php index f720b3c..1f07e44 100644 --- a/src/pharext/CliArgs.php +++ b/src/pharext/CliArgs.php @@ -89,10 +89,18 @@ class CliArgs implements \ArrayAccess } /** - * Get compiled spec + * Get original spec * @return array */ public function getSpec() { + return $this->orig; + } + + /** + * Get compiled spec + * @return array + */ + public function getCompiledSpec() { return $this->spec; } @@ -157,48 +165,6 @@ class CliArgs implements \ArrayAccess } } - /** - * Output command line help message - * @param string $prog - */ - public function help($prog) { - printf("\nUsage:\n\n $ %s", $prog); - $flags = []; - $required = []; - $optional = []; - foreach ($this->orig as $spec) { - if ($spec[3] & self::REQARG) { - if ($spec[3] & self::REQUIRED) { - $required[] = $spec; - } else { - $optional[] = $spec; - } - } else { - $flags[] = $spec; - } - } - - if ($flags) { - printf(" [-%s]", implode("|-", array_column($flags, 0))); - } - foreach ($required as $req) { - printf(" -%s ", $req[0]); - } - if ($optional) { - printf(" [-%s ]", implode("|-", array_column($optional, 0))); - } - printf("\n\n"); - foreach ($this->orig as $spec) { - printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & self::REQARG) ? " " : (($spec[3] & self::OPTARG) ? "[]" : " ")); - printf("%s%s %s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & self::REQUIRED) ? "(REQUIRED)" : ""); - if (isset($spec[4])) { - printf(" [%s]", $spec[4]); - } - printf("\n"); - } - printf("\n"); - } - /** * Retreive the default argument of an option * @param string $o diff --git a/src/pharext/CliCommand.php b/src/pharext/CliCommand.php new file mode 100644 index 0000000..1299a0e --- /dev/null +++ b/src/pharext/CliCommand.php @@ -0,0 +1,66 @@ +\n", VERSION); + } + + /** + * Output command line help message + * @param string $prog + */ + public function help($prog) { + printf("\nUsage:\n\n \$ %s", $prog); + + $flags = []; + $required = []; + $optional = []; + foreach ($this->args->getSpec() as $spec) { + if ($spec[3] & CliArgs::REQARG) { + if ($spec[3] & CliArgs::REQUIRED) { + $required[] = $spec; + } else { + $optional[] = $spec; + } + } else { + $flags[] = $spec; + } + } + + if ($flags) { + printf(" [-%s]", implode("", array_column($flags, 0))); + } + foreach ($required as $req) { + printf(" -%s ", $req[0]); + } + if ($optional) { + printf(" [-%s ]", implode("|-", array_column($optional, 0))); + } + printf("\n\n"); + foreach ($this->args->getSpec() as $spec) { + printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & CliArgs::REQARG) ? " " : (($spec[3] & CliArgs::OPTARG) ? "[]" : " ")); + printf("%s%s%s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & CliArgs::REQUIRED) ? " (REQUIRED)" : ""); + if (isset($spec[4])) { + printf(" [%s]", $spec[4]); + } + printf("\n"); + } + printf("\n"); + } + + +} \ No newline at end of file diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index cd15ba0..f6c7807 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -9,11 +9,7 @@ use Phar; */ class Installer implements Command { - /** - * Command line arguments - * @var pharext\CliArgs - */ - private $args; + use CliCommand; /** * Create the command @@ -44,23 +40,31 @@ class Installer implements Command * @see \pharext\Command::run() */ public function run($argc, array $argv) { + $errs = []; $prog = array_shift($argv); foreach ($this->args->parse(--$argc, $argv) as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } if ($this->args["help"]) { - $this->args->help($prog); + $this->header(); + $this->help($prog); exit; } foreach ($this->args->validate() as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } - if (isset($error)) { + if ($errs) { + if (!$this->args["quiet"]) { + $this->header(); + } + foreach ($errs as $err) { + $this->error("%s\n", $err); + } if (!$this->args["quiet"]) { - $this->args->help($prog); + $this->help($prog); } exit(1); } diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index a848e2d..b6b05e1 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -9,11 +9,7 @@ use Phar; */ class Packager implements Command { - /** - * Command line arguments - * @var pharext\CliArgs - */ - private $args; + use CliCommand; /** * Extension source directory @@ -32,6 +28,10 @@ class Packager implements Command CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["q", "quiet", "Less output", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + ["n", "name", "Extension name", + CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], + ["r", "release", "Extension release version", + CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], ["s", "source", "Extension source directory", CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], ["g", "git", "Use `git ls-files` instead of the standard ignore filter", @@ -41,15 +41,11 @@ class Packager implements Command ["d", "dest", "Destination directory", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, "."], - ["n", "name", "Extension name", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], - ["r", "release", "Extension release version", - CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG], ["z", "gzip", "Create additional PHAR compressed with gzip", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["Z", "bzip", "Create additional PHAR compressed with bzip", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], -]); + ]); } /** @@ -57,13 +53,15 @@ class Packager implements Command * @see \pharext\Command::run() */ public function run($argc, array $argv) { + $errs = []; $prog = array_shift($argv); foreach ($this->args->parse(--$argc, $argv) as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } if ($this->args["help"]) { - $this->args->help($prog); + $this->header(); + $this->help($prog); exit; } @@ -78,12 +76,18 @@ class Packager implements Command } foreach ($this->args->validate() as $error) { - $this->error("%s\n", $error); + $errs[] = $error; } - if (isset($error)) { + if ($errs) { + if (!$this->args["quiet"]) { + $this->header(); + } + foreach ($errs as $err) { + $this->error("%s\n", $err); + } if (!$this->args["quiet"]) { - $this->args->help($prog); + $this->help($prog); } exit(1); } diff --git a/src/pharext/Version.php b/src/pharext/Version.php new file mode 100644 index 0000000..50a2875 --- /dev/null +++ b/src/pharext/Version.php @@ -0,0 +1,5 @@ +assertSame($args->getSpec(), $spec); + $this->assertSame($args->getCompiledSpec(), $spec); } public function testParseNothing() { @@ -127,25 +127,4 @@ class CliArgsTest extends \PHPUnit_Framework_TestCase $this->assertTrue(isset($error)); } - public function testHelp() { - $this->expectOutputString(<<] - - -h|--help Display help - -v|--verbose More output - -q|--quiet Less output - -p|--prefix PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7 - -n|--common-name PHP common program name, e.g. php5 or zts-php [php] - -c|--configure Additional extension configure flags, e.g. -c --with-flag - -s|--sudo [] Installation might need increased privileges [sudo -S %s] - - -EOF - ); - $this->args->help("testprog"); - } - } diff --git a/tests/src/pharext/CliCommandTest.php b/tests/src/pharext/CliCommandTest.php new file mode 100644 index 0000000..a2bf3d9 --- /dev/null +++ b/tests/src/pharext/CliCommandTest.php @@ -0,0 +1,56 @@ +spec = [ + ["h", "help", "Display help", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], + ["v", "verbose", "More output", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + ["q", "quiet", "Less output", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], + ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], + ["n", "common-name", "PHP common program name, e.g. php5 or zts-php", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, + "php"], + ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag", + CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG], + ["s", "sudo", "Installation might need increased privileges", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG, + "sudo -S %s"] + ]; + $this->args = new CliArgs($this->spec); + } + + public function testHelp() { + $this->expectOutputString(<<] + + -h|--help Display help + -v|--verbose More output + -q|--quiet Less output + -p|--prefix PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7 + -n|--common-name PHP common program name, e.g. php5 or zts-php [php] + -c|--configure Additional extension configure flags, e.g. -c --with-flag + -s|--sudo [] Installation might need increased privileges [sudo -S %s] + + +EOF + ); + $this->help("testprog"); + } +}