diff --git a/commodore/component/__init__.py b/commodore/component/__init__.py index cd900458d..950051881 100644 --- a/commodore/component/__init__.py +++ b/commodore/component/__init__.py @@ -1,8 +1,9 @@ from pathlib import Path as P -from typing import Iterable, Optional +from typing import Dict, Iterable, Optional import _jsonnet import click +import yaml from commodore.gitrepo import GitRepo @@ -71,6 +72,12 @@ def class_file(self) -> P: def defaults_file(self) -> P: return self.target_directory / "class" / "defaults.yml" + @property + def default_values(self) -> Dict: + with open(self.defaults_file, "r", encoding="utf-8") as f: + defyaml = list(yaml.safe_load_all(f)) + return defyaml[0]["parameters"][self.parameters_key] + @property def lib_files(self) -> Iterable[P]: lib_dir = self.target_directory / "lib" @@ -79,6 +86,17 @@ def lib_files(self) -> Iterable[P]: return [] + def get_library(self, libname: str) -> P: + lib_dir = self.target_directory / "lib" + if not lib_dir.exists(): + return None + + for f in self.lib_files: + if f.absolute() == P(lib_dir / libname).absolute(): + return f.absolute() + + return None + @property def filters_file(self) -> P: return self.target_directory / "postprocess" / "filters.yml" diff --git a/commodore/component/compile.py b/commodore/component/compile.py index 3fef4594b..44af835f3 100644 --- a/commodore/component/compile.py +++ b/commodore/component/compile.py @@ -11,6 +11,7 @@ from commodore.dependency_mgmt import ( fetch_jsonnet_libraries, validate_component_library_name, + create_component_library_aliases, ) from commodore.helpers import kapitan_compile, relsymlink from commodore.inventory import Inventory @@ -61,6 +62,8 @@ def compile_component( for lib in component.lib_files: validate_component_library_name(config, component.name, lib) + create_component_library_aliases(config, component) + # Create class for fake parameters with open(inv.params_file, "w", encoding="utf-8") as file: file.write( diff --git a/commodore/dependency_mgmt.py b/commodore/dependency_mgmt.py index e96b023c8..318a7331e 100644 --- a/commodore/dependency_mgmt.py +++ b/commodore/dependency_mgmt.py @@ -26,6 +26,25 @@ def validate_component_library_name(cfg: Config, cname: str, lib: P) -> P: return lib +def create_component_library_aliases(cfg: Config, component: Component): + aliases = ( + component.default_values.get("=_metadata", {}) + .get("library_aliases", {}) + .items() + ) + for libalias, libname in aliases: + if cfg.debug: + click.echo(f" > aliasing template library {libname} to {libalias}") + libf = component.get_library(libname) + if not libf: + click.secho( + f" > [WARN] {component.name} template library alias refers to nonexistent template library", + fg="yellow", + ) + else: + relsymlink(libf, cfg.inventory.lib_dir, dest_name=libalias) + + def create_component_symlinks(cfg, component: Component): """ Create symlinks in the inventory subdirectory. @@ -49,6 +68,8 @@ def create_component_symlinks(cfg, component: Component): cfg.inventory.lib_dir, ) + create_component_library_aliases(cfg, component) + def _format_component_list(components: Iterable[str]) -> str: formatted_list = list(map(lambda c: f"'{c}'", sorted(components)))