Skip to content
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

feat: respect ignore files #409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 40 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 @@ -37,7 +37,7 @@ categories = ["development-tools", "wasm", "web-programming"]
keywords = ["leptos"]
version = "0.2.24"
edition = "2021"
rust-version = "1.71"
rust-version = "1.82"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is due to the use of is_none_or in codebase and was part of clippy warnings.

authors = ["Henrik Akesson", "Greg Johnston", "Ben Wishovich"]

[package.metadata.wix]
Expand Down Expand Up @@ -94,6 +94,7 @@ base64ct = { version = "1.6.0", features = ["alloc"] }
swc = "9.0"
swc_common = "5.0"
shlex = "1.3.0"
ignore = "0.4.23"

[dev-dependencies]
insta = { version = "1.40.0", features = ["yaml"] }
Expand Down
6 changes: 1 addition & 5 deletions src/command/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ pub async fn watch(proj: &Arc<Project>) -> Result<()> {
None
};

let _watch = service::notify::spawn(proj).await?;
if let Some(view_macros) = view_macros {
let _patch = service::patch::spawn(proj, &view_macros).await?;
}

service::notify::spawn(proj, view_macros).await?;
service::serve::spawn(proj).await;
service::reload::spawn(proj).await;

Expand Down
2 changes: 1 addition & 1 deletion src/compile/tailwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn compile_tailwind(proj: &Project, tw_conf: &TailwindConfig) -> Resul
create_default_tailwind_config(tw_conf).await?;
}

let (line, process) = tailwind_process(&proj, "tailwindcss", tw_conf).await?;
let (line, process) = tailwind_process(proj, "tailwindcss", tw_conf).await?;

match wait_piped_interruptible("Tailwind", process, Interrupt::subscribe_any()).await? {
CommandResult::Success(output) => {
Expand Down
1 change: 0 additions & 1 deletion src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod notify;
pub mod patch;
pub mod reload;
pub mod serve;
pub mod site;
167 changes: 101 additions & 66 deletions src/service/notify.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,47 @@
use crate::compile::Change;
use crate::config::Project;
use crate::ext::anyhow::{anyhow, Result};
use crate::signal::Interrupt;
use crate::signal::{Interrupt, ReloadSignal};
use crate::{
ext::{remove_nested, PathBufExt, PathExt},
ext::{PathBufExt, PathExt},
logger::GRAY,
};
use camino::Utf8PathBuf;
use itertools::Itertools;
use ignore::gitignore::Gitignore;
use leptos_hot_reload::ViewMacros;
use notify::event::ModifyKind;
use notify::{Event, EventKind, RecursiveMode, Watcher};
use std::collections::HashSet;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::task::JoinHandle;

pub(crate) const FALLBACK_POLLING_TIMEOUT: Duration = Duration::from_millis(200);

pub async fn spawn(proj: &Arc<Project>) -> Result<JoinHandle<()>> {
let mut set: HashSet<Utf8PathBuf> = HashSet::from_iter(vec![]);

set.extend(proj.lib.src_paths.clone());
set.extend(proj.bin.src_paths.clone());
set.extend(proj.watch_additional_files.clone());
set.insert(proj.js_dir.clone());

if let Some(file) = &proj.style.file {
set.insert(file.source.clone().without_last());
}

if let Some(tailwind) = &proj.style.tailwind {
set.insert(tailwind.config_file.clone());
set.insert(tailwind.input_file.clone());
}

if let Some(assets) = &proj.assets {
set.insert(assets.dir.clone());
}

let paths = remove_nested(set.into_iter().filter(|path| Path::new(path).exists()));

log::info!(
"Notify watching paths {}",
GRAY.paint(paths.iter().join(", "))
);
pub async fn spawn(proj: &Arc<Project>, view_macros: Option<ViewMacros>) -> Result<JoinHandle<()>> {
let proj = proj.clone();

Ok(tokio::spawn(async move { run(&paths, proj).await }))
Ok(tokio::spawn(async move { run(proj, view_macros).await }))
}

async fn run(paths: &[Utf8PathBuf], proj: Arc<Project>) {
async fn run(proj: Arc<Project>, view_macros: Option<ViewMacros>) {
let (sync_tx, sync_rx) = std::sync::mpsc::channel();

let proj = proj.clone();
tokio::task::spawn_blocking(move || {
while let Ok(event) = sync_rx.recv() {
match event {
Ok(event) => handle(event, proj.clone()),
Err(err) => {
log::trace!("Notify error: {err:?}");
return;
tokio::task::spawn_blocking({
let proj = proj.clone();
move || {
let mut gitignore = create_gitignore_instance(&proj);
while let Ok(event) = sync_rx.recv() {
match event {
Ok(event) => handle(event, proj.clone(), &view_macros, &mut gitignore),
Err(err) => {
log::trace!("Notify error: {err:?}");
return;
}
}
}
log::debug!("Notify stopped");
}
log::debug!("Notify stopped");
});

let mut watcher = notify::RecommendedWatcher::new(
Expand All @@ -73,9 +50,12 @@ async fn run(paths: &[Utf8PathBuf], proj: Arc<Project>) {
)
.expect("failed to build file system watcher");

let mut paths = proj.watch_additional_files.clone();
paths.push(proj.working_dir.clone());

for path in paths {
if let Err(e) = watcher.watch(Path::new(path), RecursiveMode::Recursive) {
log::error!("Notify could not watch {path:?} due to {e:?}");
if let Err(e) = watcher.watch(path.as_std_path(), RecursiveMode::Recursive) {
log::error!("Notify could not watch {:?} due to {e:?}", proj.working_dir);
}
}

Expand All @@ -84,7 +64,12 @@ async fn run(paths: &[Utf8PathBuf], proj: Arc<Project>) {
}
}

fn handle(event: Event, proj: Arc<Project>) {
fn handle(
event: Event,
proj: Arc<Project>,
view_macros: &Option<ViewMacros>,
gitignore: &mut Gitignore,
) {
if event.paths.is_empty() {
return;
}
Expand All @@ -98,23 +83,23 @@ fn handle(event: Event, proj: Arc<Project>) {
return;
};

log::trace!("Notify handle {}", GRAY.paint(format!("{:?}", event.paths)));
let paths = ignore_paths(&proj, &event.paths, gitignore);

let paths: Vec<_> = event
.paths
.into_iter()
.filter_map(|p| match convert(&p, &proj) {
Ok(p) => Some(p),
Err(e) => {
log::info!("{e}");
None
}
})
.collect();
if paths.is_empty() {
return;
}

let mut changes = Vec::new();

log::trace!("Notify handle {}", GRAY.paint(format!("{paths:?}")));

for path in paths {
if path.starts_with(".gitignore") {
*gitignore = create_gitignore_instance(&proj);
log::debug!("Notify .gitignore change {}", GRAY.paint(path.to_string()));
continue;
}

if let Some(assets) = &proj.assets {
if path.starts_with(&assets.dir) {
log::debug!("Notify asset change {}", GRAY.paint(path.to_string()));
Expand All @@ -131,6 +116,14 @@ fn handle(event: Event, proj: Arc<Project>) {
}

if path.starts_with_any(&proj.bin.src_paths) && path.is_ext_any(&["rs"]) {
if let Some(view_macros) = view_macros {
// Check if it's possible to patch
let patches = view_macros.patch(&path);
if let Ok(Some(patch)) = patches {
log::debug!("Patching view.");
ReloadSignal::send_view_patches(&patch);
}
}
log::debug!("Notify bin source change {}", GRAY.paint(path.to_string()));
changes.push(Change::BinSource);
}
Expand Down Expand Up @@ -159,16 +152,58 @@ fn handle(event: Event, proj: Arc<Project>) {
);
changes.push(Change::Additional);
}
}

if !changes.is_empty() {
Interrupt::send(&changes);
} else {
log::trace!(
"Notify changed but not watched: {}",
GRAY.paint(path.to_string())
);
}
if !changes.is_empty() {
Interrupt::send(&changes);
}
}

pub(crate) fn create_gitignore_instance(proj: &Project) -> Gitignore {
log::info!("Creating ignore list from '.gitignore' file");

let (gi, err) = Gitignore::new(proj.working_dir.join(".gitignore"));

if let Some(err) = err {
log::error!("Failed reading '.gitignore' file in the working directory: {err}\nThis causes the watcher to work expensively on file changes like changes in the 'target' path.\nCreate a '.gitignore' file and exclude common build and cache paths like 'target'");
}

gi
}

pub(crate) fn ignore_paths(
proj: &Project,
event_paths: &[PathBuf],
gitignore: &Gitignore,
) -> Vec<Utf8PathBuf> {
event_paths
.iter()
.filter_map(|p| {
let p = match convert(p, proj) {
Ok(p) => p,
Err(e) => {
log::info!("{e}");
return None;
}
};

// Check if the file excluded
let matched = gitignore.matched(p.as_std_path(), p.is_dir());
if matches!(matched, ignore::Match::Ignore(_)) {
return None;
}

// Check if the parent directories excluded
let mut parent = p.as_std_path();
while let Some(par) = parent.parent() {
if matches!(gitignore.matched(par, true), ignore::Match::Ignore(_)) {
return None;
}
parent = par;
}
Some(p)
})
.collect()
}

pub(crate) fn convert(p: &Path, proj: &Project) -> Result<Utf8PathBuf> {
Expand Down
Loading
Loading