Skip to content

Instantly share code, notes, and snippets.

@prappo
Created August 12, 2024 13:30
Show Gist options
  • Save prappo/8111a83bf37a09f169ebe0a25a62ab04 to your computer and use it in GitHub Desktop.
Save prappo/8111a83bf37a09f169ebe0a25a62ab04 to your computer and use it in GitHub Desktop.
<?php
function change_version($pattern, $version) {
// Iterate over each file and its patterns in the pattern array
foreach ($pattern as $file => $patterns) {
// Check if the file exists before proceeding
if (file_exists($file)) {
// Get the file content
$content = file_get_contents($file);
// Iterate over each pattern within the file
foreach ($patterns as $searchPattern) {
// Escape any special characters in the search pattern
$escapedPattern = preg_quote($searchPattern, '/');
// Replace the placeholder $version with a regex pattern that matches any version number format
$regexPattern = str_replace('\#version', '[0-9]+\.[0-9]+\.[0-9]+', $escapedPattern);
// Create the replacement string
$replacement = str_replace('#version', $version, $searchPattern);
// Perform the replacement in the file content using regex
$content = preg_replace("/$regexPattern/", $replacement, $content);
}
// Save the modified content back to the file
file_put_contents($file, $content);
} else {
// File does not exist
echo "File $file does not exist.\n";
}
}
}
// Example usage
$pattern = [
"stripe-checkout.php" => [
'Version: #version',
'define( \'SIMPLE_PAY_VERSION\', \'#version\' );'
],
"readme.txt" => [
'Stable tag: #version'
],
"package.json" => [
'"version": "#version",'
]
];
$version = getenv('PLUGIN_VERSION');
change_version($pattern, $version);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment