Formatting and check

This commit is contained in:
Pi Lanningham 2023-11-09 14:59:07 -05:00 committed by KtorZ
parent 689a41ded4
commit 771f6d1601
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
5 changed files with 43 additions and 32 deletions

View File

@ -47,7 +47,7 @@ pub fn exec(
match_tests, match_tests,
exact_match, exact_match,
no_traces, no_traces,
watch, ..
}: Args, }: Args,
) -> miette::Result<()> { ) -> miette::Result<()> {
crate::with_project(directory, deny, |p| { crate::with_project(directory, deny, |p| {

View File

@ -4,7 +4,6 @@ use clap::Parser;
pub mod blueprint; pub mod blueprint;
pub mod build; pub mod build;
pub mod check; pub mod check;
pub mod watch;
pub mod completion; pub mod completion;
pub mod docs; pub mod docs;
pub mod fmt; pub mod fmt;
@ -13,6 +12,7 @@ pub mod new;
pub mod packages; pub mod packages;
pub mod tx; pub mod tx;
pub mod uplc; pub mod uplc;
pub mod watch;
/// Aiken: a smart-contract language and toolchain for Cardano /// Aiken: a smart-contract language and toolchain for Cardano
#[derive(Parser)] #[derive(Parser)]

View File

@ -1,5 +1,9 @@
use notify::{RecursiveMode, Watcher, Event, event::EventAttributes}; use notify::{event::EventAttributes, Event, RecursiveMode, Watcher};
use std::{path::Path, error::Error, time::SystemTime, collections::VecDeque, sync::{Mutex, Arc}}; use std::{
collections::VecDeque,
path::Path,
sync::{Arc, Mutex},
};
#[derive(clap::Args)] #[derive(clap::Args)]
/// Type-check an Aiken project /// Type-check an Aiken project
pub struct Args { pub struct Args {
@ -9,40 +13,49 @@ pub struct Args {
} }
pub fn exec(Args { clear }: Args) -> miette::Result<()> { pub fn exec(Args { clear }: Args) -> miette::Result<()> {
let project = Path::new("../sundae-contracts/aiken").to_path_buf().canonicalize().expect(""); let project = Path::new("../sundae-contracts/aiken")
.to_path_buf()
.canonicalize()
.expect("");
let build = project.join("build").canonicalize().expect(""); let build = project.join("build").canonicalize().expect("");
let lock = project.join("aiken.lock"); let lock = project.join("aiken.lock");
let queue = Arc::new(Mutex::new(VecDeque::new())); let queue = Arc::new(Mutex::new(VecDeque::new()));
queue.lock().unwrap().push_back(Event { kind: notify::EventKind::Any, paths: vec![], attrs: EventAttributes::new() }); queue.lock().unwrap().push_back(Event {
kind: notify::EventKind::Any,
paths: vec![],
attrs: EventAttributes::new(),
});
let queue_write = queue.clone(); let queue_write = queue.clone();
let mut watcher = notify::recommended_watcher(move |res: notify::Result<Event>| { let mut watcher = notify::recommended_watcher(move |res: notify::Result<Event>| {
match res { match res {
Ok(event) => { Ok(event) => match event.kind {
match event.kind { notify::EventKind::Create(_)
notify::EventKind::Create(_) | | notify::EventKind::Modify(_)
notify::EventKind::Modify(_) | | notify::EventKind::Remove(_) => {
notify::EventKind::Remove(_) => {
let mut queue = queue_write.lock().expect("lock queue"); let mut queue = queue_write.lock().expect("lock queue");
queue.push_back(event.clone()); queue.push_back(event.clone());
drop(queue); drop(queue);
} }
_ => {} _ => {}
}
}, },
Err(e) => { Err(e) => {
println!("watch error: {:?}", e) println!("watch error: {:?}", e)
}, }
}; };
}).expect("watcher"); })
let _ = watcher.watch(Path::new("../sundae-contracts/aiken"), RecursiveMode::Recursive); .expect("watcher");
let _ = watcher.watch(
Path::new("../sundae-contracts/aiken"),
RecursiveMode::Recursive,
);
let queue_read = queue.clone(); let queue_read = queue.clone();
let mut last_evt = SystemTime::UNIX_EPOCH;
loop { loop {
std::thread::sleep(std::time::Duration::from_millis(300)); std::thread::sleep(std::time::Duration::from_millis(300));
let mut queue = queue_read.lock().expect("lock queue"); let mut queue = queue_read.lock().expect("lock queue");
let mut latest = None; let mut latest = None;
// debounce the events, and ignore build/lock changes, because they come in in large batches
while let Some(evt) = queue.pop_back() { while let Some(evt) = queue.pop_back() {
if evt.paths.iter().any(|p| { if evt.paths.iter().any(|p| {
let p = p.canonicalize().expect(""); let p = p.canonicalize().expect("");
@ -53,18 +66,12 @@ pub fn exec(Args { clear }: Args) -> miette::Result<()> {
latest = Some(evt); latest = Some(evt);
} }
drop(queue); drop(queue);
if let Some(evt) = latest { if latest.is_some() {
if clear { if clear {
println!("{esc}c", esc = 27 as char); println!("{esc}c", esc = 27 as char);
} }
let _ = crate::with_project_ok(Some(project.clone()), false, |p| { let _ = crate::with_project_ok(Some(project.clone()), false, |p| {
p.check( p.check(false, None, true, false, false.into())
false,
None,
true,
false,
false.into(),
)
}); });
} }
} }

View File

@ -96,7 +96,11 @@ where
// TODO: we probably want to rework with_project slightly to avoid duplication here, // TODO: we probably want to rework with_project slightly to avoid duplication here,
// but this is a quick hack to get the aiken watch working // but this is a quick hack to get the aiken watch working
pub fn with_project_ok<A>(directory: Option<PathBuf>, deny: bool, mut action: A) -> miette::Result<()> pub fn with_project_ok<A>(
directory: Option<PathBuf>,
deny: bool,
mut action: A,
) -> miette::Result<()>
where where
A: FnMut(&mut Project<Terminal>) -> Result<(), Vec<aiken_project::error::Error>>, A: FnMut(&mut Project<Terminal>) -> Result<(), Vec<aiken_project::error::Error>>,
{ {

View File

@ -1,8 +1,8 @@
use aiken::cmd::{ use aiken::cmd::{
blueprint::{self, address}, blueprint::{self, address},
build, check, watch, completion, docs, fmt, lsp, new, build, check, completion, docs, fmt, lsp, new,
packages::{self, add}, packages::{self, add},
tx, uplc, Cmd, tx, uplc, watch, Cmd,
}; };
use aiken_project::{config, pretty}; use aiken_project::{config, pretty};