euv-cli 0.51.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, new, and Hyperlane SSE dynamic updates across all supported platforms.
Documentation
use super::*;

/// Filesystem modification stamp used for deterministic cross-platform change detection.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
pub struct FileStamp {
    /// Nanoseconds since UNIX epoch of last file modification.
    #[get(type(copy))]
    pub(crate) modified_nanos: u128,
    /// File byte length.
    #[get(type(copy))]
    pub(crate) size_bytes: u64,
}

/// Recursive file tree snapshot capturing stamps for all watched source files.
#[derive(Clone, Debug, Default, Eq, Getter, PartialEq)]
pub struct WatchSnapshot {
    /// Ordered map from watched file paths to their modification stamps.
    pub(crate) entries: BTreeMap<PathBuf, FileStamp>,
}

/// Categorized file changes detected between two consecutive `WatchSnapshot` scans.
#[derive(Clone, Debug, Default, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
pub struct FileChangeSet {
    /// Newly created file paths.
    pub(crate) added: Vec<String>,
    /// Modified file paths.
    pub(crate) modified: Vec<String>,
    /// Deleted file paths.
    pub(crate) removed: Vec<String>,
}

/// Signed Dynamic WASM OTA manifest served at `/__euv_ota/manifest.json`.
#[derive(Clone, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
pub struct OtaManifest {
    /// Monotonically increasing hot-update version number.
    #[get(type(copy))]
    pub(crate) version: u64,
    /// Target dynamic component slot identifier.
    pub(crate) slot_id: String,
    /// HTTP URL path serving the executable `.wasm` binary.
    pub(crate) module_url: String,
    /// FNV-1a integrity checksum of the `.wasm` bytes.
    #[get(type(copy))]
    pub(crate) checksum: u64,
    /// Keyed integrity signature matching `OTA_DEV_SIGNING_KEY`.
    pub(crate) signature: String,
    /// Byte size of the compiled `.wasm` binary.
    #[get(type(copy))]
    pub(crate) size_bytes: usize,
    /// Source files that triggered this hot-update build.
    pub(crate) changed_files: Vec<String>,
}

/// In-memory pair holding the active OTA manifest and its executable `.wasm` bytes.
#[derive(Clone, Debug, Eq, Getter, New, PartialEq)]
pub struct OtaBundleState {
    /// Public manifest metadata.
    pub(crate) manifest: OtaManifest,
    /// Raw executable `.wasm` binary bytes.
    pub(crate) wasm_bytes: Vec<u8>,
}

/// Structured live-reload notification broadcast over SSE `/__euv_hmr`.
#[derive(Clone, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
pub struct HmrEvent {
    /// Monotonic reload revision number.
    #[get(type(copy))]
    pub(crate) revision: u64,
    /// Event kind (`Reload`, `OtaUpdate`, or `BuildError`).
    #[get(type(copy))]
    pub(crate) kind: HmrEventKind,
    /// Human-readable status summary.
    pub(crate) message: String,
    /// Paths of changed files that triggered this event.
    pub(crate) changed_files: Vec<String>,
}

/// Configuration for starting a live hot-reload and v1 OTA development server.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DevServerConfig {
    /// Development target (`Docs`, `Web`, `Native`, `Tui`, `Cli`, `Harmony`, `Wearable`, `Miniapp`, `Quickapp`).
    #[get(type(copy))]
    pub(crate) target: DevTargetArg,
    /// Render mode forwarded to native or web runs.
    #[get(type(copy))]
    pub(crate) render: RenderArg,
    /// Whether release optimization profile is enabled for Web or Native rebuilds.
    #[get(type(copy))]
    pub(crate) release: bool,
    /// Source root directory watched for live changes.
    pub(crate) src_dir: PathBuf,
    /// Output directory served over HTTP.
    pub(crate) out_dir: PathBuf,
    /// Optional site title (used in Docs mode).
    pub(crate) title: Option<String>,
    /// Optional base URL prefix (used in Docs mode).
    pub(crate) base_url: String,
    /// Optional Cargo package name (used in Web/Native modes).
    pub(crate) package: Option<String>,
    /// Bind host address (default `127.0.0.1`).
    pub(crate) host: String,
    /// Bind TCP port (`0` selects an available ephemeral port).
    #[get(type(copy))]
    pub(crate) port: u16,
    /// File watcher polling interval in milliseconds.
    #[get(type(copy))]
    pub(crate) poll_ms: u64,
    /// Whether to expose v1 OTA endpoints (and legacy compatibility shim) on the dev server.
    #[get(type(copy))]
    pub(crate) enable_ota: bool,
    /// When `true`, performs initial build and server check then exits immediately.
    #[get(type(copy))]
    pub(crate) once: bool,
    /// When `true`, prioritizes live v1 OTA hot-push without restarting the host app.
    #[get(type(copy))]
    #[serde(default)]
    pub(crate) hot_ota: bool,
    /// Optional watched `.json` spec, `.wasm` binary, or dynamic widget crate pushed via v1 OTA.
    #[serde(default)]
    pub(crate) watch_ota_path: Option<PathBuf>,
    /// Optional explicit v1 OTA store root directory (defaults to `<out_dir>/.euv-updates`).
    #[serde(default)]
    pub(crate) ota_store_dir: Option<PathBuf>,
    /// Logical v1 OTA project identifier mounted inside `euv dev`.
    #[serde(default)]
    pub(crate) ota_project: String,
    /// Logical v1 OTA release channel mounted inside `euv dev` (default `"dev"`).
    #[serde(default)]
    pub(crate) ota_channel: String,
    /// HMAC-SHA256 signing secret used for local v1 OTA releases.
    #[serde(default)]
    pub(crate) ota_signing_key: String,
    /// Optional target route path updated by `--watch-ota` pushes.
    #[serde(default)]
    pub(crate) ota_route: Option<String>,
    /// Optional target component slot identifier updated by `--watch-ota` pushes.
    #[serde(default)]
    pub(crate) ota_slot: Option<String>,
    /// Debounce settle window in milliseconds coalescing rapid multi-file saves.
    #[get(type(copy))]
    #[serde(default)]
    pub(crate) debounce_ms: u64,
    /// When `true`, emits `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers for `SharedArrayBuffer`.
    #[get(type(copy))]
    #[serde(default)]
    pub(crate) coop_coep: bool,
    /// When `true`, falls back to `index.html` for non-asset client-side SPA routes.
    #[get(type(copy))]
    #[serde(default)]
    pub(crate) spa_fallback: bool,
}

/// Machine-readable report returned when starting or probing the dev/docs server.
#[derive(Clone, Data, Debug, Deserialize, Eq, New, PartialEq, Serialize)]
pub struct DevServerReport {
    /// Active dev mode (`docs`, `web`, `native`, `tui`, `cli`, `harmony`, `wearable`, `miniapp`, `quickapp`).
    pub(crate) mode: String,
    /// Base HTTP URL of the live dev server.
    pub(crate) url: String,
    /// SSE hot-reload stream URL.
    pub(crate) hmr_url: String,
    /// Legacy Dynamic WASM OTA manifest compatibility URL.
    pub(crate) ota_manifest_url: String,
    /// Output bundle directory served by the dev server.
    pub(crate) out_dir: String,
    /// Initial build revision number.
    #[get(type(copy))]
    pub(crate) revision: u64,
    /// Total generated documentation pages (for Docs mode, `0` otherwise).
    #[get(type(copy))]
    pub(crate) page_count: usize,
    /// Real v1 OTA signed manifest endpoint URL.
    #[new(skip)]
    #[serde(default)]
    pub(crate) v1_manifest_url: String,
    /// Real v1 OTA SSE events endpoint URL.
    #[new(skip)]
    #[serde(default)]
    pub(crate) v1_events_url: String,
    /// Health endpoint URL (`/healthz`).
    #[new(skip)]
    #[serde(default)]
    pub(crate) health_url: String,
    /// Detected LAN IPv4 v1 OTA manifest URLs reachable from real mobile devices and emulators.
    #[new(skip)]
    #[serde(default)]
    pub(crate) lan_urls: Vec<String>,
    /// Active v1 OTA logical project identifier.
    #[new(skip)]
    #[serde(default)]
    pub(crate) ota_project: String,
    /// Active v1 OTA release channel name.
    #[new(skip)]
    #[serde(default)]
    pub(crate) ota_channel: String,
}

/// Coalescing debounced file-change accumulator preventing redundant rebuild bursts on multi-file saves.
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct DebouncedWatcher {
    /// Minimum quiet window in milliseconds before flushing accumulated changes.
    #[get(type(copy))]
    pub(crate) debounce_ms: u64,
    /// Accumulated deduplicated file changes waiting for the quiet window to expire.
    pub(crate) pending: FileChangeSet,
    /// Timestamp of the most recently recorded filesystem change.
    pub(crate) last_change_at: Option<Instant>,
}

/// Deterministic state machine tracking zero-flicker build-before-swap transitions for native targets.
#[derive(Clone, Data, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct BuildBeforeSwapController {
    /// Active running process generation (`0` before initial launch).
    #[get(type(copy))]
    pub(crate) active_generation: u64,
    /// Total successful build-then-swap replacements completed.
    #[get(type(copy))]
    pub(crate) swap_count: u64,
    /// Number of failed rebuilds where the previous running app stayed alive.
    #[get(type(copy))]
    pub(crate) kept_alive_on_error_count: u64,
    /// Most recent compiler error recorded when keeping the prior process running.
    pub(crate) last_error: Option<String>,
}

/// RAII wrapper ensuring spawned native child processes are terminated and reaped on drop or shutdown.
#[derive(Debug, Default)]
pub(crate) struct ChildProcessGuard {
    /// Active spawned child process handle, if running.
    pub(crate) child: Option<Child>,
}

/// Shared concurrent state coordinating SSE reload notifications, OTA bundles, and server shutdown.
#[derive(Debug)]
pub struct HmrHub {
    /// Monotonically incremented rebuild revision counter.
    pub(crate) revision: AtomicU64,
    /// Latest broadcast `HmrEvent`.
    pub(crate) last_event: Mutex<Option<HmrEvent>>,
    /// Latest signed Dynamic WASM OTA manifest and binary bytes.
    pub(crate) ota_state: Mutex<OtaBundleState>,
    /// Flag indicating whether background HTTP and watcher loops should keep running.
    pub(crate) running: AtomicBool,
}

/// Running hot-reload dev server handle supporting clean programmatic shutdown and inspection.
#[derive(Debug)]
pub struct DevServerHandle {
    /// Bound socket address (`ip:port`).
    pub(crate) address: SocketAddr,
    /// Served output directory path.
    pub(crate) out_dir: PathBuf,
    /// Active mode label (`docs`, `web`, `native`, `tui`, `cli`, `harmony`, `wearable`, `miniapp`, `quickapp`).
    pub(crate) mode: String,
    /// Initial page count produced during startup build.
    pub(crate) page_count: usize,
    /// Backing v1 OTA `UpdateStore` mounted inside this dev server.
    pub(crate) ota_store: UpdateStore,
    /// Logical v1 OTA project identifier served by this handle.
    pub(crate) ota_project: String,
    /// Logical v1 OTA channel name served by this handle.
    pub(crate) ota_channel: String,
    /// Shared HMR and OTA state hub.
    pub(crate) hub: Arc<HmrHub>,
    /// Background HTTP/SSE/OTA server thread.
    pub(crate) http_thread: Option<JoinHandle<()>>,
    /// Background recursive file watcher and rebuild thread.
    pub(crate) watch_thread: Option<JoinHandle<()>>,
}

/// Internal HTTP serving parameters shared across accepted connections.
#[derive(Clone, Debug, Getter)]
pub(crate) struct HttpServeContext {
    /// Output root directory served over HTTP.
    #[get(pub(crate))]
    pub(crate) out_dir: PathBuf,
    /// Optional base URL prefix stripped from incoming static requests.
    #[get(pub(crate))]
    pub(crate) base_url: String,
    /// Whether Dynamic WASM and v1 OTA endpoints are exposed.
    #[get(pub(crate), type(copy))]
    pub(crate) enable_ota: bool,
    /// Whether `COOP`/`COEP` cross-origin isolation headers are emitted.
    #[get(pub(crate), type(copy))]
    pub(crate) coop_coep: bool,
    /// Whether non-asset 404 routes fall back to `index.html`.
    #[get(pub(crate), type(copy))]
    pub(crate) spa_fallback: bool,
    /// Backing v1 OTA store mounted on `/v1/*` and `/healthz`.
    #[get(pub(crate))]
    pub(crate) ota_store: UpdateStore,
    /// Shared HMR and OTA state hub.
    #[get(pub(crate))]
    pub(crate) hub: Arc<HmrHub>,
}