Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial pass at watch filtering #873

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion crates/aiken-project/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ where
/// ```
pub fn watch_project<F, A>(
directory: Option<&Path>,
rebuild: Option<std::sync::mpsc::Receiver<()>>,
filter: F,
debounce: u32,
mut action: A,
Expand Down Expand Up @@ -225,8 +226,14 @@ where
// release the lock here, in case other events come in
drop(queue);

let force_rebuild = if let Some(ref r) = rebuild {
r.try_recv().is_ok()
} else {
false
};

// If we have an event that survived the filter, then we can construct the project and invoke the action
if latest.is_some() {
if latest.is_some() || force_rebuild {
print!("{esc}c", esc = 27 as char);
eprint!("{esc}c", esc = 27 as char);
eprintln!(
Expand Down
2 changes: 2 additions & 0 deletions crates/aiken/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ regex = "1.7.1"
serde_json = "1.0.94"
thiserror = "1.0.39"

termion = "3.0.0"

aiken-lang = { path = "../aiken-lang", version = "1.0.24-alpha" }
aiken-lsp = { path = "../aiken-lsp", version = "1.0.24-alpha" }
aiken-project = { path = '../aiken-project', version = "1.0.24-alpha" }
Expand Down
24 changes: 15 additions & 9 deletions crates/aiken/src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ pub fn exec(
}: Args,
) -> miette::Result<()> {
let result = if watch {
watch_project(directory.as_deref(), watch::default_filter, 500, |p| {
p.build(
uplc,
match filter_traces {
Some(filter_traces) => filter_traces(trace_level),
None => Tracing::All(trace_level),
},
)
})
watch_project(
directory.as_deref(),
None,
watch::default_filter,
500,
|p| {
p.build(
uplc,
match filter_traces {
Some(filter_traces) => filter_traces(trace_level),
None => Tracing::All(trace_level),
},
)
},
)
} else {
with_project(directory.as_deref(), deny, |p| {
p.build(
Expand Down
96 changes: 81 additions & 15 deletions crates/aiken/src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ use aiken_project::{
test_framework::PropertyTest,
watch::{self, watch_project, with_project},
};
use owo_colors::{OwoColorize, Stream::Stderr};
use rand::prelude::*;
use std::{path::PathBuf, process};
use std::{
path::PathBuf,
process,
sync::{Arc, Mutex},
thread,
};
use termion::input::TermRead;

#[derive(clap::Args)]
/// Type-check an Aiken project
Expand Down Expand Up @@ -87,20 +94,79 @@ pub fn exec(
let seed = seed.unwrap_or_else(|| rng.gen());

let result = if watch {
watch_project(directory.as_deref(), watch::default_filter, 500, |p| {
p.check(
skip_tests,
match_tests.clone(),
debug,
exact_match,
seed,
max_success,
match filter_traces {
Some(filter_traces) => filter_traces(trace_level),
None => Tracing::All(trace_level),
},
)
})
let mut stdin = termion::async_stdin().keys();

let (sender, receiver) = std::sync::mpsc::channel();
let filter = Arc::new(Mutex::new("".to_string()));
{
let filter = filter.clone();
thread::spawn(move || {
let mut working = "".to_string();
loop {
if let Some(c) = stdin.next() {
match c {
Ok(termion::event::Key::Backspace) => {
if !working.is_empty() {
working = working[..working.len() - 1].to_string();
}
}
Ok(termion::event::Key::Char('\n')) => {
let mut filt = filter.lock().unwrap();
*filt = working;
working = "".to_string();
sender.send(()).unwrap();
}
Ok(termion::event::Key::Char(c)) => {
working.push(c);
}
_ => {}
}
}
}
});
}

watch_project(
directory.as_deref(),
Some(receiver),
watch::default_filter,
500,
|p| {
let filt = filter.lock().unwrap();
let final_match_tests = match match_tests.clone() {
Some(existing) if filt.is_empty() => Some(existing),
Some(existing) => {
let mut e = existing.clone();
e.push(filt.clone());
Some(e)
}
None if !filt.is_empty() => Some(vec![filt.clone()]),
None => None,
};
let result = p.check(
skip_tests,
final_match_tests.clone(),
debug,
exact_match,
seed,
max_success,
match filter_traces {
Some(filter_traces) => filter_traces(trace_level),
None => Tracing::All(trace_level),
},
);
if let Some(fmt) = final_match_tests {
println!(
" {} {}",
"Filtered by:"
.if_supports_color(Stderr, |s| s.bold())
.if_supports_color(Stderr, |s| s.purple()),
fmt.join(", ")
);
}
result
},
)
} else {
with_project(directory.as_deref(), deny, |p| {
p.check(
Expand Down
10 changes: 7 additions & 3 deletions crates/aiken/src/cmd/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ pub fn exec(
}: Args,
) -> miette::Result<()> {
let result = if watch {
watch_project(directory.as_deref(), watch::default_filter, 500, |p| {
p.docs(destination.clone(), include_dependencies)
})
watch_project(
directory.as_deref(),
None,
watch::default_filter,
500,
|p| p.docs(destination.clone(), include_dependencies),
)
} else {
with_project(directory.as_deref(), deny, |p| {
p.docs(destination.clone(), include_dependencies)
Expand Down
Loading