nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! `nv` — Pure-Rust developer CLI for `native-view-engine`.
//!
//! Subcommands:
//! - `nv new app`   — scaffolds a standalone NativeView app (`Cargo.toml`, `nativeview.toml`, `src/lib.rs`, `web/index.html`).
//! - `nv init`      — initializes a NativeView app in the current directory.
//! - `nv dev`       — starts the 2-tier hot-reload daemon (`--skia`, `--web` standalone PWA, `--pair`).
//! - `nv pair`      — streams live `NvWireFrame` binary deltas over QUIC/WebTransport/WSS and ingests remote `InputEvents`.
//! - `nv build`     — builds any NV app (desktop, iOS Simulator, Android, Web PWA, or `web-pkg` / `rn-pkg` / `nitro-ui-pkg`).
//! - `nv run`       — builds and launches an NV app on desktop, iOS Simulator, Android, or Web.
//! - `nv xcframework` — builds Apple Tier-3 staticlib targets and `.xcframework` bundles.
//! - `nv sdk`       — installs, resolves, and verifies the cached read-only NativeView SDK (`sdk-manifest.json`).
//! - `nv codegen`   — hybrid FFI, React Native import, and multi-target component export codegen.
//! - `nv docs`      — validates, statically builds (SSR + WASM), and serves NV documentation sites.
//! - `nv doctor`    — verifies Rust targets, CMake/Ninja, Xcode/Android/Flutter/Swift SDKs, and caches.

mod build;
mod dev;
mod docs;
mod doctor;
mod new;
mod pair;
mod sdk;
mod static_http;
mod web_shell;

pub(crate) use nativeview_cli::{cargo_env, codegen, manifest, hot_diff};

use std::path::PathBuf;

use clap::{Parser, Subcommand};

use crate::build::{AppBuildOptions, AppRunOptions, XcframeworkOptions};
use crate::cargo_env::sdk::CLI_VERSION;
use crate::codegen::CodegenAction;
use crate::dev::DevOptions;
use crate::docs::DocsAction;
use crate::new::NewAction;
use crate::pair::PairOptions;
use crate::sdk::SdkAction;

#[derive(Parser)]
#[command(name = "nv", version, about = "Native View Engine developer CLI")]
pub(crate) struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub(crate) enum Commands {
    /// Check local toolchain, mobile SDKs, compilation caches, and React Native sources.
    Doctor,
    /// Manage the read-only cached NativeView SDK (`install`, `path`, `verify`).
    Sdk {
        #[command(subcommand)]
        action: SdkAction,
    },
    /// Scaffold a new standalone NativeView project (`nv new app <name>`).
    New {
        #[command(subcommand)]
        action: NewAction,
    },
    /// Initialize a standalone NativeView app in the current directory.
    Init {
        /// Optional kebab-case app name (defaults to the current directory name).
        #[arg(long)]
        name: Option<String>,
        /// `nativeview` crate version constraint (defaults to this CLI's version).
        #[arg(long, default_value = CLI_VERSION)]
        sdk_version: String,
    },
    /// Run the 2-tier hot-reload daemon (`nv dev`) or query/wait on a running session (`nv dev wait`, `nv dev status`).
    #[command(subcommand_precedence_over_arg = true)]
    Dev {
        #[command(subcommand)]
        action: Option<DevAction>,
        /// Optional path to an app directory or `Cargo.toml` (defaults to cwd or `nativeview-studio-workspace`).
        #[arg(value_name = "APP_PATH")]
        app_path: Option<PathBuf>,
        /// Crate/package name of the cdylib app to watch and hot-rebuild (defaults to cwd or `nativeview-studio-workspace`).
        #[arg(long, short = 'p')]
        package: Option<String>,
        /// Override NV SDK root (defaults to `$NV_SDK_ROOT`, cached SDK, or ancestor discovery).
        #[arg(long)]
        sdk_root: Option<PathBuf>,
        /// Directory(ies) to watch for Rust source changes (repeatable; defaults to app + transitive path deps).
        #[arg(long)]
        watch: Vec<PathBuf>,
        /// Bind address for the hot-reload WebSocket server (`0.0.0.0` allows LAN/phone clients).
        #[arg(long, default_value = "0.0.0.0")]
        host: String,
        /// WebSocket port for Tier-1/Tier-2 hot-reload clients (`0` selects an ephemeral port).
        #[arg(long, default_value_t = 8089)]
        port: u16,
        /// Rebuild and serve the app's Web WASM bundle on save (`hosts/web-shell` for workspace apps or standalone `[features] web` PWA).
        #[arg(long, default_value_t = false)]
        web: bool,
        /// HTTP port serving the Web dev bundle while rebuilt on save.
        #[arg(long, default_value_t = 8088)]
        web_port: u16,
        /// Skip rebuilding and serving the Web WASM dev bundle on save.
        #[arg(long, default_value_t = false)]
        no_web: bool,
        /// Launch the live cross-platform desktop preview window (`nv-skia-shell`) connected to this daemon.
        #[arg(long, default_value_t = false)]
        skia: bool,
        /// Enable live `NvWireFrame` device pairing (UDP + TCP/WebSocket delta streaming + remote input backchannel).
        #[arg(long, default_value_t = false)]
        pair: bool,
        /// UDP port for live `NvWireFrame` delta pairing when `--pair` is enabled.
        #[arg(long, default_value_t = 8091)]
        pair_udp_port: u16,
        /// TCP/WebSocket port for live `NvWireFrame` delta pairing when `--pair` is enabled.
        #[arg(long, default_value_t = 8092)]
        pair_ws_port: u16,
        /// Optional pairing token secret for `--pair` (auto-generated when omitted).
        #[arg(long)]
        pair_token: Option<String>,
        /// Bind ephemeral OS-assigned ports (`:0`) for WS, HTTP, and Pair so parallel worktrees never collide.
        #[arg(long, default_value_t = false)]
        auto_ports: bool,
    },
    /// Stream live `NvWireFrame` binary deltas over UDP + TCP/WebSocket and ingest remote `InputEvents`.
    Pair {
        /// Direct path to an already-built app cdylib exporting `nv_get_interface`.
        #[arg(long)]
        dylib: Option<PathBuf>,
        /// Workspace crate/package name of the cdylib to load (builds automatically if missing).
        #[arg(long)]
        package: Option<String>,
        /// Bind address (`0.0.0.0` allows LAN/phone clients).
        #[arg(long, default_value = "0.0.0.0")]
        host: String,
        /// UDP port for low-latency QUIC/WebTransport `NvWireFrame` datagrams.
        #[arg(long, default_value_t = 8091)]
        udp_port: u16,
        /// TCP / RFC-6455 binary WebSocket port for framed `NvWireFrame` streams.
        #[arg(long, default_value_t = 8092)]
        ws_port: u16,
        /// Optional pairing token secret (auto-generated when omitted).
        #[arg(long)]
        token: Option<String>,
        /// Initial logical viewport width in points.
        #[arg(long, default_value_t = 393.0)]
        width: f32,
        /// Initial logical viewport height in points.
        #[arg(long, default_value_t = 852.0)]
        height: f32,
        /// Target frame/poll cadence in Hz.
        #[arg(long, default_value_t = 60)]
        fps: u32,
    },
    /// Build an NV app for desktop (macOS `.app`, Linux, Windows), Android APK (`view` | `compose`), or Web (PWA + optional MV3 extension).
    Build {
        /// Optional path to an app directory or `Cargo.toml` (defaults to cwd or workspace app).
        #[arg(value_name = "APP_PATH")]
        app_path: Option<PathBuf>,
        /// Cargo package name of the `cdylib` app to build.
        #[arg(long, short = 'p')]
        package: Option<String>,
        /// Target platform (`macos`, `linux`, `windows`, `web`, `ios-sim`, `android`, `web-pkg`, `rn-pkg`, `nitro-ui-pkg`; defaults to host OS).
        #[arg(long, short = 't')]
        target: Option<String>,
        /// Host shell (`macos`, `skia`, `gtk`, `qt`, `windows`, `view`, `compose`; overrides `nativeview.toml [app.shell]`).
        #[arg(long)]
        shell: Option<String>,
        /// Build a shared mobile host runtime kit (`rn` | `nitro-ui`) with `NvRuntime.xcframework` and `libnv_android_shell.so`.
        #[arg(long)]
        kit: Option<String>,
        /// Comma-separated mobile native slices for `--target rn-pkg` / `nitro-ui-pkg` / `--kit` (`ios-sim`, `ios-device`, `ios`, `android`, `android-arm64`, `android-x86_64`, `all`, `none`).
        #[arg(long)]
        platforms: Option<String>,
        /// Build with the release profile (`--release` / `CMAKE_BUILD_TYPE=Release`).
        #[arg(long, default_value_t = false)]
        release: bool,
        /// Output directory override (defaults to `<target_dir>/nv-apps/<lib_name>/<target[-shell]>/`).
        #[arg(long)]
        out: Option<PathBuf>,
        /// Override NV SDK root (defaults to `$NV_SDK_ROOT`, cached SDK, or ancestor discovery).
        #[arg(long)]
        sdk_root: Option<PathBuf>,
        /// Print the deterministic build plan without invoking Cargo, CMake, Gradle, or packagers.
        #[arg(long, default_value_t = false)]
        dry_run: bool,
    },
    /// Build and launch an NV app on desktop, iOS Simulator, Android (`adb`), or serve its Web PWA over localhost.
    Run {
        /// Optional path to an app directory or `Cargo.toml` (defaults to cwd or workspace app).
        #[arg(value_name = "APP_PATH")]
        app_path: Option<PathBuf>,
        /// Cargo package name of the `cdylib` app to run.
        #[arg(long, short = 'p')]
        package: Option<String>,
        /// Target platform (`macos`, `linux`, `windows`, `web`, `ios-sim`, `android`; defaults to host OS).
        #[arg(long, short = 't')]
        target: Option<String>,
        /// Host shell (`macos`, `skia`, `gtk`, `qt`, `windows`, `view`, `compose`; overrides `nativeview.toml [app.shell]`).
        #[arg(long)]
        shell: Option<String>,
        /// Build with the release profile (`--release` / `CMAKE_BUILD_TYPE=Release`).
        #[arg(long, default_value_t = false)]
        release: bool,
        /// Override NV SDK root (defaults to `$NV_SDK_ROOT`, cached SDK, or ancestor discovery).
        #[arg(long)]
        sdk_root: Option<PathBuf>,
        /// Local HTTP port when running `--target web` (`http://localhost:<port>/`).
        #[arg(long, default_value_t = 8090)]
        http_port: u16,
        /// Exec macOS `Contents/MacOS/<Executable>` directly instead of `open -W -n` (auto-enabled for `--screenshot` / `--help`).
        #[arg(long, default_value_t = false)]
        direct: bool,
        /// Target iOS Simulator UDID/name (`--target ios-sim`) or Android `adb` serial (`--target android`).
        #[arg(long, value_name = "DEVICE")]
        device: Option<String>,
        /// Do not open the Simulator GUI app when launching `--target ios-sim`.
        #[arg(long, default_value_t = false)]
        headless: bool,
        /// Build/launch `--target ios-sim` without bundling a local iOS Rust cdylib (connect via pairing).
        #[arg(long, default_value_t = false)]
        pair_only: bool,
        /// Connect launched `--target ios-sim` shell to a live `nv dev --pair` or `nv pair` URI (implies `--pair-only`).
        #[arg(long, value_name = "URI")]
        pair_uri: Option<String>,
        /// Auto-discover the active worktree's `nv dev --pair` URI from `session.json` (implies `--pair-only`).
        #[arg(long, default_value_t = false)]
        pair_dev: bool,
        /// Print the deterministic build + launch plan without executing.
        #[arg(long, default_value_t = false)]
        dry_run: bool,
        /// Extra arguments passed through to the launched host shell (after `--`).
        #[arg(last = true, value_name = "SHELL_ARGS")]
        shell_args: Vec<String>,
    },
    /// Build Apple Tier-3 staticlib targets (`tvos`, `tvos-sim`, `visionos`, `visionos-sim`, `watchos`, `watchos-sim`) and `.xcframework` bundles.
    Xcframework {
        /// Target(s) to compile (repeatable or comma-separated): `tvos`, `tvos-sim`, `visionos`, `visionos-sim`, `watchos`, `watchos-sim`.
        #[arg(long = "target", short = 't', required = true, value_delimiter = ',')]
        targets: Vec<String>,
        /// Workspace crate/package name to compile as a staticlib exporting `nv_get_interface`.
        #[arg(long, short = 'p', default_value = "nativeview-studio-workspace")]
        package: String,
        /// Optional Rust library name override (defaults to `package` with `-` replaced by `_`).
        #[arg(long)]
        lib_name: Option<String>,
        /// Build with the release profile (`--release`, `profile.release.panic="abort"`).
        #[arg(long, default_value_t = false)]
        release: bool,
        /// Rustup toolchain for Tier-3 `-Z build-std=std,panic_abort` builds.
        #[arg(long, default_value = "nightly")]
        toolchain: String,
        /// Bundle built `.a` slices + `nv_abi.generated.h` into an `.xcframework` (optional path; defaults to `target/xcframework/<lib>.xcframework`).
        #[arg(long, num_args = 0..=1, default_missing_value = "auto")]
        xcframework: Option<PathBuf>,
        /// Directory containing `nv_abi.generated.h` for `.xcframework` bundling.
        #[arg(long)]
        headers: Option<PathBuf>,
        /// Override Cargo target directory (defaults to `$CARGO_TARGET_DIR` or `<repo>/target`).
        #[arg(long)]
        target_dir: Option<PathBuf>,
        /// Print the deterministic Cargo + `xcodebuild` commands, env vars, and static linkage plan without executing.
        #[arg(long, default_value_t = false)]
        dry_run: bool,
    },
    /// Bi-directional React Native <-> NV and multi-framework (Flutter, SwiftUI, Compose, Web) codegen.
    Codegen {
        #[command(subcommand)]
        action: CodegenAction,
    },
    /// Validate, statically build (SSR + WASM), and serve NV documentation sites.
    Docs {
        #[command(subcommand)]
        action: DocsAction,
    },
}

#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub(crate) enum DevAction {
    /// Block until `nv dev` processes recent edits and connected hosts acknowledge the new frame.
    Wait {
        /// Optional edited file path(s) to verify against watched roots and force-scan immediately.
        paths: Vec<PathBuf>,
        /// Target a specific package session when multiple `nv dev` daemons run in this worktree.
        #[arg(long, short = 'p')]
        package: Option<String>,
        /// Maximum time to wait in milliseconds before returning exit code 2.
        #[arg(long, default_value_t = 30_000)]
        timeout_ms: u64,
        /// Settlement condition: `auto` (wait for connected hosts, else build), `build`, or `ack`.
        #[arg(long, value_enum, default_value_t = dev::control::WaitForMode::Auto)]
        wait_for: dev::control::WaitForMode,
        /// Fail fast with exit code 2 if zero hosts are connected.
        #[arg(long, default_value_t = false)]
        require_host: bool,
        /// Emit machine-readable JSON output.
        #[arg(long, default_value_t = false)]
        json: bool,
    },
    /// Show the live worktree-scoped `nv dev` session endpoints, pair URI, and connected hosts.
    Status {
        /// Target a specific package session when multiple `nv dev` daemons run in this worktree.
        #[arg(long, short = 'p')]
        package: Option<String>,
        /// Emit machine-readable JSON output.
        #[arg(long, default_value_t = false)]
        json: bool,
    },
}

fn main() {
    let cli = Cli::parse();
    match cli.command {
        Commands::Doctor => doctor::cmd_doctor(),
        Commands::Sdk { action } => sdk::cmd_sdk(action),
        Commands::New { action } => new::cmd_new(action),
        Commands::Init { name, sdk_version } => new::cmd_init(name, &sdk_version),
        Commands::Dev {
            action,
            app_path,
            package,
            sdk_root,
            watch,
            host,
            port,
            web,
            web_port,
            no_web,
            skia,
            pair,
            pair_udp_port,
            pair_ws_port,
            pair_token,
            auto_ports,
        } => {
            if let Some(ref act) = action
                && let Err(err) = dev::wait::validate_dev_subcommand_flags(
                    act,
                    sdk_root.as_ref(),
                    &watch,
                    &host,
                    port,
                    web,
                    web_port,
                    no_web,
                    skia,
                    pair,
                    pair_udp_port,
                    pair_ws_port,
                    pair_token.as_deref(),
                    auto_ports,
                )
            {
                eprintln!("error: {err}");
                std::process::exit(2);
            }
            match action {
                Some(DevAction::Wait {
                    paths,
                    package: wait_pkg,
                    timeout_ms,
                    wait_for,
                    require_host,
                    json,
                }) => {
                    let (repo_root, target_dir) =
                        dev::wait::resolve_dev_client_workspace_dirs(app_path.as_deref(), &paths);
                    let eff_pkg = wait_pkg.as_deref().or(package.as_deref());
                    let code = dev::wait::run_dev_wait(
                        &target_dir,
                        &repo_root,
                        eff_pkg,
                        paths,
                        timeout_ms,
                        wait_for,
                        require_host,
                        json,
                    );
                    if code != 0 {
                        std::process::exit(code);
                    }
                }
                Some(DevAction::Status {
                    package: status_pkg,
                    json,
                }) => {
                    let (repo_root, target_dir) =
                        dev::wait::resolve_dev_client_workspace_dirs(app_path.as_deref(), &[]);
                    let eff_pkg = status_pkg.as_deref().or(package.as_deref());
                    let code = dev::wait::run_dev_status(&target_dir, &repo_root, eff_pkg, json);
                    if code != 0 {
                        std::process::exit(code);
                    }
                }
                None => dev::cmd_dev(DevOptions {
                    app_path,
                    package,
                    sdk_root,
                    watch,
                    host,
                    port,
                    web,
                    web_port,
                    no_web,
                    skia,
                    pair,
                    pair_udp_port,
                    pair_ws_port,
                    pair_token,
                    auto_ports,
                }),
            }
        }
        Commands::Pair {
            dylib,
            package,
            host,
            udp_port,
            ws_port,
            token,
            width,
            height,
            fps,
        } => pair::cmd_pair(PairOptions {
            dylib,
            package,
            host,
            udp_port,
            ws_port,
            token,
            width,
            height,
            fps,
        }),
        Commands::Build {
            app_path,
            package,
            target,
            shell,
            kit,
            platforms,
            release,
            out,
            sdk_root,
            dry_run,
        } => build::cmd_build(AppBuildOptions {
            app_path,
            package,
            target,
            shell,
            kit,
            platforms,
            release,
            out,
            sdk_root,
            dry_run,
        }),
        Commands::Run {
            app_path,
            package,
            target,
            shell,
            release,
            sdk_root,
            http_port,
            direct,
            device,
            headless,
            pair_only,
            pair_uri,
            pair_dev,
            dry_run,
            shell_args,
        } => build::cmd_run(AppRunOptions {
            app_path,
            package,
            target,
            shell,
            release,
            sdk_root,
            http_port,
            direct,
            device,
            headless,
            pair_only,
            pair_uri,
            pair_dev,
            dry_run,
            shell_args,
        }),
        Commands::Xcframework {
            targets,
            package,
            lib_name,
            release,
            toolchain,
            xcframework,
            headers,
            target_dir,
            dry_run,
        } => build::cmd_xcframework(XcframeworkOptions {
            targets,
            package,
            lib_name,
            release,
            toolchain,
            xcframework,
            headers,
            target_dir,
            dry_run,
        }),
        Commands::Codegen { action } => codegen::cmd_codegen(action),
        Commands::Docs { action } => docs::cmd_docs(action),
    }
}

#[cfg(test)]
mod main_tests;