1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""This module manages the global plugin registry for pw_cli.""" 15 16import argparse 17from pathlib import Path 18import sys 19from typing import Iterable 20 21from pw_cli import arguments, env, plugins 22import pw_env_setup.config_file 23 24plugin_registry = plugins.Registry(validator=plugins.callable_with_no_args) 25REGISTRY_FILE = 'PW_PLUGINS' 26 27 28def _register_builtin_plugins(registry: plugins.Registry) -> None: 29 """Registers the commands that are included with pw by default.""" 30 31 # Register these by name to avoid circular dependencies. 32 registry.register_by_name('bloat', 'pw_bloat.__main__', 'main') 33 registry.register_by_name('doctor', 'pw_doctor.doctor', 'main') 34 registry.register_by_name('emu', 'pw_emu.__main__', 'main') 35 registry.register_by_name( 36 'clang-tidy-fix', 'pw_toolchain.clang_apply_replacements', 'main' 37 ) 38 registry.register_by_name('format', 'pw_presubmit.format_code', 'main') 39 registry.register_by_name('keep-sorted', 'pw_presubmit.keep_sorted', 'main') 40 registry.register_by_name('logdemo', 'pw_cli.log', 'main') 41 registry.register_by_name('module', 'pw_module.__main__', 'main') 42 registry.register_by_name( 43 'python-packages', 'pw_env_setup.python_packages', 'main' 44 ) 45 registry.register_by_name('test', 'pw_unit_test.test_runner', 'main') 46 registry.register_by_name('watch', 'pw_watch.watch', 'main') 47 48 registry.register('help', _help_command) 49 50 51def _help_command(): 52 """Display detailed information about pw commands.""" 53 parser = argparse.ArgumentParser(description=_help_command.__doc__) 54 parser.add_argument( 55 'plugins', 56 metavar='plugin', 57 nargs='*', 58 help='command for which to display detailed info', 59 ) 60 61 print(arguments.format_help(plugin_registry), file=sys.stderr) 62 63 for line in plugin_registry.detailed_help(**vars(parser.parse_args())): 64 print(line, file=sys.stderr) 65 66 67def register() -> None: 68 _register_builtin_plugins(plugin_registry) 69 parsed_env = env.pigweed_environment() 70 pw_plugins_file: Path = parsed_env.PW_PROJECT_ROOT / REGISTRY_FILE 71 72 if pw_plugins_file.is_file(): 73 plugin_registry.register_file(pw_plugins_file) 74 else: 75 plugin_registry.register_config( 76 config=pw_env_setup.config_file.load(), 77 path=pw_env_setup.config_file.path(), 78 ) 79 80 81def errors() -> dict: 82 return plugin_registry.errors() 83 84 85def format_help() -> str: 86 return arguments.format_help(plugin_registry) 87 88 89def run(name: str, args: Iterable[str]) -> int: 90 return plugin_registry.run_with_argv(name, args) 91