From 25b7d8e0878e236e592434c10b3eb76490126579 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 9 Oct 2016 19:29:11 +0200 Subject: [PATCH] sourcedir: guess basic package info --- src/pharext/PackageInfo.php | 67 +++++++++++++++++++++++++++++++++ src/pharext/SourceDir/Basic.php | 10 ++++- src/pharext/SourceDir/Git.php | 7 +++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/pharext/PackageInfo.php diff --git a/src/pharext/PackageInfo.php b/src/pharext/PackageInfo.php new file mode 100644 index 0000000..3e2a78d --- /dev/null +++ b/src/pharext/PackageInfo.php @@ -0,0 +1,67 @@ +findPackageName($path, $header))) { + return []; + } + if (!$release = $this->findPackageReleaseVersion($path, $header, strtoupper($name))) { + return []; + } + } catch (Exception $e) { + return []; + } + + return compact("name", "release"); + } + + private function findPackageName($path, &$header = null) { + $grep_res = (new ExecCmd("grep"))->run( + array_merge( + ["-HEo", "phpext_[^ ]+_ptr"], + explode("\n", (new ExecCmd("find"))->run([ + $path, "-type", "f", "-name", "php_*.h" + ])->getOutput()) + ) + )->getOutput(); + if (!list($header, $phpext_ptr) = explode(":", $grep_res)) { + return []; + } + if (!$name = substr($phpext_ptr, 7, -4)) { + return []; + } + return $name; + + } + + private function findPackageReleaseVersion($path, $header, $uname) { + $cpp_tmp = new Tempfile("cpp"); + $cpp_hnd = $cpp_tmp->getStream(); + fprintf($cpp_hnd, "#include \"%s\"\n", $header); + fprintf($cpp_hnd, "#if defined(PHP_PECL_%s_VERSION)\n", $uname); + fprintf($cpp_hnd, "PHP_PECL_%s_VERSION\n", $uname); + fprintf($cpp_hnd, "#elif defined(PHP_%s_VERSION)\n", $uname); + fprintf($cpp_hnd, "PHP_%s_VERSION\n", $uname); + fprintf($cpp_hnd, "#elif defined(%s_VERSION)\n", $uname); + fprintf($cpp_hnd, "%s_VERSION\n", $uname); + fprintf($cpp_hnd, "#endif\n"); + fflush($cpp_hnd); + $php_inc = (new ExecCmd((PHP_BINARY ?? "php")."-config"))->run([ + "--includes" + ])->getOutput(); + $ext_inc = (new ExecCmd("find"))->run([ + $path, "-not", "-path", "*/.*", "-type", "d", "-printf", "-I'%p' " + ])->getOutput(); + $cpp_res = (new ExecCmd("cpp $php_inc $ext_inc"))->run([ + "-E", $cpp_tmp + ])->getOutput(); + return trim(substr($cpp_res, strrpos($cpp_res, "\n")), "\"\n"); + } +} diff --git a/src/pharext/SourceDir/Basic.php b/src/pharext/SourceDir/Basic.php index f960236..13891d0 100644 --- a/src/pharext/SourceDir/Basic.php +++ b/src/pharext/SourceDir/Basic.php @@ -4,6 +4,7 @@ namespace pharext\SourceDir; use pharext\Cli\Args; use pharext\License; +use pharext\PackageInfo; use pharext\SourceDir; use FilesystemIterator; @@ -16,6 +17,7 @@ use RecursiveIteratorIterator; class Basic implements IteratorAggregate, SourceDir { use License; + use PackageInfo; private $path; @@ -26,9 +28,13 @@ class Basic implements IteratorAggregate, SourceDir public function getBaseDir() { return $this->path; } - + + /** + * @inheritdoc + * @return array + */ public function getPackageInfo() { - return []; + return $this->findPackageInfo($this->getBaseDir()); } public function getLicense() { diff --git a/src/pharext/SourceDir/Git.php b/src/pharext/SourceDir/Git.php index 62dc24f..9c51c7d 100644 --- a/src/pharext/SourceDir/Git.php +++ b/src/pharext/SourceDir/Git.php @@ -3,8 +3,12 @@ namespace pharext\SourceDir; use pharext\Cli\Args; +use pharext\Exception; +use pharext\ExecCmd; use pharext\License; +use pharext\PackageInfo; use pharext\SourceDir; +use pharext\Tempfile; /** * Extension source directory which is a git repo @@ -12,6 +16,7 @@ use pharext\SourceDir; class Git implements \IteratorAggregate, SourceDir { use License; + use PackageInfo; /** * Base directory @@ -40,7 +45,7 @@ class Git implements \IteratorAggregate, SourceDir * @return array */ public function getPackageInfo() { - return []; + return $this->findPackageInfo($this->getBaseDir()); } /** -- 2.30.2