forked from municipio-se/municipio-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposer-local-merge.php
More file actions
executable file
·309 lines (254 loc) · 8.89 KB
/
composer-local-merge.php
File metadata and controls
executable file
·309 lines (254 loc) · 8.89 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/php
<?php
declare(strict_types=1);
/**
* Composer Local Merge Handler
*
* Replaces Wikimedia Composer Merge plugin with a deterministic, script-based workflow.
* This script temporarily injects local composer requirements only when needed.
*
* Workflow:
* 1. pre-install: Detect local overrides, backup, merge, and prepare for install
* 2. post-install: Restore original files and clean up backups
*/
class ComposerLocalMerge
{
private const COMPOSER_JSON = 'composer.json';
private const COMPOSER_LOCAL_JSON = 'composer.local.json';
private const COMPOSER_LOCK = 'composer.lock';
private const BACKUP_SUFFIX = '.bkup';
private string $rootPath;
private bool $hasMerged = false;
private string $flagFile;
public function __construct(string $rootPath)
{
$this->rootPath = rtrim($rootPath, '/');
$this->flagFile = $this->rootPath . '/.composer-merge-active';
}
/**
* Pre-install hook: Backup and merge local requirements if present
*/
public function preInstall(): void
{
echo "=== Composer Local Merge: Pre-Install ===\n";
$localPath = $this->getPath(self::COMPOSER_LOCAL_JSON);
if (!file_exists($localPath)) {
echo "No composer.local.json found - skipping merge\n";
return;
}
$localRequirements = $this->getLocalRequirements($localPath);
if (empty($localRequirements['require']) && empty($localRequirements['require-dev'])) {
echo "composer.local.json has no requirements - skipping merge\n";
return;
}
try {
$this->performMerge($localRequirements);
echo "Successfully merged local requirements\n";
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
$this->rollback();
exit(1);
}
}
/**
* Post-install hook: Restore original files and clean up
*/
public function postInstall(): void
{
echo "=== Composer Local Merge: Post-Install ===\n";
if (!file_exists($this->flagFile)) {
echo "No merge was performed - skipping restore\n";
return;
}
try {
$this->restore();
echo "Successfully restored original files\n";
} catch (Exception $e) {
echo "ERROR during restore: " . $e->getMessage() . "\n";
exit(1);
}
}
/**
* Perform the merge operation
*/
private function performMerge(array $localRequirements): void
{
$composerPath = $this->getPath(self::COMPOSER_JSON);
$lockPath = $this->getPath(self::COMPOSER_LOCK);
// Step 1: Backup composer.json
$this->backup($composerPath);
echo "✓ Backed up composer.json\n";
// Step 2: Backup composer.lock if it exists
if (file_exists($lockPath)) {
$this->backup($lockPath);
echo "✓ Backed up composer.lock\n";
// Step 3: Remove composer.lock to force fresh resolution
if (!unlink($lockPath)) {
throw new Exception("Failed to remove composer.lock");
}
echo "✓ Removed composer.lock\n";
}
// Step 4: Merge requirements into composer.json
// Note: Local requirements override existing ones - this allows developers
// to test specific versions or patches during development
$composer = $this->readJson($composerPath);
if (!empty($localRequirements['require'])) {
$composer['require'] = array_merge(
$composer['require'] ?? [],
$localRequirements['require']
);
echo "✓ Merged " . count($localRequirements['require']) . " require dependencies\n";
}
if (!empty($localRequirements['require-dev'])) {
$composer['require-dev'] = array_merge(
$composer['require-dev'] ?? [],
$localRequirements['require-dev']
);
echo "✓ Merged " . count($localRequirements['require-dev']) . " require-dev dependencies\n";
}
$this->writeJson($composerPath, $composer);
// Create flag file to indicate merge was performed
if (file_put_contents($this->flagFile, time()) === false) {
throw new Exception("Failed to create merge flag file");
}
$this->hasMerged = true;
}
/**
* Restore original files from backups
*/
private function restore(): void
{
$composerPath = $this->getPath(self::COMPOSER_JSON);
$lockPath = $this->getPath(self::COMPOSER_LOCK);
// Restore composer.json
$this->restoreFromBackup($composerPath);
echo "✓ Restored composer.json\n";
// Restore composer.lock if backup exists
$lockBackup = $lockPath . self::BACKUP_SUFFIX;
if (file_exists($lockBackup)) {
$this->restoreFromBackup($lockPath);
echo "✓ Restored composer.lock\n";
}
// Clean up flag file
if (file_exists($this->flagFile)) {
unlink($this->flagFile);
}
}
/**
* Rollback changes in case of error
*/
private function rollback(): void
{
echo "Rolling back changes...\n";
try {
$composerPath = $this->getPath(self::COMPOSER_JSON);
$composerBackup = $composerPath . self::BACKUP_SUFFIX;
if (file_exists($composerBackup)) {
$this->restoreFromBackup($composerPath);
echo "✓ Rolled back composer.json\n";
}
$lockPath = $this->getPath(self::COMPOSER_LOCK);
$lockBackup = $lockPath . self::BACKUP_SUFFIX;
if (file_exists($lockBackup)) {
$this->restoreFromBackup($lockPath);
echo "✓ Rolled back composer.lock\n";
}
if (file_exists($this->flagFile)) {
unlink($this->flagFile);
}
} catch (Exception $e) {
echo "ERROR during rollback: " . $e->getMessage() . "\n";
}
}
/**
* Get local requirements from composer.local.json
*/
private function getLocalRequirements(string $path): array
{
$data = $this->readJson($path);
return [
'require' => $data['require'] ?? [],
'require-dev' => $data['require-dev'] ?? []
];
}
/**
* Create backup of a file
*/
private function backup(string $path): void
{
$backup = $path . self::BACKUP_SUFFIX;
if (!copy($path, $backup)) {
throw new Exception("Failed to backup: {$path}");
}
}
/**
* Restore file from backup
*/
private function restoreFromBackup(string $path): void
{
$backup = $path . self::BACKUP_SUFFIX;
if (!file_exists($backup)) {
throw new Exception("Backup not found: {$backup}");
}
if (!copy($backup, $path)) {
throw new Exception("Failed to restore: {$path}");
}
unlink($backup);
}
/**
* Read and decode JSON file
*/
private function readJson(string $path): array
{
if (!file_exists($path)) {
throw new Exception("File not found: {$path}");
}
$content = file_get_contents($path);
$data = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Invalid JSON in {$path}: " . json_last_error_msg());
}
return $data;
}
/**
* Encode and write JSON file
*/
private function writeJson(string $path, array $data): void
{
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($json === false) {
throw new Exception("Failed to encode JSON: " . json_last_error_msg());
}
// Add newline at end of file to match standard formatting
$json .= "\n";
if (file_put_contents($path, $json) === false) {
throw new Exception("Failed to write file: {$path}");
}
}
/**
* Get full path for a file
*/
private function getPath(string $filename): string
{
return $this->rootPath . '/' . $filename;
}
}
// Main execution
if (PHP_SAPI !== 'cli') {
echo "This script must be run from the command line\n";
exit(1);
}
$action = $argv[1] ?? '';
$rootPath = getcwd();
$merger = new ComposerLocalMerge($rootPath);
switch ($action) {
case 'pre-install':
$merger->preInstall();
break;
case 'post-install':
$merger->postInstall();
break;
default:
echo "Usage: php composer-local-merge.php [pre-install|post-install]\n";
exit(1);
}