Improve gen_stub.php - drop --legacy option, generate file is exists - add --help option - add debug "Saved ..." message

This commit is contained in:
Remi Collet 2020-10-05 15:20:16 +02:00 committed by Remi Collet
parent 6ea870f5fb
commit dba6715598

View file

@ -31,8 +31,9 @@ function processStubFile(string $stubFile, Context $context) {
throw new Exception("File $stubFile does not exist"); throw new Exception("File $stubFile does not exist");
} }
$arginfoFile = str_replace('.stub.php', '', $stubFile) $arginfoFile = str_replace('.stub.php', '_arginfo.h', $stubFile);
. ($context->legacy ? '_legacy' : '') . '_arginfo.h'; $legacyFile = str_replace('.stub.php', '_legacy_arginfo.h', $stubFile);
$stubCode = file_get_contents($stubFile); $stubCode = file_get_contents($stubFile);
$stubHash = computeStubHash($stubCode); $stubHash = computeStubHash($stubCode);
$oldStubHash = extractStubHash($arginfoFile); $oldStubHash = extractStubHash($arginfoFile);
@ -43,14 +44,20 @@ function processStubFile(string $stubFile, Context $context) {
initPhpParser(); initPhpParser();
$fileInfo = parseStubFile($stubCode); $fileInfo = parseStubFile($stubCode);
if ($context->legacy) { $arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (file_put_contents($arginfoFile, $arginfoCode)) {
echo "Saved $arginfoFile\n";
}
if (file_exists($legacyFile)) {
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) { foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
$funcInfo->discardInfoForOldPhpVersions(); $funcInfo->discardInfoForOldPhpVersions();
} }
} $arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (file_put_contents($legacyFile, $arginfoCode)) {
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash); echo "Saved $legacyFile\n";
file_put_contents($arginfoFile, $arginfoCode); }
}
// Collect parameter name statistics. // Collect parameter name statistics.
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) { foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
@ -1220,14 +1227,17 @@ function initPhpParser() {
} }
$optind = null; $optind = null;
$options = getopt("f", ["force-regeneration", "parameter-stats", "legacy"], $optind); $options = getopt("fh", ["force-regeneration", "parameter-stats", "help"], $optind);
$context = new Context; $context = new Context;
$printParameterStats = isset($options["parameter-stats"]); $printParameterStats = isset($options["parameter-stats"]);
$context->legacy = isset($options["legacy"]);
$context->forceRegeneration = $context->forceRegeneration =
isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats; isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
if (isset($options["h"]) || isset($options["help"])) {
die("\nusage: gen-stub.php [ -f | --force-regeneration ] [ --parameter-stats ] [ -h | --help ] [ name.stub.php | directory ]\n\n");
}
$location = $argv[$optind] ?? "."; $location = $argv[$optind] ?? ".";
if (is_file($location)) { if (is_file($location)) {
// Generate single file. // Generate single file.