8000 Add watch mode support by Tommypop2 · Pull Request #202 · kaleidawave/ezno · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add watch mode support #202

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

Merged
merged 8 commits into from
Nov 10, 2024
Merged
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
69 changes: 69 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ base64 = "0.21"
console = "0.15"
codespan-reporting = "0.11"
enum-variants-strings = "0.3"
glob = "0.3"
# For `StrComparison` for string comparison
pretty_assertions = "1.3.0"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -59,6 +58,8 @@ native-tls = "0.2.11"
multiline-term-input = "0.1.0"
# For watching files
notify = "6.1.0"
notify-debouncer-full = "0.3.1"
glob = "0.3"

[dependencies.checker]
path = "./checker"
Expand Down
1 change: 1 addition & 0 deletions checker/src/options.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't particularly want to do this, since it required changing the internal checker code, but it seemed like the only way to get it to work as ownership is passed to the checker when it's passed in.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize), serde(default))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone)]
pub struct TypeCheckOptions {
/// Parameters cannot be reassigned
pub constant_parameters: bool,
Expand Down
175 changes: 125 additions & 50 deletions src/cli.rs
10000
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
path::{Path, PathBuf},
process::Command,
process::ExitCode,
time::Instant,
time::{Duration, Instant},
};

use crate::{
Expand Down Expand Up @@ -112,9 +112,9 @@ pub(crate) struct CheckArguments {
/// paths to definition files
#[argh(option, short = 'd')]
pub definition_file: Option<PathBuf>,
/// whether to re-check on file changes TODO #164
/// whether to re-check on file changes
#[argh(switch)]
pub _watch: bool,
pub watch: bool,
/// whether to display check time
#[argh(switch)]
pub timings: bool,
Expand Down Expand Up @@ -173,6 +173,68 @@ fn file_system_resolver(path: &Path) -> Option<String> {
}
}

fn run_checker<T: crate::ReadFromFS>(
entry_points: Vec<PathBuf>,
read_file: &T,
timings: bool,
definition_file: Option<PathBuf>,
max_diagnostics: MaxDiagnostics,
type_check_options: TypeCheckOptions,
compact_diagnostics: bool,
) -> ExitCode {
let CheckOutput { diagnostics, module_contents, chronometer, types, .. } =
check(entry_points, read_file, definition_file.as_deref(), type_check_options);

let diagnostics_count = diagnostics.count();
let current = timings.then(std::time::Instant::now);

let result = if diagnostics.has_error() {
if let MaxDiagnostics::FixedTo(0) = max_diagnostics {
let count = diagnostics.into_iter().count();
print_to_cli(format_args!(
"Found {count} type errors and warnings {}",
console::Emoji(" 😬", ":/")
))
} else {
report_diagnostics_to_cli(
diagnostics,
&module_contents,
compact_diagnostics,
max_diagnostics,
)
.unwrap();
}
ExitCode::FAILURE
} else {
// May be warnings or information here
report_diagnostics_to_cli(
diagnostics,
&module_contents,
compact_diagnostics,
max_diagnostics,
)
.unwrap();
print_to_cli(format_args!("No type errors found {}", console::Emoji("🎉", ":)")));
ExitCode::SUCCESS
};

#[cfg(not(target_family = "wasm"))]
if timings {
let reporting = current.unwrap().elapsed();
eprintln!("---\n");
eprintln!("Diagnostics:\t{}", diagnostics_count);
eprintln!("Types: \t{}", types.count_of_types());
eprintln!("Lines: \t{}", chronometer.lines);
eprintln!("Cache read: \t{:?}", chronometer.cached);
eprintln!("FS read: \t{:?}", chronometer.fs);
eprintln!("Parsed in: \t{:?}", chronometer.parse);
eprintln!("Checked in: \t{:?}", chronometer.check);
eprintln!("Reporting: \t{:?}", reporting);
}

result
}

pub fn run_cli<T: crate::ReadFromFS, U: crate::WriteToFS, V: crate::CLIInputResolver>(
cli_arguments: &[&str],
read_file: &T,
Expand All @@ -195,8 +257,7 @@ pub fn run_cli<T: crate::ReadFromFS, U: crate::WriteToFS, V: crate::CLIInputReso
CompilerSubCommand::Check(check_arguments) => {
let CheckArguments {
input,
// TODO #164
_watch,
watch,
definition_file,
timings,
compact_diagnostics,
Expand All @@ -215,57 +276,64 @@ pub fn run_cli<T: crate::ReadFromFS, U: crate::WriteToFS, V: crate::CLIInputReso
Err(_) => return ExitCode::FAILURE,
};

let CheckOutput { diagnostics, module_contents, chronometer, types, .. } =
check(entry_points, read_file, definition_file.as_deref(), type_check_options);
if watch {
#[cfg(target_family = "wasm")]
panic!("'watch' mode not supported on WASM");

let diagnostics_count = diagnostics.count();
let current = timings.then(std::time::Instant::now);
#[cfg(not(target_family = "wasm"))]
{
use notify::Watcher;
use notify_debouncer_full::new_debouncer;

let result = if diagnostics.has_error() {
if let MaxDiagnostics::FixedTo(0) = max_diagnostics {
let count = diagnostics.into_iter().count();
print_to_cli(format_args!(
"Found {count} type errors and warnings {}",
console::Emoji(" 😬", ":/")
))
} else {
report_diagnostics_to_cli(
diagnostics,
&module_contents,
let (tx, rx) = std::sync::mpsc::channel();
let mut debouncer =
new_debouncer(Duration::from_millis(200), None, tx).unwrap();

for e in &entry_points {
_ = debouncer.watcher().watch(e, notify::RecursiveMode::Recursive).unwrap();
}

// Run once
run_checker(
entry_points.clone(),
read_file,
timings,
definition_file.clone(),
max_diagnostics.clone(),
type_check_options.clone(),
compact_diagnostics,
max_diagnostics,
)
.unwrap();
);

for res in rx {
match res {
Ok(_e) => {
run_checker(
entry_points.clone(),
read_file,
timings,
definition_file.clone(),
max_diagnostics.clone(),
type_check_options.clone(),
compact_diagnostics,
);
}
Err(error) => eprintln!("Error: {error:?}"),
}
}
// Infinite loop here so the compiler is satisfied that this never returns
loop {}
}
ExitCode::FAILURE
} else {
// May be warnings or information here
report_diagnostics_to_cli(
diagnostics,
&module_contents,
compact_diagnostics,
max_diagnostics,
)
.unwrap();
print_to_cli(format_args!("No type errors found {}", console::Emoji("🎉", ":)")));
ExitCode::SUCCESS
};

#[cfg(not(target_family = "wasm"))]
if timings {
let reporting = current.unwrap().elapsed();
eprintln!("---\n");
eprintln!("Diagnostics:\t{}", diagnostics_count);
eprintln!("Types: \t{}", types.count_of_types());
eprintln!("Lines: \t{}", chronometer.lines);
eprintln!("Cache read: \t{:?}", chronometer.cached);
eprintln!("FS read: \t{:?}", chronometer.fs);
eprintln!("Parsed in: \t{:?}", chronometer.parse);
eprintln!("Checked in: \t{:?}", chronometer.check);
eprintln!("Reporting: \t{:?}", reporting);
}

result
run_checker(
entry_points,
read_file,
timings,
definition_file,
max_diagnostics,
type_check_options,
compact_diagnostics,
)
}
CompilerSubCommand::Experimental(ExperimentalArguments {
nested: ExperimentalSubcommand::Build(build_config),
Expand Down Expand Up @@ -454,6 +522,13 @@ pub fn run_cli<T: crate::ReadFromFS, U: crate::WriteToFS, V: crate::CLIInputReso
}
}

// `glob` library does not work on WASM :(
#[cfg(target_family = "wasm")]
fn get_entry_points(input: String) -> Result<Vec<PathBuf>, ()> {
Ok(vec![input.into()])
}

#[cfg(not(target_family = "wasm"))]
fn get_entry_points(input: String) -> Result<Vec<PathBuf>, ()> {
match glob::glob(&input) {
Ok(files) => {
Expand Down
Loading
Loading
0