r3-v2/r3.php

2193 lines
53 KiB
PHP
Raw Normal View History

2021-06-19 11:27:32 +02:00
#!/usr/bin/php
<?php
2021-10-01 06:08:34 +02:00
echo '';
echo ' _____ __ ';echo "\n";
echo ' _____|__ / ____ / /_ ____ ';echo "\n";
echo ' / ___/ /_ < / __ \/ __ \/ __ \ ';echo "\n";
echo ' / / ___/ /_ / /_/ / / / / /_/ /';echo "\n";
echo '/_/ /____/(_) .___/_/ /_/ .___/ ';echo "\n";
echo ' /_/ /_/ ';echo "\n";
echo "\n";
2021-09-22 08:31:29 +02:00
include "vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['PWD']);
$dotenv->load();
2021-06-21 11:43:10 +02:00
include "utils.php";
include "graph.php";
2021-06-19 11:27:32 +02:00
2021-08-04 10:50:28 +02:00
global $files;
2021-06-28 10:07:15 +02:00
2021-09-22 08:31:29 +02:00
$baseExtends = 'base';
2021-09-13 09:39:38 +02:00
function getTokens($types)
2021-06-21 21:21:54 +02:00
{
2021-06-28 09:05:07 +02:00
$tokens = [];
2021-09-13 09:39:38 +02:00
foreach ($types as $type) {
2021-06-28 09:05:07 +02:00
2021-09-13 09:39:38 +02:00
$fn = fopen('src/templates/token.db', "r");
while (!feof($fn)) {
$line = fgets($fn);
$matches = [];
if (preg_match('/^(' . $type . '.*$)/', $line, $matches)) {
$tokens[$matches[1]] = [];
}
2021-06-28 09:05:07 +02:00
}
2021-09-13 09:39:38 +02:00
fclose($fn);
2021-06-28 09:05:07 +02:00
}
return $tokens;
}
/**
2021-09-13 09:39:38 +02:00
* Warning - does not do nested token extractions
2021-06-28 09:05:07 +02:00
* @throws ErrorException
*/
2021-09-13 09:39:38 +02:00
function loadSaved($file, $tokens, $extract = false) {
2021-06-28 09:05:07 +02:00
2021-09-13 09:39:38 +02:00
$saveFile = $file;
2021-06-28 09:05:07 +02:00
2021-09-13 09:39:38 +02:00
// echo "loading file " . $saveFile . "\n";
2021-06-20 08:13:06 +02:00
2021-06-28 09:05:07 +02:00
$loadedTokens = [];
2021-06-20 08:13:06 +02:00
2021-06-28 09:05:07 +02:00
$fn = fopen($saveFile, "r");
2021-06-20 08:13:06 +02:00
2021-06-28 09:05:07 +02:00
if (!$fn) {
throw new ErrorException('Could not open save file: ' . $saveFile . "\n");
}
2021-06-20 08:13:06 +02:00
2021-06-28 09:05:07 +02:00
$currentTokenKey = null;
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
while (!feof($fn)) {
$line = fgets($fn);
2021-06-28 09:05:07 +02:00
$tokenFound = false;
2021-09-13 09:39:38 +02:00
$justFound = false;
2021-06-28 09:05:07 +02:00
foreach ($tokens as $key => $store) {
2021-09-13 09:39:38 +02:00
if (
preg_match('/\b' . $key . '\b/', $line) ||
($extract && preg_match('/\b' . $key . '_START\b/', $line))
)
2021-06-28 09:05:07 +02:00
{
if (array_key_exists($key, $loadedTokens)) {
throw new ErrorException("TOKEN ALREADY EXISTS! : $key\n");
}
$tokenFound = true;
$loadedTokens[$key] = [];
$currentTokenKey = $key;
break;
}
2021-06-21 21:21:54 +02:00
}
2021-09-13 09:39:38 +02:00
if ($extract && preg_match('/\b' . $currentTokenKey . '_END\b/', $line)) {
$currentTokenKey = null;
}
2021-06-28 09:05:07 +02:00
if (!$tokenFound && $line != false) {
2021-09-13 09:39:38 +02:00
if ($currentTokenKey != null) {
array_push($loadedTokens[$currentTokenKey], $line);
}
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
}
fclose($fn);
return $loadedTokens;
}
2021-07-03 10:40:05 +02:00
function getFileData($file, &$tokens)
{
2021-06-28 09:05:07 +02:00
$currentTokens = [];
$fn = fopen($file, "r");
while (!feof($fn)) {
$line = fgets($fn);
foreach ($tokens as $key => $store) {
if (preg_match('/\b' . $key . '_END\b/', $line)) {
array_pop($currentTokens);
break;
}
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
$size = sizeof($currentTokens);
if ($size > 0) {
array_push($tokens[$currentTokens[$size - 1]], $line);
}
foreach ($tokens as $key => $store) {
if (preg_match('/\b' . $key . '_START\b/', $line)) {
array_push($currentTokens, $key);
break;
}
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
}
fclose($fn);
2021-07-03 10:40:05 +02:00
}
function save($file, $tokens) {
2021-09-13 09:39:38 +02:00
// echo "saving file " . $file . "\n";
2021-07-03 10:40:05 +02:00
getFileData($file, $tokens);
2021-06-28 09:05:07 +02:00
$saveFile =$file . '.saved';
$saved = [];
2021-06-21 10:51:57 +02:00
2021-06-28 09:05:07 +02:00
$stores = [];
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
foreach ($tokens as $key => $store) {
if (sizeof($store) > 0) {
$stores[$key] = $store;
array_push($saved, $key . "\n");
foreach ($store as $line) {
array_push($saved, $line);
}
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
}
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
file_put_contents($saveFile, $saved);
2021-09-13 09:39:38 +02:00
// echo "saved file $saveFile\n";
2021-06-28 09:05:07 +02:00
return [
$saveFile,
$stores
];
}
function deleteSavedFile($saveFile)
{
2021-07-03 10:40:05 +02:00
if ($saveFile && file_exists($saveFile)) {
2021-06-28 09:05:07 +02:00
unlink($saveFile);
} else {
2021-07-03 10:40:05 +02:00
echo "Did not unlink file $saveFile because it did not exist.\n";
2021-06-28 09:05:07 +02:00
}
}
function getWhitespace($contents, $token)
{
$matches = [];
if (preg_match('/^\n*(\s*)\b' . $token . '\b/m', $contents, $matches)) {
2021-06-28 09:05:07 +02:00
return [
'white-space' => $matches[1],
'inline-comment' => false
];
} else {
if (preg_match('/^\n*(\s*)\/\/\b' . $token . '\b/m', $contents, $matches)) {
2021-06-28 09:05:07 +02:00
return [
'white-space' => $matches[1],
'inline-comment' => true
];
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
return null;
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
}
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
function updateSection($file, $token, $updates, $separator = "") {
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
if (getType($updates) === 'array') {
$updates = implode($separator, $updates);
}
if (strlen($updates) <= 0) {
echo "No contents to be updated for token $token\n";
2021-06-28 10:07:15 +02:00
return false;
2021-06-28 09:05:07 +02:00
}
if (substr($updates, -1) !== "\n") {
$updates .= "\n";
}
$contents = file_get_contents($file);
$whiteSpaceObject = getWhitespace($contents, $token . '_END');
if ($whiteSpaceObject === null) {
/**
* This file does not contain the token which requires an update - we can return here
*/
2021-06-28 10:07:15 +02:00
echo "Skipping token $token because it was not found but it could be generated soon...\n";
return $token;
2021-06-28 09:05:07 +02:00
}
$endToken = $token . "_END";
if ($whiteSpaceObject['inline-comment']) {
$endToken = '//' . $endToken;
}
$whiteSpace = $whiteSpaceObject['white-space'];
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
$contents = preg_replace(
'/\b' . $token . '_START\b.*?\b' . $token . '_END\b/s',
$token . "_START\n" . $updates . $whiteSpace . $endToken,
$contents
);
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
file_put_contents($file, $contents);
2021-06-28 10:07:15 +02:00
return true;
2021-06-28 09:05:07 +02:00
}
2021-06-21 21:21:54 +02:00
2021-07-03 10:40:05 +02:00
function getPropertyListItems($store)
{
$propertyListItems = [];
foreach ($store as $item) {
$item = trim($item);
$result = preg_split('/=/', $item);
$propertyListItem = '- ' . $result[0]. ' (Default value ' . $result[1] . ")";
$propertyListItem = wordwrap($propertyListItem, 100, "\n ");
2021-07-03 10:40:05 +02:00
array_push($propertyListItems, $propertyListItem);
}
return $propertyListItems;
}
2021-08-04 09:01:06 +02:00
function getMethodListItems($store, $parentStore = [], $parent = null)
{
$methodListItems = [];
foreach ($store as $item) {
2021-08-04 09:01:06 +02:00
$overrides = false;
foreach ($parentStore as $parentItem) {
if (trim($parentItem) === trim($item)) {
$overrides=true;
}
}
$details = getMethodDetails($item);
2021-08-04 09:01:06 +02:00
if ($overrides === true) {
$methodListItem = '- ' . $details['methodName'] . '(' . $details['args'] . ")\n";
$methodListItem .= " " . wordwrap('Overrides for ' . $parent->nameSpace . $parent->name . '.' . $details['methodName'] . '()', 110, "\n ") . "\n";
} else {
$methodListItem = '- ' . $details['methodName'] . '(' . $details['args'] . ")\n";
$methodListItem .= " " . wordwrap($details['comment'], 110, "\n ") . "\n";
}
array_push($methodListItems, $methodListItem);
}
return $methodListItems;
}
2021-08-04 09:01:06 +02:00
function doGetInheritedTemplateUpdates($node, $restoreTokens, $inherited = true, $for)
2021-07-01 16:39:25 +02:00
{
try {
2021-07-03 10:40:05 +02:00
2021-08-04 09:01:06 +02:00
$saveFile = $node->file . '.saved';
2021-07-03 10:40:05 +02:00
if (!file_exists($saveFile)) {
2021-08-04 09:01:06 +02:00
save($node->file, $restoreTokens);
2021-07-03 10:40:05 +02:00
}
2021-09-13 09:39:38 +02:00
$tokens = loadSaved($node->file . '.saved', $restoreTokens);
2021-08-04 09:01:06 +02:00
if ($node->parent !== null) {
$parentSaveFile = $node->parent->file . '.saved';
if (!file_exists($parentSaveFile)) {
save($node->parent->file, $restoreTokens);
}
}
2021-09-13 09:39:38 +02:00
$parentTokens = loadSaved($node->parent->file . '.saved', $restoreTokens);
2021-08-04 09:01:06 +02:00
2021-07-01 16:39:25 +02:00
} catch (ErrorException $e) {
echo $e->getMessage();
2021-09-10 07:18:08 +02:00
exit(0);
2021-07-01 16:39:25 +02:00
}
$updates = '';
2021-08-04 09:01:06 +02:00
$firstTemplate = false;
2021-07-01 16:39:25 +02:00
if ($node->parent !== null && $node->parent->name !== "R3") {
2021-08-04 09:01:06 +02:00
$updates = rtrim(doGetInheritedTemplateUpdates($node->parent, $restoreTokens, true, $for)) . "\n";
} else {
$firstTemplate = true;
2021-07-01 16:39:25 +02:00
}
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_inherited.template');
2021-07-01 16:39:25 +02:00
$CLASS_NAME = $node->name;
$token = 'CUSTOM_OPTIONS';
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
2021-07-03 10:40:05 +02:00
if ($inherited) {
$PROPERTY_LIST = '<no inherited properties>';
} else {
2021-09-06 09:38:54 +02:00
$PROPERTY_LIST = '<no properties>';
}
2021-07-03 10:40:05 +02:00
} else {
$propertyListItems = getPropertyListItems($store);
$PROPERTY_LIST = implode("\n ", $propertyListItems);
}
2021-07-03 10:40:05 +02:00
$PROPERTY_LIST = trim($PROPERTY_LIST);
2021-09-06 09:38:54 +02:00
$token = 'CUSTOM_STATIC_OPTIONS';
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
if ($inherited) {
$STATIC_PROPERTY_LIST = '<no inherited static properties>';
} else {
$STATIC_PROPERTY_LIST = '<no static properties>';
}
} else {
$propertyListItems = getPropertyListItems($store);
$STATIC_PROPERTY_LIST = implode("\n ", $propertyListItems);
}
$STATIC_PROPERTY_LIST = trim($STATIC_PROPERTY_LIST);
$token = 'CUSTOM_METHODS';
$store = getTokenStore($token, $tokens);
2021-08-04 09:01:06 +02:00
$parentStore = getTokenStore($token, $parentTokens);
if (sizeof($store) <= 0) {
2021-07-03 10:40:05 +02:00
if ($inherited) {
$METHOD_LIST = '<no inherited methods>';
} else {
$METHOD_LIST = '<no methods>';
}
} else {
2021-08-04 09:01:06 +02:00
$methodListItems = getMethodListItems($store, $parentStore, $node->parent);
2021-07-03 10:40:05 +02:00
$METHOD_LIST = implode("\n ", $methodListItems);
}
2021-07-03 10:40:05 +02:00
$METHOD_LIST = trim($METHOD_LIST);
$token = 'CUSTOM_STATIC_METHODS';
$store = getTokenStore($token, $tokens);
2021-08-04 09:01:06 +02:00
$parentStore = getTokenStore($token, $parentTokens);
if (sizeof($store) <= 0) {
2021-07-03 10:40:05 +02:00
if ($inherited) {
$STATIC_METHOD_LIST = '<no inherited static methods>';
} else {
$STATIC_METHOD_LIST = '<no static methods>';
}
} else {
2021-08-04 09:01:06 +02:00
$methodListItems = getMethodListItems($store, $parentStore, $node->parent);
2021-07-03 10:40:05 +02:00
$STATIC_METHOD_LIST = implode("\n ", $methodListItems);
}
$STATIC_METHOD_LIST = trim($STATIC_METHOD_LIST);
if ($inherited) {
$description = 'Inherited from';
} else {
$description = 'Belonging to';
}
2021-07-01 16:39:25 +02:00
2021-08-04 09:01:06 +02:00
if ($firstTemplate) {
$updated = str_replace("FIRST_TEMPLATE", "Class " . $for, $template);
} else {
$updated = preg_replace('/^.*?FIRST_TEMPLATE.*\n/m', "", $template);
}
2021-09-06 09:38:54 +02:00
if ($inherited) {
$INHERITED = 'Inherited ';
} else {
$INHERITED = '';
}
$updated = str_replace('INHERITED', $INHERITED, $updated);
2021-08-04 09:01:06 +02:00
$updated = str_replace('DESCRIPTION', $description, $updated);
2021-07-03 10:40:05 +02:00
$updated = str_replace('CLASS_NAME', $CLASS_NAME, $updated);
2021-09-06 09:38:54 +02:00
$updated = preg_replace('/\bPROPERTY_LIST\b/', $PROPERTY_LIST, $updated);
$updated = preg_replace('/\bSTATIC_PROPERTY_LIST\b/', $STATIC_PROPERTY_LIST, $updated);
2021-07-01 16:39:25 +02:00
$updated = str_replace('STATIC_METHOD_LIST', $STATIC_METHOD_LIST, $updated);
$updated = str_replace('METHOD_LIST', $METHOD_LIST, $updated);
$updates .= $updated;
return $updates;
}
function generateInherited($file, $restoreTokens)
{
echo "Generating inherited fields for $file\n";
global $graph;
$node = $graph->search('file', $file);
if ($node === null) {
/**
* This node is not part of R3
*/
return;
}
if ($node->parent === null) {
/**
* This node has no inherited properties
*/
return;
}
if ($node->parent->name === 'R3') {
/**
* This is a base class
*/
return;
}
2021-08-04 09:01:06 +02:00
$updates = doGetInheritedTemplateUpdates($node, $restoreTokens, false, $node->nameSpace . $node->nameSpaceClassName);
2021-07-01 16:39:25 +02:00
2021-09-06 08:00:04 +02:00
updateSection($file, 'GENERATED_INHERITED' , $updates);
2021-07-01 16:39:25 +02:00
}
2021-09-09 13:20:20 +02:00
function generateConstructors($file, $command)
{
2021-09-13 09:39:38 +02:00
// echo $file;
2021-09-09 13:20:20 +02:00
2021-09-22 08:31:29 +02:00
global $baseExtends;
2021-09-09 18:09:09 +02:00
$constructor = null;
2021-09-09 13:20:20 +02:00
$token = 'GENERATED_CONSTRUCTOR';
2021-09-10 13:38:58 +02:00
if (preg_match('/\bsystem_base\b/', $command)) {
2021-09-09 13:20:20 +02:00
$constructor = file_get_contents('src/templates/system_base_constructor.template');
}
2021-09-10 13:38:58 +02:00
if (preg_match('/\bsystem_extends\b/', $command)) {
2021-09-09 13:20:20 +02:00
$constructor = file_get_contents('src/templates/system_extends_constructor.template');
2021-09-22 08:31:29 +02:00
$baseExtends = 'extends';
2021-09-09 13:20:20 +02:00
}
2021-09-10 13:38:58 +02:00
if (preg_match('/\bruntime_base\b/', $command)) {
$constructor = file_get_contents('src/templates/runtime_base_constructor.template');
}
if (preg_match('/\bruntime_extends\b/', $command)) {
$constructor = file_get_contents('src/templates/runtime_extends_constructor.template');
2021-09-22 08:31:29 +02:00
$baseExtends = 'extends';
2021-09-10 13:38:58 +02:00
}
if (preg_match('/\bcomponent_base\b/', $command)) {
$constructor = file_get_contents('src/templates/component_base_constructor.template');
}
if (preg_match('/\bcomponent_extends\b/', $command)) {
$constructor = file_get_contents('src/templates/component_extends_constructor.template');
2021-09-22 08:31:29 +02:00
$baseExtends = 'extends';
2021-09-10 13:38:58 +02:00
}
if (preg_match('/\bentity_base\b/', $command)) {
$constructor = file_get_contents('src/templates/entity_base_constructor.template');
}
if (preg_match('/\bentity_extends\b/', $command)) {
$constructor = file_get_contents('src/templates/entity_extends_constructor.template');
2021-09-22 08:31:29 +02:00
$baseExtends = 'extends';
2021-09-10 13:38:58 +02:00
}
2021-09-24 09:32:27 +02:00
if (preg_match('/\bobject_base\b/', $command)) {
$constructor = file_get_contents('src/templates/object_base_constructor.template');
}
if (preg_match('/\bobject_extends\b/', $command)) {
$constructor = file_get_contents('src/templates/object_extends_constructor.template');
$baseExtends = 'extends';
}
2021-09-10 13:38:58 +02:00
if (preg_match('/\bbase\b/', $command)) {
$constructor = file_get_contents('src/templates/base_constructor.template');
2021-09-09 13:20:20 +02:00
}
if (preg_match('/\bextends\b/', $command)) {
2021-09-10 13:38:58 +02:00
$constructor = file_get_contents('src/templates/extends_constructor.template');
2021-09-22 08:31:29 +02:00
$baseExtends = 'extends';
2021-09-09 13:20:20 +02:00
}
2021-09-09 18:09:09 +02:00
if (!$constructor) {
2021-09-10 09:40:19 +02:00
echo 'No constructor found for : ' . $file;
2021-09-10 07:18:08 +02:00
return;
2021-09-09 18:09:09 +02:00
}
2021-09-09 13:20:20 +02:00
updateSection($file, $token , $constructor);
}
2021-09-13 09:39:38 +02:00
function extractOption($item, $template)
2021-06-28 09:05:07 +02:00
{
2021-09-13 09:39:38 +02:00
$item = trim($item);
2021-09-19 21:28:53 +02:00
$matches = [];
$key_value = preg_match('/^(\s*\w+)=(.*)$/', $item, $matches);
2021-09-13 09:39:38 +02:00
if ($key_value === false) {
return '';
}
2021-09-19 21:28:53 +02:00
$key = $matches[1];
$value = $matches[2];
2021-09-13 09:39:38 +02:00
$comment_value = preg_split('/\s+-\s+/', $value);
$comment = '';
if ($comment_value != false) {
$value = $comment_value[0];
if (array_key_exists(1, $comment_value)) {
$comment = $comment_value[1];
} else {
$comment = 'No comment';
}
2021-09-14 06:13:26 +02:00
$numSpaces = 1;
$matches = [];
$hasMultilineComment = preg_match('/\n^([ ]+)\*/sm', $template, $matches);
if ($hasMultilineComment) {
$str = $matches[1];
$numSpaces = strlen($str);
}
$comment = wordwrap($comment, 95, "\n" . str_pad('', $numSpaces, ' ', STR_PAD_LEFT) . "* ");
2021-09-13 09:39:38 +02:00
}
$updated = str_replace('COMMENT', $comment, $template);
$updated = str_replace('KEY', $key, $updated);
$updated = str_replace('VALUE', $value, $updated);
return $updated;
}
2021-09-22 14:43:33 +02:00
function generateRequiredComponents($file, $tokens, $token, $section)
{
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
$template = file_get_contents('src/templates/generated_required_components.template');
$updates = '';
foreach ($store as $item) {
$updates .= extractOption($item, $template);
}
updateSection($file, $section , $updates);
}
function stripRequiredComponents(&$store, $tokens)
{
$excluded = getTokenStore('CUSTOM_REQUIRED_COMPONENTS', $tokens);
if (sizeof($excluded) >= 0) {
$newStore = [];
$excludedIndexes = [];
foreach ($excluded as $excludedItem) {
$excludedItem = trim($excludedItem);
$matches = [];
$key_value = preg_match('/^(\s*\w+)=(.*)$/', $excludedItem, $matches);
if ($key_value === false) {
return '';
}
$excludedKey = $matches[1];
for ($i = 0; $i < sizeof($store); $i++) {
$item = trim($store[$i]);
$matches = [];
$key_value = preg_match('/^(\s*\w+)=(.*)$/', $item, $matches);
if ($key_value === false) {
continue;
}
$actualKey = $matches[1];
if ($actualKey === $excludedKey) {
array_push($excludedIndexes, $i);
}
}
}
for ($i = 0; $i < sizeof($store); $i++) {
if (!in_array($i, $excludedIndexes)){
array_push($newStore, $store[$i]);
}
}
$store = $newStore;
}
}
2021-09-13 09:39:38 +02:00
function generateInitOptions($file, $tokens, $token, $section)
{
2021-06-28 09:05:07 +02:00
$store = getTokenStore($token, $tokens);
// stripRequiredComponents($store, $tokens);
2021-06-28 09:05:07 +02:00
if (sizeof($store) <= 0) {
return;
}
2021-09-13 09:39:38 +02:00
// echo "Will be building options for $token\n";
2021-06-21 21:21:54 +02:00
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_custom_options_init.template');
2021-06-28 09:05:07 +02:00
2021-08-04 10:50:28 +02:00
$updates = '';
2021-06-28 09:05:07 +02:00
foreach ($store as $item) {
2021-09-13 09:39:38 +02:00
$updates .= extractOption($item, $template);
2021-06-28 09:05:07 +02:00
}
2021-09-13 09:39:38 +02:00
updateSection($file, $section , $updates);
2021-06-28 09:05:07 +02:00
}
2021-09-13 09:39:38 +02:00
function generateInitStaticOptions($file, $tokens, $token, $section)
2021-09-06 09:02:55 +02:00
{
2021-09-13 09:39:38 +02:00
2021-09-06 09:02:55 +02:00
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
2021-09-13 09:39:38 +02:00
// echo "Will be building static options for $token\n";
2021-09-06 09:02:55 +02:00
$template = file_get_contents('src/templates/generated_custom_static_options_init.template');
$updates = '';
foreach ($store as $item) {
2021-09-13 09:39:38 +02:00
$updates .= extractOption($item, $template);
2021-09-06 09:02:55 +02:00
}
2021-09-13 09:39:38 +02:00
updateSection($file, $section , $updates);
2021-09-06 09:02:55 +02:00
}
2021-06-28 09:05:07 +02:00
function getTokenStore($tokenName, $tokens)
{
2021-08-04 09:01:06 +02:00
if (is_array($tokens) && array_key_exists($tokenName, $tokens)) {
2021-06-28 09:05:07 +02:00
return $tokens[$tokenName];
}
return [];
}
2021-09-13 09:39:38 +02:00
function getInstanceMappings($tokens, $token)
2021-06-28 09:05:07 +02:00
{
$instanceMappings = [];
2021-06-21 21:21:54 +02:00
2021-09-13 09:39:38 +02:00
foreach (getTokenStore($token, $tokens) as $mapping) {
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
$mapping = trim($mapping);
2021-06-21 21:21:54 +02:00
2021-06-28 09:05:07 +02:00
$key_value = preg_split('/=/', $mapping);
if ($key_value === false) {
continue;
}
$key = $key_value[0];
$value = $key_value[1];
$instanceMappings[$key] = $value;
}
return $instanceMappings;
}
2021-09-13 09:39:38 +02:00
function isExcluded($key, $tokens, $excludedToken)
2021-06-28 09:05:07 +02:00
{
2021-09-13 09:39:38 +02:00
if (array_key_exists($excludedToken, $tokens)) {
2021-06-28 09:05:07 +02:00
$excluded = [];
2021-09-13 09:39:38 +02:00
foreach ($tokens[$excludedToken] as $exclude) {
2021-06-28 09:05:07 +02:00
array_push($excluded, trim($exclude));
}
if (in_array($key, $excluded)) {
return true;
}
}
return false;
}
2021-09-13 09:39:38 +02:00
function doInstanceUpdate($template, $tokens, $token, $mappingToken, $excludedToken)
2021-06-28 09:05:07 +02:00
{
2021-09-13 09:39:38 +02:00
2021-06-28 09:05:07 +02:00
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return null;
}
2021-09-13 09:39:38 +02:00
$instanceMappings = getInstanceMappings($tokens, $mappingToken);
2021-06-28 09:05:07 +02:00
$updates = '';
foreach ($store as $item) {
$item = trim($item);
$key_value = preg_split('/=/', $item);
if ($key_value === false) {
continue;
}
$key = $key_value[0];
$value = $key_value[0];
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
}
2021-09-13 09:39:38 +02:00
if (isExcluded($key, $tokens, $excludedToken)){
2021-06-28 09:05:07 +02:00
continue;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
2021-09-13 09:39:38 +02:00
$updates .= $updated;
2021-06-21 21:21:54 +02:00
}
2021-06-28 09:05:07 +02:00
return $updates;
}
/**
* Process updateInstance options
*/
2021-09-13 09:39:38 +02:00
function generateUpdateInstanceOptions($file, $tokens, $token, $section, $mappingToken, $excludedToken)
2021-06-28 09:05:07 +02:00
{
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_update_instance_options.template');
2021-06-28 09:05:07 +02:00
2021-09-13 09:39:38 +02:00
$updates = doInstanceUpdate($template, $tokens, $token, $mappingToken, $excludedToken);
2021-06-28 09:05:07 +02:00
if ($updates) {
2021-09-13 09:39:38 +02:00
// echo "Updating update instance options\n";
updateSection($file, $section, $updates);
2021-06-28 09:05:07 +02:00
} else {
2021-09-13 09:39:38 +02:00
// echo "No update instance options found to generate\n";
2021-06-28 09:05:07 +02:00
}
}
/**
* Process updateFromInstance options
*/
2021-09-13 09:39:38 +02:00
function generateUpdateFromInstanceOptions($file, $tokens, $token, $section, $mappingToken, $excludedToken)
2021-06-28 09:05:07 +02:00
{
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_update_from_instance_options.template');
2021-06-28 09:05:07 +02:00
2021-09-13 09:39:38 +02:00
$updates = doInstanceUpdate($template, $tokens, $token, $mappingToken, $excludedToken);
2021-06-28 09:05:07 +02:00
if ($updates) {
2021-09-13 09:39:38 +02:00
// echo "Updating update from instance options\n";
updateSection($file, $section, $updates);
2021-06-28 09:05:07 +02:00
} else {
2021-09-13 09:39:38 +02:00
// echo "No update from instance options found to generate\n";
2021-06-28 09:05:07 +02:00
}
}
2021-09-19 21:28:53 +02:00
function getEventListenerInfo($item)
{
$item = trim($item);
$eventName = preg_replace('/Event./', '', $item);
$returns = null;
$comment = null;
$values = preg_split('/\s*-\s*/', $eventName);
if (sizeof($values) > 1) {
$eventName = $values[0];
$comment = $values[1];
}
$values = preg_split('/@return[s]*\s*/', $comment);
if (sizeof($values) > 1) {
$comment = $values[0];
$returns = $values[1];
}
$methodName = 'ON_'.$eventName;
$methodTokenName = $methodName;
$methodName = to_camel_case_from_upper_underscore($methodName);
return [
'eventName' => $eventName,
'fullEventName' => 'Event.'.$eventName,
2021-09-19 21:28:53 +02:00
'returns' => $returns,
'comment' => $comment,
'methodName' => $methodName,
'methodTokenName' => $methodTokenName
];
}
2021-07-12 12:02:59 +02:00
function generateEventListenersStart($file, $tokens)
{
$token = 'CUSTOM_EVENT_LISTENERS';
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
echo "Will be building events for $file\n";
$template = file_get_contents('src/templates/generated_event_listeners_start.template');
$updated = '';
foreach ($store as $item) {
2021-09-19 21:28:53 +02:00
$info = getEventListenerInfo($item);
$updates = str_replace('FULL_EVENT_NAME', $info['fullEventName'], $template);
2021-09-19 21:28:53 +02:00
$updates = str_replace('EVENT_NAME', $info['eventName'], $updates);
$updates = str_replace('CALL_BACK', $info['methodName'], $updates);
$updated .= $updates;
}
updateSection($file, 'GENERATED_EVENT_LISTENERS_START' , $updated);
}
function generateEventListenersStop($file, $tokens)
{
$token = 'CUSTOM_EVENT_LISTENERS';
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
echo "Will be events for $file\n";
$template = file_get_contents('src/templates/generated_event_listeners_stop.template');
$updated = '';
foreach ($store as $item) {
2021-09-19 21:28:53 +02:00
$info = getEventListenerInfo($item);
2021-09-07 06:01:07 +02:00
2021-09-19 21:28:53 +02:00
$updates = str_replace('EVENT_NAME', $info['eventName'], $template);
$updated .= $updates;
}
updateSection($file, 'GENERATED_EVENT_LISTENERS_STOP' , $updated);
}
function generateStaticEventListenersStart($file, $tokens)
2021-07-12 12:02:59 +02:00
{
2021-09-06 12:11:06 +02:00
$token = 'CUSTOM_STATIC_EVENT_LISTENERS';
2021-07-12 12:02:59 +02:00
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
echo "Will be building events for $file\n";
2021-09-06 12:11:06 +02:00
$template = file_get_contents('src/templates/generated_static_event_listeners_start.template');
2021-07-12 12:02:59 +02:00
$updated = '';
foreach ($store as $item) {
2021-09-19 21:28:53 +02:00
$info = getEventListenerInfo($item);
2021-07-12 12:02:59 +02:00
$updates = str_replace('FULL_EVENT_NAME', $info['fullEventName'], $template);
2021-09-19 21:28:53 +02:00
$updates = str_replace('EVENT_NAME', $info['eventName'], $updates);
$updates = str_replace('CALL_BACK', $info['methodName'], $updates);
2021-07-12 12:02:59 +02:00
$updated .= $updates;
}
2021-09-06 12:11:06 +02:00
updateSection($file, 'GENERATED_STATIC_EVENT_LISTENERS_START' , $updated);
2021-07-12 12:02:59 +02:00
}
function generateStaticEventListenersStop($file, $tokens)
2021-07-12 12:02:59 +02:00
{
2021-09-06 12:11:06 +02:00
$token = 'CUSTOM_STATIC_EVENT_LISTENERS';
2021-07-12 12:02:59 +02:00
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
echo "Will be events for $file\n";
2021-09-06 12:11:06 +02:00
$template = file_get_contents('src/templates/generated_static_event_listeners_stop.template');
2021-07-12 12:02:59 +02:00
$updated = '';
foreach ($store as $item) {
2021-09-19 21:28:53 +02:00
$info = getEventListenerInfo($item);
2021-09-07 06:01:07 +02:00
2021-09-19 21:28:53 +02:00
$updates = str_replace('EVENT_NAME', $info['eventName'], $template);
2021-07-12 12:02:59 +02:00
$updated .= $updates;
}
2021-09-06 12:11:06 +02:00
updateSection($file, 'GENERATED_STATIC_EVENT_LISTENERS_STOP' , $updated);
2021-07-12 12:02:59 +02:00
}
2021-07-03 10:40:05 +02:00
/**
* @param $item
* @return array
*/
function getMethodDetails($item)
2021-06-28 09:05:07 +02:00
{
$item = trim($item);
2021-06-28 09:05:07 +02:00
$methodName = $item;
2021-06-28 09:05:07 +02:00
$matches = [];
$result = preg_match('/^(.*?)\(/', $item, $matches);
2021-06-28 09:05:07 +02:00
if ($result !== false && sizeof($matches) >= 2) {
$methodName = $matches[1];
}
$args = '';
$result = preg_match('/\((.*?)\)/', $item, $matches);
2021-06-28 09:05:07 +02:00
if ($result !== false && sizeof($matches) >= 2) {
$args = $matches[1];
}
2021-06-28 09:05:07 +02:00
$argsArray = preg_split('/,(\s*)/', $args);
2021-06-28 09:05:07 +02:00
$comment = 'No comment';
2021-06-28 09:05:07 +02:00
$result = preg_match('/.*?\).*?(\w.*$)/', $item, $matches);
if ($result !== false && sizeof($matches) >= 2) {
$comment = $matches[1];
}
2021-06-28 09:05:07 +02:00
$returns = preg_split('/@return[s]*\s*/', $comment);
2021-06-28 09:05:07 +02:00
if (sizeof($returns) > 1) {
$comment = $returns[0];
$returns = $returns[1];
} else {
$returns = null;
}
2021-06-28 09:05:07 +02:00
$methodTokenName = strtoupper(from_camel_case($methodName));
return [
'methodName' => $methodName,
'methodTokenName' => $methodTokenName,
'args' => $args,
'argsArray' => $argsArray,
'comment' => $comment,
'returns' => $returns
];
}
2021-09-22 13:56:43 +02:00
function getMethodTemplate($token, &$updated, $methodTokenName)
{
global $baseExtends;
$staticNormal = '';
if ($token === 'CUSTOM_STATIC_METHODS' || $token === 'TEMPLATE_STATIC_METHODS') {
$staticNormal = 'static_';
}
$templateFound = false;
/**
* Do before function
*/
$potentialTemplate = strtolower('src/templates/' . $staticNormal . $baseExtends . '_' . $methodTokenName . '.template');
if (file_exists($potentialTemplate)) {
$templateFound = true;
}
if (!$templateFound) {
$potentialTemplate = strtolower('src/templates/' . $staticNormal . $methodTokenName . '.template');
}
if (file_exists($potentialTemplate)) {
$templateFound = true;
}
if ($templateFound) {
$functionTemplate = file_get_contents($potentialTemplate);
$updated = preg_replace('/^\s*\bFUNCTION_TEMPLATE\b/m', $functionTemplate, $updated);
} else {
$updated = preg_replace('/^.*?\bFUNCTION_TEMPLATE\b.*\n/m', '', $updated);
}
$templateFound = false;
/**
* Do after function
*/
$potentialTemplate = strtolower('src/templates/' . $staticNormal . $baseExtends . '_' . $methodTokenName . '_after.template');
if (file_exists($potentialTemplate)) {
$templateFound = true;
}
if (!$templateFound) {
$potentialTemplate = strtolower('src/templates/' . $staticNormal . $methodTokenName . '_after.template');
}
if (file_exists($potentialTemplate)) {
$templateFound = true;
}
if ($templateFound) {
$functionTemplate = file_get_contents($potentialTemplate);
$updated = preg_replace('/^\s*\bFUNCTION_TEMPLATE_AFTER\b/m', $functionTemplate, $updated);
} else {
$updated = preg_replace('/^.*?\bFUNCTION_TEMPLATE_AFTER\b.*\n/m', '', $updated);
}
}
/**
* @param $template
* @param $tokens
* @param $token
* @return string|null
*/
function doMethodUpdate($template, $tokens, $token)
{
2021-09-22 08:31:29 +02:00
global $baseExtends;
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return null;
}
2021-06-29 11:09:25 +02:00
$updates = '';
foreach ($store as $item) {
$detail = getMethodDetails($item);
$argsArray = $detail['argsArray'];
$args = $detail['args'];
$methodTokenName = $detail['methodTokenName'];
$methodName = $detail['methodName'];
$comment = $detail['comment'];
$returns = $detail['returns'];
2021-06-28 10:07:15 +02:00
if (sizeof($argsArray) > 1) {
$args = implode(",\n ", $argsArray);
$args = "\n " . $args . "\n ";
$params = implode("\n * @param ", $argsArray);
2021-07-03 10:40:05 +02:00
$params = "\n * @param " . $params;
} else if (sizeof($argsArray) === 1) {
if ($args === '') {
$params = $args;
} else {
$params = "\n * @param " . $args;
}
}
if ($returns !== null) {
2021-07-12 12:02:59 +02:00
$returns = "\n * @returns " . $returns;
}
$comment = wordwrap($comment, 110, "\n * ");
2021-07-03 10:40:05 +02:00
$updated = $template;
2021-09-22 13:56:43 +02:00
getMethodTemplate($token, $updated, $methodTokenName);
2021-07-03 10:40:05 +02:00
$updated = str_replace('METHOD_ARGS', $args, $updated);
2021-06-28 10:07:15 +02:00
$updated = str_replace('METHOD_NAME_UPPERCASE', $methodTokenName, $updated);
2021-06-28 09:05:07 +02:00
$updated = str_replace('METHOD_NAME', $methodName, $updated);
$updated = str_replace('COMMENT', $comment, $updated);
$updated = str_replace('PARAMS', $params, $updated);
$updated = str_replace('RETURNS', $returns, $updated);
2021-06-28 09:05:07 +02:00
$updates .= $updated;
}
return $updates;
}
2021-09-13 09:39:38 +02:00
function generateStaticMethods($file, $tokens, $token, $section)
2021-06-28 09:05:07 +02:00
{
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_static_methods.template');
2021-06-28 09:05:07 +02:00
$updates = doMethodUpdate($template, $tokens, $token);
if ($updates) {
2021-09-13 09:39:38 +02:00
// echo "Updating static methods\n";
updateSection($file, $section, $updates);
2021-06-28 09:05:07 +02:00
} else {
2021-09-13 09:39:38 +02:00
// echo "No static methods found to generate\n";
2021-06-28 09:05:07 +02:00
}
}
2021-07-12 12:02:59 +02:00
function getEventListenerUpdates($template, $tokens, $token)
{
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return null;
}
$updates = '';
foreach ($store as $item) {
$item = trim($item);
$eventName = preg_replace('/Event./', '', $item);
$methodName = 'ON_'.$eventName;
$methodTokenName = $methodName;
$methodName = to_camel_case_from_upper_underscore($methodName);
$updated = $template;
2021-07-12 12:02:59 +02:00
$methodArgs = 'data';
$params = "\n * @param " . $methodArgs . " (The event data passed as argument - typically an R3Object)";
$returns = "\n * @return null";
$potentialTemplate = strtolower('src/templates/' . $methodTokenName . '.template');
if (file_exists($potentialTemplate)) {
$functionTemplate = file_get_contents($potentialTemplate);
$updated = preg_replace('/^\s*FUNCTION_TEMPLATE/m', $functionTemplate, $updated);
} else {
$updated = preg_replace('/^.*?FUNCTION_TEMPLATE.*\n/m', '', $updated);
}
$updated = str_replace('METHOD_NAME_UPPERCASE', $methodTokenName, $updated);
$updated = str_replace('METHOD_NAME', $methodName, $updated);
$updated = str_replace('METHOD_ARGS', $methodArgs, $updated);
$comment = 'Listens to events of type ' . $item . ' and executes this function.';
$comment = wordwrap($comment, 110, "\n * ");
$updated = str_replace('COMMENT', $comment, $updated);
$updated = str_replace('PARAMS', $params, $updated);
$updated = str_replace('RETURNS', $returns, $updated);
$updates .= $updated;
}
return $updates;
}
function getStaticEventListenerUpdates($template, $tokens, $token)
{
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return null;
}
$updates = '';
foreach ($store as $item) {
2021-07-12 12:02:59 +02:00
2021-09-19 21:28:53 +02:00
$info = getEventListenerInfo($item);
2021-07-12 12:02:59 +02:00
$updated = $template;
2021-09-08 08:25:16 +02:00
$methodArgs = 'object';
2021-07-12 12:02:59 +02:00
$params = "\n * @param " . $methodArgs . " (The event data passed as argument - typically an R3Object)";
2021-09-19 21:28:53 +02:00
$returns = "\n * @return " . $info['returns']?:'null';
2021-07-12 12:02:59 +02:00
2021-09-19 21:28:53 +02:00
$potentialTemplate = strtolower('src/templates/static_' . $info['methodTokenName'] . '.template');
2021-07-12 12:02:59 +02:00
if (file_exists($potentialTemplate)) {
$functionTemplate = file_get_contents($potentialTemplate);
$updated = preg_replace('/^\s*FUNCTION_TEMPLATE/m', $functionTemplate, $updated);
} else {
$updated = preg_replace('/^.*?FUNCTION_TEMPLATE.*\n/m', '', $updated);
}
2021-09-19 21:28:53 +02:00
$updated = str_replace('METHOD_NAME_UPPERCASE', $info['methodTokenName'], $updated);
$updated = str_replace('METHOD_NAME', $info['methodName'], $updated);
2021-07-12 12:02:59 +02:00
$updated = str_replace('METHOD_ARGS', $methodArgs, $updated);
$comment = 'Listens to events of type ' . $info['fullEventName'] . ' and executes this function. ' . $info['comment'];
2021-07-12 12:02:59 +02:00
$comment = wordwrap($comment, 110, "\n * ");
2021-09-19 21:28:53 +02:00
$returns = wordwrap($returns, 110, "\n * ");
2021-07-12 12:02:59 +02:00
$updated = str_replace('COMMENT', $comment, $updated);
$updated = str_replace('PARAMS', $params, $updated);
$updated = str_replace('RETURNS', $returns, $updated);
$updates .= $updated;
}
return $updates;
}
function generateEventListenerMethods($file, $tokens)
{
$token = 'CUSTOM_EVENT_LISTENERS';
$template = file_get_contents('src/templates/generated_methods.template');
$updates = getEventListenerUpdates($template, $tokens, $token);
if ($updates) {
2021-09-13 09:39:38 +02:00
// echo "Updating event listeners.. \n";
updateSection($file, 'GENERATED_EVENT_LISTENER_METHODS', $updates);
} else {
2021-09-13 09:39:38 +02:00
// echo "No event listeners found to generate\n";
}
}
2021-07-12 12:02:59 +02:00
function generateStaticEventListenerMethods($file, $tokens)
{
2021-09-06 12:11:06 +02:00
$token = 'CUSTOM_STATIC_EVENT_LISTENERS';
2021-07-12 12:02:59 +02:00
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_static_methods.template');
2021-07-12 12:02:59 +02:00
$updates = getStaticEventListenerUpdates($template, $tokens, $token);
2021-07-12 12:02:59 +02:00
if ($updates) {
2021-09-13 09:39:38 +02:00
// echo "Updating static event listeners.. \n";
2021-09-06 08:00:04 +02:00
updateSection($file, 'GENERATED_STATIC_EVENT_LISTENER_METHODS', $updates);
2021-07-12 12:02:59 +02:00
} else {
2021-09-13 09:39:38 +02:00
// echo "No static event listeners found to generate\n";
2021-07-12 12:02:59 +02:00
}
}
2021-10-01 12:40:04 +02:00
function generateImports($file, $tokens, $command)
{
$updates = [];
if (
array_key_exists('CUSTOM_REQUIRED_COMPONENTS', $tokens) &&
sizeof($tokens['CUSTOM_REQUIRED_COMPONENTS']) > 0
) {
array_push($updates, "const Event = require('../r3-event.js');\n");
}
if (
array_key_exists('TEMPLATE_METHODS', $tokens) &&
sizeof($tokens['TEMPLATE_METHODS']) > 0
) {
foreach ($tokens['TEMPLATE_METHODS'] as $method) {
if (preg_match('/initialize\(\)/', $method)) {
if (!preg_match('/ Component/', $command)) {
array_push($updates, "const Component = require('../r3-component/r3-component.js');\n");
}
if (!preg_match('/ Entity /', $command)) {
array_push($updates, "const Entity = require('../r3-entity/r3-entity.js');\n");
}
}
}
}
if (sizeof($updates) === 0) {
return;
}
updateSection($file, 'GENERATED_IMPORTS', $updates);
}
2021-09-13 09:39:38 +02:00
function generateMethods($file, $tokens, $token, $section)
2021-06-28 09:05:07 +02:00
{
2021-09-06 08:00:04 +02:00
$template = file_get_contents('src/templates/generated_methods.template');
2021-06-28 09:05:07 +02:00
$updates = doMethodUpdate($template, $tokens, $token);
if ($updates) {
2021-09-13 09:39:38 +02:00
updateSection($file, $section, $updates);
2021-06-28 09:05:07 +02:00
} else {
2021-09-13 09:39:38 +02:00
// echo "No methods found to generate\n";
2021-06-28 09:05:07 +02:00
}
2021-06-21 21:21:54 +02:00
}
2021-10-01 12:40:04 +02:00
function buildImportsAndBody(&$imports, &$body, &$tests, &$testImports, $node, $type, $nameSpace)
2021-09-18 11:06:19 +02:00
{
$originalNameSpace = $nameSpace;
foreach ($node->children as $child) {
$nameSpace = $originalNameSpace . '.' . $child->nameSpaceClassName;
2021-10-01 06:08:34 +02:00
$file = str_replace('src/r3/r3-' . str_replace('r3-', '', strtolower(from_camel_case($type,'-'))), '.', $child->file);
2021-10-01 12:40:04 +02:00
$important = true;
if (preg_match('/src\/r3/', $file)) {
$important = false;
$file = preg_replace('/src\/r3/','..',$file);
}
2021-09-18 11:06:19 +02:00
array_push($imports, "const " . $child->name . ' = require(\'' . $file . "');");
array_push($body, "$nameSpace = " . $child->name . ';');
2021-10-01 12:40:04 +02:00
if ($important) {
array_push($testImports, "const " . $child->name . ' = require(\'../src/r3/r3-' . str_replace('r3object','object', strtolower($type)) . '/' . $file . "');");
array_push($tests, "\t\t\tconst " . lcfirst($child->name) . " = new " . $child->name . '();');
}
2021-09-18 11:06:19 +02:00
$child->nameSpace = $nameSpace;
2021-10-01 12:40:04 +02:00
buildImportsAndBody($imports, $body, $tests, $testImports, $child, $type, $nameSpace);
2021-09-18 11:06:19 +02:00
}
}
function generateIndex($types)
{
/**
* Graph $graph
*/
global $graph;
2021-09-18 08:52:21 +02:00
$template = file_get_contents('src/templates/index.template');
2021-09-18 08:52:21 +02:00
foreach ($types as $type) {
$imports = [];
2021-10-01 12:40:04 +02:00
$testImports = [];
2021-09-08 06:41:00 +02:00
$body = [];
$exports = [];
2021-10-01 12:40:04 +02:00
$tests = [];
2021-09-18 11:06:19 +02:00
$nodes = $graph->walk();
2021-09-18 08:52:21 +02:00
2021-09-18 11:06:19 +02:00
foreach ($nodes as $node) {
2021-09-18 08:52:21 +02:00
2021-09-18 11:06:19 +02:00
if (preg_match("/\b$type\b/", $node->name)) {
2021-09-18 08:52:21 +02:00
2021-10-01 06:08:34 +02:00
$file = str_replace('src/r3/r3-' . str_replace('r3-', '', strtolower(from_camel_case($type,'-'))), '.', $node->file);
2021-09-18 08:52:21 +02:00
2021-09-18 11:06:19 +02:00
array_push($imports, "const " . $node->name . ' = require(\'' . $file . "');");
2021-09-08 06:41:00 +02:00
2021-10-01 12:40:04 +02:00
buildImportsAndBody($imports, $body, $tests, $testImports,$node, $type, $type);
2021-09-18 08:52:21 +02:00
}
}
2021-09-08 06:41:00 +02:00
2021-09-18 11:06:19 +02:00
array_push($exports, "module.exports = $type;");
2021-10-01 06:08:34 +02:00
$indexFile = 'src/r3/r3-'. str_replace('r3-', '', strtolower(from_camel_case($type,'-'))) . '/index.js';
2021-09-07 09:08:37 +02:00
file_put_contents($indexFile, $template);
2021-09-06 08:00:04 +02:00
updateSection($indexFile, 'GENERATED_IMPORTS', implode("\n", $imports));
2021-09-08 06:41:00 +02:00
updateSection($indexFile, 'GENERATED_INDEX_BODY', implode("\n", $body));
updateSection($indexFile, 'GENERATED_EXPORTS', implode("\n", $exports));
2021-10-01 12:40:04 +02:00
$testFile = 'test/R3.test.js';
updateSection($testFile, 'GENERATED_' . to_upper_underscore($type) . '_CREATE', implode("\n", $tests));
updateSection($testFile, 'GENERATED_' . to_upper_underscore($type) . '_IMPORTS', implode("\n", $testImports));
}
}
2021-09-07 07:58:29 +02:00
function generateR3($nodes, $graph)
2021-07-04 08:24:20 +02:00
{
$imports = [];
$defines = [];
2021-09-10 09:40:19 +02:00
$modifiedPaths = [
'/\br3-component.js$/',
'/\br3-entity.js$/',
2021-10-01 06:08:34 +02:00
'/\br3-object.js$/',
2021-09-10 09:40:19 +02:00
'/\br3-runtime.js$/',
'/\br3-system.js$/'
];
2021-07-04 08:24:20 +02:00
foreach ($nodes as $node) {
2021-09-07 07:58:29 +02:00
2021-09-08 06:41:00 +02:00
if (
2021-10-01 08:36:52 +02:00
$node->parent === null ||
2021-09-08 06:41:00 +02:00
$node->parent->parent === null || //i.e. is a base class
$node->parent->name === 'Event' || //i.e. extends Event directly
$node->parent->name === 'R3Object' //i.e. extends Object directly
) {
$file = str_replace('src/r3', '.', $node->file);
2021-09-10 09:40:19 +02:00
foreach ($modifiedPaths as $modifiedPath) {
$file = preg_replace($modifiedPath, '', $file);
}
2021-10-01 08:36:52 +02:00
array_push($imports, "const " . $node->name . " = require('" . $file . "');");
if ($node->parent !== null) {
array_push($defines, 'R3.' . $node->nameSpaceClassName . ' = ' . $node->name);
}
2021-07-04 20:32:41 +02:00
}
2021-09-07 07:58:29 +02:00
2021-07-04 08:24:20 +02:00
}
2021-09-08 08:25:16 +02:00
$component = $graph->search('name', 'Component');
$children = $graph->flatten($component);
foreach ($children as $child) {
2021-09-18 11:06:19 +02:00
if (sizeof($child->children) === 0) {
array_push($defines, 'R3.' . $child->nameSpaceClassName . ' = ' . $child->nameSpace);
}
2021-09-08 08:25:16 +02:00
}
2021-10-01 08:36:52 +02:00
$r3Index = 'src/r3/index.js';
2021-07-04 08:24:20 +02:00
2021-10-01 08:36:52 +02:00
save($r3Index, getTokens(['CUSTOM']));
2021-07-04 08:24:20 +02:00
2021-10-01 08:36:52 +02:00
$tokens = loadSaved($r3Index . '.saved', getTokens(['CUSTOM']));
2021-07-04 08:24:20 +02:00
2021-10-01 08:36:52 +02:00
$template = file_get_contents('src/templates/r3_index.template');
2021-07-04 08:24:20 +02:00
2021-10-01 08:36:52 +02:00
file_put_contents($r3Index, $template);
2021-07-04 20:32:41 +02:00
2021-10-01 09:39:42 +02:00
deleteSavedFile($r3Index . '.saved');
2021-07-04 20:32:41 +02:00
2021-10-01 08:36:52 +02:00
$version = file_get_contents('version');
2021-07-04 08:24:20 +02:00
2021-07-04 20:32:41 +02:00
$packageJson = file_get_contents('package.json');
$packageJson = preg_replace('/(.*version.*: ").*(".*)/', '${1}' . $version . '${2}', $packageJson);
file_put_contents('package.json', $packageJson);
2021-10-01 08:36:52 +02:00
$r3js = 'src/r3/r3.js';
$contents = file_get_contents($r3js);
$contents = str_replace(' = \'__COMPILE_DATE__\'', ' = \'' . date("Y M d - H:i:s a") . '\'', $contents);
$contents = str_replace(' = \'__VERSION__\'', ' = \'' . $version . '\'', $contents);
file_put_contents($r3js, $contents);
2021-07-04 20:32:41 +02:00
2021-09-08 06:41:00 +02:00
$exports = 'module.exports = R3;';
2021-07-04 20:32:41 +02:00
2021-10-01 08:36:52 +02:00
$systemBody = [];
2021-07-05 10:21:36 +02:00
2021-07-04 20:32:41 +02:00
foreach ($nodes as $node) {
/**
* Node $node
*/
if (preg_match('/\bSystem\w+\b/', $node->name)){
2021-10-01 08:36:52 +02:00
array_push($systemBody, 'R3.' . $node->nameSpace . ".Start();");
2021-07-04 20:32:41 +02:00
}
}
2021-10-01 08:36:52 +02:00
if (in_array('CUSTOM_DEFINES', array_keys($tokens))) {
updateSection($r3Index, 'CUSTOM_DEFINES', $tokens['CUSTOM_DEFINES']);
}
updateSection($r3Index, 'GENERATED_IMPORTS', implode("\n", $imports));
updateSection($r3Index, 'GENERATED_DEFINES', implode(";\n", $defines) . ";");
updateSection($r3Index, 'GENERATED_SYSTEM', implode("\n", $systemBody));
updateSection($r3Index, 'GENERATED_EXPORTS', $exports);
2021-07-04 20:32:41 +02:00
2021-07-05 10:21:36 +02:00
}
2021-08-04 10:50:28 +02:00
function generateEvents()
{
2021-09-13 09:39:38 +02:00
echo "Generating Events\n";
2021-08-04 10:50:28 +02:00
global $files;
$events = [];
foreach ($files as $file) {
// $file = './src/r3/' . $file;
// echo $file . "\n";
// continue;
if (
preg_match('/\.js$/', $file) &&
!preg_match('/r3\-event/', $file)
) {
2021-09-13 09:39:38 +02:00
// echo "processing file " . $file . "\n";
2021-08-04 10:50:28 +02:00
$fn = fopen($file, "r");
while (!feof($fn)) {
$line = fgets($fn);
2021-09-08 04:59:31 +02:00
$matches = [];
$eventMatchRegex = '/Event\.[A-Z]{2}[A-Z0-9_]+/';
2021-08-04 10:50:28 +02:00
if (
2021-09-08 04:59:31 +02:00
preg_match($eventMatchRegex, $line) &&
preg_match_all($eventMatchRegex, $line, $matches) &&
$matches[0] && $matches[0][0]
2021-08-04 10:50:28 +02:00
) {
2021-09-08 04:59:31 +02:00
$event = $matches[0][0];
2021-08-04 10:50:28 +02:00
2021-09-08 04:59:31 +02:00
if (in_array($event, $events)) {
// Do nothing
} else {
array_push($events, $event);
2021-08-04 10:50:28 +02:00
}
}
}
fclose($fn);
}
}
array_push($events, 'Event.START');
array_push($events, 'Event.PAUSE');
array_push($events, 'Event.RESTART');
sort($events);
$i = 1;
$eventList = '';
$eventFunction = "Event.GetEventName = function(eventId) {\n\n\tswitch(eventId) {\n";
foreach ($events as $event) {
$eventList .= $event . " = " . "0x" . dechex($i) . ";\n";
$eventFunction .= "\t\tcase 0x" . dechex($i). " : return '" . strtolower(str_replace('Event.', '', $event)) . "';\n";
$i++;
}
$eventList .= "Event.MAX_EVENTS = " . "0x" . dechex($i) . ";\n\n";
$eventFunction .= "\t\tdefault :\n\t\t\tthrow new Error('Event type not defined : ' + eventId);\n";
$eventFunction .= "\t}\n\n";
$eventFunction .= "};\n";
2021-09-13 09:39:38 +02:00
// echo $eventList;
// echo $eventFunction;
2021-08-04 10:50:28 +02:00
2021-09-06 08:00:04 +02:00
updateSection('./src/r3/r3-event.js', 'GENERATED_EVENTS' , $eventList . $eventFunction);
2021-08-04 10:50:28 +02:00
}
2021-10-01 09:39:42 +02:00
function getSource($node, &$source)
2021-09-18 11:06:19 +02:00
{
2021-10-01 09:39:42 +02:00
$contents = file_get_contents($node->file);
array_push($source, $contents);
2021-09-18 11:06:19 +02:00
foreach ($node->children as $child) {
2021-10-01 09:39:42 +02:00
getSource($child, $source);
2021-09-18 11:06:19 +02:00
}
}
function buildDefines(&$defines, $node, $nameSpace)
{
$originalNameSpace = $nameSpace;
foreach ($node->children as $child) {
$nameSpace = $originalNameSpace . '.' . $child->nameSpaceClassName;
array_push($defines, $nameSpace. ' = ' . $child->nameSpaceClassName);
buildDefines($defines, $child, $nameSpace);
}
}
2021-07-05 10:21:36 +02:00
function generateR3Dist($nodes)
{
2021-09-18 11:06:19 +02:00
global $graph;
2021-10-01 09:39:42 +02:00
$r3jsDist = 'dist/r3.js';
2021-10-01 08:36:52 +02:00
$r3IndexTemplate = 'src/templates/r3_index.template';
2021-10-01 09:39:42 +02:00
$r3jsIndex = 'src/r3/index.js';
2021-07-04 20:32:41 +02:00
2021-10-01 09:39:42 +02:00
$template = file_get_contents($r3IndexTemplate);
file_put_contents($r3jsDist, $template);
2021-07-04 20:32:41 +02:00
2021-10-01 09:39:42 +02:00
$r3 = $graph->search('name', 'R3');
$source = [];
2021-07-05 10:21:36 +02:00
2021-10-01 09:39:42 +02:00
getSource($r3,$source);
2021-07-05 10:21:36 +02:00
2021-10-01 09:39:42 +02:00
updateSection($r3jsDist, 'GENERATED_SOURCE', $source);
2021-07-05 10:21:36 +02:00
2021-10-01 09:39:42 +02:00
$r3jsDistPointer = fopen($r3jsDist, "a");
2021-07-05 10:21:36 +02:00
2021-10-01 09:39:42 +02:00
$generateTokens = getTokens(['GENERATED']);
$customTokens = getTokens(['CUSTOM']);
2021-07-04 20:32:41 +02:00
2021-09-08 07:48:51 +02:00
$indexFiles = [
'src/r3/r3-component/index.js',
2021-09-10 07:18:08 +02:00
'src/r3/r3-entity/index.js',
2021-10-01 06:08:34 +02:00
'src/r3/r3-object/index.js',
'src/r3/r3-runtime/index.js',
'src/r3/r3-system/index.js',
2021-09-08 07:48:51 +02:00
];
foreach ($indexFiles as $indexFile) {
$savedGenerate = save($indexFile, $generateTokens)[1];
foreach ($savedGenerate as $key => $store) {
if ($key != 'GENERATED_INDEX_BODY') {
continue;
}
foreach ($store as $line) {
2021-10-01 09:39:42 +02:00
fwrite($r3jsDistPointer, $line);
2021-09-08 07:48:51 +02:00
}
}
deleteSavedFile($indexFile . '.saved');
}
2021-10-01 09:39:42 +02:00
$savedGenerate = save($r3jsIndex, $generateTokens)[1];
$savedCustom = save($r3jsIndex, $customTokens)[1];
2021-09-18 11:06:19 +02:00
foreach ($savedGenerate as $key => $store)
{
if ($key === 'GENERATED_IMPORTS') {
continue;
}
foreach ($store as $line) {
2021-10-01 09:39:42 +02:00
fwrite($r3jsDistPointer, $line);
2021-09-18 11:06:19 +02:00
}
}
2021-07-05 10:21:36 +02:00
foreach ($savedCustom as $key => $store)
{
foreach ($store as $line) {
2021-10-01 09:39:42 +02:00
fwrite($r3jsDistPointer, $line);
2021-07-05 10:21:36 +02:00
}
}
2021-10-01 09:39:42 +02:00
fclose($r3jsDistPointer);
2021-07-05 10:21:36 +02:00
2021-10-01 12:40:04 +02:00
deleteSavedFile($r3jsIndex . '.saved');
2021-07-05 10:21:36 +02:00
/**
* Now start cleaning up the file
*/
2021-10-01 09:39:42 +02:00
$contents = file_get_contents($r3jsDist);
2021-07-05 10:21:36 +02:00
2021-09-14 06:13:26 +02:00
$tokensForRemoval = [
'GENERATED_IMPORTS',
'GENERATED_EXPORTS',
'TEMPLATE_OPTIONS',
'CUSTOM_OPTIONS',
'TEMPLATE_STATIC_OPTIONS',
'CUSTOM_STATIC_OPTIONS',
'TEMPLATE_INSTANCE_OPTIONS_MAPPING',
'CUSTOM_INSTANCE_OPTIONS_MAPPING',
'TEMPLATE_EXCLUDED_FROM_INSTANCE_OPTIONS',
'CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS',
'TEMPLATE_METHODS',
'CUSTOM_METHODS',
'TEMPLATE_STATIC_METHODS',
'CUSTOM_STATIC_METHODS',
'CUSTOM_EVENT_LISTENERS',
'CUSTOM_STATIC_EVENT_LISTENERS'
];
foreach ($tokensForRemoval as $tokenForRemoval) {
$contents = preg_replace('/\n^[\/\s]*?\b'.$tokenForRemoval.'_START\b.*?\b'.$tokenForRemoval.'_END.*?$/sm', '', $contents);
}
2021-08-04 09:01:06 +02:00
2021-07-05 10:21:36 +02:00
$contents = preg_replace('/\n^\bmodule\.exports\s+=\s+{.*?}$/sm', '', $contents);
$contents = preg_replace('/\n^\bmodule\.exports\s+=.*?$/sm', '', $contents);
$contents = preg_replace('/\n^\bconst.*?= require.*?$/sm', '', $contents);
foreach ($generateTokens as $generateTokenKey => $generateTokenValue) {
/**
* Remove generate tokens
*/
$contents = preg_replace('/.*\b' . $generateTokenKey . '(_START|_END)\b.*?\n/m', '', $contents );
}
foreach ($customTokens as $customTokenKey => $customTokenValue) {
/**
* Remove generate tokens
*/
$contents = preg_replace('/.*\b' . $customTokenKey . '(_START|_END)\b.*?\n/m', '', $contents );
}
$contents = preg_replace('/\n^\/\*\*\s+\*\*\/\s*?$/sm', '', $contents);
$contents = preg_replace('/\n\n\n+/sm', "\n\n", $contents);
2021-09-22 08:31:29 +02:00
$contents = str_replace('__API_URL__', $_ENV['API_URL'], $contents);
2021-10-01 09:39:42 +02:00
file_put_contents($r3jsDist, $contents);
2021-07-05 10:21:36 +02:00
2021-07-04 08:24:20 +02:00
}
2021-07-12 12:02:59 +02:00
function updateParentSystems($nodes)
{
foreach ($nodes as $node) {
2021-10-01 08:36:52 +02:00
$className = $node->name;
2021-07-12 12:02:59 +02:00
2021-10-01 08:36:52 +02:00
if ($node->parent === null) {
2021-07-12 12:02:59 +02:00
$contents = file_get_contents($node->file);
$contents = preg_replace('/CLASS_NAME/', $className, $contents);
file_put_contents($node->file, $contents);
2021-10-01 08:36:52 +02:00
continue;
2021-07-12 12:02:59 +02:00
}
2021-10-01 08:36:52 +02:00
$parentName = $node->parent->name;
if ($node->parent->parent === null) {
/**
* We are working with the base system class
*/
$parentName = $node->name;
}
$contents = file_get_contents($node->file);
$contents = preg_replace('/PARENT_SYSTEM/', $parentName, $contents);
$contents = preg_replace('/CLASS_NAME/', $className, $contents);
$contents = preg_replace('/SYSTEM_NAME/', $className, $contents);
file_put_contents($node->file, $contents);
2021-07-12 12:02:59 +02:00
}
2021-10-01 08:36:52 +02:00
2021-07-12 12:02:59 +02:00
}
2021-08-04 10:50:28 +02:00
function buildNodeList($file)
{
2021-09-13 09:39:38 +02:00
// echo "loading file $file\n";
2021-08-04 10:50:28 +02:00
global $nodeList;
$fn = fopen($file, "r");
$line = '';
$found = false;
while (!feof($fn) && !$found) {
$line = fgets($fn);
if (preg_match('/^class\s+/', $line)) {
$found = true;
break;
}
}
if ($found) {
// echo $line . "\n";
$matches = [];
$result = preg_match('/^class\s+(.*?)(\s+extends\s+(.*?)\s+|\s+?{)/', $line, $matches);
if ($result === false) {
throw new ErrorException('Could not match');
}
$extends = null;
if (sizeof($matches) === 4) {
$class = $matches[1];
$extends = $matches[3];
} else if (sizeof($matches) === 3) {
$class = $matches[1];
} else {
throw new ErrorException('Unhandled case');
}
$nameSpace = '';
$nameSpaceClassName = $class;
2021-09-10 09:40:19 +02:00
$needles = [
'Component',
'Entity',
'Runtime',
'System'
];
2021-09-17 14:42:18 +02:00
$isBaseClass = true;
2021-08-04 10:50:28 +02:00
if ($extends) {
2021-09-10 09:40:19 +02:00
2021-09-17 14:42:18 +02:00
$isBaseClass = false;
2021-08-04 10:50:28 +02:00
$nameSpaceClassName = str_replace($extends, '', $class);
2021-09-10 09:40:19 +02:00
foreach ($needles as $needle) {
if ($needle != $nameSpaceClassName) {
$nameSpaceClassName = str_replace($needle, '', $nameSpaceClassName);
}
}
2021-08-04 10:50:28 +02:00
$nameSpaceClassName = str_replace('R3', '', $nameSpaceClassName);
}
$node = new Node(
$file,
$class,
$nameSpace,
$nameSpaceClassName,
2021-09-17 14:42:18 +02:00
$extends,
2021-09-18 08:52:21 +02:00
[],
2021-09-17 14:42:18 +02:00
$isBaseClass
2021-08-04 10:50:28 +02:00
);
array_push($nodeList, $node);
} else {
echo "not found\n";
}
fclose($fn);
2021-09-08 05:33:01 +02:00
}
function generateOutOfClassImplementationDefines($graph, $types)
{
foreach ($types as $type) {
2021-09-17 14:42:18 +02:00
if ($type === 'Runtime') {
2021-09-08 05:33:01 +02:00
2021-09-18 08:52:21 +02:00
$i = 0;
2021-09-17 14:42:18 +02:00
$nodes = $graph->walk();
2021-09-08 05:33:01 +02:00
2021-09-17 14:42:18 +02:00
foreach ($nodes as $node) {
2021-09-18 08:52:21 +02:00
if (preg_match('/\bRuntime\b/', $node->name) && $node->isBaseClass) {
$parent = $node;
$updateList = [];
foreach ($parent->children as $child) {
array_push($updateList, 'Runtime.' . strtoupper(from_camel_case(str_replace('Runtime','Base', $child->name))) . ' = 0x' . dechex($i) . ";\n");
$i++;
}
foreach ($parent->children as $child) {
2021-09-18 11:06:19 +02:00
foreach ($child->children as $implementation) {
array_push($updateList, 'Runtime.' . strtoupper(from_camel_case(str_replace('Runtime', '', $implementation->name))) . ' = 0x' . dechex($i) . ";\n");
$i++;
}
2021-09-18 08:52:21 +02:00
}
updateSection($parent->file, 'GENERATED_OUT_OF_CLASS_IMPLEMENTATION' , $updateList);
2021-09-17 14:42:18 +02:00
}
2021-09-18 08:52:21 +02:00
2021-09-17 14:42:18 +02:00
}
} else {
2021-09-18 08:52:21 +02:00
$i = 0;
$updateList = [];
$parent = $graph->search('name', $type);
2021-09-17 14:42:18 +02:00
$children = $graph->flatten($parent);
foreach ($children as $child) {
2021-09-18 11:06:19 +02:00
array_push($updateList, $parent->name . '.' . strtoupper(from_camel_case(str_replace($parent->name, '', $child->name))) . ' = 0x' . dechex($i) . ";\n");
2021-09-17 14:42:18 +02:00
$i++;
}
array_push($updateList, $parent->name . '.MAX_' . strtoupper($parent->name) . ' = 0x' . dechex($i) . ";\n");
2021-09-18 08:52:21 +02:00
updateSection($parent->file, 'GENERATED_OUT_OF_CLASS_IMPLEMENTATION' , $updateList);
2021-09-17 14:42:18 +02:00
}
2021-09-08 05:33:01 +02:00
2021-09-18 08:52:21 +02:00
2021-09-08 05:33:01 +02:00
}
2021-08-04 10:50:28 +02:00
}
//function generateRuntimes()
//{
//
// updateSection(
// 'src/r3/r3-runtime.js',
2021-09-06 08:00:04 +02:00
// 'GENERATED_RUNTIMES',
2021-08-04 10:50:28 +02:00
// [
// 'Runtime.RUNTIME_DEFAULT'
// ]
// );
//
//}
$nodeList = [];
/**
* @throws ErrorException
*/
foreach ($files as $file) {
$saveFile = null;
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";
2021-09-10 07:18:08 +02:00
exit(0);
2021-08-04 10:50:28 +02:00
}
2021-09-13 09:39:38 +02:00
$tokens = getTokens(['CUSTOM']);
2021-08-04 10:50:28 +02:00
$result = save($file, $tokens);
2021-09-10 07:18:08 +02:00
exit(1);
2021-08-04 10:50:28 +02:00
} else if ($argv[2] == 'restore') {
$saveFile = $file . '.saved';
2021-09-13 09:39:38 +02:00
$restoreTokens = getTokens(['CUSTOM']);
2021-08-04 10:50:28 +02:00
$restoreTokenKeys = array_keys($restoreTokens);
try {
2021-09-13 09:39:38 +02:00
$tokens = loadSaved($file . '.saved', $restoreTokens);
} catch (ErrorException $e) {
echo $e->getMessage();
exit(0);
}
$templateTokens = getTokens(['TEMPLATE']);
try {
$tokenData = loadSaved($file, $templateTokens, true);
2021-08-04 10:50:28 +02:00
} catch (ErrorException $e) {
echo $e->getMessage();
2021-09-10 07:18:08 +02:00
exit(0);
2021-08-04 10:50:28 +02:00
}
2021-09-13 09:39:38 +02:00
$tokens = array_merge($tokens, $tokenData);
2021-08-04 10:50:28 +02:00
$skipped = [];
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store);
}
}
2021-09-13 09:39:38 +02:00
echo "Generating Constructors\n";
2021-08-04 10:50:28 +02:00
2021-09-09 13:20:20 +02:00
generateConstructors($file, $argv[3]);
2021-09-13 09:39:38 +02:00
echo "Generating Methods\n";
2021-10-01 12:40:04 +02:00
generateImports($file, $tokens, $argv[3]);
2021-09-13 09:39:38 +02:00
generateMethods($file, $tokens, 'TEMPLATE_METHODS', 'GENERATED_TEMPLATE_METHODS');
2021-08-04 10:50:28 +02:00
2021-09-13 09:39:38 +02:00
generateMethods($file, $tokens, 'CUSTOM_METHODS', 'GENERATED_METHODS');
generateStaticMethods($file, $tokens, 'CUSTOM_STATIC_METHODS', 'GENERATED_STATIC_METHODS');
generateStaticMethods($file, $tokens, 'TEMPLATE_STATIC_METHODS', 'GENERATED_TEMPLATE_STATIC_METHODS');
2021-08-04 10:50:28 +02:00
generateEventListenerMethods($file, $tokens);
2021-08-04 10:50:28 +02:00
generateStaticEventListenerMethods($file, $tokens);
2021-09-13 09:39:38 +02:00
echo "Generating Options\n";
generateInitOptions($file, $tokens, 'TEMPLATE_OPTIONS', 'GENERATED_TEMPLATE_OPTIONS_INIT');
2021-08-04 10:50:28 +02:00
2021-09-13 09:39:38 +02:00
generateInitOptions($file, $tokens, 'CUSTOM_OPTIONS', 'GENERATED_OPTIONS_INIT');
2021-09-06 09:02:55 +02:00
2021-09-13 09:39:38 +02:00
generateInitStaticOptions($file, $tokens, 'CUSTOM_STATIC_OPTIONS', 'GENERATED_STATIC_OPTIONS_INIT');
2021-08-04 10:50:28 +02:00
2021-09-13 09:39:38 +02:00
generateInitStaticOptions($file, $tokens, 'TEMPLATE_STATIC_OPTIONS', 'GENERATED_TEMPLATE_STATIC_OPTIONS_INIT');
2021-08-04 10:50:28 +02:00
2021-09-22 14:43:33 +02:00
generateRequiredComponents($file, $tokens, 'CUSTOM_REQUIRED_COMPONENTS', 'GENERATED_REQUIRED_COMPONENTS');
2021-09-13 09:39:38 +02:00
// generateCreateInstanceOptions($file, $tokens);
generateUpdateInstanceOptions($file, $tokens, 'CUSTOM_OPTIONS', 'GENERATED_UPDATE_INSTANCE_OPTIONS', 'CUSTOM_INSTANCE_OPTIONS_MAPPING', 'CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS');
generateUpdateInstanceOptions($file, $tokens, 'TEMPLATE_OPTIONS', 'GENERATED_TEMPLATE_UPDATE_INSTANCE_OPTIONS', 'TEMPLATE_INSTANCE_OPTIONS_MAPPING', 'TEMPLATE_EXCLUDED_FROM_INSTANCE_OPTIONS');
generateUpdateFromInstanceOptions($file, $tokens, 'CUSTOM_OPTIONS', 'GENERATED_UPDATE_FROM_INSTANCE_OPTIONS', 'CUSTOM_INSTANCE_OPTIONS_MAPPING', 'CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS');
generateUpdateFromInstanceOptions($file, $tokens, 'TEMPLATE_OPTIONS', 'GENERATED_TEMPLATE_UPDATE_FROM_INSTANCE_OPTIONS', 'TEMPLATE_INSTANCE_OPTIONS_MAPPING', 'TEMPLATE_EXCLUDED_FROM_INSTANCE_OPTIONS');
echo "Generating Event Listeners\n";
2021-08-04 10:50:28 +02:00
generateEventListenersStart($file, $tokens);
generateEventListenersStop($file, $tokens);
generateStaticEventListenersStart($file, $tokens);
generateStaticEventListenersStop($file, $tokens);
2021-08-04 10:50:28 +02:00
/**
* 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)) {
$result = updateSection($file, $token, $store);
if ($result !== true && $result !== false) {
array_push($skipped, $result);
}
}
}
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";
2021-09-10 07:18:08 +02:00
exit(0);
2021-08-04 10:50:28 +02:00
} else {
deleteSavedFile($saveFile);
2021-09-10 07:18:08 +02:00
exit(1);
2021-08-04 10:50:28 +02:00
}
} else if ($argv[2] == 'build-dist') {
buildNodeList($file);
}
}
if ($argv[2] == 'generate-events') {
generateEvents();
}
2021-07-05 10:21:36 +02:00
if ($argv[2] == 'build-dist') {
2021-07-01 16:39:25 +02:00
global $nodeList;
/**
* Now generate the graph based on the new classes
*/
$graph = new Graph(
$nodeList
);
2021-07-01 16:39:25 +02:00
2021-09-13 09:39:38 +02:00
$restoreTokens = getTokens(['CUSTOM']);
2021-07-01 16:39:25 +02:00
$restoreTokenKeys = array_keys($restoreTokens);
/**
* Now start processing the files again - this time generating linked objects / inherited properties / methods
*/
foreach ($files as $file) {
generateInherited($file, $restoreTokens);
}
generateIndex(
[
2021-09-07 09:08:37 +02:00
'Component',
2021-10-01 06:08:34 +02:00
'Entity',
'R3Object',
2021-09-10 07:18:08 +02:00
'Runtime',
2021-10-01 06:08:34 +02:00
'System',
]
);
2021-07-04 08:24:20 +02:00
$nodes = $graph->walk();
2021-10-01 08:36:52 +02:00
// // Remove R3 (first node) from the list
// array_shift($nodes);
2021-07-12 12:02:59 +02:00
updateParentSystems($nodes);
2021-09-08 05:33:01 +02:00
generateOutOfClassImplementationDefines(
$graph,
[
'Component',
2021-10-01 06:08:34 +02:00
'Entity',
'R3Object',
2021-09-10 07:18:08 +02:00
'Runtime',
2021-10-01 06:08:34 +02:00
'System',
2021-09-08 05:33:01 +02:00
]
);
2021-09-13 09:39:38 +02:00
echo "Building Distribution\n";
2021-09-07 07:58:29 +02:00
generateR3($nodes, $graph);
2021-07-05 10:21:36 +02:00
generateR3Dist($nodes);
2021-07-04 08:24:20 +02:00
2021-07-01 16:39:25 +02:00
foreach ($files as $file) {
$saveFile = $file . '.saved';
deleteSavedFile($saveFile);
2021-07-03 10:40:05 +02:00
2021-07-01 16:39:25 +02:00
}
}
2021-09-10 07:18:08 +02:00
exit(1);
2021-06-19 11:27:32 +02:00
?>