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 2023-02-09 15:05:40 -08:00 committed by GitHub
commit bea16f151f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

@ -129,9 +129,30 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
} = cfg;
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 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()) {
let aborted = Arc::new(Mutex::new(false));
@ -177,10 +198,6 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
// watch for changes
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 (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 aborted = aborted.clone();
let channels = channels.clone();
@ -242,7 +254,7 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
matches!(
path.extension().and_then(|p| p.to_str()),
Some("rs" | "toml" | "css" | "html" | "js")
)&&
) &&
// skip excluded paths
!excluded_paths.iter().any(|p| path.starts_with(p)) &&
// respect .gitignore

View file

@ -25,14 +25,22 @@ pub struct FileMap<Ctx: HotReloadingContext> {
impl<Ctx: HotReloadingContext> FileMap<Ctx> {
/// Create a new FileMap from a crate directory
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(
root: PathBuf,
filter: &mut impl FnMut(&Path) -> bool,
) -> io::Result<HashMap<PathBuf, (String, Option<Template<'static>>)>> {
let mut files = HashMap::new();
if root.is_dir() {
for entry in (fs::read_dir(root)?).flatten() {
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") {
if let Ok(mut file) = File::open(root.clone()) {
@ -45,7 +53,7 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
}
let result = Self {
map: find_rs_files(path).unwrap(),
map: find_rs_files(path, &mut filter).unwrap(),
phantom: std::marker::PhantomData,
};
result