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 Commands: Commands are triggered near your cursor, making the development process more intuitive and less disruptive.

  • Intelligent Cargo.toml Parsing: Automatically detects and parses Cargo.toml if the current file is of type bin or lib. This feature is essential for build operations and ensures that your project's configuration is always up-to-date.

  • One-Key Compilation & Execution: Bind Cargo run/build/test commands to a single keymap (CMD + R or CTRL + R), enabling swift project compilation and execution.

  • 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: Integrates with cargo-nextest (if installed) to offer up to 3x better performance on tests. This feature optimizes test execution, making it faster and more efficient.

  • Real-Time Feedback: Immediate visual feedback on the status of build and test processes, helping you identify and fix issues more quickly.

  • Customizable Environment: Tailor Cargo Runner to your workflow with customizable settings and keybindings.

  • Override Arguments : Quickly Override Command Arguments on different context such as: run , build, test, doctest and bench

Demo Screenshot

Override Arguments

Adding Arguments

  1. Press CMD+SHIFT+R
  2. Choose context from any of the following options:
    • run
    • build
    • test
    • doctest
    • bench
  3. Type those parameters you wanna add to override the default e.g.
--jobs 5 --all-features

Removing Arguments

  1. Press CMD+SHIFT+R

  2. Choose context from any of the following options:

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

This would remove the parameters --jobs 5 --all-features on .cargo_runner.toml file

Cargo Run

Press CMD+R on Cursor

  1. Simple Rust project with src/main.rs
  2. Any Rust file that has main function located at /bin/* folder (Implicit not declared on Cargo.toml)
  3. any file declared as [[bin]] on Cargo.toml e.g.
[[bin]] 
path = "src/example.rs"
name ="example"
  1. Workspace Crates

Run


Cargo Build

Press CMD+R on Cursor

  1. Any build.rs file that has main() fn

Build

Cargo Test or Cargo Nextest (if installed)

Press CMD+R on Cursor

  1. Any file on lib.rs or main.rs or file declared as [[bin]] that has the macro : #[test] and #[tokio::test]

Note: If you press inside the context of function test then it would run that single test, if you run it outside any function test which is inside any mod test it would run the whole test

Test

Doc Test (Lib crate ONLY)

Press CMD+R on Cursor Any file on crate Type lib that has doc test

  1. Multiline Comment Doctest
/**
    Logout from the service
    ```
    use crate::auth_service::auth::auth_service_server::AuthService;
    use crate::auth_service::auth::{LogoutRequest, LogoutResponse};
    use crate::auth_service::auth_impl::AuthServiceImpl;
    use tonic::Request;

    let service = AuthServiceImpl::default();
    let request = Request::new(LogoutRequest {
        token: "".to_string(),
    });
    let rt = tokio::runtime::Runtime::new();
    let response = rt.unwrap().block_on(service.logout(request)).unwrap();
    assert_eq!(response.into_inner().success, true);
    ```
    */
    async fn logout(
  1. using Doc Macro Doc Test
#[doc = r#"Signup to the service
use crate::auth_service::auth::auth_service_server::AuthService;
use crate::auth_service::auth::{SignupRequest, SignupResponse};
use crate::auth_service::auth_impl::AuthServiceImpl;
use tonic::Request;
let service = AuthServiceImpl::default();
let request = Request::new(SignupRequest {
    username: "Tonic".to_string(),
    password: "".to_string(),
});
let rt = tokio::runtime::Runtime::new();
let response = rt.unwrap().block_on(service.signup(request)).unwrap();
assert_eq!(response.into_inner().token, "Hello Tonic!".to_string());
```"#]
  1. Single Line /// comment Doc Test
///    Logout from the service
///    ```
///    use crate::auth_service::auth::auth_service_server::AuthService;
///    use crate::auth_service::auth::{LogoutRequest, LogoutResponse};
///    use crate::auth_service::auth_impl::AuthServiceImpl;
///    use tonic::Request;
///
///    let service = AuthServiceImpl::default();
///    let request = Request::new(LogoutRequest {
///        token: "".to_string(),
///    });
///    let rt = tokio::runtime::Runtime::new();
///    let response = rt.unwrap().block_on(service.logout(request)).unwrap();
///    assert_eq!(response.into_inner().success, true);
///    ```

Test

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

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

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

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

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....

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.

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.