Developing and Integrating Custom Commands with Empress' `bench_manager` Guide

Introduction

Empress’ bench_manager module is a powerful tool for developers working within the Empress framework. It allows for the creation and execution of custom commands, enhancing the functionality and adaptability of the system. A key component of this is the interaction between the bench_manager and the custom applications you develop.

In this guide, we will delve into how to create custom commands that are accessible to the bench_manager, exploring the process of creating a commands module under your parent module. We will also discuss the integration of these commands within the system and showcase how to execute them.

Creating Custom Commands

To make your custom command available to the bench_manager, you need to create a commands module under your parent module. This module will contain your custom commands, written with a click wrapper and a commands variable. The commands variable should hold a list of click function objects, which represent your commands.

The directory structure of a Empress App, let’s say flags, with a custom command might look like this:

frappe-bench
|──apps
    |── frappe
    ├── flags
    │   ├── README.md
    │   ├── flags
    │   │   ├── commands    <------ commands module
    │   ├── license.txt
    │   ├── requirements.txt
    │   └── setup.py

The commands module can be a single file, such as commands.py, or a directory with an __init__.py file.

For instance, if you have a custom application named ‘flags’, you might create a custom command like this:

# file_path: frappe-bench/apps/flags/flags/commands.py
import click

@click.command('set-flags')
@click.argument('state', type=click.Choice(['on', 'off']))
def set_flags(state):
    from flags.utils import set_flags
    set_flags(state=state)

commands = [
    set_flags
]

Executing Custom Commands

Once your commands module is set up with your custom commands, they can be executed within the context of the current bench.

For example, to execute the set_flags command, you would enter:

➜ bench set-flags
Flags are set to state: 'on'

Conclusion: The Power of Custom Commands

Through the bench_manager module, Empress enables developers to extend the functionality of their applications by creating custom commands. This feature allows for increased flexibility and customization of the Empress system, empowering developers to create more robust and tailored business solutions. By integrating these commands into the system architecture, developers can enhance their application’s interaction with the Empress framework, ultimately driving more efficient and effective operations.