hauchiwa 0.17.0

Flexible static website generator library with incremental rebuilds and cached image optimization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Watch mode is implemented as a three-part system:
//!
//! 1. **File watcher**: Uses the `notify` crate to monitor filesystem
//!    events recursively. It includes debouncing to prevent duplicate builds
//!    from rapid file saves.
//! 2. **WebSocket server**: Spawns a dedicated thread using `tungstenite`
//!    to maintain persistent connections with open browser tabs.
//! 3. **Client script**: The [`Environment`](crate::Environment) injects
//!    a lightweight JavaScript snippet into generated pages. This script
//!    connects to the WebSocket server and listens for a `"reload"` message.
//!
//! ## The Loop
//!
//! When a file change is detected:
//! 1. The graph identifies and rebuilds only the "dirty" subgraph
//!    (incremental build).
//! 2. Upon successful completion, the executor signals the WebSocket
//!    thread.
//! 3. The server broadcasts the reload command to all connected clients,
//!    triggering an immediate browser refresh.

use crate::engine::{collect_manifest, run_once_parallel, run_tasks_parallel};
use crate::{Environment, Mode, Website};

use std::collections::HashSet;
use std::env;
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::Duration;

use camino::{Utf8Path, Utf8PathBuf};

use glob::Pattern;
use notify::RecursiveMode;
use notify_debouncer_full::{DebounceEventResult, new_debouncer};
use petgraph::visit::IntoNodeReferences;
use tungstenite::WebSocket;

pub fn watch<G: Send + Sync>(
    site: &mut Website<G>,
    data: G,
    static_entries: Vec<(Utf8PathBuf, Utf8PathBuf)>,
) -> anyhow::Result<()> {
    let (tcp, port) = reserve_port()?;
    let pwd = env::current_dir()?;

    let globals = Environment {
        generator: "hauchiwa",
        mode: Mode::Watch,
        port: Some(port),
        data,
    };

    let prev_meta = crate::snapshot::SnapshotMeta::load().ok().flatten();

    let (mut cache, mut snapshot, _) = run_once_parallel(site, &globals)?;
    for (source, dist_rel) in &static_entries {
        snapshot.insert_static_file(dist_rel.clone(), source.clone());
    }
    tracing::info!("collected {} pages", snapshot.page_count());
    match prev_meta {
        Some(ref prev) => snapshot.commit_diff_meta(prev)?,
        None => snapshot.commit()?,
    }
    snapshot.to_meta().save()?;
    let mut prev_snapshot = snapshot;

    tracing::info!("initial build completed, now watching for changes...");
    let clients = Arc::new(Mutex::new(vec![]));

    let _thread_i = new_thread_ws_incoming(tcp, clients.clone());
    let (tx_reload, _thread_o) = new_thread_ws_reload(clients.clone());

    let (tx, rx) = std::sync::mpsc::channel::<DebounceEventResult>();
    let mut debouncer = new_debouncer(Duration::from_millis(250), None, move |result| {
        tx.send(result).ok();
    })?;

    let mut watched = HashSet::new();
    let mut filters = HashSet::new();
    for (_, task) in site.graph.node_references() {
        for path in &task.watched() {
            if let Ok((path, pattern)) = resolve_watch_path(path) {
                watched.insert(path);
                filters.insert(pattern);
            } else {
                tracing::error!("failed to resolve path: {}", &path);
            };
        }
    }

    // Collapse watched paths to reduce the number of watches
    let watched = collapse_watch_paths(watched);

    for path in watched {
        tracing::info!("watching {}", path);
        debouncer.watch(path, RecursiveMode::Recursive)?;
    }

    #[cfg(feature = "server")]
    let _thread_http = super::http::start();

    loop {
        match rx.recv() {
            Ok(Ok(events)) => {
                tracing::debug!("{:?} events received", events);

                let mut dirty_nodes = HashSet::new();
                for de in events {
                    for path in &de.event.paths {
                        if !filters.iter().any(|filter| filter.matches_path(path)) {
                            continue;
                        }

                        if let Some(path) = Utf8Path::from_path(path) {
                            let Ok(path) = path.strip_prefix(&pwd) else {
                                continue;
                            };
                            for index in site.graph.node_indices() {
                                let task = &site.graph[index];
                                if task.is_dirty(path) {
                                    dirty_nodes.insert(index);
                                }
                            }
                        }
                    }
                }

                if !dirty_nodes.is_empty() {
                    tracing::info!("change detected, re-running tasks...");
                    let mut to_rerun = HashSet::new();
                    for start_node in &dirty_nodes {
                        let mut dfs = petgraph::visit::Dfs::new(&site.graph, *start_node);
                        while let Some(nx) = dfs.next(&site.graph) {
                            to_rerun.insert(nx);
                        }
                    }

                    let _diagnostics = match run_tasks_parallel(
                        site,
                        &globals,
                        &mut cache,
                        &to_rerun,
                        &dirty_nodes,
                    ) {
                        Ok(res) => res,
                        Err(e) => {
                            tracing::error!("Error running tasks: {}", e);
                            continue;
                        }
                    };

                    let mut snapshot = collect_manifest(&cache, &site.graph);
                    for (source, dist_rel) in &static_entries {
                        snapshot.insert_static_file(dist_rel.clone(), source.clone());
                    }
                    tracing::info!("collected {} pages", snapshot.page_count());
                    if let Err(e) = snapshot.commit_diff(&prev_snapshot) {
                        tracing::error!("failed to write pages to dist: {}", e);
                        continue;
                    }
                    if let Err(e) = snapshot.to_meta().save() {
                        tracing::warn!("failed to save snapshot meta: {}", e);
                    }
                    prev_snapshot = snapshot;
                    tx_reload.send(()).ok();
                    tracing::info!("rebuild complete, watching for changes...");
                }
            }
            Ok(Err(e)) => tracing::error!("watch error: {:?}", e),
            Err(e) => tracing::error!("watch error: {:?}", e),
        }
    }
}

fn reserve_port() -> std::io::Result<(TcpListener, u16)> {
    let listener = match TcpListener::bind("127.0.0.1:1337") {
        Ok(sock) => sock,
        Err(_) => TcpListener::bind("127.0.0.1:0")?,
    };

    let addr = listener.local_addr()?;
    let port = addr.port();
    Ok((listener, port))
}

fn new_thread_ws_incoming(
    server: TcpListener,
    client: Arc<Mutex<Vec<WebSocket<TcpStream>>>>,
) -> JoinHandle<()> {
    std::thread::spawn(move || {
        for stream in server.incoming() {
            #[allow(clippy::unwrap_used)]
            // IO errors from incoming streams are logged or skipped; accept errors abort the connection
            let socket = tungstenite::accept(stream.unwrap()).unwrap();
            #[allow(clippy::unwrap_used)] // poisoned mutex means a thread panicked - unrecoverable
            client.lock().unwrap().push(socket);
        }
    })
}

fn new_thread_ws_reload(
    client: Arc<Mutex<Vec<WebSocket<TcpStream>>>>,
) -> (Sender<()>, JoinHandle<()>) {
    let (tx, rx) = std::sync::mpsc::channel();

    let thread = std::thread::spawn(move || {
        while rx.recv().is_ok() {
            #[allow(clippy::unwrap_used)] // poisoned mutex means a thread panicked - unrecoverable
            let mut clients = client.lock().unwrap();
            let mut broken = vec![];

            for (i, socket) in clients.iter_mut().enumerate() {
                match socket.send("reload".into()) {
                    Ok(_) => {}
                    Err(tungstenite::error::Error::Io(e)) => {
                        if e.kind() == std::io::ErrorKind::BrokenPipe {
                            broken.push(i);
                        }
                    }
                    Err(e) => {
                        tracing::error!("Error: {e:?}");
                    }
                }
            }

            for i in broken.into_iter().rev() {
                clients.remove(i);
            }

            // Close all but the last 10 connections
            let len = clients.len();
            if len > 10 {
                for mut socket in clients.drain(0..len - 10) {
                    socket.close(None).ok();
                }
            }
        }
    });

    (tx, thread)
}

/// Splits a glob string into a canonicalized static root path (for
/// watching) and a compiled absolute Pattern (for matching).
pub fn resolve_watch_path(glob_str: impl AsRef<str>) -> anyhow::Result<(Utf8PathBuf, Pattern)> {
    let path = Utf8Path::new(glob_str.as_ref());

    // Split path into static root and dynamic suffix (containing wildcards)
    let components: Vec<_> = path.components().collect();
    let split_idx = components
        .iter()
        .position(|c| c.as_str().contains(['*', '?', '[']))
        .unwrap_or(components.len());

    let root_part: Utf8PathBuf = components.iter().take(split_idx).collect();
    let suffix_part: Utf8PathBuf = components.iter().skip(split_idx).collect();

    // Canonicalize the static root (must exist on disk)
    let absolute_root = root_part.canonicalize_utf8()?;

    // If the suffix is empty, we must check if the root is a file or
    // directory. If it's a file, we watch its parent to ensure atomic
    // writes are caught.
    let (watch_root, match_pattern_str) =
        if suffix_part.as_str().is_empty() && absolute_root.is_file() {
            // Case: Concrete File (e.g., "README.md") -> Watch Parent, Match File
            let parent = absolute_root
                .parent()
                .unwrap_or(&absolute_root)
                .to_path_buf();
            (parent, absolute_root)
        } else {
            // Case: Directory (e.g., "src/") or Wildcard (e.g., "src/**/*.rs")
            // -> Watch Dir, Match Pattern
            let pattern_str = absolute_root.join(&suffix_part);
            (absolute_root, pattern_str)
        };

    let pattern = Pattern::new(watch_root.join(match_pattern_str).as_str())?;

    Ok((watch_root, pattern))
}

/// Reduces a set of paths to the minimal set of watch roots.
///
/// If we watch `/a` and `/a/b`, we only need to watch `/a` because
/// the watcher is recursive. This function sorts the paths and filters
/// out any path that is a subdirectory of a previously accepted path.
fn collapse_watch_paths(paths: HashSet<Utf8PathBuf>) -> Vec<Utf8PathBuf> {
    let mut paths: Vec<_> = paths.into_iter().collect();
    paths.sort();

    let mut filtered = Vec::new();
    for path in paths {
        if let Some(last) = filtered.last()
            && path.starts_with(last)
        {
            continue;
        }
        filtered.push(path);
    }

    filtered
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_concrete_file() {
        // Input: "README.md" (concrete file)
        let (watch, pattern) = resolve_watch_path("README.md").expect("Should resolve");

        let cwd = Utf8PathBuf::try_from(std::env::current_dir().unwrap()).unwrap();

        // Expectation:
        // Watch: "$CWD/README.md"
        // Pattern: "$CWD/README.md"
        assert_eq!(watch.as_str(), cwd);
        assert_eq!(pattern.as_str(), cwd.join("README.md"));
    }

    #[test]
    fn test_concrete_directory() {
        // Input: "src" (concrete directory)
        let (watch, pattern) = resolve_watch_path("src").expect("Should resolve");

        let cwd = Utf8PathBuf::try_from(std::env::current_dir().unwrap()).unwrap();

        // Expectation:
        // Watch: "src" directory
        // Pattern: "src"
        assert_eq!(watch.as_str(), cwd.join("src"));
        assert_eq!(pattern.as_str(), cwd.join("src"));
    }

    #[test]
    fn test_directory_wildcard() {
        // Input: "src/**/*.rs"
        let (watch, pattern) = resolve_watch_path("src/**/*.rs").expect("Should resolve");

        let cwd = Utf8PathBuf::try_from(std::env::current_dir().unwrap()).unwrap();

        // Expectation:
        // Watch: "src" directory (the static part)
        // Pattern: "src/**/*.rs"
        assert_eq!(watch.as_str(), cwd.join("src/"));
        assert_eq!(pattern.as_str(), cwd.join("src/**/*.rs"));
    }

    #[test]
    fn test_collapse_watch_paths() {
        let mut paths = HashSet::new();
        paths.insert(Utf8PathBuf::from("/a"));
        paths.insert(Utf8PathBuf::from("/a/b"));
        paths.insert(Utf8PathBuf::from("/a/b/c"));
        paths.insert(Utf8PathBuf::from("/b"));
        paths.insert(Utf8PathBuf::from("/c/d"));

        let collapsed = collapse_watch_paths(paths);

        // Expected: /a, /b, /c/d
        // /a/b and /a/b/c are covered by /a.
        assert_eq!(
            collapsed,
            vec![
                Utf8PathBuf::from("/a"),
                Utf8PathBuf::from("/b"),
                Utf8PathBuf::from("/c/d")
            ]
        );
    }

    #[test]
    fn test_collapse_watch_paths_siblings() {
        let mut paths = HashSet::new();
        paths.insert(Utf8PathBuf::from("/a/x"));
        paths.insert(Utf8PathBuf::from("/a/y"));

        let collapsed = collapse_watch_paths(paths);

        // Expected: /a/x, /a/y (neither is a parent of the other)
        assert_eq!(
            collapsed,
            vec![Utf8PathBuf::from("/a/x"), Utf8PathBuf::from("/a/y")]
        );
    }

    #[test]
    fn test_collapse_watch_paths_similar_names() {
        let mut paths = HashSet::new();
        paths.insert(Utf8PathBuf::from("/foo"));
        paths.insert(Utf8PathBuf::from("/foo-bar"));

        let collapsed = collapse_watch_paths(paths);

        // Expected: /foo, /foo-bar
        // /foo-bar is not a subdirectory of /foo
        assert_eq!(
            collapsed,
            vec![Utf8PathBuf::from("/foo"), Utf8PathBuf::from("/foo-bar")]
        );
    }
}