r3-v2/update_options.php

267 lines
6.9 KiB
PHP
Executable File

#!/usr/bin/php
<?php
include "utils.php";
global $files;
foreach ($files as $file) {
echo $file . "\n";
echo "processing file " . $file . "\n";
$fn = fopen($file, "r");
$saveLine = true;
$options = [];
$instanceMappings = [];
$excludeOptions = [];
$saveOption = false;
$saveInstanceMappings = false;
$saveExcludeOptions = false;
$lines = [];
while (!feof($fn)) {
$line = fgets($fn);
/**
* if exiting an options definition block - stop saving that definition
*/
if (preg_match('/\bOPTIONS_END\b/', $line)) {
$saveOption = false;
}
if (preg_match('/\bINSTANCE_OPTIONS_MAPPING_END\b/', $line)) {
$saveInstanceMappings = false;
}
if (preg_match('/\bEXCLUDED_FROM_INSTANCE_OPTIONS_END\b/', $line)) {
$saveExcludeOptions = false;
}
/**
* if exiting an options implementation block - save the line for later
*/
if (preg_match('/\bOPTIONS_INIT_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bCREATE_INSTANCE_OPTIONS_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bUPDATE_INSTANCE_OPTIONS_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bUPDATE_FROM_INSTANCE_OPTIONS_END\b/', $line)) {
$saveLine = true;
}
/**
* Now save the line
*/
if ($saveLine) {
array_push($lines, $line);
}
/**
* if entering an options implementation block - do not save the line for later
*/
if (preg_match('/\bOPTIONS_INIT_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bCREATE_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bUPDATE_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bUPDATE_FROM_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if ($saveOption) {
$line = trim($line);
$key_value = preg_split('/=/', $line);
if ($key_value === false) {
continue;
}
$options[$key_value[0]] = $key_value[1];
}
if ($saveInstanceMappings) {
$line = trim($line);
$key_value = preg_split('/=/', $line);
if ($key_value === false) {
continue;
}
$instanceMappings[$key_value[0]] = $key_value[1];
}
if ($saveExcludeOptions) {
$line = trim($line);
array_push($excludeOptions, $line);
print_r($excludeOptions);
}
/**
* If entering an options definition block - start saving that definition
*/
if (preg_match('/\bOPTIONS_START\b/', $line)) {
$saveOption = true;
}
if (preg_match('/\bINSTANCE_OPTIONS_MAPPING_START\b/', $line)) {
$saveInstanceMappings = true;
}
if (preg_match('/\bEXCLUDED_FROM_INSTANCE_OPTIONS_START\b/', $line)) {
$saveExcludeOptions = true;
}
}
fclose($fn);
file_put_contents($file, $lines);
/**
* Process the constructor options
*/
$header = file_get_contents('src/templates/options_init_header.template');
$template = file_get_contents('src/templates/options_init.template');
$newOptions = $header;
foreach ($options as $key => $value) {
$updated = str_replace('KEY', $key, $template);
$updated = str_replace('VALUE', $value, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bOPTIONS_INIT_START\b.*?\/\/\bOPTIONS_INIT_END\b/s',
"//OPTIONS_INIT_START\n" . $newOptions . "\t\t//OPTIONS_INIT_END",
$contents
);
file_put_contents($file, $contents);
/**
* Process createInstance options
*/
$template = file_get_contents('src/templates/create_instance_options.template');
$newOptions = [];
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
array_push($newOptions, $updated);
}
if (sizeof($newOptions) > 0) {
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bCREATE_INSTANCE_OPTIONS_START\b.*?\/\/\bCREATE_INSTANCE_OPTIONS_END\b/s',
"//CREATE_INSTANCE_OPTIONS_START\n" . implode(",\n", $newOptions) . "\n\t\t\t\t//CREATE_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
}
/**
* Process updateInstance options
*/
$template = file_get_contents('src/templates/update_instance_options.template');
$newOptions = '';
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bUPDATE_INSTANCE_OPTIONS_START\b.*?\/\/\bUPDATE_INSTANCE_OPTIONS_END\b/s',
"//UPDATE_INSTANCE_OPTIONS_START\n" . $newOptions. "\t\t//UPDATE_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
/**
* Process updateFromInstance options
*/
$template = file_get_contents('src/templates/update_from_instance_options.template');
$newOptions = '';
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bUPDATE_FROM_INSTANCE_OPTIONS_START\b.*?\/\/\bUPDATE_FROM_INSTANCE_OPTIONS_END\b/s',
"//UPDATE_FROM_INSTANCE_OPTIONS_START\n" . $newOptions. "\t\t//UPDATE_FROM_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
}
exit(0);
?>