From cf821f62158018fca974f338e4ee8c06612f5214 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 29 Apr 2024 15:06:52 +1200 Subject: [PATCH] NEW Migrate logic from workflows for reasoning about repository metadata --- .gitignore | 2 + composer.json | 12 ++++++ src/BranchLogic.php | 63 ++++++++++++++++++++++++++++++ src/MetaData.php | 94 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/BranchLogic.php create mode 100644 src/MetaData.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55940e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..abae2ff --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "silverstripe/supported-modules", + "description": "Metadata about Silverstripe CMS supported modules and other repositories maintained by Silverstripe", + "autoload": { + "psr-4": { + "SilverStripe\\SupportedModules\\": "src/" + } + }, + "require": { + "composer/semver": "^3.4" + } +} diff --git a/src/BranchLogic.php b/src/BranchLogic.php new file mode 100644 index 0000000..90acc5a --- /dev/null +++ b/src/BranchLogic.php @@ -0,0 +1,63 @@ + $repoBranches) { + if (is_numeric($cmsMajor) && in_array($branchMajor, $repoBranches)) { + return $cmsMajor; + } + } + return ''; + } + + private static function getCmsMajorFromComposerJson(?stdClass $composerJsonContent = null): string + { + if ($composerJsonContent === null) { + return ''; + } + foreach (MetaData::getAllRepositoryMetaData() as $categoryData) { + foreach ($categoryData as $repoData) { + $composerName = $repoData['packagist'] ?? null; + if ($composerName === null || !isset($composerJsonContent->require->$composerName)) { + continue; + } + $parser = new VersionParser(); + $constraint = $parser->parseConstraints($composerJsonContent->require->$composerName); + $boundedVersion = explode('.', $constraint->getLowerBound()->getVersion()); + $composerVersionMajor = $boundedVersion[0]; + // If it's a non-numeric branch constraint or something unstable, don't use it + if ($composerVersionMajor === 0) { + continue; + } + foreach ($repoData['majorVersionMapping'] as $cmsMajor => $repoBranches) { + if (is_numeric($cmsMajor) && in_array($composerVersionMajor, $repoBranches)) { + return $cmsMajor; + } + } + } + } + return ''; + } +} \ No newline at end of file diff --git a/src/MetaData.php b/src/MetaData.php new file mode 100644 index 0000000..3eb2aee --- /dev/null +++ b/src/MetaData.php @@ -0,0 +1,94 @@ + $categoryData) { + // Skip anything that can't be lockstepped + if ($category !== self::CATEGORY_SUPPORTED) { + continue; + } + // Find lockstepped repos + foreach ($categoryData as $repoData) { + if (isset($repoData['lockstepped']) && $repoData['lockstepped'] && !empty($repoData['packagist'])) { + $repos[$repoData['packagist']] = $repoData['majorVersionMapping']; + } + } + } + return $repos; + } + + /** + * Get all metadata about all repositories we have information about + */ + public static function getAllRepositoryMetaData(): array + { + if (empty(self::$repositoryMetaData)) { + $rawJson = file_get_contents(__DIR__ . '/../repositories.json'); + $decodedJson = json_decode($rawJson, true); + if ($decodedJson === null) { + throw new RuntimeException('Could not parse repositories.json data: ' . json_last_error_msg()); + } + self::$repositoryMetaData = $decodedJson; + } + return self::$repositoryMetaData; + } +} \ No newline at end of file