Skip to content

Commit

Permalink
Improve install formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Mar 24, 2024
1 parent 628dc4c commit 8aaf991
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 100 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## [0.12.0] 2024-03-24
### Added
- Support for different kinds of indentation, either specific number of spaces or tabs through `--indent` argument (examples: `--indent=2` or `--indent=tabs`) or `indent` entry in `.gersemirc` (examples: `indent: 2` or `indent: tabs`) (#15)
- support for different kinds of indentation, either specific number of spaces or tabs through `--indent` argument (examples: `--indent=2` or `--indent=tabs`) or `indent` entry in `.gersemirc` (examples: `indent: 2` or `indent: tabs`) (#15)

### Changed
- formatting of `install` command will now correctly recognize sections like `RUNTIME`, `ARCHIVE`, `FILE_SET` etc.

### Fixed
- inconsistent formatting of `add_library` (#17)
Expand Down
68 changes: 44 additions & 24 deletions gersemi/command_invocation_dumpers/project_command_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
from .section_aware_command_invocation_dumper import SectionAwareCommandInvocationDumper
from .target_link_libraries_command_dumper import TargetLinkLibraries
from .two_word_keyword_isolator import TwoWordKeywordIsolator
from .two_word_keyword_isolator import AnyMatcher, TwoWordKeywordIsolator


class AddCustomCommand(CommandLineFormatter, MultipleSignatureCommandInvocationDumper):
Expand Down Expand Up @@ -220,40 +220,60 @@ class IncludeExternalMsProject(ArgumentAwareCommandInvocationDumper):
one_value_keywords = ["TYPE", "GUID", "PLATFORM"]


class Install(TwoWordKeywordIsolator, MultipleSignatureCommandInvocationDumper):
two_words_keywords = [("INCLUDES", "DESTINATION")]
_Install_artifact_kinds = [
"ARCHIVE",
"LIBRARY",
"RUNTIME",
"OBJECTS",
"FRAMEWORK",
"BUNDLE",
"PUBLIC_HEADER",
"PRIVATE_HEADER",
"RESOURCE",
"FILE_SET.*",
"CXX_MODULES_BMI",
]
_Install_artifact_group = dict(
options=[
"OPTIONAL",
"EXCLUDE_FROM_ALL",
"NAMELINK_ONLY",
"NAMELINK_SKIP",
],
one_value_keywords=[
"DESTINATION",
"COMPONENT",
"NAMELINK_COMPONENT",
],
multi_value_keywords=[
"PERMISSIONS",
"CONFIGURATIONS",
],
)


class Install(
TwoWordKeywordIsolator,
SectionAwareCommandInvocationDumper,
MultipleSignatureCommandInvocationDumper,
):
two_words_keywords = [("INCLUDES", "DESTINATION"), ("FILE_SET", AnyMatcher())]

customized_signatures = {
"TARGETS": dict(
options=[
"ARCHIVE",
"LIBRARY",
"RUNTIME",
"OBJECTS",
"FRAMEWORK",
"BUNDLE",
"PRIVATE_HEADER",
"PUBLIC_HEADER",
"RESOURCE",
"OPTIONAL",
"EXCLUDE_FROM_ALL",
"NAMELINK_ONLY",
"NAMELINK_SKIP",
],
sections={
artifact_kind: _Install_artifact_group
for artifact_kind in _Install_artifact_kinds
},
one_value_keywords=[
"EXPORT",
"DESTINATION",
"COMPONENT",
"NAMELINK_COMPONENT",
"RUNTIME_DEPENDENCY_SET",
"FILE_SET",
],
multi_value_keywords=[
"TARGETS",
"PERMISSIONS",
"CONFIGURATIONS",
"INCLUDES DESTINATION",
"RUNTIME_DEPENDENCIES",
*_Install_artifact_kinds,
],
),
"FILES": dict(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from contextlib import contextmanager
from typing import Dict, Iterable
import re
from typing import Iterable, Mapping
from gersemi.ast_helpers import is_commented_argument
from .argument_aware_command_invocation_dumper import (
ArgumentAwareCommandInvocationDumper,
)


Sections = Dict[str, Dict[str, Iterable[str]]]
Sections = Mapping[str, Mapping[str, Iterable[str]]]


class Match(str):
def __eq__(self, other):
return re.search(self, other) is not None

def __hash__(self):
return hash(str(self))


def create_section_patch(section, old_class):
Expand All @@ -22,7 +31,7 @@ class Impl(old_class):


class SectionAwareCommandInvocationDumper(ArgumentAwareCommandInvocationDumper):
sections: Dict[str, Dict[str, Iterable[str]]] = {}
sections: Sections = {}

@contextmanager
def _update_section_characteristics(self, keyword):
Expand All @@ -33,6 +42,12 @@ def _update_section_characteristics(self, keyword):
finally:
self.__class__ = old_class

def _get_matcher(self, keyword):
for item in self.sections:
if Match(item) == keyword:
return item
return None

def _format_group(self, group) -> str:
result = self._try_to_format_into_single_line(group, separator=" ")
if result is not None:
Expand All @@ -46,10 +61,11 @@ def _format_group(self, group) -> str:
else:
keyword, *_ = front_node.children

if keyword not in self.sections:
keyword_matcher = self._get_matcher(keyword)
if keyword_matcher is None:
return original_format_group(group)

with self._update_section_characteristics(keyword):
with self._update_section_characteristics(keyword_matcher):
subgroups = self._split_arguments(rest)
formatted_front = original_format_group([front_node])
with self.indented():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from typing import Sequence, Tuple
from typing import Sequence, Tuple, Union
from lark import Tree
from lark.visitors import Transformer, Transformer_InPlace
from gersemi.ast_helpers import is_keyword
from gersemi.base_command_invocation_dumper import BaseCommandInvocationDumper
from gersemi.utils import advance


class AnyMatcher:
def __eq__(self, other):
return True


class PrependLhs(Transformer):
def __init__(self, lhs):
super().__init__()
Expand Down Expand Up @@ -50,7 +55,7 @@ def arguments(self, children):


class TwoWordKeywordIsolator(BaseCommandInvocationDumper):
two_words_keywords: Sequence[Tuple[str, str]] = []
two_words_keywords: Sequence[Tuple[str, Union[str, AnyMatcher]]] = []

def _preprocess_arguments(self, arguments):
preprocessed = arguments
Expand Down
6 changes: 5 additions & 1 deletion tests/formatter/install_command.in.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ install(EXPORT_ANDROID_MK long_arg______________________________________________

install(EXPORT_ANDROID_MK long_arg____________________________________________________________ DESTINATION long_arg____________________________________________________________ NAMESPACE long_arg____________________________________________________________ FILE long_arg____________________________________________________________ PERMISSIONS OWNER_READ CONFIGURATIONS Debug EXPORT_LINK_INTERFACE_LIBRARIES COMPONENT long_arg____________________________________________________________ EXCLUDE_FROM_ALL)

install(EXPORT_ANDROID_MK long_arg____________________________________________________________ DESTINATION long_arg____________________________________________________________ NAMESPACE long_arg____________________________________________________________ FILE long_arg____________________________________________________________ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE CONFIGURATIONS Debug EXPORT_LINK_INTERFACE_LIBRARIES COMPONENT long_arg____________________________________________________________ EXCLUDE_FROM_ALL)
install(EXPORT_ANDROID_MK long_arg____________________________________________________________ DESTINATION long_arg____________________________________________________________ NAMESPACE long_arg____________________________________________________________ FILE long_arg____________________________________________________________ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE CONFIGURATIONS Debug EXPORT_LINK_INTERFACE_LIBRARIES COMPONENT long_arg____________________________________________________________ EXCLUDE_FROM_ALL)

install(TARGETS FOO EXPORT BAR FILE_SET FOOBAR DESTINATION BAZ PERMISSIONS OWNER_READ CONFIGURATIONS Debug COMPONENT FOO NAMELINK_ONLY INCLUDES DESTINATION FOO)

install(TARGETS ${target} EXPORT ${target}Targets RUNTIME DESTINATION "${OBS_EXECUTABLE_DESTINATION}" COMPONENT Development EXCLUDE_FROM_ALL LIBRARY DESTINATION "${OBS_LIBRARY_DESTINATION}" COMPONENT Development EXCLUDE_FROM_ALL ARCHIVE DESTINATION "${OBS_LIBRARY_DESTINATION}" COMPONENT Development EXCLUDE_FROM_ALL FRAMEWORK DESTINATION Frameworks____________________ COMPONENT Development EXCLUDE_FROM_ALL INCLUDES DESTINATION "${include_destination}" PUBLIC_HEADER DESTINATION "${include_destination}" COMPONENT Development EXCLUDE_FROM_ALL)
Loading

0 comments on commit 8aaf991

Please sign in to comment.