From d51659db3e4e22e4b276b966390bd1a881f3ae6a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Apr 2021 11:01:57 +0200 Subject: [PATCH] Improved cli build and publish to use project references --- src/cpl_cli/command/new_service.py | 3 +- src/cpl_cli/configuration/build_settings.py | 6 +++ .../configuration/build_settings_name_enum.py | 1 + src/cpl_cli/configuration/settings_helper.py | 3 +- src/cpl_cli/publish/publisher_service.py | 45 ++++++++++++------- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/cpl_cli/command/new_service.py b/src/cpl_cli/command/new_service.py index 87349515..20c3bb22 100644 --- a/src/cpl_cli/command/new_service.py +++ b/src/cpl_cli/command/new_service.py @@ -109,7 +109,8 @@ class NewService(CommandABC): '*/logs', '*/tests' ], - BuildSettingsNameEnum.package_data.value: {} + BuildSettingsNameEnum.package_data.value: {}, + BuildSettingsNameEnum.project_references.value: [] } self._build.from_dict(self._build_dict) diff --git a/src/cpl_cli/configuration/build_settings.py b/src/cpl_cli/configuration/build_settings.py index f24be025..167db345 100644 --- a/src/cpl_cli/configuration/build_settings.py +++ b/src/cpl_cli/configuration/build_settings.py @@ -23,6 +23,7 @@ class BuildSettings(ConfigurationModelABC): self._included: Optional[list[str]] = None self._excluded: Optional[list[str]] = None self._package_data: Optional[dict[str, list[str]]] = None + self._project_references: Optional[list[str]] = None @property def project_type(self): @@ -60,6 +61,10 @@ class BuildSettings(ConfigurationModelABC): def package_data(self) -> dict[str, list[str]]: return self._package_data + @property + def project_references(self) -> list[str]: + return self._project_references + def from_dict(self, settings: dict): try: self._project_type = settings[BuildSettingsNameEnum.project_type.value] @@ -71,6 +76,7 @@ class BuildSettings(ConfigurationModelABC): self._included = settings[BuildSettingsNameEnum.included.value] self._excluded = settings[BuildSettingsNameEnum.excluded.value] self._package_data = settings[BuildSettingsNameEnum.package_data.value] + self._project_references = settings[BuildSettingsNameEnum.project_references.value] if sys.platform == 'win32': self._source_path = str(self._source_path).replace('/', '\\') diff --git a/src/cpl_cli/configuration/build_settings_name_enum.py b/src/cpl_cli/configuration/build_settings_name_enum.py index 18d78749..5c0dee28 100644 --- a/src/cpl_cli/configuration/build_settings_name_enum.py +++ b/src/cpl_cli/configuration/build_settings_name_enum.py @@ -12,3 +12,4 @@ class BuildSettingsNameEnum(Enum): included = 'Included' excluded = 'Excluded' package_data = 'PackageData' + project_references = 'ProjectReferences' diff --git a/src/cpl_cli/configuration/settings_helper.py b/src/cpl_cli/configuration/settings_helper.py index e4229445..8a7c47ad 100644 --- a/src/cpl_cli/configuration/settings_helper.py +++ b/src/cpl_cli/configuration/settings_helper.py @@ -42,5 +42,6 @@ class SettingsHelper: BuildSettingsNameEnum.include_package_data.value: build.include_package_data, BuildSettingsNameEnum.included.value: build.included, BuildSettingsNameEnum.excluded.value: build.excluded, - BuildSettingsNameEnum.package_data.value: build.package_data + BuildSettingsNameEnum.package_data.value: build.package_data, + BuildSettingsNameEnum.project_references.value: build.project_references } diff --git a/src/cpl_cli/publish/publisher_service.py b/src/cpl_cli/publish/publisher_service.py index 96717cc4..1c61243a 100644 --- a/src/cpl_cli/publish/publisher_service.py +++ b/src/cpl_cli/publish/publisher_service.py @@ -149,6 +149,28 @@ class PublisherService(PublisherABC): return False + def _read_sources_from_path(self, path: str): + """ + Reads all source files from given path + :param path: + :return: + """ + for r, d, f in os.walk(path): + for file in f: + relative_path = os.path.relpath(r) + file_path = os.path.join(relative_path, os.path.relpath(file)) + if self._is_file_excluded(file_path): + continue + + if len(d) > 0: + for directory in d: + empty_dir = os.path.join(os.path.dirname(file_path), directory) + if len(os.listdir(empty_dir)) == 0: + self._included_dirs.append(empty_dir) + + if not self._is_path_excluded(relative_path): + self._included_files.append(os.path.relpath(file_path)) + def _read_sources(self): """ Reads all source files and save included files @@ -167,21 +189,16 @@ class PublisherService(PublisherABC): elif os.path.isfile(rel_path): self._included_files.append(rel_path) - for r, d, f in os.walk(self._source_path): - for file in f: - relative_path = os.path.relpath(r) - file_path = os.path.join(relative_path, os.path.relpath(file)) - if self._is_file_excluded(file_path): - continue + self._read_sources_from_path(self._source_path) - if len(d) > 0: - for directory in d: - empty_dir = os.path.join(os.path.dirname(file_path), directory) - if len(os.listdir(empty_dir)) == 0: - self._included_dirs.append(empty_dir) + for project in self._build_settings.project_references: + project = os.path.abspath(os.path.join(self._source_path, project)) + if not os.path.isfile(os.path.abspath(project)): + Console.error(f'Cannot import project: {project}') + return - if not self._is_path_excluded(relative_path): - self._included_files.append(os.path.relpath(file_path)) + self.exclude(f'*/{os.path.basename(project)}') + self._read_sources_from_path(os.path.dirname(project)) def _create_packages(self): """ @@ -196,8 +213,6 @@ class PublisherService(PublisherABC): title = self._get_module_name_from_dirs(file) if title == '': title = self._project_settings.name - elif not title.__contains__('.'): - title = f'{self._project_settings.name}.{title}' module_py_lines: list[str] = [] imports = ''