-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBuild.php
218 lines (187 loc) · 6 KB
/
Build.php
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
<?php
/**
* Rebuilds the types table in the README file and creates a typescript file
*
* Reads a spec.json file and creates a new types table.
* Leaves the remainder of the file as-is.
*
* @author Code for Recovery <[email protected]>
*
* @since 1.0.0
*/
namespace Code4Recovery;
class Build
{
/**
* The path to the json file containing spec data.
* @var array
*/
private array $tables = [
[
'file' => './data/types.json',
'delimiterTop' => '<!-- Types -->',
'delimiterBottom' => '<!-- End Types -->',
],
[
'file' => './data/proposed-new.json',
'delimiterTop' => '<!-- Proposed Types -->',
'delimiterBottom' => '<!-- End Proposed Types -->',
],
[
'file' => './data/proposed-change.json',
'delimiterTop' => '<!-- Proposed Changed Types -->',
'delimiterBottom' => '<!-- End Proposed Changed Types -->',
]
];
/**
* The path to the readme.md that will be created.
* @var string
*/
private string $readmeFile = './README.md';
/**
* Languages used in the types table
* @var array
*/
private array $languages = [
'en' => 'English',
'es' => 'Español',
'fr' => 'Français',
'ja' => '日本語',
'nl' => 'Nederlands',
'pt' => 'Português',
'sk' => 'Slovenčina',
'sv' => 'Svenska',
];
/**
* Constructor.
*
* @param $specFile: Path to spec json file, relative to project root.
* @param $readmeFile: Path to readme file, relative to project root.
*/
public function __construct()
{
foreach ($this->tables as $table) {
$this->writeReadme($table);
}
$this->writeTypeScript();
$this->writePHPObject();
}
/**
* Builds a types table.
*
* @return string Markdown for table
*/
private function createTable($table): string
{
return implode(PHP_EOL, [
$table['delimiterTop'],
$this->createTableHeader(),
$this->createTableRows($table['file']),
$table['delimiterBottom'],
]);
}
/**
* Creates the table header markup.
*
* @return string
*/
private function createTableHeader(): string
{
// Columns
$headerColumns = array_merge(['Code'], array_values($this->languages));
// Dashes
$rows = [$headerColumns, array_fill(0, count($headerColumns), '---')];
// Build final markup with line breaks
return implode(PHP_EOL, array_map([$this, 'createTableRow'], $rows));
}
/**
* Creates a table row separated (and surrounded) by pipes.
*
* @param array $row
*
* @return string
*/
private function createTableRow($row): string
{
return implode('|', array_merge([''], $row, ['']));
}
/**
* Gets the contents of the spec file and creates the table rows markup.
*
* @return string
*/
private function createTableRows($specFile): string
{
// Init empty array
$specRows = [];
// Get spec data & language codes
$specJson = json_decode(file_get_contents($specFile), true);
$languages = array_keys($this->languages);
// Loop through types from spec
foreach ($specJson as $key => $value) {
// Begin row output. Empty the $columns array each time.
$specColumns = ['`' . $key . '`'];
// Loop through languages
foreach ($languages as $languageKey) {
// Add translation to columns
$specColumns[] = array_key_exists($languageKey, $value) ? $value[$languageKey] : '-';
}
// Add empty array values so our row is surrounded by pipes
$specRows[] = $this->createTableRow($specColumns);
}
return implode(PHP_EOL, $specRows);
}
/**
* Get the contents of the readme, replace the types table, and re-write.
*
* @param string $tableContent The markdown to write to the file
*
* @return void
*/
private function writeReadme(array $table): void
{
// Get the current readme contents
$readmeContents = file_get_contents($this->readmeFile);
// Get the new table content
$tableContent = $this->createTable($table);
// Replace existing table
$result = preg_replace('#(' . preg_quote($table['delimiterTop']) . ')(.*)(' . preg_quote($table['delimiterBottom']) . ')#siU', $tableContent, $readmeContents);
// Write new file
$readmeHandle = fopen($this->readmeFile, "w") or die("Unable to open file!");
fwrite($readmeHandle, $result);
fclose($readmeHandle);
}
/**
* Write the typescript file
*
* @return void
*/
private function writeTypeScript(): void
{
file_put_contents('./src/languages.ts', 'export const languages = ' . json_encode(array_keys($this->languages)) . ' as const;');
$json = file_get_contents('./data/types.json');
file_put_contents('./src/types.ts', 'export const types = ' . $json . ';');
}
private function writePHPObject(): void
{
// Fetch the JSON file
$jsonData = file_get_contents('./data/types.json');
if ($jsonData === false) {
die("Error fetching JSON file.");
}
// Decode JSON data
$dataObject = json_decode($jsonData);
if ($dataObject === null) {
die("Error decoding JSON data.");
}
// Prepare the PHP content
$phpContent = "<?php\n\n" . 'return ' . var_export($dataObject, true) . ";\n";
// Write to types.php in the src directory
$fileWritten = file_put_contents(__DIR__ . '/src/types.php', $phpContent);
if ($fileWritten === false) {
die("Error writing to types.php.");
}
echo "types.php successfully written.\n";
}
}
new Build();