2018-12-18 10:29:14 +01:00
|
|
|
use std::{
|
2019-01-12 18:17:52 +01:00
|
|
|
fmt, fs,
|
2018-12-18 10:29:14 +01:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
2018-12-18 14:38:05 +01:00
|
|
|
use relative_path::RelativePathBuf;
|
2019-01-12 18:17:52 +01:00
|
|
|
use thread_worker::WorkerHandle;
|
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2018-12-18 11:35:05 +01:00
|
|
|
|
2019-01-12 18:17:52 +01:00
|
|
|
use crate::{has_rs_extension, watcher::WatcherChange, VfsRoot};
|
2018-12-18 11:35:05 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
pub(crate) enum Task {
|
|
|
|
AddRoot {
|
|
|
|
root: VfsRoot,
|
|
|
|
path: PathBuf,
|
|
|
|
filter: Box<Fn(&DirEntry) -> bool + Send>,
|
|
|
|
},
|
2019-01-16 19:30:20 +01:00
|
|
|
HandleChange(WatcherChange),
|
|
|
|
LoadChange(WatcherChange),
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AddRootResult {
|
2018-12-18 14:38:05 +01:00
|
|
|
pub(crate) root: VfsRoot,
|
|
|
|
pub(crate) files: Vec<(RelativePathBuf, String)>,
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
#[derive(Debug)]
|
2019-01-12 18:17:52 +01:00
|
|
|
pub enum WatcherChangeData {
|
|
|
|
Create { path: PathBuf, text: String },
|
|
|
|
Write { path: PathBuf, text: String },
|
|
|
|
Remove { path: PathBuf },
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TaskResult {
|
|
|
|
AddRoot(AddRootResult),
|
2019-01-12 18:17:52 +01:00
|
|
|
HandleChange(WatcherChange),
|
|
|
|
LoadChange(Option<WatcherChangeData>),
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:04:15 +01:00
|
|
|
impl fmt::Debug for TaskResult {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str("TaskResult { ... }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 14:38:05 +01:00
|
|
|
pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>;
|
2018-12-18 11:35:05 +01:00
|
|
|
|
|
|
|
pub(crate) fn start() -> (Worker, WorkerHandle) {
|
|
|
|
thread_worker::spawn("vfs", 128, |input_receiver, output_sender| {
|
|
|
|
input_receiver
|
2018-12-30 21:23:31 +01:00
|
|
|
.into_iter()
|
2018-12-18 11:35:05 +01:00
|
|
|
.map(handle_task)
|
2018-12-30 21:23:31 +01:00
|
|
|
.try_for_each(|it| output_sender.send(it))
|
|
|
|
.unwrap()
|
2018-12-18 11:35:05 +01:00
|
|
|
})
|
|
|
|
}
|
2018-12-18 11:23:23 +01:00
|
|
|
|
2018-12-18 14:38:05 +01:00
|
|
|
fn handle_task(task: Task) -> TaskResult {
|
2019-01-07 21:35:18 +01:00
|
|
|
match task {
|
|
|
|
Task::AddRoot { root, path, filter } => {
|
|
|
|
log::debug!("loading {} ...", path.as_path().display());
|
|
|
|
let files = load_root(path.as_path(), &*filter);
|
|
|
|
log::debug!("... loaded {}", path.as_path().display());
|
|
|
|
TaskResult::AddRoot(AddRootResult { root, files })
|
|
|
|
}
|
2019-01-16 19:30:20 +01:00
|
|
|
Task::HandleChange(change) => {
|
|
|
|
// forward as is because Vfs has to decide if we should load it
|
|
|
|
TaskResult::HandleChange(change)
|
|
|
|
}
|
2019-01-12 18:17:52 +01:00
|
|
|
Task::LoadChange(change) => {
|
|
|
|
log::debug!("loading {:?} ...", change);
|
|
|
|
let data = load_change(change);
|
|
|
|
TaskResult::LoadChange(data)
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2018-12-18 14:38:05 +01:00
|
|
|
fn load_root(root: &Path, filter: &dyn Fn(&DirEntry) -> bool) -> Vec<(RelativePathBuf, String)> {
|
2018-12-18 11:18:55 +01:00
|
|
|
let mut res = Vec::new();
|
2018-12-18 14:38:05 +01:00
|
|
|
for entry in WalkDir::new(root).into_iter().filter_entry(filter) {
|
2018-12-18 11:18:55 +01:00
|
|
|
let entry = match entry {
|
|
|
|
Ok(entry) => entry,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("watcher error: {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !entry.file_type().is_file() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let path = entry.path();
|
2018-12-29 23:45:01 +01:00
|
|
|
if !has_rs_extension(path) {
|
2018-12-18 11:18:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let text = match fs::read_to_string(path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("watcher error: {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2018-12-18 14:38:05 +01:00
|
|
|
let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap();
|
|
|
|
res.push((path.to_owned(), text))
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2019-01-12 18:17:52 +01:00
|
|
|
|
|
|
|
fn load_change(change: WatcherChange) -> Option<WatcherChangeData> {
|
|
|
|
let data = match change {
|
|
|
|
WatcherChange::Create(path) => {
|
|
|
|
let text = match fs::read_to_string(&path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
2019-01-16 19:30:20 +01:00
|
|
|
log::warn!("watcher error \"{}\": {}", path.display(), e);
|
2019-01-12 18:17:52 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
WatcherChangeData::Create { path, text }
|
|
|
|
}
|
|
|
|
WatcherChange::Write(path) => {
|
|
|
|
let text = match fs::read_to_string(&path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
2019-01-16 19:30:20 +01:00
|
|
|
log::warn!("watcher error \"{}\": {}", path.display(), e);
|
2019-01-12 18:17:52 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
WatcherChangeData::Write { path, text }
|
|
|
|
}
|
|
|
|
WatcherChange::Remove(path) => WatcherChangeData::Remove { path },
|
|
|
|
WatcherChange::Rescan => {
|
|
|
|
// this should be handled by Vfs::handle_task
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(data)
|
|
|
|
}
|