예제 #1
0
def driver_no_subcommand(add_back: bool = False) -> bool:
    choices: List[Choice[Callable[[], bool]]] = [
        Choice("List All Builtin Drivers", partial(search_driver, "")),
        Choice("Search for Builtin Driver", search_driver),
        Choice("Install a Builtin Driver", install_driver),
    ]
    if add_back:
        choices.append(Choice("<- Back", lambda: False))
    subcommand = (ListPrompt("What do you want to do?",
                             choices).prompt(style=default_style).data)
    return subcommand()
예제 #2
0
def create_project() -> bool:
    click.secho("Loading adapters...")
    adapters = {x.name: x for x in _get_adapters()}
    click.clear()
    answers = {}

    answers["project_name"] = InputPrompt(
        "Project Name:",
        validator=lambda x: len(x) > 0).prompt(style=default_style)

    dir_name = (answers["project_name"].lower().replace(" ",
                                                        "-").replace("-", "_"))
    src_choices: List[Choice[bool]] = [
        Choice(f'1) In a "{dir_name}" folder', False),
        Choice('2) In a "src" folder', True),
    ]
    answers["use_src"] = (ListPrompt(
        "Where to store the plugin?",
        src_choices).prompt(style=default_style).data)

    answers["plugins"] = {"builtin": []}
    answers["plugins"]["builtin"] = [
        choice.data for choice in CheckboxPrompt(
            "Which builtin plugin(s) would you like to use?",
            [Choice(name, name) for name in _get_builtin_plugins()],
        ).prompt(style=default_style)
    ]

    answers["adapters"] = {"builtin": []}

    confirm = False
    while not confirm:
        answers["adapters"]["builtin"] = [
            choice.data.dict() for choice in CheckboxPrompt(
                "Which adapter(s) would you like to use?",
                [Choice(name, adapter) for name, adapter in adapters.items()],
            ).prompt(style=default_style)
        ]
        if not answers["adapters"]["builtin"]:
            confirm = ConfirmPrompt(
                "You haven't chosen any adapter. Please confirm.",
                default_choice=False,
            ).prompt(style=default_style)
        else:
            confirm = True
    cookiecutter(
        str((Path(__file__).parent.parent / "project").resolve()),
        no_input=True,
        extra_context=answers,
    )

    for adapter in answers["adapters"]["builtin"]:
        _call_pip_install(adapter["project_link"])
    return True
예제 #3
0
파일: adapter.py 프로젝트: nonebot/nb-cli
def adapter_no_subcommand(add_back: bool = False) -> bool:
    choices: List[Choice[Callable[[], bool]]] = [
        Choice("Create a Custom Adapter", create_adapter),
        Choice("List All Published Adapters", partial(search_adapter, "")),
        Choice("Search for Published Adapters", search_adapter),
        Choice("Install a Published Adapter", install_adapter),
    ]
    if add_back:
        choices.append(Choice("<- Back", lambda: False))
    subcommand = (ListPrompt("What do you want to do?",
                             choices).prompt(style=default_style).data)
    return subcommand()
예제 #4
0
파일: deploy.py 프로젝트: nonebot/nb-cli
def deploy_no_subcommand(add_back: bool = False) -> bool:
    choices: List[Choice[Callable[[], bool]]] = [
        Choice("Build Docker Image for the Bot", build_docker_image),
        Choice("Deploy the Bot to Docker", run_docker_image),
        Choice("Stop the Bot Container in Docker", exit_docker_image),
    ]
    if add_back:
        choices.append(Choice("<- Back", lambda: False))
    subcommand = (
        ListPrompt("What do you want to do?", choices)
        .prompt(style=default_style)
        .data
    )
    return subcommand()
예제 #5
0
파일: plugin.py 프로젝트: nonebot/nb-cli
def create_plugin(
    name: Optional[str] = None,
    plugin_dir: Optional[str] = None,
    template: Optional[str] = None,
) -> bool:
    if not name:
        name = InputPrompt(
            "Plugin Name:", validator=lambda x: len(x) > 0
        ).prompt(style=default_style)

    if not plugin_dir:
        detected: List[Choice[None]] = [
            Choice(str(x)) for x in Path(".").glob("**/plugins/") if x.is_dir()
        ]
        plugin_dir = (
            ListPrompt(
                "Where to store the plugin?", detected + [Choice("Other")]
            )
            .prompt(style=default_style)
            .name
        )
        if plugin_dir == "Other":
            plugin_dir = InputPrompt(
                "Plugin Dir:",
                validator=lambda x: len(x) > 0 and Path(x).is_dir(),
            ).prompt(style=default_style)
    elif not Path(plugin_dir).is_dir():
        click.secho(f"Plugin Dir is not a directory!", fg="yellow")
        plugin_dir = InputPrompt(
            "Plugin Dir:", validator=lambda x: len(x) > 0 and Path(x).is_dir()
        ).prompt(style=default_style)
    if not template:
        sub_plugin = ConfirmPrompt(
            "Do you want to load sub plugins in current plugin?",
            default_choice=False,
        ).prompt(style=default_style)
        cookiecutter(
            str((Path(__file__).parent.parent / "plugin").resolve()),
            no_input=True,
            output_dir=plugin_dir,
            extra_context={"plugin_name": name, "sub_plugin": sub_plugin},
        )
    else:
        cookiecutter(
            template, output_dir=plugin_dir, extra_context={"plugin_name": name}
        )
    return True
예제 #6
0
파일: plugin.py 프로젝트: nonebot/nb-cli
def plugin_no_subcommand(add_back: bool = False) -> bool:
    choices: List[Choice[Callable[[], bool]]] = [
        Choice("Create a New NoneBot Plugin", create_plugin),
        Choice("List All Published Plugins", partial(search_plugin, "")),
        Choice("Search for Published Plugin", search_plugin),
        Choice("Install a Published Plugin", install_plugin),
        Choice("Update a Published Plugin", update_plugin),
        Choice("Remove an Installed Plugin", uninstall_plugin),
    ]
    if add_back:
        choices.append(Choice("<- Back", lambda: False))
    subcommand = (
        ListPrompt("What do you want to do?", choices)
        .prompt(style=default_style)
        .data
    )
    return subcommand()
예제 #7
0
파일: __init__.py 프로젝트: nonebot/nb-cli
def handle_no_subcommand():
    draw_logo()
    click.echo("\n\b")
    click.secho("Welcome to NoneBot CLI!", fg="green", bold=True)

    while True:
        choices: List[Choice[Callable[[], bool]]] = [
            Choice("Show Logo", draw_logo),
            Choice("Create a New Project", create_project),
            Choice("Run the Bot in Current Folder", run_bot),
            Choice("Driver ->", partial(driver_no_subcommand, True)),
            Choice("Plugin ->", partial(plugin_no_subcommand, True)),
            Choice("Adapter ->", partial(adapter_no_subcommand, True)),
            Choice("Deploy ->", partial(deploy_no_subcommand, True)),
        ]
        subcommand = (ListPrompt("What do you want to do?",
                                 choices).prompt(style=default_style).data)
        result = subcommand()
        if result:
            break
예제 #8
0
파일: adapter.py 프로젝트: nonebot/nb-cli
def create_adapter(name: Optional[str] = None,
                   adapter_dir: Optional[str] = None) -> bool:
    if not name:
        name = InputPrompt(
            "Adapter Name:",
            validator=lambda x: len(x) > 0).prompt(style=default_style)

    if not adapter_dir:
        detected: List[Choice[None]] = [
            Choice(str(x))
            for x in Path(".").glob("**/adapters/") if x.is_dir()
        ] or [
            Choice(f"{x}/adapters/")
            for x in Path(".").glob("*/") if x.is_dir()
            and not x.name.startswith(".") and not x.name.startswith("_")
        ]
        adapter_dir = (ListPrompt(
            "Where to store the adapter?",
            detected + [Choice("Other")]).prompt(style=default_style).name)
        if adapter_dir == "Other":
            adapter_dir = InputPrompt(
                "Adapter Dir:",
                validator=lambda x: len(x) > 0 and Path(x).is_dir(),
            ).prompt(style=default_style)
    elif not Path(adapter_dir).is_dir():
        click.secho(f"Adapter Dir is not a directory!", fg="yellow")
        adapter_dir = InputPrompt(
            "Adapter Dir:",
            validator=lambda x: len(x) > 0 and Path(x).is_dir()).prompt(
                style=default_style)

    cookiecutter(
        str((Path(__file__).parent.parent / "adapter").resolve()),
        no_input=True,
        output_dir=adapter_dir,
        extra_context={
            "adapter_name": name,
        },
    )
    return True