From 771f6d160120c932700977529ef0eb4d155bf005 Mon Sep 17 00:00:00 2001 From: Pi Lanningham Date: Thu, 9 Nov 2023 14:59:07 -0500 Subject: [PATCH] Formatting and check --- crates/aiken/src/cmd/check.rs | 2 +- crates/aiken/src/cmd/mod.rs | 2 +- crates/aiken/src/cmd/watch.rs | 61 +++++++++++++++++++---------------- crates/aiken/src/lib.rs | 6 +++- crates/aiken/src/main.rs | 4 +-- 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/crates/aiken/src/cmd/check.rs b/crates/aiken/src/cmd/check.rs index d34f31ed..0372b026 100644 --- a/crates/aiken/src/cmd/check.rs +++ b/crates/aiken/src/cmd/check.rs @@ -47,7 +47,7 @@ pub fn exec( match_tests, exact_match, no_traces, - watch, + .. }: Args, ) -> miette::Result<()> { crate::with_project(directory, deny, |p| { diff --git a/crates/aiken/src/cmd/mod.rs b/crates/aiken/src/cmd/mod.rs index 58356166..b96c0727 100644 --- a/crates/aiken/src/cmd/mod.rs +++ b/crates/aiken/src/cmd/mod.rs @@ -4,7 +4,6 @@ use clap::Parser; pub mod blueprint; pub mod build; pub mod check; -pub mod watch; pub mod completion; pub mod docs; pub mod fmt; @@ -13,6 +12,7 @@ pub mod new; pub mod packages; pub mod tx; pub mod uplc; +pub mod watch; /// Aiken: a smart-contract language and toolchain for Cardano #[derive(Parser)] diff --git a/crates/aiken/src/cmd/watch.rs b/crates/aiken/src/cmd/watch.rs index f00f9e82..efc224d7 100644 --- a/crates/aiken/src/cmd/watch.rs +++ b/crates/aiken/src/cmd/watch.rs @@ -1,5 +1,9 @@ -use notify::{RecursiveMode, Watcher, Event, event::EventAttributes}; -use std::{path::Path, error::Error, time::SystemTime, collections::VecDeque, sync::{Mutex, Arc}}; +use notify::{event::EventAttributes, Event, RecursiveMode, Watcher}; +use std::{ + collections::VecDeque, + path::Path, + sync::{Arc, Mutex}, +}; #[derive(clap::Args)] /// Type-check an Aiken project pub struct Args { @@ -9,62 +13,65 @@ pub struct Args { } 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 lock = project.join("aiken.lock"); 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 mut watcher = notify::recommended_watcher(move |res: notify::Result| { match res { - Ok(event) => { - match event.kind { - notify::EventKind::Create(_) | - notify::EventKind::Modify(_) | - notify::EventKind::Remove(_) => { - let mut queue = queue_write.lock().expect("lock queue"); - queue.push_back(event.clone()); - drop(queue); - } - _ => {} + Ok(event) => match event.kind { + notify::EventKind::Create(_) + | notify::EventKind::Modify(_) + | notify::EventKind::Remove(_) => { + let mut queue = queue_write.lock().expect("lock queue"); + queue.push_back(event.clone()); + drop(queue); } + _ => {} }, Err(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 mut last_evt = SystemTime::UNIX_EPOCH; loop { std::thread::sleep(std::time::Duration::from_millis(300)); let mut queue = queue_read.lock().expect("lock queue"); 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() { if evt.paths.iter().any(|p| { let p = p.canonicalize().expect(""); p.starts_with(&build) || p.starts_with(&lock) - }) { + }) { continue; } latest = Some(evt); } drop(queue); - if let Some(evt) = latest { + if latest.is_some() { if clear { println!("{esc}c", esc = 27 as char); } let _ = crate::with_project_ok(Some(project.clone()), false, |p| { - p.check( - false, - None, - true, - false, - false.into(), - ) + p.check(false, None, true, false, false.into()) }); } } diff --git a/crates/aiken/src/lib.rs b/crates/aiken/src/lib.rs index 7aad48ad..d730ee9f 100644 --- a/crates/aiken/src/lib.rs +++ b/crates/aiken/src/lib.rs @@ -96,7 +96,11 @@ where // 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 -pub fn with_project_ok(directory: Option, deny: bool, mut action: A) -> miette::Result<()> +pub fn with_project_ok( + directory: Option, + deny: bool, + mut action: A, +) -> miette::Result<()> where A: FnMut(&mut Project) -> Result<(), Vec>, { diff --git a/crates/aiken/src/main.rs b/crates/aiken/src/main.rs index 62e9828e..16791d25 100644 --- a/crates/aiken/src/main.rs +++ b/crates/aiken/src/main.rs @@ -1,8 +1,8 @@ use aiken::cmd::{ blueprint::{self, address}, - build, check, watch, completion, docs, fmt, lsp, new, + build, check, completion, docs, fmt, lsp, new, packages::{self, add}, - tx, uplc, Cmd, + tx, uplc, watch, Cmd, }; use aiken_project::{config, pretty};