euv-cli 0.44.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, and new across Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use super::*;

/// Default dev server configuration targeting local Web development with OTA enabled.
impl Default for DevServerConfig {
    /// Returns a sensible default `DevServerConfig` bound to `127.0.0.1:8080`.
    ///
    /// # Returns
    ///
    /// - `Self` - Default development server configuration.
    fn default() -> Self {
        Self {
            target: DevTargetArg::Web,
            render: RenderArg::Auto,
            release: false,
            src_dir: PathBuf::from("."),
            out_dir: PathBuf::from(OUT_DIR_WEB),
            title: None,
            base_url: String::new(),
            package: None,
            host: DEFAULT_DEV_HOST.to_string(),
            port: 8080,
            poll_ms: 250,
            enable_ota: true,
            once: false,
        }
    }
}

/// Change-set inspection helpers for `FileChangeSet`.
impl FileChangeSet {
    /// Returns `true` when no files were added, modified, or removed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - File change set reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether the change set is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.get_added().is_empty()
            && self.get_modified().is_empty()
            && self.get_removed().is_empty()
    }

    /// Returns a flattened list of every added, modified, and removed file path.
    ///
    /// # Arguments
    ///
    /// - `&Self` - File change set reference.
    ///
    /// # Returns
    ///
    /// - `Vec<String>` - Combined changed path list.
    #[must_use]
    pub fn all_paths(&self) -> Vec<String> {
        let mut out: Vec<String> = Vec::with_capacity(
            self.get_added().len() + self.get_modified().len() + self.get_removed().len(),
        );
        out.extend(self.get_added().iter().cloned());
        out.extend(self.get_modified().iter().cloned());
        out.extend(self.get_removed().iter().cloned());
        out
    }
}

/// Recursive directory scanning and deterministic diffing for `WatchSnapshot`.
impl WatchSnapshot {
    /// Scans `root` recursively collecting modification stamps for files matching `allowed_exts`.
    ///
    /// # Arguments
    ///
    /// - `&Path` - Root project or documentation directory.
    /// - `&[&str]` - Allowed lowercase file extensions (for example `["md", "rs"]`).
    ///
    /// # Returns
    ///
    /// - `io::Result<Self>` - Collected directory snapshot or I/O error.
    pub fn scan(root: &Path, allowed_exts: &[&str]) -> io::Result<Self> {
        Self::scan_excluding(root, allowed_exts, None)
    }

    /// Scans `root` recursively while excluding `exclude_dir` so generated outputs never self-trigger rebuilds.
    ///
    /// # Arguments
    ///
    /// - `&Path` - Root project or documentation directory.
    /// - `&[&str]` - Allowed lowercase file extensions.
    /// - `Option<&Path>` - Optional output directory excluded from watching.
    ///
    /// # Returns
    ///
    /// - `io::Result<Self>` - Collected directory snapshot or I/O error.
    pub fn scan_excluding(
        root: &Path,
        allowed_exts: &[&str],
        exclude_dir: Option<&Path>,
    ) -> io::Result<Self> {
        let mut entries: BTreeMap<PathBuf, FileStamp> = BTreeMap::new();
        if !root.exists() {
            return Ok(Self { entries });
        }
        let Some(ex) = exclude_dir else {
            scan_dir_stamps(root, allowed_exts, None, None, false, &mut entries)?;
            return Ok(Self { entries });
        };
        let (canonical_exclude, exclude_is_canon): (PathBuf, bool) = match fs::canonicalize(ex) {
            Ok(canon) => (canon, true),
            Err(_) => (ex.to_path_buf(), false),
        };
        let canonical_root: Option<PathBuf> = if exclude_is_canon {
            fs::canonicalize(root).ok()
        } else {
            None
        };
        let (canon_dir, check_real_exclude): (Option<&Path>, bool) =
            if let Some(cr) = canonical_root.as_deref() {
                if canonical_exclude.starts_with(cr) || cr.starts_with(&canonical_exclude) {
                    (Some(cr), true)
                } else {
                    (None, false)
                }
            } else {
                (None, true)
            };
        scan_dir_stamps(
            root,
            allowed_exts,
            Some(canonical_exclude.as_path()),
            canon_dir,
            check_real_exclude,
            &mut entries,
        )?;
        Ok(Self { entries })
    }

    /// Computes added, modified, and removed paths comparing `self` (previous) against `next`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Previous snapshot.
    /// - `&Self` - Freshly scanned snapshot.
    ///
    /// # Returns
    ///
    /// - `FileChangeSet` - Categorized file differences.
    #[must_use]
    pub fn diff(&self, next: &Self) -> FileChangeSet {
        let mut added: Vec<String> = Vec::new();
        let mut modified: Vec<String> = Vec::new();
        let mut removed: Vec<String> = Vec::new();
        let mut prev_iter: Peekable<btree_map::Iter<'_, PathBuf, FileStamp>> =
            self.get_entries().iter().peekable();
        let mut next_iter: Peekable<btree_map::Iter<'_, PathBuf, FileStamp>> =
            next.get_entries().iter().peekable();
        while let (Some(&(prev_path, prev_stamp)), Some(&(next_path, next_stamp))) =
            (prev_iter.peek(), next_iter.peek())
        {
            match prev_path.cmp(next_path) {
                cmp::Ordering::Less => {
                    removed.push(prev_path.display().to_string());
                    let _: Option<(&PathBuf, &FileStamp)> = prev_iter.next();
                }
                cmp::Ordering::Greater => {
                    added.push(next_path.display().to_string());
                    let _: Option<(&PathBuf, &FileStamp)> = next_iter.next();
                }
                cmp::Ordering::Equal => {
                    if prev_stamp != next_stamp {
                        modified.push(next_path.display().to_string());
                    }
                    let _: Option<(&PathBuf, &FileStamp)> = prev_iter.next();
                    let _: Option<(&PathBuf, &FileStamp)> = next_iter.next();
                }
            }
        }
        for (prev_path, _) in prev_iter {
            removed.push(prev_path.display().to_string());
        }
        for (next_path, _) in next_iter {
            added.push(next_path.display().to_string());
        }
        FileChangeSet::new(added, modified, removed)
    }
}

/// Thread-safe HMR broadcast and OTA bundle coordination for `HmrHub`.
impl HmrHub {
    /// Creates a new `HmrHub` initialized at revision `1` with `initial_ota`.
    ///
    /// # Arguments
    ///
    /// - `OtaBundleState` - Initial signed Dynamic WASM OTA artifact.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized hub ready for concurrent access.
    #[must_use]
    pub fn new(initial_ota: OtaBundleState) -> Self {
        Self {
            revision: AtomicU64::new(1),
            last_event: Mutex::new(Some(HmrEvent::new(
                1,
                HmrEventKind::Reload,
                MSG_INITIAL_BUILD_READY.to_string(),
                Vec::new(),
            ))),
            ota_state: Mutex::new(initial_ota),
            running: AtomicBool::new(true),
        }
    }

    /// Returns the current monotonic rebuild revision counter.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    ///
    /// # Returns
    ///
    /// - `u64` - Current revision number.
    #[must_use]
    pub fn revision(&self) -> u64 {
        let Self { revision, .. } = self;
        revision.load(Ordering::Acquire)
    }

    /// Increments the revision counter and publishes a new `HmrEvent` to all SSE subscribers.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    /// - `HmrEventKind` - Event variant (`Reload`, `OtaUpdate`, or `BuildError`).
    /// - `String` - Human-readable event description.
    /// - `Vec<String>` - List of changed source paths.
    ///
    /// # Returns
    ///
    /// - `u64` - Newly assigned revision number.
    pub fn publish(&self, kind: HmrEventKind, message: String, changed_files: Vec<String>) -> u64 {
        let Self {
            revision,
            last_event,
            ..
        } = self;
        let next_rev: u64 = revision.fetch_add(1, Ordering::AcqRel) + 1;
        let ev: HmrEvent = HmrEvent::new(next_rev, kind, message, changed_files);
        if let Ok(mut guard) = last_event.lock() {
            *guard = Some(ev);
        }
        next_rev
    }

    /// Replaces the active signed Dynamic WASM OTA bundle served over HTTP.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    /// - `OtaBundleState` - Freshly compiled and signed OTA artifact.
    pub fn set_ota_state(&self, next_state: OtaBundleState) {
        let Self { ota_state, .. } = self;
        if let Ok(mut guard) = ota_state.lock() {
            *guard = next_state;
        }
    }

    /// Returns a cloned copy of the latest published `HmrEvent`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    ///
    /// # Returns
    ///
    /// - `Option<HmrEvent>` - Latest event snapshot.
    #[must_use]
    pub fn current_event(&self) -> Option<HmrEvent> {
        let Self { last_event, .. } = self;
        last_event
            .lock()
            .ok()
            .and_then(|g: MutexGuard<'_, Option<HmrEvent>>| g.clone())
    }

    /// Returns a cloned snapshot of the active OTA manifest and `.wasm` bytes.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    ///
    /// # Returns
    ///
    /// - `Option<OtaBundleState>` - Active OTA artifact if lock is unpoisoned.
    #[must_use]
    pub fn ota_snapshot(&self) -> Option<OtaBundleState> {
        let Self { ota_state, .. } = self;
        ota_state
            .lock()
            .ok()
            .map(|g: MutexGuard<'_, OtaBundleState>| g.clone())
    }

    /// Signals all background HTTP and watcher threads to exit cleanly.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    pub fn stop(&self) {
        let Self { running, .. } = self;
        running.store(false, Ordering::Release);
    }

    /// Returns `true` while the dev server has not been shut down.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hub reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Running state flag.
    #[must_use]
    pub fn is_running(&self) -> bool {
        let Self { running, .. } = self;
        running.load(Ordering::Acquire)
    }
}

/// Runtime inspection and graceful shutdown operations for `DevServerHandle`.
impl DevServerHandle {
    /// Returns the bound local TCP socket address (`ip:port`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Server handle reference.
    ///
    /// # Returns
    ///
    /// - `SocketAddr` - Bound address.
    #[must_use]
    pub fn address(&self) -> SocketAddr {
        let Self { address, .. } = self;
        *address
    }

    /// Returns the base HTTP URL for this running dev server.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Server handle reference.
    ///
    /// # Returns
    ///
    /// - `String` - Formatted `http://<ip>:<port>` URL.
    #[must_use]
    pub fn url(&self) -> String {
        format!("http://{}", self.address())
    }

    /// Returns the current live rebuild revision counter.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Server handle reference.
    ///
    /// # Returns
    ///
    /// - `u64` - Active revision number.
    #[must_use]
    pub fn revision(&self) -> u64 {
        let Self { hub, .. } = self;
        hub.revision()
    }

    /// Builds a structured `DevServerReport` describing this active server.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Server handle reference.
    ///
    /// # Returns
    ///
    /// - `DevServerReport` - Serializable report snapshot.
    #[must_use]
    pub fn report(&self) -> DevServerReport {
        let Self {
            out_dir,
            mode,
            page_count,
            hub,
            ..
        } = self;
        let base_url: String = self.url();
        DevServerReport::new(
            mode.clone(),
            base_url.clone(),
            format!("{base_url}{HMR_ENDPOINT}"),
            format!("{base_url}{OTA_MANIFEST_ENDPOINT}"),
            out_dir.display().to_string(),
            hub.revision(),
            *page_count,
        )
    }

    /// Gracefully stops the HTTP listener and file watcher threads and joins them.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable server handle.
    pub fn shutdown(&mut self) {
        let Self {
            address,
            hub,
            http_thread,
            watch_thread,
            ..
        } = self;
        hub.stop();
        drop(TcpStream::connect(*address));
        if let Some(handle) = watch_thread.take() {
            drop(handle.join());
        }
        if let Some(handle) = http_thread.take() {
            drop(handle.join());
        }
    }
}

/// Ensures background server threads are cleanly stopped when `DevServerHandle` is dropped.
impl Drop for DevServerHandle {
    /// Signals shutdown and joins background threads on drop.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Handle being dropped.
    fn drop(&mut self) {
        self.shutdown();
    }
}