From: Michael Wallner Date: Fri, 15 May 2015 10:52:54 +0000 (+0200) Subject: license helper X-Git-Tag: v4.0.0~5 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=e44f0adc7b18845a2cd3dc63fdab5d9bcdc10f76;p=pharext%2Fpharext license helper --- diff --git a/bin/pharext b/bin/pharext index e9c765d..8f90155 100755 Binary files a/bin/pharext and b/bin/pharext differ diff --git a/src/pharext/License.php b/src/pharext/License.php new file mode 100644 index 0000000..0f339fd --- /dev/null +++ b/src/pharext/License.php @@ -0,0 +1,51 @@ +mergeLicensePattern($name, strtolower($name)); + } + $exts = []; + foreach (["t{,e}xt", "rst", "asc{,i,ii}", "m{,ark}d{,own}", "htm{,l}"] as $ext) { + $exts[] = $this->mergeLicensePattern(strtoupper($ext), $ext); + } + + $pattern = "{". implode(",", $names) ."}{,.{". implode(",", $exts) ."}}"; + + if (($glob = glob("$dir/$pattern", GLOB_BRACE))) { + return current($glob); + } + } + + private function mergeLicensePattern($upper, $lower) { + $pattern = ""; + $length = strlen($upper); + for ($i = 0; $i < $length; ++$i) { + if ($lower{$i} === $upper{$i}) { + $pattern .= $upper{$i}; + } else { + $pattern .= "[" . $upper{$i} . $lower{$i} . "]"; + } + } + return $pattern; + } + + public function readLicense($file) { + $text = file_get_contents($file); + switch (strtolower(pathinfo($file, PATHINFO_EXTENSION))) { + case "htm": + case "html": + $text = strip_tags($text); + break; + } + return $text; + } +} diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index cd3b4d6..b54d284 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -243,16 +243,10 @@ class Packager implements Command */ private function createPackage() { try { - if (($glob = glob($this->source->getBaseDir()."/LICENSE*"))) { - $license = file_get_contents(current($glob)); - } else { - $this->warn("Could not find any LICENSE.* files!\n"); - $license = "UNKNOWN\n"; - } $meta = array_merge(Metadata::all(), [ "name" => $this->args->name, "release" => $this->args->release, - "license" => $license, + "license" => $this->source->getLicense(), "stub" => "pharext_installer.php", "type" => $this->args->zend ? "zend_extension" : "extension", ]); diff --git a/src/pharext/SourceDir.php b/src/pharext/SourceDir.php index 8620668..c7068ba 100644 --- a/src/pharext/SourceDir.php +++ b/src/pharext/SourceDir.php @@ -19,6 +19,12 @@ interface SourceDir extends \Traversable */ public function getPackageInfo(); + /** + * Retrieve the full text license + * @return string + */ + public function getLicense(); + /** * Provide installer command line args * @return array|Traversable diff --git a/src/pharext/SourceDir/Basic.php b/src/pharext/SourceDir/Basic.php index 414c201..fa180fe 100644 --- a/src/pharext/SourceDir/Basic.php +++ b/src/pharext/SourceDir/Basic.php @@ -3,6 +3,7 @@ namespace pharext\SourceDir; use pharext\Cli\Args; +use pharext\License; use pharext\SourceDir; use FilesystemIterator; @@ -14,6 +15,8 @@ use RecursiveIteratorIterator; class Basic implements IteratorAggregate, SourceDir { + use License; + private $path; public function __construct($path) { @@ -28,6 +31,13 @@ class Basic implements IteratorAggregate, SourceDir return []; } + public function getLicense() { + if (($file = $this->findLicense($this->getBaseDir()))) { + return $this->readLicense($file); + } + return "UNKNOWN"; + } + public function getArgs() { return []; } diff --git a/src/pharext/SourceDir/Git.php b/src/pharext/SourceDir/Git.php index e17a305..62dc24f 100644 --- a/src/pharext/SourceDir/Git.php +++ b/src/pharext/SourceDir/Git.php @@ -2,8 +2,8 @@ namespace pharext\SourceDir; -use pharext\Command; use pharext\Cli\Args; +use pharext\License; use pharext\SourceDir; /** @@ -11,6 +11,8 @@ use pharext\SourceDir; */ class Git implements \IteratorAggregate, SourceDir { + use License; + /** * Base directory * @var string @@ -41,6 +43,17 @@ class Git implements \IteratorAggregate, SourceDir return []; } + /** + * @inheritdoc + * @return string + */ + public function getLicense() { + if (($file = $this->findLicense($this->getBaseDir()))) { + return $this->readLicense($file); + } + return "UNKNOWN"; + } + /** * @inheritdoc * @return array diff --git a/src/pharext/SourceDir/Pecl.php b/src/pharext/SourceDir/Pecl.php index 351eaa2..277e122 100644 --- a/src/pharext/SourceDir/Pecl.php +++ b/src/pharext/SourceDir/Pecl.php @@ -5,13 +5,15 @@ namespace pharext\SourceDir; use pharext\Cli\Args; use pharext\Exception; use pharext\SourceDir; -use pharext\Tempfile; +use pharext\License; /** * A PECL extension source directory containing a v2 package.xml */ class Pecl implements \IteratorAggregate, SourceDir { + use License; + /** * The package.xml * @var SimpleXmlElement @@ -73,6 +75,25 @@ class Pecl implements \IteratorAggregate, SourceDir } } + /** + * @inheritdoc + * @return string + */ + public function getLicense() { + if (($license = $this->sxe->xpath("/pecl:package/pecl:license"))) { + if (($file = $this->findLicense($this->getBaseDir(), $license[0]["filesource"]))) { + return $this->readLicense($file); + } + } + if (($file = $this->findLicense($this->getBaseDir()))) { + return $this->readLicense($file); + } + if ($license) { + return $license[0] ." ". $license[0]["uri"]; + } + return "UNKNOWN"; + } + /** * @inheritdoc * @see \pharext\SourceDir::getArgs()