Merge pull request #825 from Demonthos/skip-excluded-files-intitial-read-hot-reload

Hot reloading: filter initial read of files by excluded files
This commit is contained in:
Jon Kelley authored and GitHub committed 2023-02-09 15:05:40 -08:00
commit bea16f151f
2 files changed
+33 -13

No files matched your search

+23 -11
View File
@@ -129,9 +129,30 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
} = cfg; } = cfg;
if let Ok(crate_dir) = PathBuf::from_str(root_path) { if let Ok(crate_dir) = PathBuf::from_str(root_path) {
// try to find the gitingore file
let gitignore_file_path = crate_dir.join(".gitignore");
let (gitignore, _) = ignore::gitignore::Gitignore::new(gitignore_file_path);
// convert the excluded paths to absolute paths
let excluded_paths = excluded_paths
.iter()
.map(|path| crate_dir.join(PathBuf::from(path)))
.collect::<Vec<_>>();
let temp_file = std::env::temp_dir().join("@dioxusin"); let temp_file = std::env::temp_dir().join("@dioxusin");
let channels = Arc::new(Mutex::new(Vec::new())); let channels = Arc::new(Mutex::new(Vec::new()));
let file_map = Arc::new(Mutex::new(FileMap::<Ctx>::new(crate_dir.clone()))); let file_map = Arc::new(Mutex::new(FileMap::<Ctx>::new_with_filter(
crate_dir.clone(),
|path| {
// skip excluded paths
excluded_paths.iter().any(|p| path.starts_with(p)) ||
// respect .gitignore
gitignore
.matched_path_or_any_parents(path, path.is_dir())
.is_ignore()
},
)));
if let Ok(local_socket_stream) = LocalSocketListener::bind(temp_file.as_path()) { if let Ok(local_socket_stream) = LocalSocketListener::bind(temp_file.as_path()) {
let aborted = Arc::new(Mutex::new(false)); let aborted = Arc::new(Mutex::new(false));
@@ -177,10 +198,6 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
// watch for changes // watch for changes
std::thread::spawn(move || { std::thread::spawn(move || {
// try to find the gitingore file
let gitignore_file_path = crate_dir.join(".gitignore");
let (gitignore, _) = ignore::gitignore::Gitignore::new(gitignore_file_path);
let mut last_update_time = chrono::Local::now().timestamp(); let mut last_update_time = chrono::Local::now().timestamp();
let (tx, rx) = std::sync::mpsc::channel(); let (tx, rx) = std::sync::mpsc::channel();
@@ -198,11 +215,6 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
} }
} }
let excluded_paths = excluded_paths
.iter()
.map(|path| crate_dir.join(PathBuf::from(path)))
.collect::<Vec<_>>();
let mut rebuild = { let mut rebuild = {
let aborted = aborted.clone(); let aborted = aborted.clone();
let channels = channels.clone(); let channels = channels.clone();
@@ -242,7 +254,7 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
matches!( matches!(
path.extension().and_then(|p| p.to_str()), path.extension().and_then(|p| p.to_str()),
Some("rs" | "toml" | "css" | "html" | "js") Some("rs" | "toml" | "css" | "html" | "js")
)&& ) &&
// skip excluded paths // skip excluded paths
!excluded_paths.iter().any(|p| path.starts_with(p)) && !excluded_paths.iter().any(|p| path.starts_with(p)) &&
// respect .gitignore // respect .gitignore
@@ -25,14 +25,22 @@ pub struct FileMap<Ctx: HotReloadingContext> {
impl<Ctx: HotReloadingContext> FileMap<Ctx> { impl<Ctx: HotReloadingContext> FileMap<Ctx> {
/// Create a new FileMap from a crate directory /// Create a new FileMap from a crate directory
pub fn new(path: PathBuf) -> Self { pub fn new(path: PathBuf) -> Self {
Self::new_with_filter(path, |_| false)
}
/// Create a new FileMap from a crate directory
pub fn new_with_filter(path: PathBuf, mut filter: impl FnMut(&Path) -> bool) -> Self {
fn find_rs_files( fn find_rs_files(
root: PathBuf, root: PathBuf,
filter: &mut impl FnMut(&Path) -> bool,
) -> io::Result<HashMap<PathBuf, (String, Option<Template<'static>>)>> { ) -> io::Result<HashMap<PathBuf, (String, Option<Template<'static>>)>> {
let mut files = HashMap::new(); let mut files = HashMap::new();
if root.is_dir() { if root.is_dir() {
for entry in (fs::read_dir(root)?).flatten() { for entry in (fs::read_dir(root)?).flatten() {
let path = entry.path(); let path = entry.path();
files.extend(find_rs_files(path)?); if !filter(&path) {
files.extend(find_rs_files(path, filter)?);
}
} }
} else if root.extension().and_then(|s| s.to_str()) == Some("rs") { } else if root.extension().and_then(|s| s.to_str()) == Some("rs") {
if let Ok(mut file) = File::open(root.clone()) { if let Ok(mut file) = File::open(root.clone()) {
@@ -45,7 +53,7 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
} }
let result = Self { let result = Self {
map: find_rs_files(path).unwrap(), map: find_rs_files(path, &mut filter).unwrap(),
phantom: std::marker::PhantomData, phantom: std::marker::PhantomData,
}; };
result result