Adding or changing turbo-module templates
In addition to generating the bindings between Hermes and Rust, uniffi-bindgen-react-native
generates the files needed to run this as a turbo-module. The list of files are documented elsewhere in this book.
Templates are written for Askama templating library.
Changing the templates for these files is relatively simple. This PR is a good example of adding a file.
- Template files are in the
codegen/templates
directory. - Template configuration are in
codegen/mod.rs
file.
Adding a new template
- Add new template to the
codegen/templates
directory. - Add a new
RenderedFile
struct, which specifies the template, and its path to thefiles
module incodegen/mod.rs
. - Add an entry to the list of generated files in this book.
The template context will have quite a lot of useful information data-structures about the project; the most prominent:
ModuleMetadata
, which is generated from thelib.a
file from the uniffi contents of the Rust library.ProjectConfig
which is the in-memory representation of the YAML configuration file.CrateMetadata
which is data about the crate derived fromcargo metadata
.
Testing changes
The ./scripts/test-turbo-modules.sh
script runs a suite of tests, simulating the steps in the Getting Started tutorial:
- do the names of the ubrn generated files match up with the files builder-bob generated files?
- do generated identifiers in the ubrn generated files match up with those generated by builder-bob?
- does an Android app compile
- does an iOS app compile
If you want to test a particular configuration, you can use the command with a series of options:
Usage: ./scripts/test-turbo-modules.sh [options] [PROJECT_DIR]
Options:
-A, --android Build for Android.
-I, --ios Build for iOS.
-C, --ubrn-config Use a ubrn config file.
-T, --app-tsx Use a App.tsx file.
-s, --slug PROJECT_SLUG Specify the project slug (default: my-test-library).
-i, --ios-name IOS_NAME Specify the iOS project name (default: MyTestLibrary).
-u, --builder-bob-version VERSION Specify the version of builder-bob to use (default: latest).
-k, --keep-directory-on-exit Keep the PROJECT_DIR directory even if an error does not occur.
-f, --force-new-directory If PROJECT_DIR directory exist, remove it first.
-h, --help Display this help message.
Arguments:
PROJECT_DIR Specify the root directory for the project (default: my-test-library).
For example, to test if a configuration builds then runs:
fixtures=./integration/fixtures/turbo-module-testing
directory=/tmp/my-test-library
./scripts/test-turbo-modules.sh \
--ubrn-config $fixtures/ubrn.config.yaml \
--app-tsx $fixtures/App.tsx \
--ios \
--android \
--keep-directory-on-exit \
--force-new-directory \
"$directory"
cd "$directory"
yarn example start