Giter VIP home page Giter VIP logo

cargo-runner's Introduction

VSCode Cargo Runner

10X Rust Developer Tool to Run, Build, or Test without Mental Overhead

cover

Cargo Runner is a powerful and intuitive tool designed to streamline the development workflow for Rust programmers. It integrates seamlessly into your development environment, allowing you to execute essential tasks like running, building, and testing Rust projects with minimal effort and maximum efficiency.

Features

  • Context Aware Command Runner : Just fire the Keystroke CMD + R let it do the magic of either doing cargo run | build | test | bench.

  • Makefile Integration: Offers the ability to override Cargo run or Cargo build commands with a custom Makefile, providing more control and flexibility for complex build processes.

  • Enhanced Testing with Cargo-Nextest: well-defined preset and integration to cargo-nextest command if it is installed it will replace the default cargo test command, if you need more power you can override arguments.

  • Override Arguments : Quickly Override Command Arguments on different context such as: run , build, test, bench and env using CMD + SHIFT +R

  • Codelldb Debugger Integration: Automatically integrates with codelldb debugger for running Rust tests if there is a breakpoint.

  • Rust Analyzer Runnables Fallback: Uses Rust Analyzer runnables if there is no supported context match, e.g., doc tests.

  • Support Cargo Workspace : Cargo runner works on simply rust project to complex Cargo workspace.

Demo Screenshot

Cargo Run

  1. Go Inside any files that can be run :

    • src/main.rs
    • src/bin/*
    • examples/*
    • any file declare as [[bin]] on Cargo.toml

e.g.

[[bin]] 
path = "src/example.rs"
name ="example"

Note: the file you wanna run must have a fn main block

  1. Press CMD+R

Run


Cargo Build

  1. Go to Any build.rs file that hasfn main block

  2. Press CMD+R on Cursor

Build


Cargo Test or Cargo Nextest (if installed) for specific test

  1. Go to any file with test block supports anykind of test macro e.g. #[test] or #[tokio::test]

  2. Place your cursor inside the test block you wanna run test

  3. Press CMD+R

Note: If Cargo Nextest is installed it would use that as default test runner instead of cargo test

Test

Debug Test

  1. Add a breakpoint to any test block

  2. Place your cursor inside the test block that have that breakpoint

  3. Press CMD+R

Run

Fallback Runner (using rust-analyzer) for running all test

  1. Place your cursor somewhere inside of a mod test block

  2. Press CMD+R

fallback

Fallback Runner (using rust-analyzer) for running doc-test

  1. Place your cursor somewhere inside a doc test block

  2. Press CMD+R

fallback

Adding Command Arguments

  1. Press CMD+SHIFT+R

  2. Choose context from any of the following options:

    • run
    • build
    • test
    • bench
    • env
  3. Type those parameters you wanna add to override the default

e.g. choose: env

  1. Type on the user input the args you wanna pass as override argument

e.g.

RUSTFLAGS="-Awarnings"

Note: .cargo_runner.toml would be created per create , you can have multiple .cargo_runner.toml on workspace

Important: You may wanna add this to .gitignore file

Removing Command Arguments

  1. Press CMD+SHIFT+R

  2. Choose context from any of the following options:

    • run
    • build
    • test
    • bench
    • env
  3. Press Enter (dont type anything)

This would remove the parameters RUSTFLAGS="-Awarnings" on .cargo_runner.toml file


Advanced Features

Custom Build Scripts with Makefile.

Create a Makefile on Rust project, you can have multiple Makefile if your working with Cargo Workspace The choice is yours

Makefile

below is example makefile , you can add to you project to test

# Makefile for a Rust project using cargo-leptos and cargo-nextest

# Default target
.PHONY: all
all: build

# Build target
.PHONY: build
build:
	cargo build --package REPLACE_WITH_YOUR_PACKAGE_NAME

.PHONY: run
run:
	cargo run --package REPLACE_WITH_YOUR_PACKAGE_NAME --bin REPLACE_WITH_YOUR_BIN_NAME

# Test target
.PHONY: test
test:
	cargo test

# Clean up
.PHONY: clean
clean:
	cargo clean

By providing a comprehensive and user-friendly tool, Cargo Runner aims to significantly enhance the productivity and efficiency of Rust developers.

cargo-runner's People

Contributors

codeitlikemiley avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

cargo-runner's Issues

Handle arguments

While cmd-R is convenient, sometimes we need to start our runs with arguments, like:

$ cargo run -- --num_agents=5 --use_submarine_base

Suggestions:

  1. Clarify the documentation to explain how arguments are handled by cargo-runner.

  2. Add a mechanism, if not present, to allow adding and changing arguments.

Naturally, we wouldn't want to interfere with the convenience of the simple cmd-R command. So maybe shift-cmd-R brings up a dialog box for parameters? Or cargo-runner would look for a default file like .cargo-runner-args and append its contents, if present? Either way, it should default to the last used arguments.

Add support to run benchmark rust nightly

on nightly version of rust we can do something like this
see: https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html

#![feature(test)]

extern crate test;

pub fn add_two(a: i32) -> i32 {
    a + 2
}

#[cfg(test)]
mod tests {
    use super::*;
    use test::Bencher;

    #[test]
    fn it_works() {
        assert_eq!(4, add_two(2));
    }

    #[bench]
    fn bench_add_two(b: &mut Bencher) {
        b.iter(|| add_two(2));
    }
}

note:
we need to parse #![feature(test)] , for this to be a valid nightly rust bench

then matching #[bench] pattern

placing our cursor on any of this line

#[bench]
    fn bench_add_two(b: &mut Bencher) {
        b.iter(|| add_two(2));
    }

should trigger the appropriate bench command

pure text should be properly resolved

There are cases when a user uses a non rust convention parameters

e.g.
-- /path/to/file1

which resolves to

cargo run -p packageName --bin binName -- --=/path/to/file1

that should resolve to

cargo run -p packageName --bin binName -- path/to/file1

also passing text without -- right now is erroring out Cannot read properties of undefined (reading 'replace')

not all CLI built with rust arent using the cli convention as such there would be error on the overriding of parameters

possible solution is...

pass in the text as is....

Add support to benchmark stable rust with Criterion

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use hover_bench::fibonacci;
/// if we place our cursor on line: 30 `c.bench_function`
/// parse the id which is fib_100 //TODO: Create this fn
/// we use the get_package fn to get package
/// we use find_cargo fn to get the nearest cargo.toml on workspace
/// read file content of cargo.toml
/// parse and check if we have [[bench]] // TODO: adjust CargoType
/// if we have push each new value to a our vec of benches // TODO: add logic to pushing element that match on list of bench
/// 
/// use case 1: benches/fibonacci.rs or benches/fibonacci/main.rs (default naming convention)
/// `Cargo.toml`
/// ```toml
/// [[bench]]
/// name = "fibonacci"
/// harness = false
/// ```
/// use case 2: custom path - benches/same_name/same_name.rs
/// ```toml
/// [[bench]]
/// name = "same_name"
/// harness = false
/// path = "benches/same_name/same_name.rs"
/// ```
/// 
/// TODO: generate commands the following commands
/// Commands to run
/// 1. Run specific benchmark on specific crate
/// ```rust
/// cargo bench --package hover_bench --bench fibonacci
/// ```
/// 2. Run specific benchmark on specific crate and specific input
/// ```sh
/// cargo bench --package hover_bench --bench fibonacci -- fib_100
/// ```
fn criterion_benchmark(c: &mut Criterion) { // TODO: Pressing CMD+R here or outside of this scope we should run cmd #1
    //TODO:pressing CMD + R below should invoke cmd #2 
    c.bench_function("fib_100", |b| b.iter(|| fibonacci(black_box(100))));
}
// tuple type macro with at least 2 params, we can add as much benchmark function to run
criterion_group!(benches, criterion_benchmark); // TODO: this would run command #1
// this is the same as calling fn main () {}
criterion_main!(benches); // TODO: this would run command #1

Support Running Single File Rust Scripts

When on a single file rust script , pressing CMD + R

we should check for the first line of the file to contain

#!/usr/bin/env -S cargo +nightly -Zscript

we should use a pattern since we can also declare this as

#!/usr/bin/env cargo +nightly -Zscript

if the pattern match then we should

execute:

./filename.rs

User should be the one to make the their single file rust script executable?

or on the first run if it is not executable then make the file executable

2nd and succeeding run would just invoke ./filename.rs

Failing on remote instance

I can't get Cargo Runner to work when SSH'ed into a remote instance. Running cargo run and cargo run --package test01 --bin test01 in the VS Code terminal works fine, but executing Cargo Runner results in an error.

 *  Executing task: cargo run --package test01 --bin test01 

/bin/bash: line 1: cargo: command not found

 *  The terminal process "/bin/bash '-c', 'cargo run --package test01 --bin test01'" failed to launch (exit code: 127). 

Any idea what the reason could be?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.