Repository navigation
Expand file tree
/
Copy pathgenerate-php-envs.php
More file actions
125 lines (102 loc) · 3.86 KB
/
Copy pathgenerate-php-envs.php
File metadata and controls
125 lines (102 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
$sourceFile = __DIR__ . '/src/support/phpEnvironments.ts';
if (!file_exists($sourceFile)) {
echo "Error: Source file not found: $sourceFile\n";
exit(1);
}
$content = file_get_contents($sourceFile);
$pattern = '/export const phpEnvironments: Record<PhpEnvironment, PhpEnvironmentConfig> = \{(.*?)\};/s';
if (!preg_match($pattern, $content, $matches)) {
echo "Error: Could not find phpEnvironments object\n";
exit(1);
}
$objectContent = $matches[1];
$environments = [];
$lines = explode("\n", $objectContent);
$currentEnv = null;
$currentEnvData = [];
$insideEnv = false;
$braceDepth = 0;
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
// Check if starting a new environment
if (preg_match('/^(\w+):\s*\{/', $line, $matches)) {
// Save previous environment if exists
if ($currentEnv && !empty($currentEnvData)) {
$environments[$currentEnv] = $currentEnvData;
}
$currentEnv = $matches[1];
$currentEnvData = [];
$insideEnv = true;
$braceDepth = 1;
continue;
}
if (!$insideEnv) {
continue;
}
// Count braces to handle nested objects
$braceDepth += substr_count($line, '{') - substr_count($line, '}');
// If we've closed all braces, we're done with this environment
if ($braceDepth <= 0) {
if ($currentEnv) {
$environments[$currentEnv] = $currentEnvData;
$currentEnv = null;
$currentEnvData = [];
}
$insideEnv = false;
continue;
}
// Parse property line
if (preg_match('/^(\w+):\s*(.+?)(?:,\s*)?$/', $line, $propMatch)) {
$propName = $propMatch[1];
$propValue = trim($propMatch[2], ' ,');
// Skip function properties (test function)
if (strpos($propValue, '=>') !== false || strpos($propValue, 'function') !== false) {
continue;
}
// Parse different value types
if ($propValue === 'true') {
$currentEnvData[$propName] = true;
} elseif ($propValue === 'false') {
$currentEnvData[$propName] = false;
} elseif (preg_match('/^\[(.+)\]$/', $propValue, $arrayMatch)) {
// Handle array values
$arrayContent = $arrayMatch[1];
$arrayItems = preg_split('/,\s*/', $arrayContent);
$currentEnvData[$propName] = array_map(function($item) {
return trim($item, '"\'');
}, $arrayItems);
} elseif (preg_match('/^["\'](.+)["\']$/', $propValue, $stringMatch)) {
// Handle string values
$currentEnvData[$propName] = $stringMatch[1];
} else {
// Handle other scalar values (remove trailing comma if present)
$currentEnvData[$propName] = rtrim($propValue, ',');
}
}
}
// Don't forget the last environment
if ($currentEnv && !empty($currentEnvData)) {
$environments[$currentEnv] = $currentEnvData;
}
$packageJson = json_decode(file_get_contents(__DIR__ . '/package.json'), true);
$enums = array_keys($environments);
$enumItemLabels = array_map(function($env) {
return $env['label'];
}, array_values($environments));
$markdownEnumDescriptions = array_map(function($env) {
return $env['description'] ?? $env['label'];
}, array_values($environments));
$packageJson['contributes']['configuration']['properties']['Laravel.phpEnvironment']['enum'] = $enums;
$packageJson['contributes']['configuration']['properties']['Laravel.phpEnvironment']['enumItemLabels'] = $enumItemLabels;
$packageJson['contributes']['configuration']['properties']['Laravel.phpEnvironment']['markdownEnumDescriptions'] = $markdownEnumDescriptions;
file_put_contents(
__DIR__ . '/package.json',
json_encode($packageJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL
);
if (!isset($building)) {
exec('npm run format');
}