summaryrefslogtreecommitdiff
path: root/src/batchmode.rs
diff options
context:
space:
mode:
authorstuebinm2024-04-12 16:17:06 +0200
committerstuebinm2024-04-12 16:17:06 +0200
commit422dbd5209fddac75412d8e7de0137f732c7dbc4 (patch)
treee315575c8f3e62df29ea1ab5f6c4886b2cbb2767 /src/batchmode.rs
parent0567f916d4365c8dc0be99d194fe6d157befbc81 (diff)
some restructuring, arg handling, re-enable batchmode
Diffstat (limited to '')
-rw-r--r--src/batchmode.rs38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/batchmode.rs b/src/batchmode.rs
index 8398bce..84b0dff 100644
--- a/src/batchmode.rs
+++ b/src/batchmode.rs
@@ -1,30 +1,43 @@
use std::{path::PathBuf, fs, sync::{Arc, Mutex}};
+use rowan::ast::AstNode;
use threadpool::ThreadPool;
-use crate::status_reporter::*;
+use crate::{status_reporter::*, queries::{SyntaxNode, Parse}, parse_nexp, apply_changes};
-// TODO: make this usable
-// (this module just here to keep old code around for a bit)
-pub enum Task {}
#[allow(unreachable_code, unused)]
-pub fn batchmode(tasks: Vec<(PathBuf, Task)>) {
+pub fn batchmode(tasks: Vec<PathBuf>, query: Parse, debug: bool) {
+ fn do_task(path: PathBuf, query: Parse, debug: bool) -> anyhow::Result<(PathBuf, Option<String>)> {
+
+ let (content, nexp) = match parse_nexp(&path) {
+ Err(e) => {
+ anyhow::bail!("could not parse file {path:?}")
+ },
+ Ok(exp) => exp
+ };
+
+ let (changes, _) = query.apply(&content, nexp.syntax().clone())?;
+
+ let changed = apply_changes(&content, changes, debug);
+
+ Ok((path, if changed != content { Some(changed) } else { None }))
+ }
let pool = ThreadPool::new(16);
let results = Arc::new(Mutex::new(vec![]));
let printer = Arc::new(StatusReport::new(tasks.len(), tasks.len()));
- for (path, task) in tasks {
+ for path in tasks {
pool.execute({
let results = Arc::clone(&results);
let printer = Arc::clone(&printer);
+ let query = query.clone();
move || {
printer.enter_file(&format!("{path:?}"));
- let result: anyhow::Result<(PathBuf, String)> = todo!();
-
+ let result = do_task(path, query, debug);
results.lock().unwrap().push(result);
}
});
@@ -35,18 +48,20 @@ pub fn batchmode(tasks: Vec<(PathBuf, Task)>) {
println!("\n\nSummary:");
let mut c_errors = 0;
let mut c_total = 0;
+ let mut c_changes = 0;
for r in results.lock().unwrap().iter() {
match r {
Err(e) => {
println!(" {}", e);
c_errors += 1;
},
+ Ok((_, Some(_))) => c_changes += 1,
_ => ()
}
c_total += 1;
}
- println!("\n ({c_total} sites total, {c_errors} errors, generated {} edits)", c_total - c_errors);
+ println!("\n ({c_total} sites total, {c_errors} errors, generated {} edits)", c_changes);
let edits: Vec<_> = Arc::into_inner(results).unwrap().into_inner().unwrap()
.into_iter()
@@ -55,7 +70,8 @@ pub fn batchmode(tasks: Vec<(PathBuf, Task)>) {
println!("applying changes ...");
for (filename, content) in edits {
- fs::write(&filename, content.as_bytes()).unwrap();
- // println!("{}", content);
+ if let Some(content) = content {
+ fs::write(&filename, content.as_bytes()).unwrap();
+ }
}
}