Check out the announcement post to learn more about cargo-px
and the problems it solves with respect to code generation in Rust projects.
You can install cargo-px
from crates.io with
bash
cargo install cargo-px --locked
It is designed as a cargo
proxy: instead of invoking cargo <CMD> <ARGS>
, you go for cargo px <CMD> <ARGS>
. For example, you go for cargo px build --all-features
instead of cargo build --all-features
.
cargo px
examines your workspace every time you invoke it.
If any of your crates needs to be generated, it will invoke the respective code generators before forwarding the command and its arguments to cargo.
cargo px
leverages the metadata
section.
In the crate that you want to see generated, you fill in the [package.metadata.px.generate
] section as follows:
```toml [package] name = "..." version = "..."
[package.metadata.px.generate]
generatortype = "cargoworkspace_binary"
generator_name = "bp"
generator_args = ["--quiet", "--profile", "optimised"] ```
cargo-px
will detect the configuration and invoke cargo run --bin bp -- --quiet --profile="optimised"
for you.
If there are multiple crates that need to be code-generated, cargo-px
will invoke the respective code-generators in an order that takes into account the dependency graph (i.e. dependencies are always code-generated before their dependents).
cargo-px
will also set two environment variables for the code generator:
CARGO_PX_GENERATED_PKG_MANIFEST_PATH
, the path to the Cargo.toml
file of the crate that needs to be generated;CARGO_PX_WORKSPACE_ROOT_DIR
, the path to the Cargo.toml
file that defines the current workspace (i.e. the one that contains the [workspace]
section).You can use the cargo_px_env
crate to retrieve and work with these environment variables.
If you are committing the generated code, it might be desirable to verify in CI that it's up-to-date.
You can do so by running:
```bash
cargo px check
git
in version controlgit diff --quiet HEAD || (echo "The code-generated crates are stale. Re-run 'cargo px check locally and commit the results" && exit 1) ```
Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.