r3-v2/update_templates.php

101 lines
3.0 KiB
PHP
Executable File

#!/usr/bin/php
<?php
if (isset($argc)) {
for ($i = 0; $i < $argc; $i++) {
echo "Argument #" . $i . " - " . $argv[$i] . "\n";
}
}
else {
echo "argc and argv disabled\n";
}
$save = [
'options' => '/^\s*\bOPTIONS_START\b.*\bOPTIONS_END\b/sm',
'instance_mappings' => '/^\s*\bINSTANCE_OPTIONS_MAPPING_START\b.*\bINSTANCE_OPTIONS_MAPPING_END\b/sm',
'linked_objects' => '/^\s*\bLINKED_OBJECTS_START\b.*\bLINKED_OBJECTS_END\b/sm',
'excluded_options' => '/^\s*\bEXCLUDED_FROM_INSTANCE_OPTIONS_START\b.*\bEXCLUDED_FROM_INSTANCE_OPTIONS_END\b/sm',
'custom_options' => '/^\s*\/\/\bCUSTOM_OPTIONS_INIT_START\b.*\/\/\bCUSTOM_OPTIONS_INIT_END\b/sm',
'custom_before_init' => '/^\s*\/\/\bCUSTOM_BEFORE_INIT_START\b.*\/\/\bCUSTOM_BEFORE_INIT_END\b/sm',
'custom_create_instance' => '/^\s*\/\/\bCUSTOM_CREATE_INSTANCE_START\b.*\/\/\bCUSTOM_CREATE_INSTANCE_END\b/sm',
'custom_update_instance' => '/^\s*\/\/\bCUSTOM_UPDATE_INSTANCE_START\b.*\/\/\bCUSTOM_UPDATE_INSTANCE_END\b/sm',
'custom_update_from_instance' => '/^\s*\/\/\bCUSTOM_UPDATE_FROM_INSTANCE_START\b.*\/\/\bCUSTOM_UPDATE_FROM_INSTANCE_END\b/sm',
'custom_dispose' => '/^\s*\/\/\bCUSTOM_DISPOSE_START\b.*\/\/\bCUSTOM_DISPOSE_END\b/sm',
'custom_dispose_instance' => '/^\s*\/\/\bCUSTOM_DISPOSE_INSTANCE_START\b.*\/\/\bCUSTOM_DISPOSE_INSTANCE_END\b/sm',
'custom_implementation' => '/^\s*\/\/\bCUSTOM_IMPLEMENTATION_START\b.*\/\/\bCUSTOM_IMPLEMENTATION_END\b/sm'
];
if ($argv[2] == 'all') {
$files = scandir('src/r3', SCANDIR_SORT_DESCENDING);
$newFiles = [];
for ($i = 0; $i < sizeof($files); $i++) {
if (preg_match('/\.js$/', $files[$i])) {
array_push($newFiles, 'src/r3/' . $files[$i]);
}
}
$files = $newFiles;
} else {
$files = [$argv[2]];
}
function getContents($contents, $regex) {
$matches = [];
preg_match($regex, $contents, $matches);
if (sizeof($matches) > 0) {
return $matches[0];
} else {
return null;
}
}
foreach ($files as $file) {
if ($argv[1] == 'save') {
echo "saving file " . $file . "\n";
$contents = file_get_contents($file);
$saved = [];
foreach ($save as $key => $regex) {
$value = getContents($contents, $regex);
if ($value) {
array_push($saved, "\n");
array_push($saved, $value);
array_push($saved, "\n");
}
}
file_put_contents($file . '.saved', $saved);
echo "saved file " . $file . ".saved\n";
} else if ($argv[1] == 'restore') {
echo "restoring file " . $file . "\n";
$saved = file_get_contents($file . '.saved');
$new = file_get_contents($file);
foreach ($save as $key => $regex) {
$value = getContents($saved, $regex);
echo "restoring " . $key . " in " . $file;
if ($value && preg_match($regex, $new)) {
$new = preg_replace($regex, $value, $new);
}
}
file_put_contents($file, $new);
}
}
exit(0);
?>