Skip to content

Commit

Permalink
refact: use concrete types instead of vec of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Jun 9, 2023
1 parent 97c86d6 commit 35e6f32
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/longtask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo "Started task $1 $2"
while true; do
echo "Long task running... $count"
count=$((count+1))
sleep 3
sleep 5

if [ $count -eq "$2" ]; then
echo "Task $1 finished"
Expand Down
16 changes: 8 additions & 8 deletions src/cli/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl Command for WatchCommand {
if let Some(rules) = self.watches.run_on_init() {
stdout::info("Running on init commands.");

let results = rules::as_list(rules)
let results = rules::commands(rules)
.iter()
.map(|task| {
stdout::info(&format!("---- running: {} ----", task));
cmd::execute(task)
.map(|c| {
stdout::info(&format!("---- running: {} ----", c));
cmd::execute(c)
})
.collect::<Vec<Result<(), String>>>();

Expand All @@ -67,11 +67,11 @@ impl Command for WatchCommand {
self.verbose,
);

let results = rules::as_list(rules)
let results = rules::commands(rules)
.iter()
.map(|task| {
stdout::info(&format!("---- running: {} ----", task));
cmd::execute(task)
.map(|c| {
stdout::info(&format!("---- running: {} ----", c));
cmd::execute(c)
})
.collect::<Vec<Result<(), String>>>();

Expand Down
3 changes: 2 additions & 1 deletion src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ impl Rules {
}
}

pub fn as_list(rules: Vec<Vec<String>>) -> Vec<String> {
pub fn commands(rules: Vec<Rules>) -> Vec<String> {
rules
.iter()
.map(|rule| rule.commands())
.flat_map(|rule| rule.to_vec())
.collect::<Vec<String>>()
}
Expand Down
49 changes: 25 additions & 24 deletions src/watches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ impl Watches {

/// Returns the commands for first rule found for the given path
///
pub fn watch(&self, path: &str) -> Option<Vec<Vec<String>>> {
pub fn watch(&self, path: &str) -> Option<Vec<Rules>> {
let cmds = self
.rules
.iter()
.cloned()
.filter(|r| !r.ignore(path) && r.watch(path))
.map(|r| r.commands())
.collect::<Vec<Vec<String>>>();
.collect::<Vec<Rules>>();

if !cmds.is_empty() {
Some(cmds)
Expand All @@ -32,13 +32,13 @@ impl Watches {

/// Returns the commands for the rules that should run on init
///
pub fn run_on_init(&self) -> Option<Vec<Vec<String>>> {
pub fn run_on_init(&self) -> Option<Vec<Rules>> {
let cmds = self
.rules
.iter()
.cloned()
.filter(|r| r.run_on_init())
.map(|r| r.commands())
.collect::<Vec<Vec<String>>>();
.collect::<Vec<Rules>>();

if !cmds.is_empty() {
Some(cmds)
Expand Down Expand Up @@ -73,8 +73,8 @@ mod tests {
assert!(watches.watch(&get_absolute_path("test/main.rs")).is_some());
assert!(watches.watch(&get_absolute_path(".")).is_some());

let result = watches.watch(&get_absolute_path(".")).unwrap();
assert_eq!(vec!["cargo build"], result[0]);
let result = rules::commands(watches.watch(&get_absolute_path(".")).unwrap());
assert_eq!(vec!["cargo build"], result);
}

#[test]
Expand Down Expand Up @@ -129,8 +129,8 @@ mod tests {
change: 'src/**'
";
let watches = Watches::new(rules::from_yaml(&file_content).expect("Error parsing yaml"));
let result = watches.watch("src/test.rs").unwrap();
assert_eq!(vec!["cargo build"], result[0])
let result = rules::commands(watches.watch("src/test.rs").unwrap());
assert_eq!("cargo build", result[0])
}

#[test]
Expand All @@ -146,11 +146,11 @@ mod tests {
";
let watches = Watches::new(rules::from_yaml(&file_content).expect("Error parsing yaml"));

let result = watches.watch("test/test.rs").unwrap();
assert_eq!(vec!["cargo test"], result[0]);
let result = rules::commands(watches.watch("test/test.rs").unwrap());
assert_eq!("cargo test", result[0]);

let result_src = watches.watch("src/test.rs").unwrap();
assert_eq!(vec!["cargo build"], result_src[0]);
let result_src = rules::commands(watches.watch("src/test.rs").unwrap());
assert_eq!("cargo build", result_src[0]);
}

#[test]
Expand All @@ -170,13 +170,11 @@ mod tests {
";
let watches = Watches::new(rules::from_yaml(&file_content).expect("Error parsing yaml"));

let result = watches.watch("test/test.rs").unwrap();
assert_eq!(vec!["echo same"], result[0]);
assert_eq!(vec!["cargo test"], result[1]);
let result = rules::commands(watches.watch("src/test.rs").unwrap());
assert_eq!(vec!["echo same", "cargo build"], result);

let result_multiple = watches.watch("src/test.rs").unwrap();
assert_eq!(vec!["echo same"], result_multiple[0]);
assert_eq!(vec!["cargo build"], result_multiple[1]);
let result_multiple = rules::commands(watches.watch("test/test.rs").unwrap());
assert_eq!(vec!["echo same", "cargo test"], result_multiple);
}

#[test]
Expand Down Expand Up @@ -226,12 +224,15 @@ mod tests {
change: 'test/**'
";
let watches = Watches::new(rules::from_yaml(&file_content).expect("Error parsing yaml"));
let results = watches.run_on_init().unwrap();
let results = rules::commands(watches.run_on_init().unwrap());

assert_eq!(results[0], vec!["cargo build".to_string(),]);
assert_eq!(
results[1],
vec!["cat foo".to_string(), "cat bar".to_string(),]
results,
vec![
"cargo build".to_string(),
"cat foo".to_string(),
"cat bar".to_string(),
]
);
}
}
6 changes: 3 additions & 3 deletions src/workers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cmd::spawn_command;
use rules;
use rules::{self, Rules};
use std::sync::mpsc::channel;
use std::sync::mpsc::{Sender, TryRecvError};
use std::thread::JoinHandle;
Expand Down Expand Up @@ -150,9 +150,9 @@ impl Worker {
Ok(())
}

pub fn schedule(&self, rules: Vec<Vec<String>>) -> Result<(), String> {
pub fn schedule(&self, rules: Vec<Rules>) -> Result<(), String> {
if let Some(scheduler) = self.scheduler.as_ref() {
if let Err(err) = scheduler.send(rules::as_list(rules)) {
if let Err(err) = scheduler.send(rules::commands(rules)) {
return Err(format!("{:?}", err));
}
}
Expand Down

0 comments on commit 35e6f32

Please sign in to comment.