Skip to content

Commit

Permalink
pickFiles - Extract method 'find()'
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jul 21, 2022
1 parent 93dd7a1 commit 8ade0d3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
49 changes: 49 additions & 0 deletions util/pickFiles.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,55 @@ public static function getAll(): array {
return $allFiles;
}

/**
* Find all known snapshots that match a filter-expression.
*
* @param string|string[] $filters
* List of filter expressions.
* Ex: '4.*', '5.0.*'
* Ex: '@4.5', '@4.2..4.5', '@4.2..', '@4.5:10'
* @return string[]
* List of snapshots (file-paths).
*/
public static function find($filters): array {
$allFiles = UpgradeSnapshots::getAll();

$filters = (array) $filters;
$files = [];

foreach ($filters as $arg) {
if ($arg[0] === '@') {
$parsedFilter = UpgradeSnapshots::parseFilterExpr($arg);

$matches = array_filter($allFiles, function($f) use ($parsedFilter) {
$fileVer = UpgradeSnapshots::parseFileVer($f);
if ($parsedFilter['minVer'] && version_compare($fileVer, $parsedFilter['minVer'], '<=')) {
return FALSE;
}
if ($parsedFilter['maxVer'] && version_compare($fileVer, $parsedFilter['maxVer'], '>=')) {
return FALSE;
}
return TRUE;
});

if ($parsedFilter['maxCount'] > 0) {
$matches = UpgradeSnapshots::pickSubset($matches, $parsedFilter['maxCount']);
}

$files = array_merge($files, $matches);
}
elseif (strpos($arg, '*')) {
$files = array_merge($files,
(array) glob(UpgradeSnapshots::getPath() . DIRECTORY_SEPARATOR . $arg));
}
else {
throw new \InvalidArgumentException("Unrecognized filter: $arg");
}
}

return $files;
}

/**
* @param string $a
* Filter expression that selects range of test examples.
Expand Down
29 changes: 2 additions & 27 deletions util/pickFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ function main($args) {
return 1;
}

$allFiles = UpgradeSnapshots::getAll();

$files = array();
$maxCount = 0;
while (!empty($args)) {
Expand All @@ -44,31 +42,8 @@ function main($args) {
elseif (file_exists($arg)) {
$files[] = $arg;
}
elseif ($arg[0] === '@') {
$filters = UpgradeSnapshots::parseFilterExpr($arg);

$matches = array_filter($allFiles, function($f) use ($filters) {
$fileVer = UpgradeSnapshots::parseFileVer($f);
if ($filters['minVer'] && version_compare($fileVer, $filters['minVer'], '<=')) {
return FALSE;
}
if ($filters['maxVer'] && version_compare($fileVer, $filters['maxVer'], '>=')) {
return FALSE;
}
return TRUE;
});

if ($filters['maxCount'] > 0) {
$matches = UpgradeSnapshots::pickSubset($matches, $filters['maxCount']);
}

$files = array_merge($files, $matches);
}
elseif (strpos($arg, '*')) {
$files = array_merge(
$files,
(array) glob(UpgradeSnapshots::getPath() . DIRECTORY_SEPARATOR . $arg)
);
elseif ($arg[0] === '@' || strpos($arg, '*')) {
$files = array_merge($files, UpgradeSnapshots::find($arg));
}
else {
help("Unrecognized argument or missing file: $arg\n");
Expand Down

0 comments on commit 8ade0d3

Please sign in to comment.