From 0a0c6829fea8e4682a067f5d29e1f3a0583eeed9 Mon Sep 17 00:00:00 2001 From: Dan Forsberg Date: Tue, 22 Aug 2017 19:42:25 +0300 Subject: [PATCH] Copy installer.php from gh-pages branch --- wscli-php/installer.php | 639 +++------------------------------------- 1 file changed, 33 insertions(+), 606 deletions(-) diff --git a/wscli-php/installer.php b/wscli-php/installer.php index 5fcec94..a556e82 100644 --- a/wscli-php/installer.php +++ b/wscli-php/installer.php @@ -3,10 +3,6 @@ namespace { - use Herrera\Version\Comparator; - use Herrera\Version\Dumper; - use Herrera\Version\Parser; - $n = PHP_EOL; set_error_handler( @@ -81,48 +77,44 @@ function () { echo "Download$n"; echo "--------$n$n"; + $base_url = 'https://isecurefi.github.io/wscli-php/'; - // Retrieve manifest - echo " - Downloading manifest...$n"; - - $manifest = file_get_contents('https://isecurefi.github.io/wscli-php/manifest.json'); - - echo " - Reading manifest...$n"; - - $manifest = json_decode($manifest); - $current = null; - - foreach ($manifest as $item) { - $item->version = Parser::toVersion($item->version); - - if ($current - && (Comparator::isGreaterThan($item->version, $current->version))) { - $current = $item; - } + echo " - Downloading wscli.phar...$n"; + $res = file_put_contents("wscli.phar", file_get_contents($base_url . "wscli.phar")); + if ($res === FALSE || $res <= 0) { + echo " x Downloading and writing wscli.phar failed.$n"; + exit(1); } - - if (!$item) { - echo " x No application download was found.$n"; + + echo " - Downloading wscli.phar.pubkey..$n"; + $res = file_put_contents("wscli.phar.pubkey", file_get_contents($base_url . "wscli.phar.pubkey")); + if ($res === FALSE || $res <= 0) { + echo " x Downloading and writing wscli.phar.pubkey failed.$n"; + exit(1); } - echo " - Downloading " . $item->name . " v", Dumper::toString($item->version), "...$n"; - - file_put_contents($item->name, file_get_contents($item->url)); - file_put_contents($item->name . ".pubkey", file_get_contents($item->publicKey)); + echo " - Downloading wscli.phar.version..$n"; + $res = file_put_contents("wscli.phar.version", file_get_contents($base_url . "wscli.phar.version")); + if ($res === FALSE || $res <= 0) { + echo " x Downloading and writing wscli.phar.pubkey failed.$n"; + exit(1); + } echo " - Checking file checksum...$n"; - - if ($item->sha1 !== hash("sha1", file_get_contents($item->name))) { - unlink($item->name); - - echo " x The download was corrupted.$n"; + $sha_calculated = trim(hash("sha1", file_get_contents("wscli.phar"))); + $sha_version = trim(file_get_contents("wscli.phar.version")); + if ($sha_calculated !== $sha_version) { + unlink("wscli.phar"); + unlink("wscli.phar.version"); + unlink("wscli.phar.pubkey"); + echo " x The download was corrupted ($sha_calculated != $sha_version).$n"; exit(1); } echo " - Checking if valid Phar...$n"; try { - new Phar($item->name); + new Phar("wscli.phar"); } catch (Exception $e) { echo " x The Phar is not valid.$n$n"; @@ -131,21 +123,13 @@ function () { echo " - Making wscli executable...$n"; - @chmod($item->name, 0755); - - /* - echo " - Copying wscli.phar executable to /usr/local/bin/ ...$n"; - - try { - copy($item->name, "/usr/local/bin/wscli"); - } catch (Exception $e) { - echo " x Installing the binary failed, check permissions.$n$n"; + @chmod("wscli.phar", 0755); - throw $e; - } - */ - - echo "{$n}wscli installed on local directory. You may want to copy it into e.g. /usr/local/bin/.{$n}"; + echo "{$n}"; + echo "NOTE:{$n}"; + echo " wscli.phar installed on local directory. You may want to copy {$n}" + ." wscli.phar and wscli.phar.pubkey into e.g. /usr/local/bin/ as {$n}" + ." wscli and wscli.pubkey. Sudo may be needed.{$n}"; /** * Checks a condition, outputs a message, and exits if failed. @@ -160,7 +144,7 @@ function check($success, $failure, $condition, $exit = true) global $n; if ($condition()) { - echo ' - ', $success, $n; + echo ' - ', $success, $n; } else { echo ' * ', $failure, $n; @@ -170,560 +154,3 @@ function check($success, $failure, $condition, $exit = true) } } } - -namespace Herrera\Version\Exception - -{ - use Exception; - - /** - * Throw if an invalid version string representation is used. - * - * @author Kevin Herrera - */ - class InvalidStringRepresentationException extends VersionException - { - /** - * The invalid string representation. - * - * @var string - */ - private $version; - - /** - * Sets the invalid string representation. - * - * @param string $version The string representation. - */ - public function __construct($version) - { - parent::__construct( - sprintf( - 'The version string representation "%s" is invalid.', - $version - ) - ); - - $this->version = $version; - } - - /** - * Returns the invalid string representation. - * - * @return string The invalid string representation. - */ - public function getVersion() - { - return $this->version; - } - } - - /** - * The base library exception class. - * - * @author Kevin Herrera - */ - class VersionException extends Exception - { - } -} - -namespace Herrera\Version - -{ - use Herrera\Version\Exception\InvalidStringRepresentationException; - - /** - * Compares two Version instances. - * - * @author Kevin Herrera - */ - class Comparator - { - /** - * The version is equal to another. - */ - const EQUAL_TO = 0; - - /** - * The version is greater than another. - */ - const GREATER_THAN = 1; - - /** - * The version is less than another. - */ - const LESS_THAN = -1; - - /** - * Compares one version with another. - * - * @param Version $left The left version to compare. - * @param Version $right The right version to compare. - * - * @return integer Returns Comparator::EQUAL_TO if the two versions are - * equal. If the left version is less than the right - * version, Comparator::LESS_THAN is returned. If the left - * version is greater than the right version, - * Comparator::GREATER_THAN is returned. - */ - public static function compareTo(Version $left, Version $right) - { - switch (true) { - case ($left->getMajor() < $right->getMajor()): - return self::LESS_THAN; - case ($left->getMajor() > $right->getMajor()): - return self::GREATER_THAN; - case ($left->getMinor() > $right->getMinor()): - return self::GREATER_THAN; - case ($left->getMinor() < $right->getMinor()): - return self::LESS_THAN; - case ($left->getPatch() > $right->getPatch()): - return self::GREATER_THAN; - case ($left->getPatch() < $right->getPatch()): - return self::LESS_THAN; - // @codeCoverageIgnoreStart - } - // @codeCoverageIgnoreEnd - - return self::compareIdentifiers( - $left->getPreRelease(), - $right->getPreRelease() - ); - } - - /** - * Checks if the left version is equal to the right. - * - * @param Version $left The left version to compare. - * @param Version $right The right version to compare. - * - * @return boolean TRUE if the left version is equal to the right, FALSE - * if not. - */ - public static function isEqualTo(Version $left, Version $right) - { - return (self::EQUAL_TO === self::compareTo($left, $right)); - } - - /** - * Checks if the left version is greater than the right. - * - * @param Version $left The left version to compare. - * @param Version $right The right version to compare. - * - * @return boolean TRUE if the left version is greater than the right, - * FALSE if not. - */ - public static function isGreaterThan(Version $left, Version $right) - { - return (self::GREATER_THAN === self::compareTo($left, $right)); - } - - /** - * Checks if the left version is less than the right. - * - * @param Version $left The left version to compare. - * @param Version $right The right version to compare. - * - * @return boolean TRUE if the left version is less than the right, - * FALSE if not. - */ - public static function isLessThan(Version $left, Version $right) - { - return (self::LESS_THAN === self::compareTo($left, $right)); - } - - /** - * Compares the identifier components of the left and right versions. - * - * @param array $left The left identifiers. - * @param array $right The right identifiers. - * - * @return integer Returns Comparator::EQUAL_TO if the two identifiers are - * equal. If the left identifiers is less than the right - * identifiers, Comparator::LESS_THAN is returned. If the - * left identifiers is greater than the right identifiers, - * Comparator::GREATER_THAN is returned. - */ - public static function compareIdentifiers(array $left, array $right) - { - if ($left && empty($right)) { - return self::LESS_THAN; - } elseif (empty($left) && $right) { - return self::GREATER_THAN; - } - - $l = $left; - $r = $right; - $x = self::GREATER_THAN; - $y = self::LESS_THAN; - - if (count($l) < count($r)) { - $l = $right; - $r = $left; - $x = self::LESS_THAN; - $y = self::GREATER_THAN; - } - - foreach (array_keys($l) as $i) { - if (!isset($r[$i])) { - return $x; - } - - if ($l[$i] === $r[$i]) { - continue; - } - - if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) { - $l[$i] = intval($l[$i]); - } - - if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) { - $r[$i] = intval($r[$i]); - } - - if ($li && $ri) { - return ($l[$i] > $r[$i]) ? $x : $y; - } elseif (!$li && $ri) { - return $x; - } elseif ($li && !$ri) { - return $y; - } - - return strcmp($l[$i], $r[$i]); - } - - return self::EQUAL_TO; - } - } - - /** - * Dumps the Version instance to a variety of formats. - * - * @author Kevin Herrera - */ - class Dumper - { - /** - * Returns the components of a Version instance. - * - * @param Version $version A version. - * - * @return array The components. - */ - public static function toComponents(Version $version) - { - return array( - Parser::MAJOR => $version->getMajor(), - Parser::MINOR => $version->getMinor(), - Parser::PATCH => $version->getPatch(), - Parser::PRE_RELEASE => $version->getPreRelease(), - Parser::BUILD => $version->getBuild() - ); - } - - /** - * Returns the string representation of a Version instance. - * - * @param Version $version A version. - * - * @return string The string representation. - */ - public static function toString(Version $version) - { - return sprintf( - '%d.%d.%d%s%s', - $version->getMajor(), - $version->getMinor(), - $version->getPatch(), - $version->getPreRelease() - ? '-' . join('.', $version->getPreRelease()) - : '', - $version->getBuild() - ? '+' . join('.', $version->getBuild()) - : '' - ); - } - } - - /** - * Parses the string representation of a version number. - * - * @author Kevin Herrera - */ - class Parser - { - /** - * The build metadata component. - */ - const BUILD = 'build'; - - /** - * The major version number component. - */ - const MAJOR = 'major'; - - /** - * The minor version number component. - */ - const MINOR = 'minor'; - - /** - * The patch version number component. - */ - const PATCH = 'patch'; - - /** - * The pre-release version number component. - */ - const PRE_RELEASE = 'pre'; - - /** - * Returns a Version builder for the string representation. - * - * @param string $version The string representation. - * - * @return Builder A Version builder. - */ - public static function toBuilder($version) - { - return Builder::create()->importComponents( - self::toComponents($version) - ); - } - - /** - * Returns the components of the string representation. - * - * @param string $version The string representation. - * - * @return array The components of the version. - * - * @throws InvalidStringRepresentationException If the string representation - * is invalid. - */ - public static function toComponents($version) - { - if (!Validator::isVersion($version)) { - throw new InvalidStringRepresentationException($version); - } - - if (false !== strpos($version, '+')) { - list($version, $build) = explode('+', $version); - - $build = explode('.', $build); - } - - if (false !== strpos($version, '-')) { - list($version, $pre) = explode('-', $version); - - $pre = explode('.', $pre); - } - - list( - $major, - $minor, - $patch - ) = explode('.', $version); - - return array( - self::MAJOR => intval($major), - self::MINOR => intval($minor), - self::PATCH => intval($patch), - self::PRE_RELEASE => isset($pre) ? $pre : array(), - self::BUILD => isset($build) ? $build : array(), - ); - } - - /** - * Returns a Version instance for the string representation. - * - * @param string $version The string representation. - * - * @return Version A Version instance. - */ - public static function toVersion($version) - { - $components = self::toComponents($version); - - return new Version( - $components['major'], - $components['minor'], - $components['patch'], - $components['pre'], - $components['build'] - ); - } - } - - /** - * Validates version information. - * - * @author Kevin Herrera - */ - class Validator - { - /** - * The regular expression for a valid identifier. - */ - const IDENTIFIER_REGEX = '/^[0-9A-Za-z\-]+$/'; - - /** - * The regular expression for a valid semantic version number. - */ - const VERSION_REGEX = '/^\d+\.\d+\.\d+(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/'; - - /** - * Checks if a identifier is valid. - * - * @param string $identifier A identifier. - * - * @return boolean TRUE if the identifier is valid, FALSE If not. - */ - public static function isIdentifier($identifier) - { - return (true == preg_match(self::IDENTIFIER_REGEX, $identifier)); - } - - /** - * Checks if a number is a valid version number. - * - * @param integer $number A number. - * - * @return boolean TRUE if the number is valid, FALSE If not. - */ - public static function isNumber($number) - { - return (true == preg_match('/^\d+$/', $number)); - } - - /** - * Checks if the string representation of a version number is valid. - * - * @param string $version The string representation. - * - * @return boolean TRUE if the string representation is valid, FALSE if not. - */ - public static function isVersion($version) - { - return (true == preg_match(self::VERSION_REGEX, $version)); - } - } - - /** - * Stores and returns the version information. - * - * @author Kevin Herrera - */ - class Version - { - /** - * The build metadata identifiers. - * - * @var array - */ - protected $build; - - /** - * The major version number. - * - * @var integer - */ - protected $major; - - /** - * The minor version number. - * - * @var integer - */ - protected $minor; - - /** - * The patch version number. - * - * @var integer - */ - protected $patch; - - /** - * The pre-release version identifiers. - * - * @var array - */ - protected $preRelease; - - /** - * Sets the version information. - * - * @param integer $major The major version number. - * @param integer $minor The minor version number. - * @param integer $patch The patch version number. - * @param array $pre The pre-release version identifiers. - * @param array $build The build metadata identifiers. - */ - public function __construct( - $major = 0, - $minor = 0, - $patch = 0, - array $pre = array(), - array $build = array() - ) { - $this->build = $build; - $this->major = $major; - $this->minor = $minor; - $this->patch = $patch; - $this->preRelease = $pre; - } - - /** - * Returns the build metadata identifiers. - * - * @return array The build metadata identifiers. - */ - public function getBuild() - { - return $this->build; - } - - /** - * Returns the major version number. - * - * @return integer The major version number. - */ - public function getMajor() - { - return $this->major; - } - - /** - * Returns the minor version number. - * - * @return integer The minor version number. - */ - public function getMinor() - { - return $this->minor; - } - - /** - * Returns the patch version number. - * - * @return integer The patch version number. - */ - public function getPatch() - { - return $this->patch; - } - - /** - * Returns the pre-release version identifiers. - * - * @return array The pre-release version identifiers. - */ - public function getPreRelease() - { - return $this->preRelease; - } - } -}