DEV Community

Ramin Farajpour Cami
Ramin Farajpour Cami

Posted on

How to write your building tools for multiple applications with cargo

Floki the boat builder

How to write your building tools for multiple applications with cargo [cargo-floki]

(You can have multiple outputs of multiple applications at the same time by executing a command.)

Building a tool for managing the build process of multiple applications can be quite complex,
and writing build tools in Rust involves creating a program that automates various tasks related to building,
testing, and packaging your Rust projects.

How to install cargo-floki :

$ cargo install --path .
Enter fullscreen mode Exit fullscreen mode
Usage:
$  cargo floki --help
Usage: cargo-floki [OPTIONS] <COMMAND>

Commands:
  init    Adds a default floki.toml file to current directory
  build   Compile the client and server
  clean   Remove the target directories (in app, client and server)
  test    Run the cargo tests for app, client and server
  update  Run the cargo update for app, client and server
  run     Run app
  doc     Docs
  help    Print this message or the help of the given subcommand(s)

Options:
  -r, --release          Build artifacts in release mode, with optimizations
  -v, --verbose...       Verbosity (none: errors & warnings, -v: verbose, --vv: very verbose, --vvv: output everything)
  -c, --config <CONFIG>  Path to configuration file (defaults to './floki.toml')
  -h, --help             Print help

Enter fullscreen mode Exit fullscreen mode

Below is a basic outline of how you might approach building such a tool:

Project Structure:

Create a standardized project structure to ensure consistency across different applications. This can simplify the build process by having a predictable layout.

We have 2 project for my example app and client:

$ tree
.
├── app (Project 1)
│   ├── Cargo.toml
│   └── src
│       └── lib.rs  
└── client (Project 2)
    ├── Cargo.toml
    └── src
        └── main.rs
Enter fullscreen mode Exit fullscreen mode

Configuration:

Allow for configuration options so users can customize the build process for their specific needs.
Consider using configuration files, environment variables, or a configuration management system.

I define project path app and client and generate floki.toml :

$ cargo floki init
$ cat floki.toml 
[floki]
main_service = "floki_projects/app"
client_service = "floki_projects/client"
Enter fullscreen mode Exit fullscreen mode

Logging and Reporting:

Implement logging and reporting features to provide detailed feedback during the build process. This helps developers identify and fix issues more efficiently.

$ cargo floki -v build
Enter fullscreen mode Exit fullscreen mode

Testing:

Implement testing for your build tool itself to ensure reliability and stability.

I write a test for app project:

$ cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running unittests src/main.rs (cargo-floki/target/debug/deps/client-5bd14cdeb41d4264)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

$ cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running unittests src/lib.rs (cargo-floki/target/debug/deps/app-f55b4b7fb682729e)

running 1 test
test tests::test_add ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests app

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


Enter fullscreen mode Exit fullscreen mode

Documentation:

Provide comprehensive documentation to help users understand how to configure and use the build tool effectively.

Generate documentation for all projects:

$ cargo floki doc
Enter fullscreen mode Exit fullscreen mode

Continuous Integration (CI) Integration:

Ensure compatibility with popular CI/CD systems like Jenkins, Travis CI, GitLab CI, or GitHub Actions. This facilitates automated builds and deployments.

Build all projects that cleans, build, testing the project directory.

$ cargo floki build
$ cargo floki -r build
$ cargo floki clean
$ cargo floki update
Enter fullscreen mode Exit fullscreen mode

All these steps are for ease of work and speed of application development.

Github code : https://github.com/raminfp/cargo-floki

Top comments (0)