From 4e1839950dd39fd793346e724e4157bce8a169bf Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 5 Mar 2015 15:27:09 +0100 Subject: [PATCH] refactor some commonly used code into a trait --- Makefile | 9 +++- bin/pharext | Bin 30606 -> 31334 bytes src/pharext/CliArgs.php | 52 ++++----------------- src/pharext/CliCommand.php | 66 +++++++++++++++++++++++++++ src/pharext/Installer.php | 24 ++++++---- src/pharext/Packager.php | 34 ++++++++------ src/pharext/Version.php | 5 ++ tests/src/pharext/CliArgsTest.php | 23 +--------- tests/src/pharext/CliCommandTest.php | 56 +++++++++++++++++++++++ 9 files changed, 177 insertions(+), 92 deletions(-) create mode 100644 src/pharext/CliCommand.php create mode 100644 src/pharext/Version.php create mode 100644 tests/src/pharext/CliCommandTest.php 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 1d0ed19cfaa22712c9e73d3f8071c7730db041ed..222569dcaec5e47649e0d94fb6c282067ef8d059 100755 GIT binary patch delta 1213 zcmcIkO=uHQ5Z;v5X4k5B%Mr0a=Tujs=Ug95=UfVHzG= z-Bptq=B>?e*RrzyaeWDeS(|FU23M`9e$|>%klFP+UhRbEWt1s1$K9BH6NPP77eg7a zZm=H0M3V~&8>yWR%bGF!v}67uO=|?Rt%0T5Y+hy#+S&{;R96ExI~!pv9IP%7ISc*a zXdR`d%6XjbRdEUyGEK&G*uTu!((`_~kKmI_Xc+E#mie++XGCdFNkdMJx~1;CoI&oT^U!t?ZDw@XiojfSW9=`W zkDY@{F)vKVp1?%h#qjtF9!$j#R5HKET@@d7w?Th*Xk8g)*RDiYPk$uQ)#*V&WJfU7 zJq9z5W)Oq5AcoAww!ro(W5TA;Q?S;USHD%mia-;5a-3dS)G!;Yyo|;Jr=wcHglH&$ zhD1NixR0)4$03mhoYX52IN;g3x+K1PNeJGl=VTc7^f~^@WGao*hu%w4u-C9D<7@q+ znq}~r0>M%$NA+S!OF~kI=?E`JtO9iz7(b3+c}uGiXxS~$=@GPwy?--!CcZPlMd^ug zJNecGk>ejha_j#}$!;j=G1GtJ#aDPSS`cXQ8W%EI!I)^9G8^gO7n3tpP&ED5GSW|A zZ|~k8N^}kMCCdxiZuc9r#cL+psl%>3i-$qCYm0^I@(3?M-e z?RO_&{p5qvnqXBcIT#q`-Mr;H`G}Me4IG;jt*n_g&o2AKxcQchFze)SCCkZbN|Kv5Dp|8ljx}YU z{J}JjPennsSV>1grJyK3UDJAUftedieoorig; + } + + /** + * 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"); + } +} -- 2.39.5