nigeleke / sbt-rust

An sbt plugin to integrate Rust / Cargo / Yew / Trunk developments within Scala & SBT projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sbt-rust

BSD 3 Clause License Language Build Version Scala Steward badge

Site | GitHub | API

An sbt plugin to integrate Rust development within Scala & SBT projects. This is useful for Rust projects built with Cargo or Yew projects built with Trunk.

Commands

The plugin attempts to use the trunk package manager by default. This default can be overridden by setting Rust / tooling := CargoPackageManager, in which case cargo will then be used.

Command CargoPackageManager TrunkPackageManager sbt Command
rustClean cargo clean [1] trunk clean clean
rustCargoClean [2] cargo clean [1] cargo clean [1]
rustBuild cargo build [3] trunk build compile
rustTest cargo test cargo test test
rustRun cargo run [4] trunk serve [5] run
rustRelease cargo build [6] --release trunk build --release
rustPackage cargo package cargo package package
rustConfig cargo config get [7] trunk config show
rustDoc cargo doc cargo doc doc
[1] Rust / cleanOptions
[2] Forces cargo clean when using trunk package manager (trunk clean deletes dist folder, but not target folder contents)
[3] Rust / debugOptions
[4] Rust / runOptions
[5] Waits for server to exit or ctrl-c from the user. The ctrl-c will abort the server process.
[6] Rust / releaseOptions
[7] Requires nightly build (as stands in April 2023)

Motivation

The plugin is intended for use in a multi-project build.sbt file, where the frontend project is developed using Yew. This enables the Yew project to be developed while remaining within the Scala sbt infrastructure.

The plugin really only "makes sense" for multi-project environments. If the user only has a single cargo / trunk project they may as well use the tool directly. However a build.sbt could be created as a wrapper and this plugin used if desired. The expected folder structure for this is demonstrated in the sbt-test scripted tests.

Usage

Pre-requisites

Rust, Cargo, Yew and / or Trunk must be installed independently (as required) and their binaries accessible on the current PATH.

The plugin is not currently published in the Maven Repository (or similar), however it is easy enough to build and publish locally, as shown below:

git clone https://github.com/nigeleke/sbt-rust.git
sbt publishLocal

plugins.sbt

addSbtPlugin("nigeleke" % "sbt-rust" % "<version>")

build.sbt

val scala3Version = "3.m.n"

lazy val root = project
  .in(file("."))
  .settings(
    name           := "myproject"
  )
  .aggregate(core, ui)

lazy val core = project
  .in(file("core"))
  .settings(
    name           := "myproject-core",
    scalaVersion   := scala3Version,
    ...
  )

lazy val ui = project
  .in(file("ui"))
  .enablePlugins(RustPlugin)
  .settings(
    name := "myproject-ui",
    Rust / tooling             := TrunkPackageManager, // optional, default. Allowed CargoPackageManager or TrunkPackageManager
    Rust / debugOptions   := "",   // optional, default
    Rust / releaseOptions := "",   // optional, default
    Rust / runOptions     := "",   // optional, default
    ...
  )

The folder layout and files within the ui folder are expected to match the cargo new ui or trunk new ui layout. As such, the cargo or trunk tooling can continue to be used independently. (Consideration was given to forcing the standard using src/main/rust folder structure but this was rejected as being too opinionated, and not inline with the defaults of the underlying cargo and trunk tooling)

Plugin Development

This plugin requires sbt 1.0.0+

Testing

Run test for regular unit tests.

Run scripted for sbt script tests.

Acknowledgements

  1. ChatGPT examples, which in turn were derived from:

    a. https://www.scala-sbt.org/1.x/docs/Writing-Plugins.html

    b. 404 - https://github.com/lightbend/activator-rust/blob/master/src/main/scala/RustPlugin.scala

    c. 404 - https://github.com/jeffreyolchovy/sbt-rustup/blob/master/src/main/scala/sbtrustup/RustupPlugin.scala

  2. Pritam Kadam - Write & test your own scala SBT plugin…

  3. Stackoverflow: Running custom sbt plugin task for one project in a multi-project build.sbt

  4. Stackoverflow: How can I use sbt commands like clean and compile in my custom sbt plugin

About

An sbt plugin to integrate Rust / Cargo / Yew / Trunk developments within Scala & SBT projects.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Scala 89.8%Language:Rust 6.3%Language:Nix 2.5%Language:HTML 1.4%