forked from pronskiy/php-code-golf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_php_tasks.php
29 lines (21 loc) · 1.08 KB
/
update_php_tasks.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
<?php
$positionMarker = '/*caret*/';
$positionMarkerPattern = '/\/\*caret\*\//';
$fileName = 'phpTasks';
$json = json_decode(file_get_contents(__DIR__ . '/' . $fileName . '.json'));
foreach ($json->tasks as &$task) {
['filename' => $filename, 'extension' => $extension] = pathinfo($task->source);
$initialFilename = $task->source;
$targetFilename = substr($initialFilename, 0, strlen($initialFilename) - strlen($filename . "." . $extension)) .
sprintf("%s_.%s", $filename, $extension);
$tasksDir = __DIR__ . '/tasks_php_new/';
$initialCode = trim(file_get_contents($tasksDir . $initialFilename));
$targetCode = trim(file_get_contents($tasksDir . $targetFilename));
$initialOffset = strpos($initialCode, $positionMarker);
$initialCode = preg_replace($positionMarkerPattern, '', $initialCode, 1);
$task->initialCode = $initialCode;
$task->targetCode = $targetCode;
$task->initialOffset = $initialOffset;
unset($task->source);
}
file_put_contents(__DIR__ . '/' . $fileName . '_.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));