-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add parallel mode between independent commands (#4)
* feat: add parallel mode * release: 0.1.4 * doc: update README
- Loading branch information
Showing
7 changed files
with
188 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,80 @@ | ||
# cargo-q | ||
|
||
Cargo subcommand to run multiple Cargo commands in a time. | ||
A Cargo subcommand that allows running multiple Cargo commands in a time. | ||
|
||
<details> | ||
<summary>TODO</summary> | ||
|
||
- ✅ Add sequential execution | ||
- ✅ Add ; as command separator | ||
- ✅ Add & as command separator | ||
- ❌ Add > as command separator | ||
- ❌ Add parallel execution | ||
- ✅ Add ; as command separator for independent commands | ||
- ✅ Add & as command separator for dependent commands | ||
- ✅ Add parallel execution between independent commands | ||
- ❌ Add > as command separator for dependent commands | ||
- ❌ Support mixed separators | ||
|
||
</details> | ||
|
||
## Installation | ||
|
||
```bash | ||
cargo install cargo-q | ||
``` | ||
|
||
## Features | ||
|
||
- Run multiple Cargo commands sequentially | ||
- Use different separators for command execution: | ||
- Space: Run commands sequentially (independent execution) | ||
- `;`: Run independent commands sequentially | ||
- `&`: Run commands with dependencies (each command depends on previous command's success) | ||
- Support parallel execution for independent commands | ||
- Verbose mode for detailed output | ||
|
||
## Usage | ||
|
||
### Run a command | ||
### Run a Single Command | ||
|
||
```bash | ||
cargo q cmd | ||
cargo q check | ||
``` | ||
|
||
### Run multiple commands | ||
### Run Multiple Commands | ||
|
||
#### Sequential Execution (Space Separator) | ||
```bash | ||
# default quiet mode | ||
cargo q "check test" # run `check` first then test whether `check` is successful | ||
cargo q 'check test' # ' and " are the same | ||
cargo q "test --features feature1 ; run" # if a command has dash or parameters, use ; as separator | ||
# Run commands sequentially and independently | ||
cargo q "check test" # Runs check, then test | ||
cargo q 'check test' # Single and double quotes both work | ||
``` | ||
|
||
cargo q "check & test & run" # run `check` first, then `test` if `check` is successful, and `run` if both are successful | ||
cargo q "check&test&run" # same as above | ||
#### Independent Commands (`;` Separator) | ||
```bash | ||
# Run commands sequentially and independently | ||
cargo q "test --features feature1 ; run" # Commands with parameters need ; separator | ||
``` | ||
|
||
#### Dependent Commands (`&` Separator) | ||
```bash | ||
# Run commands with explicit dependencies | ||
cargo q "check & test & run" # Each command runs only if previous command succeeds | ||
cargo q "check&test&run" # Spaces around & are optional | ||
``` | ||
|
||
cargo q "test > analyze" # run `test` first, then `analyze` with `test`'s output | ||
cargo q "test>analyze" # same as above | ||
### Parallel Execution | ||
|
||
# verbose mode | ||
cargo q -v "check test" # run `check` first, then `test` if `check` is successful | ||
cargo q --verbose "check test" # same as above | ||
```bash | ||
# Run independent commands in parallel | ||
cargo q -p "build -r; build" # Run both commands in parallel | ||
cargo q --parallel "check; test" # Same as above | ||
``` | ||
|
||
### Run commands in parallel | ||
### Verbose Output | ||
|
||
```bash | ||
cargo q -p "build -r; build" # run `build -r` and `build` in parallel | ||
cargo q --parallel "build -r; build" # same as above | ||
cargo q -v "check test" # Show detailed output | ||
cargo q --verbose "check test" # Same as above | ||
``` | ||
|
||
## License | ||
|
||
Licensed under Apache-2.0 license ([LICENSE](LICENSE) or http://opensource.org/licenses/Apache-2.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::sync::mpsc::{self, Receiver, Sender}; | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread::{self, JoinHandle}; | ||
|
||
type Job = Box<dyn FnOnce() + Send + 'static>; | ||
|
||
pub struct ThreadPool { | ||
workers: Vec<Worker>, | ||
sender: Option<Sender<Job>>, | ||
} | ||
|
||
struct Worker { | ||
_id: usize, | ||
thread: Option<JoinHandle<()>>, | ||
} | ||
|
||
impl ThreadPool { | ||
pub fn new(size: usize) -> ThreadPool { | ||
assert!(size > 0); | ||
|
||
let (sender, receiver) = mpsc::channel(); | ||
let receiver = Arc::new(Mutex::new(receiver)); | ||
let mut workers = Vec::with_capacity(size); | ||
|
||
for id in 0..size { | ||
workers.push(Worker::new(id, Arc::clone(&receiver))); | ||
} | ||
|
||
ThreadPool { | ||
workers, | ||
sender: Some(sender), | ||
} | ||
} | ||
|
||
pub fn execute<F>(&self, f: F) | ||
where | ||
F: FnOnce() + Send + 'static, | ||
{ | ||
let job = Box::new(f); | ||
self.sender.as_ref().unwrap().send(job).unwrap(); | ||
} | ||
} | ||
|
||
impl Drop for ThreadPool { | ||
fn drop(&mut self) { | ||
drop(self.sender.take()); | ||
|
||
for worker in &mut self.workers { | ||
if let Some(thread) = worker.thread.take() { | ||
thread.join().unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Worker { | ||
fn new(id: usize, receiver: Arc<Mutex<Receiver<Job>>>) -> Worker { | ||
let thread = thread::spawn(move || loop { | ||
let message = receiver.lock().unwrap().recv(); | ||
|
||
match message { | ||
Ok(job) => { | ||
job(); | ||
} | ||
Err(_) => { | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
Worker { | ||
_id: id, | ||
thread: Some(thread), | ||
} | ||
} | ||
} |