Skip to content

Commit c0df81c

Browse files
committed
feat(core): ignore files/directories in .gitignore
1 parent f161028 commit c0df81c

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shared/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ tracing-appender = "0.2.1"
2828
notify = "5.0.0-pre.13"
2929
dirs = "4.0"
3030
wax = "0.4.0"
31+
32+
# WARN: api totally changed on later versions.
33+
# It would make sense to create custom function
34+
gitignore = "1.0.7"
35+

shared/src/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::tracing::info;
21
use crate::workspace::Workspace;
32
use anyhow::{Ok, Result};
43
use libproc::libproc::proc_pid;

shared/src/watch.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::state::SharedState;
22
use crate::Command;
3-
use notify::{Error, Event, RecommendedWatcher, RecursiveMode, Watcher};
3+
use notify::{recommended_watcher, Error, Event, RecursiveMode, Watcher};
44
use std::path::Path;
55
use std::result::Result;
6-
use std::sync::Arc;
7-
use tokio::sync::{mpsc, Mutex};
8-
use tracing::{debug, info, trace};
9-
use wax::{Glob, Pattern};
6+
use std::sync::{Arc, Mutex};
7+
use tokio::sync::mpsc;
8+
use tracing::{debug, trace};
109

1110
// TODO: Stop handle
1211

@@ -42,9 +41,9 @@ pub async fn update(state: SharedState, _msg: Command) {
4241
///
4342
/// This will compare last_seen with path, updates `last_seen` if not match,
4443
/// else returns true.
45-
async fn should_ignore(last_seen: Arc<Mutex<String>>, path: &str) -> bool {
44+
fn is_seen(last_seen: Arc<Mutex<String>>, path: &str) -> bool {
4645
let path = path.to_string();
47-
let mut last_seen = last_seen.lock().await;
46+
let mut last_seen = last_seen.lock().unwrap();
4847
if last_seen.to_string() == path {
4948
return true;
5049
} else {
@@ -54,34 +53,51 @@ async fn should_ignore(last_seen: Arc<Mutex<String>>, path: &str) -> bool {
5453
}
5554

5655
fn new(state: SharedState, root: String) -> tokio::task::JoinHandle<anyhow::Result<()>> {
57-
// NOTE: should watch for registerd directories?
58-
let ignore = wax::any::<Glob, _>(["**/.git/**", "**/*.xcodeproj/**", "**/.*"]).unwrap();
56+
type NotifyResult = Result<Event, Error>;
57+
use gitignore::File;
58+
use wax::{any, Glob, Pattern};
5959

6060
tokio::spawn(async move {
6161
let (tx, mut rx) = mpsc::channel(100);
6262

63-
let mut watcher = RecommendedWatcher::new(move |res: Result<Event, Error>| {
63+
let custom_ignore = any::<Glob, _>(["**/.git/**", "**/*.xcodeproj/**", "**/.*"]).unwrap();
64+
let root_path = Path::new(&root);
65+
let gitignore_path = root_path.join(".gitignore");
66+
67+
// HACK: To ignore seen paths.
68+
let last_seen = Arc::new(Mutex::new(String::default()));
69+
70+
// NOTE: outdate API
71+
let gitignore = Arc::new(Mutex::new(File::new(&gitignore_path).unwrap()));
72+
73+
recommended_watcher(move |res: NotifyResult| {
6474
if res.is_ok() {
6575
tx.blocking_send(res.unwrap()).unwrap()
6676
};
67-
})?;
68-
69-
watcher.watch(Path::new(&root), RecursiveMode::Recursive)?;
70-
71-
// HACK: ignore seen paths.
72-
let last_seen = Arc::new(Mutex::new(String::default()));
77+
})?
78+
.watch(root_path, RecursiveMode::Recursive)?;
7379

7480
while let Some(event) = rx.recv().await {
7581
let state = state.clone();
7682
let path = match event.paths.get(0) {
77-
Some(p) => match p.clone().to_str() {
78-
Some(s) => s.to_string(),
79-
None => continue,
80-
},
83+
Some(p) => p.clone(),
84+
None => continue,
85+
};
86+
87+
let path_string = match path.to_str() {
88+
Some(s) => s.to_string(),
8189
None => continue,
8290
};
8391

84-
if should_ignore(last_seen.clone(), &path).await || ignore.is_match(&*path) {
92+
if is_seen(last_seen.clone(), &path_string)
93+
|| custom_ignore.is_match(&*path_string)
94+
|| !gitignore
95+
.lock()
96+
.unwrap()
97+
.included_files()
98+
.unwrap()
99+
.contains(&path)
100+
{
85101
continue;
86102
}
87103

shared/src/workspace.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::project::{Project, Target, TargetMap};
22
use anyhow::{bail, Ok, Result};
3-
use std::path::{Path, PathBuf};
3+
use std::path::PathBuf;
44

5-
use ::tracing::{debug, info, trace};
65
use libproc::libproc::proc_pid;
76
use notify::EventKind;
87
use std::process::Stdio;
@@ -34,7 +33,7 @@ impl Workspace {
3433
Project::new_from_project_yml(path).await?
3534
};
3635

37-
info!("[New::{}]: {:?}", project.name(), root);
36+
tracing::info!("[New::{}]: {:?}", project.name(), root);
3837

3938
Ok(Self {
4039
root,
@@ -53,7 +52,7 @@ impl Workspace {
5352
let name = self.project.name();
5453
self.clients.retain(|&pid| {
5554
if proc_pid::name(pid).is_err() {
56-
debug!("[Update::{}]: Remove Client: {pid}", name);
55+
tracing::debug!("[Update::{}]: Remove Client: {pid}", name);
5756
false
5857
} else {
5958
true
@@ -66,7 +65,7 @@ impl Workspace {
6665
// Remove no longer active clients
6766
self.update_clients();
6867
// NOTE: Implicitly assuming that pid is indeed a valid pid
69-
debug!("[Update::{}] Add Client: {pid}", self.name());
68+
tracing::debug!("[Update::{}] Add Client: {pid}", self.name());
7069
self.clients.push(pid)
7170
}
7271

@@ -98,7 +97,7 @@ impl Workspace {
9897
}
9998

10099
/// Regenerate compiled commands and xcodeGen if project.yml exists
101-
pub async fn on_dirctory_change(&self, _path: String, _event: EventKind) -> Result<()> {
100+
pub async fn on_dirctory_change(&self, _path: PathBuf, _event: EventKind) -> Result<()> {
102101
if self.is_xcodegen_project() {
103102
/*
104103
FIXME: make xCodeGen binary path configurable.
@@ -119,7 +118,7 @@ impl Workspace {
119118
.expect("Failed to run xcodeGen.");
120119

121120
if xcodegen.success() {
122-
info!("Generated {}.xcodeproj", self.name());
121+
tracing::info!("Generated {}.xcodeproj", self.name());
123122
}
124123
}
125124

0 commit comments

Comments
 (0)