mirror of
https://github.com/php-win-ext/php-sdk-binary-tools.git
synced 2026-04-29 12:03:15 +02:00
implement cache, allow passing unknown branch and hope for best
This commit is contained in:
+30
-14
@@ -12,6 +12,7 @@ $lopt = array(
|
||||
"check",
|
||||
"stability:",
|
||||
"arch:",
|
||||
"crt:",
|
||||
"help",
|
||||
"deps:",
|
||||
);
|
||||
@@ -19,6 +20,8 @@ $lopt = array(
|
||||
$cmd = NULL;
|
||||
$stability = NULL;
|
||||
$arch = NULL;
|
||||
$branch = NULL;
|
||||
$crt = NULL;
|
||||
|
||||
try {
|
||||
|
||||
@@ -36,7 +39,7 @@ try {
|
||||
|
||||
case "b":
|
||||
case "branch":
|
||||
Config::setCurrentBranchName($val);
|
||||
$branch = $val;
|
||||
break;
|
||||
|
||||
case "s":
|
||||
@@ -52,7 +55,7 @@ try {
|
||||
if ("x64" != $val && "x86" != $val) {
|
||||
throw new Exception("Unknown arch keyword, either x86 or x64 is accepted");
|
||||
}
|
||||
$arch = $val;
|
||||
Config::setCurrentArchName($val);
|
||||
break;
|
||||
|
||||
case "d":
|
||||
@@ -68,6 +71,10 @@ try {
|
||||
case "update":
|
||||
$cmd = "update";
|
||||
break;
|
||||
|
||||
case "crt":
|
||||
Config::setCurrentCrtName($val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +82,17 @@ try {
|
||||
usage();
|
||||
}
|
||||
|
||||
if (!Config::getDepsLocalPath()) {
|
||||
if (file_exists("../deps")) {
|
||||
Config::setDepsLocalPath(realpath("../deps"));
|
||||
} else {
|
||||
usage(3);
|
||||
}
|
||||
}
|
||||
|
||||
if ($branch) {
|
||||
Config::setCurrentBranchName($branch);
|
||||
}
|
||||
if (!Config::getCurrentBranchName()) {
|
||||
/* Try to figure out the branch. For now it only works if CWD is in php-src. */
|
||||
$fl = "main/php_version.h";
|
||||
@@ -99,24 +117,16 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
if (!Config::getDepsLocalPath()) {
|
||||
if (file_exists("../deps")) {
|
||||
Config::setDepsLocalPath(realpath("../deps"));
|
||||
} else {
|
||||
usage(3);
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL === $arch) {
|
||||
if (NULL === Config::getCurrentArchName()) {
|
||||
/* XXX this might be not true for other compilers! */
|
||||
passthru("where cl.exe >nul", $status);
|
||||
if (!$status) {
|
||||
exec("cl.exe /? 2>&1", $a, $status);
|
||||
if (!$status) {
|
||||
if (preg_match(",x64,", $a[0])) {
|
||||
$arch = "x64";
|
||||
Config::setCurrentArchName("x64");
|
||||
} else {
|
||||
$arch = "x86";
|
||||
Config::setCurrentArchName("x86");
|
||||
}
|
||||
} else {
|
||||
usage(3);
|
||||
@@ -124,6 +134,11 @@ try {
|
||||
} else {
|
||||
usage(3);
|
||||
}
|
||||
$arch = Config::getCurrentArchName();
|
||||
}
|
||||
|
||||
if (NULL === Config::getCurrentCrtName()) {
|
||||
Config::setCurrentCrtName(Config::getCurrentBranchData()["crt"]);
|
||||
}
|
||||
|
||||
if (NULL === $stability) {
|
||||
@@ -158,7 +173,7 @@ try {
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
//var_dump($e);
|
||||
// var_dump($e);
|
||||
echo "\nError: ", $e->getMessage(), PHP_EOL;
|
||||
exit(3);
|
||||
}
|
||||
@@ -170,6 +185,7 @@ function usage(int $code = -1)
|
||||
echo " -a --arch Architecture, x86 or x64.", PHP_EOL;
|
||||
echo " -b --branch Use dependencies for a specific branch. If omited, CWD is used to guess.", PHP_EOL;
|
||||
echo " -c --check Check for dependency updates.", PHP_EOL;
|
||||
echo " --crt Usually not needed! Pass a string like vc11, vc14, etc. and hope for best.", PHP_EOL;
|
||||
echo " -d --deps Path to the dependencies directory. If omited, CWD is used to guess.", PHP_EOL;
|
||||
echo " -h --help Show help message.", PHP_EOL;
|
||||
echo " -s --stability One of stable or staging.", PHP_EOL;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace SDK;
|
||||
|
||||
use SDK\Dependency\Fetcher;
|
||||
use SDK\Dependency\Cache;
|
||||
use SDK\Exception;
|
||||
|
||||
class Config
|
||||
@@ -13,23 +15,12 @@ class Config
|
||||
|
||||
/* protected static $sdkNugetFeedUrl = "http://127.0.0.1/sdk/nuget"; */
|
||||
|
||||
protected static $supportedBranches = array (
|
||||
'master' => array(
|
||||
"crt" => "vc14",
|
||||
),
|
||||
'7.1' => array(
|
||||
"crt" => "vc14",
|
||||
),
|
||||
'7.0' => array(
|
||||
"crt" => "vc14",
|
||||
),
|
||||
/*'5.6' => array(
|
||||
"crt" => "vc11",
|
||||
),*/
|
||||
);
|
||||
protected static $knownBranches = array ();
|
||||
|
||||
/* Helper props and methods. */
|
||||
protected static $currentBranchName = NULL;
|
||||
protected static $currentArchName = NULL;
|
||||
protected static $currentCrtName = NULL;
|
||||
protected static $depsLocalPath = NULL;
|
||||
|
||||
public static function getDepsHost() : string
|
||||
@@ -45,15 +36,54 @@ class Config
|
||||
return self::$depsBaseUri;
|
||||
}
|
||||
|
||||
public static function getSupportedBranches() : array
|
||||
public static function setCurrentArchName(string $arch)
|
||||
{
|
||||
return self::$supportedBranches;
|
||||
self::$currentArchName = $arch;
|
||||
}
|
||||
|
||||
public static function getCurrentArchName()
|
||||
{
|
||||
return self::$currentArchName;
|
||||
}
|
||||
|
||||
public static function setCurrentCrtName(string $crt)
|
||||
{
|
||||
self::$currentCrtName = $crt;
|
||||
}
|
||||
|
||||
public static function getCurrentCrtName()
|
||||
{
|
||||
return self::$currentCrtName;
|
||||
}
|
||||
|
||||
public static function getKnownBranches() : array
|
||||
{
|
||||
if (empty(self::$knownBranches)) {
|
||||
$cache_file = "supported_branches.txt";
|
||||
$cache = new Cache(self::getDepsLocalPath());
|
||||
$fetcher = new Fetcher(self::$depsHost, self::$depsPort);
|
||||
|
||||
$tmp = $fetcher->getByUri(self::$depsBaseUri . "/series/supported_branches.txt");
|
||||
if (false !== $tmp) {
|
||||
$cache->cachecontent($cache_file, $tmp, true);
|
||||
} else {
|
||||
$data = $cache->getCachedContent($cache_file, true);
|
||||
}
|
||||
|
||||
$data = json_decode($tmp, true);
|
||||
if (!is_array($data)) {
|
||||
throw new Exception("Failed to fetch supported branches");
|
||||
}
|
||||
self::$knownBranches = $data;
|
||||
}
|
||||
|
||||
return self::$knownBranches;
|
||||
}
|
||||
|
||||
public static function setCurrentBranchName(string $name)
|
||||
{
|
||||
if (!array_key_exists($name, self::$supportedBranches)) {
|
||||
throw new Exception("Unsupported branch '$name'");
|
||||
if (!array_key_exists($name, self::getKnownBranches())) {
|
||||
// throw new Exception("Unsupported branch '$name'");
|
||||
}
|
||||
|
||||
self::$currentBranchName = $name;
|
||||
@@ -66,11 +96,20 @@ class Config
|
||||
|
||||
public static function getCurrentBranchData() : array
|
||||
{
|
||||
if (isset(self::$supportedBranches[self::$currentBranchName])) {
|
||||
return self::$supportedBranches[self::$currentBranchName];
|
||||
if (array_key_exists(self::$currentBranchName, self::getKnownBranches())) {
|
||||
return self::getKnownBranches()[self::$currentBranchName];
|
||||
}
|
||||
|
||||
throw new Exception("Unknown branch " . self::$currentBranchName);
|
||||
$arch = Config::getCurrentArchName();
|
||||
$crt = Config::getCurrentCrtName();
|
||||
if (NULL !== $arch && NULL !== $crt) {
|
||||
$ret = array();
|
||||
$ret["crt"] = $crt;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
throw new Exception("Not enough data to handle branch '" . self::$currentBranchName . "'");
|
||||
}
|
||||
|
||||
public static function getSdkNugetFeedUrl() : string
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace SDK\Dependency;
|
||||
|
||||
use SDK\Config;
|
||||
use SDK\Exception;
|
||||
|
||||
class Cache
|
||||
{
|
||||
protected $id;
|
||||
protected $hash;
|
||||
|
||||
public function __construct(string $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->hash = md5($id);
|
||||
/* XXX pass as arg, fine for now. */
|
||||
}
|
||||
|
||||
protected function getCacheablePath(string $path, bool $relative = false) : string
|
||||
{
|
||||
if ($relative) {
|
||||
$dir = Config::getCacheDir();
|
||||
$name = $path;
|
||||
} else {
|
||||
$dir = dirname($path);
|
||||
$name = basename($path);
|
||||
}
|
||||
|
||||
return $dir . DIRECTORY_SEPARATOR . $this->hash . "." . $name;
|
||||
}
|
||||
|
||||
public function fileIsCached(string $path, bool $relative = false) : bool
|
||||
{
|
||||
return file_exists($this->getCacheablePath($path, $relative));
|
||||
}
|
||||
|
||||
public function cachedContentDiffers(string $path, string $content, bool $relative = false) : bool
|
||||
{
|
||||
$p = $this->getCacheablePath($path, $relative);
|
||||
|
||||
if (!file_exsits($p)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$old_sum = md5_file($p);
|
||||
$new_sum = md5($content);
|
||||
|
||||
return $old_sum != $new_sum;
|
||||
}
|
||||
|
||||
public function cacheContent(string $path, string $content, bool $relative = false)
|
||||
{
|
||||
$p = $this->getCacheablePath($path, $relative);
|
||||
|
||||
if (strlen($content) !== file_put_contents($p, $content)) {
|
||||
throw new Exception("Couldn't cache '$p'");
|
||||
}
|
||||
}
|
||||
|
||||
public function getCachedContent(string $path, bool $relative = false)
|
||||
{
|
||||
$p = $this->getCacheablePath($path, $relative);
|
||||
|
||||
if ($this->isFileCached($p)) {
|
||||
return file_get_contents($p);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class Fetcher
|
||||
protected $series;
|
||||
|
||||
|
||||
public function __construct(string $stability, string $arch, string $host, int $port, Series $series = NULL)
|
||||
public function __construct(string $host, int $port, string $arch = NULL, string $stability = NULL, Series $series = NULL)
|
||||
{
|
||||
$this->stability = $stability;
|
||||
$this->arch = $arch;
|
||||
@@ -27,7 +27,7 @@ class Fetcher
|
||||
return $this->series;
|
||||
}
|
||||
|
||||
public function setSeries(Series $fetcher)
|
||||
public function setSeries(Series $series)
|
||||
{
|
||||
$this->series = $series;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ class Manager
|
||||
|
||||
$host = Config::getDepsHost();
|
||||
$port = Config::getDepsPort();
|
||||
$fetcher = new Fetcher($this->stability, $this->arch, $host, $port, NULL);
|
||||
$fetcher = new Fetcher($host, $port, $this->arch, $this->stability);
|
||||
$series = new Series($this->stability, $this->arch, NULL);
|
||||
$fetcher->setSeries($series);
|
||||
$series->setFetcher($fetcher);
|
||||
|
||||
Reference in New Issue
Block a user