Skip to content

Commit

Permalink
[.NET] [sourcegen] Write preambles in generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
burkenyo authored and speth committed Aug 17, 2022
1 parent c8192bf commit a1646f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions interfaces/sourcegen/sourcegen/_helpers.py
@@ -1,6 +1,7 @@
# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.

import os
import textwrap

def normalize(code: str, indent: int = 0, trim_first: bool = False):
Expand All @@ -13,3 +14,7 @@ def normalize(code: str, indent: int = 0, trim_first: bool = False):
code = code[indent:]

return code

def get_preamble():
with open(os.path.dirname(__file__) + '/preamble.txt', 'r') as preamble_file:
return preamble_file.read()
12 changes: 11 additions & 1 deletion interfaces/sourcegen/sourcegen/csharp/_SourceGenerator.py
Expand Up @@ -5,7 +5,7 @@
import os
import typing

from .._helpers import normalize
from .._helpers import normalize, get_preamble
from .._types import *
from .._types import _unpack

Expand Down Expand Up @@ -42,6 +42,8 @@ class SourceGenerator(SourceGeneratorBase):
'double*': 'double[]'
}

_preamble = '/*\n' + get_preamble() + '*/'

@staticmethod
def _join_params(params):
return ', '.join((p.p_type + ' ' + p.name for p in params))
Expand Down Expand Up @@ -211,6 +213,8 @@ def generate_source(self, incl_file: os.DirEntry, funcs: list[Func]):
functions_text = '\n\n'.join((self._get_function_text(f) for f in cs_funcs))

interop_text = normalize(f'''
{normalize(self._preamble, 12)}
using System.Runtime.InteropServices;
namespace Cantera.Interop;
Expand All @@ -233,6 +237,8 @@ def generate_source(self, incl_file: os.DirEntry, funcs: list[Func]):
handles_text = '\n\n'.join((self._get_base_handle_text(h) for h in handles))

handles_text = normalize(f'''
{normalize(self._preamble, 12)}
namespace Cantera.Interop;
{normalize(handles_text, 12, True)}
Expand All @@ -248,6 +254,8 @@ def finalize(self):
for d in self._config['derived_handles'].items()))

derived_handles_text = normalize(f'''
{normalize(self._preamble, 12)}
namespace Cantera.Interop;
{derived_handles}
Expand All @@ -263,6 +271,8 @@ def finalize(self):
for (c_name, cs_name) in props.items()))

clazz_text = normalize(f'''
{normalize(self._preamble, 16)}
using Cantera.Interop;
namespace Cantera;
Expand Down
5 changes: 5 additions & 0 deletions interfaces/sourcegen/sourcegen/preamble.txt
@@ -0,0 +1,5 @@
This file is part of Cantera. See License.txt in the top-level directory or
at https://cantera.org/license.txt for license and copyright information.

This file was generated by sourcegen. It will be re-generated by the
Cantera build process. Do not manually edit.

0 comments on commit a1646f7

Please sign in to comment.