|
12 | 12 | import azure_cli_diff_tool |
13 | 13 | from azdev.utilities import display, require_azure_cli, heading, get_path_table, filter_by_git_diff |
14 | 14 | from .custom import DiffExportFormat, get_commands_meta, STORED_DEPRECATION_KEY |
15 | | -from .util import export_commands_meta |
| 15 | +from .util import export_commands_meta, dump_command_tree, add_to_command_tree |
16 | 16 | from ..statistics import _create_invoker_and_load_cmds, _get_command_source, \ |
17 | 17 | _command_codegen_info # pylint: disable=protected-access |
18 | 18 | from ..statistics.util import filter_modules |
@@ -129,3 +129,60 @@ def export_command_meta(modules=None, git_source=None, git_target=None, git_repo |
129 | 129 |
|
130 | 130 | def cmp_command_meta(base_meta_file, diff_meta_file, only_break=False, output_type="text", output_file=None): |
131 | 131 | return azure_cli_diff_tool.meta_diff(base_meta_file, diff_meta_file, only_break, output_type, output_file) |
| 132 | + |
| 133 | + |
| 134 | +def export_command_tree(modules, output_file=None): |
| 135 | + require_azure_cli() |
| 136 | + |
| 137 | + # allow user to run only on CLI or extensions |
| 138 | + cli_only = modules == ['CLI'] |
| 139 | + ext_only = modules == ['EXT'] |
| 140 | + if cli_only or ext_only: |
| 141 | + modules = None |
| 142 | + |
| 143 | + selected_modules = get_path_table(include_only=modules) |
| 144 | + |
| 145 | + if cli_only: |
| 146 | + selected_modules['ext'] = {} |
| 147 | + if ext_only: |
| 148 | + selected_modules['core'] = {} |
| 149 | + selected_modules['mod'] = {} |
| 150 | + |
| 151 | + if not any(selected_modules.values()): |
| 152 | + logger.warning('No commands selected to check.') |
| 153 | + |
| 154 | + selected_mod_names = list(selected_modules['mod'].keys()) |
| 155 | + selected_mod_names += list(selected_modules['ext'].keys()) |
| 156 | + selected_mod_names += list(selected_modules['core'].keys()) |
| 157 | + |
| 158 | + if selected_mod_names: |
| 159 | + display('Modules selected: {}\n'.format(', '.join(selected_mod_names))) |
| 160 | + |
| 161 | + heading('Export Command Tree') |
| 162 | + start = time.time() |
| 163 | + display('Initializing with loading command table...') |
| 164 | + from azure.cli.core import get_default_cli # pylint: disable=import-error |
| 165 | + az_cli = get_default_cli() |
| 166 | + |
| 167 | + # load commands, args, and help |
| 168 | + _create_invoker_and_load_cmds(az_cli) |
| 169 | + |
| 170 | + stop = time.time() |
| 171 | + logger.info('Commands loaded in %i sec', stop - start) |
| 172 | + display('Commands loaded in {} sec'.format(stop - start)) |
| 173 | + command_loader = az_cli.invocation.commands_loader |
| 174 | + |
| 175 | + # trim command table to selected_modules |
| 176 | + command_loader = filter_modules(command_loader, modules=selected_mod_names) |
| 177 | + |
| 178 | + if not command_loader.command_table: |
| 179 | + logger.warning('No commands selected to check.') |
| 180 | + |
| 181 | + command_tree = {} |
| 182 | + |
| 183 | + for command_name, command in command_loader.command_table.items(): |
| 184 | + module_source = _get_command_source(command_name, command)['module'] |
| 185 | + # The command tree is a tree structure like our azExtCmdTree: https://aka.ms/azExtCmdTree |
| 186 | + add_to_command_tree(command_tree, command_name, module_source) |
| 187 | + |
| 188 | + dump_command_tree(command_tree, output_file) |
0 commit comments