Authoring Extensions
This guide will walk you through the process of creating a Bebop extension. Bebop supports multiple languages for extension development, and the creation process is interactive and user-friendly.
Getting Started
Run the Extension Creator Script
To start creating your Bebop extension, run the following command in your terminal:
This script will guide you through an interactive process to set up your extension project.
Interactive Setup Process
The Extension Creator script will walk you through the following steps:
-
Choose Directory:
- You’ll be prompted to enter a directory name for your extension.
- This name will also be used as the extension name.
- If you don’t provide a name, a default name with a timestamp will be used.
-
Select EDK (Extension Development Kit):
- You’ll be presented with options to choose your preferred EDK:
- TypeScript
- TinyGo
- AssemblyScript
- Select the EDK that best fits your development preferences.
- You’ll be presented with options to choose your preferred EDK:
-
Download and Extract EDK:
- The script will download and extract the selected EDK into your chosen directory.
-
Finalize Setup:
- The script will modify the
chord.json
file in your project, setting the correct name and initial version.
- The script will modify the
Post-Setup
After the script completes, you’ll find your new extension project in the directory you specified. The script will provide you with next steps, including:
- Changing to your new extension directory:
- A reminder to visit the Bebop documentation for more information on developing extensions.
Understanding the Extension Structure
Let’s explore the structure of your new extension project, using TypeScript as an example.
The main file for your extension is typically src/index.ts
. Here’s its basic structure:
chordCompile
is the entry point for your extension. It’s called by the Bebop compiler.readContext()
provides theCompilerContext
, which contains all the information about the Bebop schemas being processed.generateCode
is where you’ll implement your custom logic to generate code based on the input schemas.commitResult
sends the generated code back to the Bebop compiler.
Developing Your Extension
Let’s walk through an example of creating a simple generator extension. This extension will generate basic validation functions for structs.
- First, ensure your
chord.json
includes the necessary contribution definitions:
- Now, let’s implement the
generateCode
function:
This simple generator creates a basic validation function for each struct marked with the @validate
decorator, checking if all fields have non-null values.
Understanding the Generator Context
The CompilerContext
provides access to all the information about the Bebop schemas being processed. Key properties include:
definitions
: A map of all type definitions (structs, enums, etc.) in the schema.services
: A map of all service definitions.options
: Compiler options and metadata.
Building Your Extension
To build your extension, use the chordc
command-line tool:
This command uses the configuration in your chord.json
file to build your extension.
Testing Your Extension
To test your extension locally:
- Create a test Bebop schema that uses your decorators.
- Run the Bebop compiler with your extension on this schema.
- Verify that the output matches your expectations.
Next Steps
Once you’ve developed and tested your extension, you’re ready to publish it!
By publishing your extension, you can share your work with the Bebop community and allow others to use your custom generators and decorators in their projects.
Remember, extensions are a powerful way to extend Bebop’s capabilities. They allow you to create custom code generation logic, add new decorators, and tailor Bebop to specific use cases.