-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbuild-pre.php
More file actions
116 lines (103 loc) · 2.89 KB
/
build-pre.php
File metadata and controls
116 lines (103 loc) · 2.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
#!/bin/php
<?php
declare(strict_types = 1);
/**
* This script is meant to be run from github actions and not locally.
* It prepares the environment before running the main build.php script.
* This script will check if there are any modifications to the
* composer.local.json file and if so, it will remove the composer.lock
* file to ensure a fresh install of dependencies without collisions.
*
* This script takes no arguments.
*
*/
Class ComposerLocalHandler {
/**
* Path to composer.local.json
*
* @var string
*/
private $composerLocalPath;
/**
* Path to composer.lock
*
* @var string
*/
private $composerLockPath;
/**
* Constructor
*
* @param string $rootPath
*/
public function __construct(string $rootPath) {
$this->composerLocalPath = $rootPath . '/composer.local.json';
$this->composerLockPath = $rootPath . '/composer.lock';
if (file_exists($this->composerLocalPath) && $this->hasModifications()) {
$removed = $this->removeComposerLock();
if ($removed) {
echo "Removed composer.lock due to composer.local.json modification.\n";
} else {
echo "Failed to remove composer.lock despite modifications in composer.local.json.\n";
}
} else {
echo "No modifications detected in composer.local.json.\n";
}
}
/**
* Check if composer.local.json has modifications, (check for diff between main origin and fork).
*
* @return bool
*/
public function hasModifications(): bool {
$expectedJson = $this->getExpectedJson();
$currentJson = $this->getCurrentJson($this->composerLocalPath);
return $expectedJson !== $currentJson;
}
/**
* Get the expected composer.local.json content.
*
* @return array
*/
public function getExpectedJson(): array {
$expectedJson = '{
"name": "municipio-se/municipio-deployment-custom",
"license": "MIT",
"description": "Additions for you own install of municipo.",
"require": {}
}';
return $this->jsonDecode($expectedJson);
}
/**
* Get the current composer.local.json content.
*
* @param string $path
* @return array
*/
private function getCurrentJson(string $path): array
{
return $this->jsonDecode(
file_get_contents($path)
);
}
/**
* Decode json string to array.
*
* @param string $json
* @return array
*/
private function jsonDecode(string $json): array {
return json_decode($json, true);
}
/**
* Remove composer.lock file.
*
* @return void
*/
public function removeComposerLock(): bool {
if (file_exists($this->composerLockPath)) {
return unlink($this->composerLockPath);
}
return false;
}
}
new ComposerLocalHandler(getcwd());