|
4 | 4 | * Update Phel version in config.toml based on installed Phel package version |
5 | 5 | */ |
6 | 6 |
|
| 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 | + |
7 | 10 | $versionFinderFile = __DIR__ . '/../vendor/phel-lang/phel-lang/src/php/Console/Application/VersionFinder.php'; |
8 | 11 | $configFile = __DIR__ . '/../config.toml'; |
9 | 12 |
|
10 | 13 | 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"); |
12 | 15 | exit(1); |
13 | 16 | } |
14 | 17 |
|
15 | 18 | if (!file_exists($configFile)) { |
16 | | - echo "Error: config.toml not found\n"; |
| 19 | + fwrite(STDERR, "Error: config.toml not found\n"); |
17 | 20 | exit(1); |
18 | 21 | } |
19 | 22 |
|
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"); |
25 | 26 | exit(1); |
26 | 27 | } |
27 | 28 |
|
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"); |
40 | 31 | exit(0); |
41 | 32 | } |
42 | 33 |
|
43 | | -// Read config.toml |
| 34 | +$phelVersion = $matches[1]; |
44 | 35 | $configContent = file_get_contents($configFile); |
45 | | - |
46 | | -// Update version |
47 | 36 | $updatedContent = preg_replace( |
48 | | - '/^phel_version\s*=\s*"[^"]*"/m', |
| 37 | + CONFIG_VERSION_PATTERN, |
49 | 38 | 'phel_version = "' . $phelVersion . '"', |
50 | 39 | $configContent |
51 | 40 | ); |
|
55 | 44 | exit(0); |
56 | 45 | } |
57 | 46 |
|
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 | +} |
60 | 51 |
|
61 | 52 | echo "Updated Phel version in config.toml to: $phelVersion\n"; |
0 commit comments