Repository navigation
Expand file tree
/
Copy pathdata-update.php
More file actions
128 lines (97 loc) · 3.48 KB
/
Copy pathdata-update.php
File metadata and controls
128 lines (97 loc) · 3.48 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
<?php
class SmithingAutoUpdater
{
private const ITEMDATA_FILE_URL = 'https://raw.githubusercontent.com/pmmp/BedrockData/master/item_tags.json';
private const PATTERN_FILE = 'src/KRUNCHSHooT/LibTrimArmor/PatternType.php';
private const PATTERN_TAG = 'minecraft:trim_templates';
private const MATERIAL_FILE = 'src/KRUNCHSHooT/LibTrimArmor/MaterialType.php';
private const MATERIAL_TAG = 'minecraft:trim_materials';
public function start()
{
$data = $this->getTrimData();
if (is_null($data)) {
echo "Failed to fetch data\n";
return;
}
$isChanged = false;
foreach ([
self::MATERIAL_TAG => self::MATERIAL_FILE,
self::PATTERN_TAG => self::PATTERN_FILE
] as $key => $value) {
$tagData = $data[$key];
if ($this->checkData($tagData, $value)) {
$this->updateData($tagData, $value);
$this->commitChanges($value);
$isChanged = true;
}
}
if ($isChanged) {
$this->pushChanges();
}
}
private function getTrimData(): ?array
{
$init = curl_init(self::ITEMDATA_FILE_URL);
curl_setopt($init, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($init);
curl_close($init);
return $response ? json_decode($response, true) : null;
}
private function getTemplateName(string $template): string
{
return explode('_', str_replace('minecraft:', '', $template))[0];
}
private function checkData(array $data, string $filename): bool
{
$constants = $this->getCurrentConstants($filename);
$newConstants = array_map(function (string $template) {
return strtoupper($this->getTemplateName($template));
}, $data);
sort($constants);
sort($newConstants);
return $constants != $newConstants;
}
private function getCurrentConstants(string $filename): array
{
$content = file_get_contents($filename);
preg_match_all('/public const (\w+)/', $content, $matches);
return $matches[1];
}
private function updateData(array $data, string $filename)
{
$constants = [];
foreach ($data as $template) {
$constants[] = 'public const ' . strtoupper($this->getTemplateName($template)) . ' = ' . '\'' . strtolower($this->getTemplateName($template)) . '\';';
}
$constantsString = implode("\n ", $constants);
$splitted = explode('/', $filename);
$className = str_replace('.php', '', $splitted[count($splitted) - 1]);
array_shift($splitted);
array_pop($splitted);
$namespace = implode('\\', $splitted);
$fileContent = <<<PHP
<?php
namespace $namespace;
class $className {
$constantsString
}
PHP;
file_put_contents($filename, $fileContent);
}
private function setupGithubAction()
{
exec('git config --global user.name "' . getenv('GIT_NAME') . '"');
exec('git config --global user.email "' . getenv('GIT_EMAIL') . '"');
}
private function commitChanges(string $filename)
{
exec('git add ' . $filename);
}
private function pushChanges()
{
$this->setupGithubAction();
exec('git commit -m "chore(auto-update): 🔧 Update Smithing Template"');
exec('git push origin master');
}
}
(new SmithingAutoUpdater())->start();