diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2023-06-30 15:53:56 -0700 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2023-07-25 15:50:21 -0400 |
commit | 492d8e90ad0fca985c4c09e518157230efaa5618 (patch) | |
tree | c377308d98f066c69e2ada3ccf7ed8639246a4eb /mesonbuild | |
parent | 5449d10f01bc765e33e041a15f7c90e752ba7539 (diff) | |
download | meson-492d8e90ad0fca985c4c09e518157230efaa5618.zip meson-492d8e90ad0fca985c4c09e518157230efaa5618.tar.gz meson-492d8e90ad0fca985c4c09e518157230efaa5618.tar.bz2 |
templates: use common classes for remaining languages
These all break down into either a single file or a file + header. This
means a little more customization per class, but not too much.
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/templates/cpptemplates.py | 53 | ||||
-rw-r--r-- | mesonbuild/templates/ctemplates.py | 51 | ||||
-rw-r--r-- | mesonbuild/templates/cudatemplates.py | 53 | ||||
-rw-r--r-- | mesonbuild/templates/dlangtemplates.py | 53 | ||||
-rw-r--r-- | mesonbuild/templates/fortrantemplates.py | 46 | ||||
-rw-r--r-- | mesonbuild/templates/objcpptemplates.py | 51 | ||||
-rw-r--r-- | mesonbuild/templates/objctemplates.py | 51 | ||||
-rw-r--r-- | mesonbuild/templates/rusttemplates.py | 53 | ||||
-rw-r--r-- | mesonbuild/templates/samplefactory.py | 4 | ||||
-rw-r--r-- | mesonbuild/templates/sampleimpl.py | 88 | ||||
-rw-r--r-- | mesonbuild/templates/valatemplates.py | 48 |
11 files changed, 182 insertions, 369 deletions
diff --git a/mesonbuild/templates/cpptemplates.py b/mesonbuild/templates/cpptemplates.py index 75a5ee2..d3d29d3 100644 --- a/mesonbuild/templates/cpptemplates.py +++ b/mesonbuild/templates/cpptemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileHeaderImpl hello_cpp_template = '''#include <iostream> @@ -143,42 +141,13 @@ pkg_mod.generate( ''' -class CppProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.cpp' - open(source_name, 'w', encoding='utf-8').write(hello_cpp_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_cpp_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - class_name = uppercase_token[0] + lowercase_token[1:] - test_exe_name = lowercase_token + '_test' - namespace = lowercase_token - lib_hpp_name = lowercase_token + '.hpp' - lib_cpp_name = lowercase_token + '.cpp' - test_cpp_name = lowercase_token + '_test.cpp' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'class_name': class_name, - 'namespace': namespace, - 'header_file': lib_hpp_name, - 'source_file': lib_cpp_name, - 'test_source_file': test_cpp_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_hpp_name, 'w', encoding='utf-8').write(lib_hpp_template.format(**kwargs)) - open(lib_cpp_name, 'w', encoding='utf-8').write(lib_cpp_template.format(**kwargs)) - open(test_cpp_name, 'w', encoding='utf-8').write(lib_cpp_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_cpp_meson_template.format(**kwargs)) +class CppProject(FileHeaderImpl): + + source_ext = 'cpp' + header_ext = 'hpp' + exe_template = hello_cpp_template + exe_meson_template = hello_cpp_meson_template + lib_template = lib_cpp_template + lib_header_template = lib_hpp_template + lib_test_template = lib_cpp_test_template + lib_meson_template = lib_cpp_meson_template diff --git a/mesonbuild/templates/ctemplates.py b/mesonbuild/templates/ctemplates.py index 16e6c44..14eeaf7 100644 --- a/mesonbuild/templates/ctemplates.py +++ b/mesonbuild/templates/ctemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileHeaderImpl lib_h_template = '''#pragma once @@ -126,40 +124,13 @@ test('basic', exe) ''' -class CProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.c' - open(source_name, 'w', encoding='utf-8').write(hello_c_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_c_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_h_name = lowercase_token + '.h' - lib_c_name = lowercase_token + '.c' - test_c_name = lowercase_token + '_test.c' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'header_file': lib_h_name, - 'source_file': lib_c_name, - 'test_source_file': test_c_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) - open(lib_c_name, 'w', encoding='utf-8').write(lib_c_template.format(**kwargs)) - open(test_c_name, 'w', encoding='utf-8').write(lib_c_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_c_meson_template.format(**kwargs)) +class CProject(FileHeaderImpl): + + source_ext = 'c' + header_ext = 'h' + exe_template = hello_c_template + exe_meson_template = hello_c_meson_template + lib_template = lib_c_template + lib_header_template = lib_h_template + lib_test_template = lib_c_test_template + lib_meson_template = lib_c_meson_template diff --git a/mesonbuild/templates/cudatemplates.py b/mesonbuild/templates/cudatemplates.py index ce77504..f59d79a 100644 --- a/mesonbuild/templates/cudatemplates.py +++ b/mesonbuild/templates/cudatemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileHeaderImpl hello_cuda_template = '''#include <iostream> @@ -143,42 +141,13 @@ pkg_mod.generate( ''' -class CudaProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.cu' - open(source_name, 'w', encoding='utf-8').write(hello_cuda_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_cuda_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - class_name = uppercase_token[0] + lowercase_token[1:] - test_exe_name = lowercase_token + '_test' - namespace = lowercase_token - lib_h_name = lowercase_token + '.h' - lib_cuda_name = lowercase_token + '.cu' - test_cuda_name = lowercase_token + '_test.cu' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'class_name': class_name, - 'namespace': namespace, - 'header_file': lib_h_name, - 'source_file': lib_cuda_name, - 'test_source_file': test_cuda_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) - open(lib_cuda_name, 'w', encoding='utf-8').write(lib_cuda_template.format(**kwargs)) - open(test_cuda_name, 'w', encoding='utf-8').write(lib_cuda_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_cuda_meson_template.format(**kwargs)) +class CudaProject(FileHeaderImpl): + + source_ext = 'cu' + header_ext = 'h' + exe_template = hello_cuda_template + exe_meson_template = hello_cuda_meson_template + lib_template = lib_cuda_template + lib_header_template = lib_h_template + lib_test_template = lib_cuda_test_template + lib_meson_template = lib_cuda_meson_template diff --git a/mesonbuild/templates/dlangtemplates.py b/mesonbuild/templates/dlangtemplates.py index d5adf92..6a8a071 100644 --- a/mesonbuild/templates/dlangtemplates.py +++ b/mesonbuild/templates/dlangtemplates.py @@ -13,9 +13,9 @@ # limitations under the License. from __future__ import annotations -import re +from mesonbuild.templates.sampleimpl import FileImpl -from mesonbuild.templates.sampleimpl import SampleImpl +import typing as T hello_d_template = '''module main; @@ -104,39 +104,16 @@ endif ''' -class DlangProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.d' - open(source_name, 'w', encoding='utf-8').write(hello_d_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_d_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_m_name = lowercase_token - lib_d_name = lowercase_token + '.d' - test_d_name = lowercase_token + '_test.d' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'module_file': lib_m_name, - 'source_file': lib_d_name, - 'test_source_file': test_d_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_d_name, 'w', encoding='utf-8').write(lib_d_template.format(**kwargs)) - open(test_d_name, 'w', encoding='utf-8').write(lib_d_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_d_meson_template.format(**kwargs)) +class DlangProject(FileImpl): + + source_ext = 'd' + exe_template = hello_d_template + exe_meson_template = hello_d_meson_template + lib_template = lib_d_template + lib_test_template = lib_d_test_template + lib_meson_template = lib_d_meson_template + + def lib_kwargs(self) -> T.Dict[str, str]: + kwargs = super().lib_kwargs() + kwargs['module_file'] = self.lowercase_token + return kwargs diff --git a/mesonbuild/templates/fortrantemplates.py b/mesonbuild/templates/fortrantemplates.py index 6c21cad..8895e32 100644 --- a/mesonbuild/templates/fortrantemplates.py +++ b/mesonbuild/templates/fortrantemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileImpl lib_fortran_template = ''' ! This procedure will not be exported and is not @@ -103,37 +101,11 @@ test('basic', exe) ''' -class FortranProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.f90' - open(source_name, 'w', encoding='utf-8').write(hello_fortran_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_fortran_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_fortran_name = lowercase_token + '.f90' - test_fortran_name = lowercase_token + '_test.f90' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'source_file': lib_fortran_name, - 'test_source_file': test_fortran_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_fortran_name, 'w', encoding='utf-8').write(lib_fortran_template.format(**kwargs)) - open(test_fortran_name, 'w', encoding='utf-8').write(lib_fortran_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_fortran_meson_template.format(**kwargs)) +class FortranProject(FileImpl): + + source_ext = 'f90' + exe_template = hello_fortran_template + exe_meson_template = hello_fortran_meson_template + lib_template = lib_fortran_template + lib_meson_template = lib_fortran_meson_template + lib_test_template = lib_fortran_test_template diff --git a/mesonbuild/templates/objcpptemplates.py b/mesonbuild/templates/objcpptemplates.py index 4ec785c..a102165 100644 --- a/mesonbuild/templates/objcpptemplates.py +++ b/mesonbuild/templates/objcpptemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileHeaderImpl lib_h_template = '''#pragma once @@ -126,40 +124,13 @@ test('basic', exe) ''' -class ObjCppProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.mm' - open(source_name, 'w', encoding='utf-8').write(hello_objcpp_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_objcpp_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_h_name = lowercase_token + '.h' - lib_objcpp_name = lowercase_token + '.mm' - test_objcpp_name = lowercase_token + '_test.mm' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'header_file': lib_h_name, - 'source_file': lib_objcpp_name, - 'test_source_file': test_objcpp_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) - open(lib_objcpp_name, 'w', encoding='utf-8').write(lib_objcpp_template.format(**kwargs)) - open(test_objcpp_name, 'w', encoding='utf-8').write(lib_objcpp_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_objcpp_meson_template.format(**kwargs)) +class ObjCppProject(FileHeaderImpl): + + source_ext = 'mm' + header_ext = 'h' + exe_template = hello_objcpp_template + exe_meson_template = hello_objcpp_meson_template + lib_template = lib_objcpp_template + lib_header_template = lib_h_template + lib_test_template = lib_objcpp_test_template + lib_meson_template = lib_objcpp_meson_template diff --git a/mesonbuild/templates/objctemplates.py b/mesonbuild/templates/objctemplates.py index 331b76c..4e31beb 100644 --- a/mesonbuild/templates/objctemplates.py +++ b/mesonbuild/templates/objctemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileHeaderImpl lib_h_template = '''#pragma once @@ -126,40 +124,13 @@ test('basic', exe) ''' -class ObjCProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.m' - open(source_name, 'w', encoding='utf-8').write(hello_objc_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_objc_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_h_name = lowercase_token + '.h' - lib_objc_name = lowercase_token + '.m' - test_objc_name = lowercase_token + '_test.m' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'header_file': lib_h_name, - 'source_file': lib_objc_name, - 'test_source_file': test_objc_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_h_name, 'w', encoding='utf-8').write(lib_h_template.format(**kwargs)) - open(lib_objc_name, 'w', encoding='utf-8').write(lib_objc_template.format(**kwargs)) - open(test_objc_name, 'w', encoding='utf-8').write(lib_objc_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_objc_meson_template.format(**kwargs)) +class ObjCProject(FileHeaderImpl): + + source_ext = 'm' + header_ext = 'h' + exe_template = hello_objc_template + exe_meson_template = hello_objc_meson_template + lib_template = lib_objc_template + lib_header_template = lib_h_template + lib_test_template = lib_objc_test_template + lib_meson_template = lib_objc_meson_template diff --git a/mesonbuild/templates/rusttemplates.py b/mesonbuild/templates/rusttemplates.py index 8408359..26548b8 100644 --- a/mesonbuild/templates/rusttemplates.py +++ b/mesonbuild/templates/rusttemplates.py @@ -13,9 +13,9 @@ # limitations under the License. from __future__ import annotations -import re +import typing as T -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileImpl lib_rust_template = '''#![crate_name = "{crate_file}"] @@ -74,39 +74,16 @@ test('basic', exe) ''' -class RustProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.rs' - open(source_name, 'w', encoding='utf-8').write(hello_rust_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_rust_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - function_name = lowercase_token[0:3] + '_func' - test_exe_name = lowercase_token + '_test' - lib_crate_name = lowercase_token - lib_rs_name = lowercase_token + '.rs' - test_rs_name = lowercase_token + '_test.rs' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'function_name': function_name, - 'crate_file': lib_crate_name, - 'source_file': lib_rs_name, - 'test_source_file': test_rs_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_rs_name, 'w', encoding='utf-8').write(lib_rust_template.format(**kwargs)) - open(test_rs_name, 'w', encoding='utf-8').write(lib_rust_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_rust_meson_template.format(**kwargs)) +class RustProject(FileImpl): + + source_ext = 'rs' + exe_template = hello_rust_template + exe_meson_template = hello_rust_meson_template + lib_template = lib_rust_template + lib_test_template = lib_rust_test_template + lib_meson_template = lib_rust_meson_template + + def lib_kwargs(self) -> T.Dict[str, str]: + kwargs = super().lib_kwargs() + kwargs['crate_file'] = self.lowercase_token + return kwargs diff --git a/mesonbuild/templates/samplefactory.py b/mesonbuild/templates/samplefactory.py index e3c8551..8e200e2 100644 --- a/mesonbuild/templates/samplefactory.py +++ b/mesonbuild/templates/samplefactory.py @@ -29,10 +29,10 @@ from mesonbuild.templates.ctemplates import CProject if T.TYPE_CHECKING: from ..minit import Arguments - from .sampleimpl import ClassImpl, SampleImpl + from .sampleimpl import ClassImpl, FileHeaderImpl, FileImpl, SampleImpl -_IMPL: T.Mapping[str, T.Union[T.Type[ClassImpl], T.Type[SampleImpl]]] = { +_IMPL: T.Mapping[str, T.Union[T.Type[ClassImpl], T.Type[FileHeaderImpl], T.Type[FileImpl]]] = { 'c': CProject, 'cpp': CppProject, 'cs': CSharpProject, diff --git a/mesonbuild/templates/sampleimpl.py b/mesonbuild/templates/sampleimpl.py index 0d31aa4..e34cad7 100644 --- a/mesonbuild/templates/sampleimpl.py +++ b/mesonbuild/templates/sampleimpl.py @@ -26,6 +26,9 @@ class SampleImpl(metaclass=abc.ABCMeta): def __init__(self, args: Arguments): self.name = args.name self.version = args.version + 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.abstractmethod def create_executable(self) -> None: @@ -35,17 +38,6 @@ class SampleImpl(metaclass=abc.ABCMeta): def create_library(self) -> None: 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 @@ -70,6 +62,11 @@ class ClassImpl(SampleImpl): def source_ext(self) -> str: pass + +class ClassImpl(SampleImpl): + + """For Class based languages, like Java and C#""" + def create_executable(self) -> None: source_name = f'{self.capitalized_token}.{self.source_ext}' with open(source_name, 'w', encoding='utf-8') as f: @@ -102,3 +99,72 @@ class ClassImpl(SampleImpl): 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)) + + +class FileImpl(SampleImpl): + + """File based languages without headers""" + + def create_executable(self) -> None: + source_name = f'{self.lowercase_token}.{self.source_ext}' + with open(source_name, 'w', encoding='utf-8') as f: + f.write(self.exe_template.format(project_name=self.name)) + 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 lib_kwargs(self) -> T.Dict[str, str]: + """Get Language specific keyword arguments + + :return: A dictionary of key: values to fill in the templates + """ + return { + 'utoken': self.uppercase_token, + 'ltoken': self.lowercase_token, + 'header_dir': self.lowercase_token, + 'class_name': self.capitalized_token, + 'function_name': f'{self.lowercase_token[0:3]}_func', + 'namespace': self.lowercase_token, + 'source_file': f'{self.lowercase_token}.{self.source_ext}', + 'test_source_file': f'{self.lowercase_token}_test.{self.source_ext}', + '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, + } + + def create_library(self) -> None: + lib_name = f'{self.lowercase_token}.{self.source_ext}' + test_name = f'{self.lowercase_token}_test.{self.source_ext}' + kwargs = self.lib_kwargs() + 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)) + + +class FileHeaderImpl(FileImpl): + + @abc.abstractproperty + def header_ext(self) -> str: + pass + + @abc.abstractproperty + def lib_header_template(self) -> str: + pass + + def lib_kwargs(self) -> T.Dict[str, str]: + kwargs = super().lib_kwargs() + kwargs['header_file'] = f'{self.lowercase_token}.{self.header_ext}' + return kwargs + + def create_library(self) -> None: + super().create_library() + kwargs = self.lib_kwargs() + with open(kwargs['header_file'], 'w', encoding='utf-8') as f: + f.write(self.lib_header_template.format_map(kwargs)) diff --git a/mesonbuild/templates/valatemplates.py b/mesonbuild/templates/valatemplates.py index b6a4614..aa82de7 100644 --- a/mesonbuild/templates/valatemplates.py +++ b/mesonbuild/templates/valatemplates.py @@ -13,9 +13,7 @@ # limitations under the License. from __future__ import annotations -import re - -from mesonbuild.templates.sampleimpl import SampleImpl +from mesonbuild.templates.sampleimpl import FileImpl hello_vala_template = '''void main (string[] args) {{ @@ -84,39 +82,11 @@ test('{test_name}', test_exe) ''' -class ValaProject(SampleImpl): - - def create_executable(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - source_name = lowercase_token + '.vala' - open(source_name, 'w', encoding='utf-8').write(hello_vala_template.format(project_name=self.name)) - open('meson.build', 'w', encoding='utf-8').write( - hello_vala_meson_template.format(project_name=self.name, - exe_name=lowercase_token, - source_name=source_name, - version=self.version)) - - def create_library(self) -> None: - lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower()) - uppercase_token = lowercase_token.upper() - class_name = uppercase_token[0] + lowercase_token[1:] - test_exe_name = lowercase_token + '_test' - namespace = lowercase_token - lib_vala_name = lowercase_token + '.vala' - test_vala_name = lowercase_token + '_test.vala' - kwargs = {'utoken': uppercase_token, - 'ltoken': lowercase_token, - 'header_dir': lowercase_token, - 'class_name': class_name, - 'namespace': namespace, - 'source_file': lib_vala_name, - 'test_source_file': test_vala_name, - 'test_exe_name': test_exe_name, - 'project_name': self.name, - 'lib_name': lowercase_token, - 'test_name': lowercase_token, - 'version': self.version, - } - open(lib_vala_name, 'w', encoding='utf-8').write(lib_vala_template.format(**kwargs)) - open(test_vala_name, 'w', encoding='utf-8').write(lib_vala_test_template.format(**kwargs)) - open('meson.build', 'w', encoding='utf-8').write(lib_vala_meson_template.format(**kwargs)) +class ValaProject(FileImpl): + + source_ext = 'vala' + exe_template = hello_vala_template + exe_meson_template = hello_vala_meson_template + lib_template = lib_vala_template + lib_test_template = lib_vala_test_template + lib_meson_template = lib_vala_meson_template |