diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2023-06-30 14:58:30 -0700 |
|---|---|---|
| committer | Eli Schwartz <eschwartz93@gmail.com> | 2023-07-25 15:50:21 -0400 |
| commit | 5449d10f01bc765e33e041a15f7c90e752ba7539 (patch) | |
| tree | 66ae8c35da4c517f3d8cad6a43817fe06603c328 /mesonbuild/templates/sampleimpl.py | |
| parent | bbe649a5fc7bb0b955b2688c4d797b71830c8aeb (diff) | |
| download | meson-5449d10f01bc765e33e041a15f7c90e752ba7539.tar.gz | |
templates: use a common template for C# and Java
The only real differences between these generators is the file extension
and the templates themselves. We can uses a shared abstract class
with a few abstract properties to provide all of this to the same base
class. This results in less code duplication and easier maintanence.
I've made a few cleanups to the shared template:
- use `str.capitalize()` instead of `str.upper()[0] + str[1:]`
- use `open` as a context manager
- use f-strings
- put some duplicate calculations in the initializer
Diffstat (limited to 'mesonbuild/templates/sampleimpl.py')
| -rw-r--r-- | mesonbuild/templates/sampleimpl.py | 78 |
1 files changed, 75 insertions, 3 deletions
diff --git a/mesonbuild/templates/sampleimpl.py b/mesonbuild/templates/sampleimpl.py index 4d586b1fe..0d31aa447 100644 --- a/mesonbuild/templates/sampleimpl.py +++ b/mesonbuild/templates/sampleimpl.py @@ -13,20 +13,92 @@ # limitations under the License. from __future__ import annotations +import abc +import re import typing as T if T.TYPE_CHECKING: from ..minit import Arguments -class SampleImpl: +class SampleImpl(metaclass=abc.ABCMeta): def __init__(self, args: Arguments): self.name = args.name self.version = args.version + @abc.abstractmethod def create_executable(self) -> None: - raise NotImplementedError('Sample implementation for "executable" not implemented!') + pass + @abc.abstractmethod def create_library(self) -> None: - raise NotImplementedError('Sample implementation for "library" not implemented!') + pass + + +class ClassImpl(SampleImpl): + + """For Class based languages, like Java and C#""" + + def __init__(self, args: Arguments): + super().__init__(args) + self.lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) + self.uppercase_token = self.lowercase_token.upper() + self.capitalized_token = self.lowercase_token.capitalize() + + @abc.abstractproperty + def exe_template(self) -> str: + pass + + @abc.abstractproperty + def exe_meson_template(self) -> str: + pass + + @abc.abstractproperty + def lib_template(self) -> str: + pass + + @abc.abstractproperty + def lib_test_template(self) -> str: + pass + + @abc.abstractproperty + def lib_meson_template(self) -> str: + pass + + @abc.abstractproperty + def source_ext(self) -> str: + pass + + def create_executable(self) -> None: + source_name = f'{self.capitalized_token}.{self.source_ext}' + with open(source_name, 'w', encoding='utf-8') as f: + f.write(self.exe_template.format(project_name=self.name, + class_name=self.capitalized_token)) + with open('meson.build', 'w', encoding='utf-8') as f: + f.write(self.exe_meson_template.format(project_name=self.name, + exe_name=self.name, + source_name=source_name, + version=self.version)) + + def create_library(self) -> None: + lib_name = f'{self.capitalized_token}.{self.source_ext}' + test_name = f'{self.capitalized_token}_test.{self.source_ext}' + kwargs = {'utoken': self.uppercase_token, + 'ltoken': self.lowercase_token, + 'class_test': f'{self.capitalized_token}_test', + 'class_name': self.capitalized_token, + 'source_file': lib_name, + 'test_source_file': test_name, + 'test_exe_name': f'{self.lowercase_token}_test', + 'project_name': self.name, + 'lib_name': self.lowercase_token, + 'test_name': self.lowercase_token, + 'version': self.version, + } + with open(lib_name, 'w', encoding='utf-8') as f: + f.write(self.lib_template.format(**kwargs)) + with open(test_name, 'w', encoding='utf-8') as f: + f.write(self.lib_test_template.format(**kwargs)) + with open('meson.build', 'w', encoding='utf-8') as f: + f.write(self.lib_meson_template.format(**kwargs)) |
