Using cargo build scripts
Testing sdk also has the interface for cargo build scripts. Some IDEs can analyze files generated in build scripts, providing code completion and error highlighting for code generated in build scripts. But using it may be a little bit tricky because build scripts are not designed for such things.
To set up IDE you need to do the following:
CLion:
- in the Help -> Actions -> Experimental Futuresenableorg.rust.cargo.evaluate.build.scripts
- refresh cargo project in order to update generated code: change Cargo.tomland build from IDE or pressRefresh Cargo Projectin Cargo tab
VS Code:
- install the rust-analyzerplugin
- change Cargo.tomlto let plugin update code from generated files
The update will not work instantly: you should build service to wasm, and then trigger build.rs run again, but for the native target.
And here is the example of using this:
- build.rs
- src/main.rs
- Cargo.toml
rust
rust
rust
rust
toml
toml
- We create a vector of pairs (service_name, service_description) to pass to the generator. The structure is the same with multi-service marine_test.
- We check if we build for a non-wasm target. As we build this marine service only for wasm32-wasiand tests are built for native target, we can generatemarine_test_envonly for tests. This is needed because our generator depends on the artifacts fromwasm32-wasibuild. We suggest using a separate crate for using build scripts for testing purposes. It is here for simplicity.
- We pass our services, the name of the file to generate, and a path to the build script file to the marine_test_envgenerator. Just always usefile!()for the last argument. The generated file will be in the directory specified by theOUT_DIRvariable, which is set by cargo. The build script must not change any files outside of this directory.
- We set up conditions to re-run the build script. It must be customized, a good choice is to re-run the build script when .wasm files or Config.tomlare changed.
- We import the generated file with the marine_test_envdefinition to the project.
- Do not forget to add marine-rs-sdk-testto thebuild-dependenciessection ofCargo.toml.