Skip to content

Commit be09900

Browse files
committed
refactor: update-phel-version.php
1 parent 1f3fb9d commit be09900

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

build/update-phel-version.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,37 @@
44
* Update Phel version in config.toml based on installed Phel package version
55
*/
66

7+
const VERSION_PATTERN = '/public\s+const\s+string\s+LATEST_VERSION\s*=\s*[\'"]v?(\d+\.\d+\.\d+)[\'"]/';
8+
const CONFIG_VERSION_PATTERN = '/^phel_version\s*=\s*"[^"]*"/m';
9+
710
$versionFinderFile = __DIR__ . '/../vendor/phel-lang/phel-lang/src/php/Console/Application/VersionFinder.php';
811
$configFile = __DIR__ . '/../config.toml';
912

1013
if (!file_exists($versionFinderFile)) {
11-
echo "Error: VersionFinder.php not found. Make sure phel-lang/phel-lang is installed.\n";
14+
fwrite(STDERR, "Error: VersionFinder.php not found. Make sure phel-lang/phel-lang is installed.\n");
1215
exit(1);
1316
}
1417

1518
if (!file_exists($configFile)) {
16-
echo "Error: config.toml not found\n";
19+
fwrite(STDERR, "Error: config.toml not found\n");
1720
exit(1);
1821
}
1922

20-
// Read VersionFinder.php to extract LATEST_VERSION constant
21-
$versionFinderContent = file_get_contents($versionFinderFile);
22-
23-
if (!$versionFinderContent) {
24-
echo "Error: Failed to read VersionFinder.php\n";
23+
$versionFinderContent = @file_get_contents($versionFinderFile);
24+
if ($versionFinderContent === false) {
25+
fwrite(STDERR, "Error: Failed to read VersionFinder.php\n");
2526
exit(1);
2627
}
2728

28-
// Extract version from LATEST_VERSION constant
29-
$phelVersion = null;
30-
if (preg_match(
31-
'/public\s+const\s+string\s+LATEST_VERSION\s*=\s*[\'"]v?(\d+\.\d+\.\d+)[\'"]/',
32-
$versionFinderContent,
33-
$matches
34-
)) {
35-
$phelVersion = $matches[1];
36-
}
37-
38-
if (!$phelVersion) {
39-
echo "Warning: Could not determine Phel version from VersionFinder.php\n";
29+
if (!preg_match(VERSION_PATTERN, $versionFinderContent, $matches)) {
30+
fwrite(STDERR, "Warning: Could not determine Phel version from VersionFinder.php\n");
4031
exit(0);
4132
}
4233

43-
// Read config.toml
34+
$phelVersion = $matches[1];
4435
$configContent = file_get_contents($configFile);
45-
46-
// Update version
4736
$updatedContent = preg_replace(
48-
'/^phel_version\s*=\s*"[^"]*"/m',
37+
CONFIG_VERSION_PATTERN,
4938
'phel_version = "' . $phelVersion . '"',
5039
$configContent
5140
);
@@ -55,7 +44,9 @@
5544
exit(0);
5645
}
5746

58-
// Write updated config
59-
file_put_contents($configFile, $updatedContent);
47+
if (@file_put_contents($configFile, $updatedContent) === false) {
48+
fwrite(STDERR, "Error: Failed to write config.toml\n");
49+
exit(1);
50+
}
6051

6152
echo "Updated Phel version in config.toml to: $phelVersion\n";

0 commit comments

Comments
 (0)