def _merge_sub_resources_in_aaz()

in src/aaz_dev/command/controller/workspace_manager.py [0:0]


    def _merge_sub_resources_in_aaz(self):
        """Merge the commands of subresources which exported in aaz but not exist in current workspace"""
        updated_cfgs = []
        inserted_commands = set()
        for ws_leaf in self.iter_command_tree_leaves():
            editor = self.load_cfg_editor_by_command(ws_leaf)
            existing_sub_resources = {}
            for cmd_names, command in editor.iter_commands():
                for r in command.resources:
                    if not r.subresource:
                        continue
                    key = (r.id, r.version)
                    if key not in existing_sub_resources:
                        existing_sub_resources[key] = set()
                    existing_sub_resources[key].add(r.subresource)
                    if r.subresource.endswith("[]") or r.subresource.endswith("{}"):
                        existing_sub_resources[key].add(r.subresource[:-2])

            aaz_ref = {}
            for (r_id, r_version), r_sub_resources in existing_sub_resources.items():
                pre_cfg_reader = self.aaz_specs.load_resource_cfg_reader(
                    editor.cfg.plane, resource_id=r_id, version=r_version
                )
                if not pre_cfg_reader:
                    continue

                for cmd_names, command in pre_cfg_reader.iter_commands():
                    insert_command = True
                    for r in command.resources:
                        if (r.id, r.version) not in existing_sub_resources:
                            insert_command = False
                            break
                        if not r.subresource or r.subresource in r_sub_resources:
                            insert_command = False
                            break
                    if not insert_command:
                        continue
                    cmd_names_str = ' '.join(cmd_names)
                    if cmd_names_str in inserted_commands:
                        continue
                    if self.find_command_tree_leaf(*cmd_names) is not None:
                        logger.error(
                            f"Command '{' '.join(cmd_names)}' in workspace conflict the name of Subresource Command in `aaz`")
                        continue
                    if self.find_command_tree_node(*cmd_names) is not None:
                        logger.error(
                            f"Command Group '{' '.join(cmd_names)}' in workspace conflict the name of Subresource Command in `aaz`")
                        continue
                    command = CMDCommand(command.to_native())
                    command.link()
                    editor._add_command(*cmd_names, command=command)
                    inserted_commands.add(cmd_names_str)
                    aaz_ref[cmd_names_str] = r_version
            if aaz_ref:
                editor.reformat()
                updated_cfgs = [(editor, aaz_ref)]

        for editor, aaz_ref in updated_cfgs:
            self._unset_cfg_editor(editor)
            self._set_cfg_editor(editor, aaz_ref)