From 02a5871f890b790bbd19695ee9acf42db1eba0d5 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 17 Dec 2024 23:14:48 +0300 Subject: [PATCH] Splitting codegen output to many files assumed to be stored under non VCS directory --- src/codegen.inc.php | 56 ++++++++++++++++++- ...degen_bundle.twig => codegen_factory.twig} | 4 +- tpl/codegen_types_slice.twig | 16 ++++++ tpl/macro.twig | 4 +- 4 files changed, 75 insertions(+), 5 deletions(-) rename tpl/{codegen_bundle.twig => codegen_factory.twig} (91%) create mode 100644 tpl/codegen_types_slice.twig diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 31ade4d..c4e717b 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -2,7 +2,7 @@ namespace metagen_cs; use Exception; -function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) : string +function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) : array { $twig = get_twig(); @@ -15,7 +15,21 @@ function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) : if(!isset($options['namespace'])) $options['namespace'] = 'BitGames.Autogen'; - return $twig->render('codegen_bundle.twig', $options); + $sliced_units = slice_units($meta->getUnits(), 50); + + $output = array(); + + foreach($sliced_units as $slice_idx => $slice_units) + { + $slice_options = $options; + $slice_options['slice_idx'] = $slice_idx; + $slice_options['slice_units'] = $slice_units; + $output['types_'.$slice_idx.'.cs'] = $twig->render('codegen_types_slice.twig', $slice_options); + } + + $output['factory.cs'] = $twig->render('codegen_factory.twig', $options); + + return $output; } function get_twig(array $inc_path = []) : \Twig\Environment @@ -713,3 +727,41 @@ function get_field_index(\mtgMetaStruct $struct, string $field_name): int } return -1; } + +function paginate($total, $step) +{ + //pages are returned as an array where each element is in interval [N, Y) + $pages = array(); + + $steps = (int)($total/$step); + $rest = $total % $step; + + for($i=1;$i<=$steps;++$i) + $pages[] = array(($i-1)*$step, $i*$step); + + if($rest != 0) + $pages[] = array(($i-1)*$step, ($i-1)*$step + $rest); + + return $pages; +} + +//slices array like this: [[idx0,[..]], [idx1,[..]], ...] +function slice_units(array $units, $max) +{ + $pages = paginate(sizeof($units), $max); + + $sliced = array(); + + $units_keys = array_keys($units); + + foreach($pages as $idx => $page) + { + $slice = array(); + for($i = $page[0];$i<$page[1];++$i) + { + $slice[] = $units[$units_keys[$i]]; + } + $sliced[$idx] = $slice; + } + return $sliced; +} diff --git a/tpl/codegen_bundle.twig b/tpl/codegen_factory.twig similarity index 91% rename from tpl/codegen_bundle.twig rename to tpl/codegen_factory.twig index 122d1a9..31dfe44 100644 --- a/tpl/codegen_bundle.twig +++ b/tpl/codegen_factory.twig @@ -9,7 +9,9 @@ using System; namespace {{namespace}} { {% endif %} -{{ macro.decl_units(meta) }} +{%- for ai in get_all_declarable_accessor_interfaces(meta) ~%} + {{ macro.decl_accessor_interface(ai) }} +{%- endfor ~%} static public class AutogenBundle { diff --git a/tpl/codegen_types_slice.twig b/tpl/codegen_types_slice.twig new file mode 100644 index 0000000..88d3497 --- /dev/null +++ b/tpl/codegen_types_slice.twig @@ -0,0 +1,16 @@ +//THIS FILE IS GENERATED AUTOMATICALLY, DO NOT TOUCH IT! +using System.Collections.Generic; +using metagen; +using System; + +{%- import "macro.twig" as macro -%} + +{% if namespace is defined ~%} +namespace {{namespace}} { +{% endif %} + +{{ macro.decl_units(slice_units) }} + +{% if namespace is defined ~%} +} //namespace {{namespace}} +{% endif %} diff --git a/tpl/macro.twig b/tpl/macro.twig index d99b178..55b26b6 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -1,6 +1,6 @@ -{% macro decl_units(meta) %} +{% macro decl_units(units) %} -{%- for u in meta.getunits ~%} +{%- for u in units ~%} {%- if u.object is instanceof('\\mtgMetaStruct') -%} {{ _self.decl_struct(u.object) }} {%- elseif u.object is instanceof('\\mtgMetaEnum') -%}