before cleanup of update_templates.php

master
Theunis J. Botha 2021-06-28 10:07:15 +02:00
parent 9107d31d22
commit c2d0155b44
3 changed files with 66 additions and 28 deletions

View File

@ -21,6 +21,12 @@ const EXTEND_CLASS = require('EXTEND_CLASS_FILE_NAME');
CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS_START
CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS_END
CUSTOM_METHODS_START
CUSTOM_METHODS_END
CUSTOM_STATIC_METHODS_START
CUSTOM_STATIC_METHODS_END
**/
class CLASS_NAME extends EXTEND_CLASS {
@ -43,6 +49,12 @@ class CLASS_NAME extends EXTEND_CLASS {
//GENERATE_DISPOSE_INSTANCE_START
//GENERATE_DISPOSE_INSTANCE_END
//GENERATE_METHODS_START
//GENERATE_METHODS_END
//GENERATE_STATIC_METHODS_START
//GENERATE_STATIC_METHODS_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END
}

View File

@ -43,6 +43,8 @@ CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD
CUSTOM_STATIC_METHODS
CUSTOM_STATIC_START_METHOD
CUSTOM_STATIC_STOP_METHOD
CUSTOM_STATIC_TEST_METHOD
CUSTOM_STOP_METHOD
CUSTOM_TEST_RENAMED_METHOD
CUSTOM_UPDATE_FROM_INSTANCE
CUSTOM_UPDATE_INSTANCE

View File

@ -3,6 +3,15 @@
include "utils.php";
function from_camel_case($input) {
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
function getIndex($needle, $haystack) {
$index = 0;
@ -374,7 +383,7 @@ function updateSection($file, $token, $updates, $separator = "") {
if (strlen($updates) <= 0) {
echo "No contents to be updated for token $token\n";
return;
return false;
}
if (substr($updates, -1) !== "\n") {
@ -389,8 +398,8 @@ function updateSection($file, $token, $updates, $separator = "") {
/**
* This file does not contain the token which requires an update - we can return here
*/
echo "Skipping token $token because it was not found\n";
return;
echo "Skipping token $token because it was not found but it could be generated soon...\n";
return $token;
}
$endToken = $token . "_END";
@ -416,6 +425,8 @@ function updateSection($file, $token, $updates, $separator = "") {
);
file_put_contents($file, $contents);
return true;
}
function generateInitOptions($file, $tokens)
@ -629,8 +640,10 @@ function doMethodUpdate($template, $tokens, $token)
$args = $matches[1];
}
$methodTokenName = strtoupper(from_camel_case($methodName));
$updated = str_replace('METHOD_ARGS', $args, $template);
$updated = str_replace('METHOD_NAME_UPPERCASE', strtoupper($methodName), $updated);
$updated = str_replace('METHOD_NAME_UPPERCASE', $methodTokenName, $updated);
$updated = str_replace('METHOD_NAME', $methodName, $updated);
$updates .= $updated;
}
@ -680,6 +693,18 @@ foreach ($files as $file) {
if ($argv[2] == 'save') {
$saveFile = $file . '.saved';
if (file_exists($saveFile)) {
echo "A previous restore operation did not complete - please remove the saved file before trying this again\n";
echo "The save file is located at $saveFile\n";
echo "Remove easily with:\n";
echo "rm $saveFile\n";
exit(1);
}
$tokens = getTokens('CUSTOM');
$result = save($file, $tokens);
@ -688,12 +713,16 @@ foreach ($files as $file) {
} else if ($argv[2] == 'restore') {
$saveFile = $file . '.saved';
$restoreTokens = getTokens('CUSTOM');
$restoreTokenKeys = array_keys($restoreTokens);
$tokens = loadSaved($file, $restoreTokens);
$skipped = [];
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store);
@ -714,36 +743,31 @@ foreach ($files as $file) {
echo `r3 update-token-db`;
$restoreTokens = getTokens('CUSTOM');
$restoreTokenKeys = array_keys($restoreTokens);
$tokens = loadSaved($file, $restoreTokens);
/**
* Try to restore the rest of the old data because now methods were generated.
* If not all data restores now - a method name / token has changed and we need
* to notify the user
*/
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store);
$result = updateSection($file, $token, $store);
if ($result !== true && $result !== false) {
array_push($skipped, $result);
}
}
}
exit(0);
// restore($file, $tokens, $saved);
//
// /**
// * At this point - we restored the headers but the method definitions are not installed
// * We can't restore them until we have expanded what those definitions are
// * i.e. -
// * 1. we need to update_methods.php (to define the placeholders)
// * 2. then we meed to restore those methods from saved[]
// */
// $methodTokens = install_method_templates($file);
//
// restore($file, $methodTokens, $saved);
//
// echo "Restored file " . $file;
// deleteSavedFile($saveFile);
if (sizeof($skipped) !== 0) {
echo "Some tokens could not be restored because they could not be found in the new version\n";
print_r($skipped);
echo "Please restore them manually from the saved file :$saveFile\n";
echo "If you do not do it now - on the next template update code will be overwritten and you could lose code!!!\n";
exit(1);
} else {
deleteSavedFile($saveFile);
exit(0);
}
} else if ($argv[2] == 'update-methods') {