Create file with helper functions

To avoid code repetition I created a file,
which contains function that can be reused.
[MAILPOET-6097]
This commit is contained in:
Jan Lysý
2024-06-17 19:15:24 +02:00
committed by Jan Lysý
parent dd4bd67ed2
commit aed63c7cd3
3 changed files with 52 additions and 66 deletions

View File

@@ -1,5 +1,7 @@
<?php
require_once __DIR__ . '/helpers.php';
function getWooCommerceVersions() {
$url = "https://api.wordpress.org/plugins/info/1.0/woocommerce.json";
$response = file_get_contents($url);
@@ -12,52 +14,6 @@ function getWooCommerceVersions() {
return array_keys($data['versions']);
}
function filterStableVersions($versions) {
return array_filter($versions, function($version) {
// Only include stable versions (exclude versions with -rc, -beta, -alpha, etc.)
return !preg_match('/-(rc|beta|alpha|dev|nightly|pl)/i', $version);
});
}
function getLatestAndPreviousMinorMajorVersions($versions) {
usort($versions, 'version_compare');
$currentVersion = end($versions);
$previousVersion = null;
foreach (array_reverse($versions) as $version) {
if (version_compare($version, $currentVersion, '<') && getMinorMajorVersion($version) !== getMinorMajorVersion($currentVersion)) {
$previousVersion = $version;
break;
}
}
return [$currentVersion, $previousVersion];
}
function getMinorMajorVersion($version) {
$parts = explode('.', $version);
return $parts[0] . '.' . $parts[1];
}
function replaceVersionInFile($filePath, $pattern, $replacement): void {
$content = file_get_contents($filePath);
if ($content === false) {
die("Failed to read the file at $filePath.");
}
$updatedContent = preg_replace($pattern, $replacement, $content);
if ($updatedContent === null || $updatedContent === $content) {
echo "Nothing to update in $filePath\n";
return;
}
if (file_put_contents($filePath, $updatedContent) === false) {
die("Failed to write the updated file at $filePath.");
}
}
function replacePreviousVersion($previousVersion) {
replaceVersionInFile(
__DIR__ . './../../../.circleci/config.yml',
@@ -68,7 +24,7 @@ function replacePreviousVersion($previousVersion) {
$allVersions = getWooCommerceVersions();
$stableVersions = filterStableVersions($allVersions);
list($latestVersion, $previousVersion) = getLatestAndPreviousMinorMajorVersions($stableVersions);
[$latestVersion, $previousVersion] = getLatestAndPreviousMinorMajorVersions($stableVersions);
echo "Latest WooCommerce version: $latestVersion\n";

View File

@@ -1,5 +1,7 @@
<?php
require_once __DIR__ . '/helpers.php';
function fetchData($url): array {
$response = file_get_contents($url);
return json_decode($response, true);
@@ -44,25 +46,6 @@ function getLatestAndPreviousVersions($sortedVersions) {
return [reset($latestVersionGroup), end($previousVersionGroup)];
}
function replaceVersionInFile($filePath, $pattern, $replacement) {
$content = file_get_contents($filePath);
if ($content === false) {
die("Failed to read the file at $filePath.");
}
$updatedContent = preg_replace($pattern, $replacement, $content);
if ($updatedContent === null || $updatedContent === $content) {
echo "Nothing to update in $filePath\n";
return;
}
if (file_put_contents($filePath, $updatedContent) === false) {
die("Failed to write the updated file at $filePath.");
}
}
function replaceLatestVersion($latestVersion) {
replaceVersionInFile(
__DIR__ . './../../../mailpoet/tests/docker/docker-compose.yml',

47
.github/workflows/scripts/helpers.php vendored Normal file
View File

@@ -0,0 +1,47 @@
<?php
function replaceVersionInFile($filePath, $pattern, $replacement) {
$content = file_get_contents($filePath);
if ($content === false) {
die("Failed to read the file at $filePath.");
}
$updatedContent = preg_replace($pattern, $replacement, $content);
if ($updatedContent === null || $updatedContent === $content) {
echo "Nothing to update in $filePath\n";
return;
}
if (file_put_contents($filePath, $updatedContent) === false) {
die("Failed to write the updated file at $filePath.");
}
}
function filterStableVersions($versions) {
return array_filter($versions, function($version) {
// Only include stable versions (exclude versions with -rc, -beta, -alpha, etc.)
return preg_match('/^\d+\.\d+\.\d+$/', $version);
});
}
function getLatestAndPreviousMinorMajorVersions($versions) {
usort($versions, 'version_compare');
$currentVersion = end($versions);
$previousVersion = null;
foreach (array_reverse($versions) as $version) {
if (version_compare($version, $currentVersion, '<') && getMinorMajorVersion($version) !== getMinorMajorVersion($currentVersion)) {
$previousVersion = $version;
break;
}
}
return [$currentVersion, $previousVersion];
}
function getMinorMajorVersion($version) {
$parts = explode('.', $version);
return $parts[0] . '.' . $parts[1];
}