From c2d0155b4411298c1ef8cde1ab1bf3c86beae009 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Mon, 28 Jun 2021 10:07:15 +0200 Subject: [PATCH] before cleanup of update_templates.php --- src/templates/extends.template | 12 +++++ src/templates/token.db | 2 + update_templates.php | 80 ++++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/templates/extends.template b/src/templates/extends.template index 04b4d57..b6ee6d0 100644 --- a/src/templates/extends.template +++ b/src/templates/extends.template @@ -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 } diff --git a/src/templates/token.db b/src/templates/token.db index 901ae92..3068cd2 100644 --- a/src/templates/token.db +++ b/src/templates/token.db @@ -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 diff --git a/update_templates.php b/update_templates.php index e8df9de..688523d 100755 --- a/update_templates.php +++ b/update_templates.php @@ -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') {