#!/usr/bin/env python3

# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0

"""
This script is only used for bootstrapping and building this package.

The installed command is generated by the `console_scripts` entry point.
"""

import os
import sys

# add this package to the Python path
pkg_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, pkg_root)


# override entry point discovery
from colcon_core import extension_point  # noqa: E402

custom_extension_points = {}


def custom_load_extension_points(  # noqa: D103
    group_name, *, excludes=None
):
    global custom_extension_points
    assert group_name in custom_extension_points, \
        f"get_extension_points() not overridden for group '{group_name}'"
    return {
        k: v for k, v in custom_extension_points[group_name].items()
        if excludes is None or k not in excludes}


# override function before importing other modules
extension_point.load_extension_points = custom_load_extension_points


from colcon_core.command import HOME_ENVIRONMENT_VARIABLE  # noqa: E402 I202
from colcon_core.command import LOG_LEVEL_ENVIRONMENT_VARIABLE  # noqa: E402
from colcon_core.environment.path import PathEnvironment  # noqa: E402
from colcon_core.environment.path \
    import PythonScriptsPathEnvironment  # noqa: E402
from colcon_core.environment.pythonpath \
    import PythonPathEnvironment  # noqa: E402
from colcon_core.event_handler.console_direct \
    import ConsoleDirectEventHandler  # noqa: E402
from colcon_core.event_handler.console_start_end \
    import ConsoleStartEndEventHandler  # noqa: E402
from colcon_core.event_handler.log_command \
    import LogCommandEventHandler  # noqa: E402
from colcon_core.executor \
    import DEFAULT_EXECUTOR_ENVIRONMENT_VARIABLE  # noqa: E402
from colcon_core.executor.sequential import SequentialExecutor  # noqa: E402
from colcon_core.extension_point \
    import EXTENSION_BLOCKLIST_ENVIRONMENT_VARIABLE  # noqa: E402
from colcon_core.package_augmentation.python \
    import PythonPackageAugmentation  # noqa: E402
from colcon_core.package_discovery.path \
    import PathPackageDiscovery  # noqa: E402
from colcon_core.package_identification.ignore \
    import IgnorePackageIdentification  # noqa: E402
from colcon_core.package_identification.python \
    import PythonPackageIdentification  # noqa: E402
from colcon_core.prefix_path.colcon import ColconPrefixPath  # noqa: E402
from colcon_core.shell import ALL_SHELLS_ENVIRONMENT_VARIABLE  # noqa: E402
from colcon_core.shell.bat import BatShell  # noqa: E402
from colcon_core.shell.dsv import DsvShell  # noqa: E402
from colcon_core.shell.sh import ShShell  # noqa: E402
from colcon_core.task.python.build import PythonBuildTask  # noqa: E402
from colcon_core.task.python.test import PythonTestTask  # noqa: E402
from colcon_core.task.python.test.pytest \
    import PytestPythonTestingStep  # noqa: E402
from colcon_core.task.python.test.setuppy_test \
    import SetuppyPythonTestingStep  # noqa: E402
from colcon_core.verb.build import BuildVerb  # noqa: E402
from colcon_core.verb.test import TestVerb  # noqa: E402


custom_extension_points.update({
    'colcon_core.argument_parser': {},
    'colcon_core.environment': {
        'path': PathEnvironment,
        'pythonscriptspath': PythonScriptsPathEnvironment,
        'pythonpath': PythonPathEnvironment,
    },
    'colcon_core.environment_variable': {
        'all_shells': ALL_SHELLS_ENVIRONMENT_VARIABLE,
        'default_executor': DEFAULT_EXECUTOR_ENVIRONMENT_VARIABLE,
        'extension_blocklist': EXTENSION_BLOCKLIST_ENVIRONMENT_VARIABLE,
        'home': HOME_ENVIRONMENT_VARIABLE,
        'log_level': LOG_LEVEL_ENVIRONMENT_VARIABLE,
    },
    'colcon_core.event_handler': {
        'console_direct': ConsoleDirectEventHandler,
        'console_start_end': ConsoleStartEndEventHandler,
        'log_command': LogCommandEventHandler,
    },
    'colcon_core.executor': {
        'sequential': SequentialExecutor,
    },
    'colcon_core.extension_point': {
        # there is no point in registering the extension_point extensions here
        # since they can't be queried through pkg_resources without installing
    },
    'colcon_core.package_augmentation': {
        'python': PythonPackageAugmentation,
    },
    'colcon_core.package_discovery': {
        'path': PathPackageDiscovery,
    },
    'colcon_core.package_identification': {
        'ignore': IgnorePackageIdentification,
        'python': PythonPackageIdentification,
    },
    'colcon_core.package_selection': {},
    'colcon_core.prefix_path': {
        'colcon': ColconPrefixPath,
    },
    'colcon_core.python_testing': {
        'pytest': PytestPythonTestingStep,
        'setuppy_test': SetuppyPythonTestingStep,
    },
    'colcon_core.shell': {
        'bat': BatShell,
        'dsv': DsvShell,
        'sh': ShShell,
    },
    'colcon_core.shell.find_installed_packages': {
        # Not essential for bootstrapping
    },
    'colcon_core.task.build': {
        'python': PythonBuildTask,
    },
    'colcon_core.task.test': {
        'python': PythonTestTask,
    },
    'colcon_core.verb': {
        'build': BuildVerb,
        'test': TestVerb,
    },
})

from colcon_core.command import main  # noqa: E402 I100 I202

if __name__ == '__main__':
    sys.exit(main() or 0)
