Commit e56fb000 authored by Adrian DC's avatar Adrian DC
Browse files

feat(setup): implement support for '*.function' profiles as aliases

Issue: #2
---

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 0d1b8e93
Loading
Loading
Loading
Loading
+58 −1
Changes for setup.py: 58 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -58,11 +58,15 @@ class SetupAssets(Command):
        ('prepare', None, 'Prepare assets after setup'),
    ]

    # Git alias configuration
    # Git alias configuration, pylint: disable=too-many-locals
    @staticmethod
    def git_configuration_alias(keep: bool = False) -> None:

        # Variables
        comment: str
        command: str
        function_list: List[str]
        function_line: str
        section_current: str
        section_last: str = ''

@@ -97,6 +101,8 @@ class SetupAssets(Command):

            # Iterate through profiles files
            for root, _, _ in sorted(walk(SetupAssets.PROFILES)):

                # Handle profile aliases
                for alias_file in sorted(glob(join(root, '*.alias'))):

                    # Parse profile section
@@ -131,6 +137,57 @@ class SetupAssets(Command):
                                ]
                            ])

                # Handle profile functions
                for function_file in sorted(glob(join(root, '*.function'))):

                    # Parse profile section
                    section_current = root.split(sep)[-1]
                    if section_last != section_current:
                        section_last = section_current
                        asset.writelines([
                            f'{line}{SetupAssets.GIT_CONFIGURATION_SEPARATOR}'
                            for line in [
                                '',
                                '\t#####',
                                f'\t## {PROJECT_NAME}: {root}',
                                '\t###',
                            ]
                        ])

                    # Parse profile file
                    function_name = splitext(basename(function_file))[0]
                    with open(function_file, encoding='utf8', mode='r') as profile_file:
                        lines = profile_file.readlines()
                        if len(lines) >= 2:
                            comment = lines[0].strip().lstrip('# ')
                            function_list = lines[1:]

                            # Prepare function syntax
                            function_list = [
                                line.replace('\\"', '\\\\"').replace('"', '\\"')
                                for line in function_list
                            ]
                            function_line = ''
                            if len(function_list) == 1:
                                function_line = f'{function_list[0].rstrip()} #'
                            else:
                                function_line = '\\'
                                for line in function_list:
                                    if not function_line.endswith('\\'):
                                        function_line += ' \\'
                                    function_line += f'\n\t    {line.rstrip()}'
                                function_line += ' \\\n\t#'

                            # Inject function configuration
                            asset.writelines([
                                f'{line}{SetupAssets.GIT_CONFIGURATION_SEPARATOR}'
                                for line in [
                                    '\t#',
                                    f'\t# {comment}',
                                    f'\t{function_name} = "! {function_line}"',
                                ]
                            ])

    # Assets initialization
    def initialize_options(self) -> None:
        self.clean = None