vesper-player-cli 0.10.0

The vesper command-line tool for authoring, validating, and installing plugins.
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
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;

use clap::{Args, Subcommand};
use serde_json::{Value, json};

use crate::cli_error::{CliError, CliResult};
use crate::external_process::run_interruptible_capture_with_timeout;

mod kit;

const TARGET: &str = "aarch64-unknown-linux-ohos";
const MINIMUM_RUNTIME_API: u32 = 26;
const SHARED_PACKAGES: &[&str] = &[
    "player-model",
    "player-runtime",
    "player-download",
    "player-preload",
    "player-playlist",
    "player-platform-mobile",
];
const MAX_TOOL_BYTES: usize = 64 * 1024;
const MAX_CARGO_BYTES: usize = 8 * 1024 * 1024;
const UNVERIFIED: &[&str] = &[
    "Release native linking and dynamic dependencies",
    "Node-API, HAR packaging and independent consumer loading",
    "API 26 physical-device playback and resource-loader reuse",
];

#[derive(Debug, Args)]
pub(crate) struct HarmonyArgs {
    /// Repository root. Defaults to VESPER_REPO_ROOT or the current directory.
    #[arg(long, global = true)]
    root: Option<PathBuf>,
    /// Extracted Command Line Tools root; also accepts VESPER_HARMONY_COMMAND_LINE_TOOLS.
    #[arg(long, global = true)]
    command_line_tools: Option<PathBuf>,
    #[command(subcommand)]
    command: HarmonyCommand,
}

#[derive(Debug, Subcommand)]
enum HarmonyCommand {
    /// Reports Rust, SDK and bundled tool identities without installing tools.
    Doctor,
    /// Audits the candidate shared mobile dependency tree for the OHOS target.
    AuditDependencies(CargoArgs),
    /// Cross-checks shared Rust crates; this does not link a native library.
    CheckShared(CargoArgs),
    /// Builds the three preview host-kit HARs and separated native symbols.
    KitHar(CargoArgs),
    /// Runs host-kit ownership, source, cache, download and system regressions.
    KitHostContracts,
    /// Verifies staged HARs, checksums and optional consumer native bytes.
    KitVerify(KitVerifyArgs),
    /// Builds a consumer using existing staged host-kit HARs only.
    KitConsumer(KitConsumerArgs),
    /// Runs one artifact-bound scenario on an already running official emulator.
    EmulatorGate(EmulatorArgs),
}

#[derive(Debug, Args)]
struct KitVerifyArgs {
    #[arg(long, default_value = "target/harmony-kit/dist")]
    directory: PathBuf,
    #[arg(long)]
    hap: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct KitConsumerArgs {
    #[arg(long)]
    har: PathBuf,
    #[arg(long)]
    ui: bool,
    #[arg(long)]
    example: bool,
    #[arg(long)]
    no_internet: bool,
}

#[derive(Debug, Args)]
struct EmulatorArgs {
    #[arg(long)]
    instance: String,
    #[arg(long)]
    device: String,
    #[arg(long)]
    hap: PathBuf,
    #[arg(long)]
    output: PathBuf,
    #[arg(long, default_value = "Run kit checks")]
    button: String,
    #[arg(long, default_value = "dev.vesper.harmony.kitconsumer")]
    bundle: String,
}

#[derive(Debug, Args)]
struct CargoArgs {
    /// Uses only locally cached Cargo dependencies.
    #[arg(long)]
    offline: bool,
}

pub(crate) fn run(arguments: HarmonyArgs) -> CliResult<()> {
    let root = crate::contract::resolve_repository_root(arguments.root.as_deref())
        .map_err(|error| CliError::manifest_or_package(error.to_string()))?;
    let tools = arguments
        .command_line_tools
        .or_else(|| {
            std::env::var_os("VESPER_HARMONY_COMMAND_LINE_TOOLS")
                .filter(|value| !value.is_empty())
                .map(PathBuf::from)
        })
        .map(|path| {
            if path.is_absolute() {
                path
            } else {
                root.join(path)
            }
        });
    match arguments.command {
        HarmonyCommand::Doctor => doctor(&root, tools.as_deref()),
        HarmonyCommand::AuditDependencies(options) => audit_dependencies(&root, options.offline),
        HarmonyCommand::CheckShared(options) => check_shared(&root, options.offline),
        HarmonyCommand::KitHar(options) => kit::build(&root, tools.as_deref(), options.offline),
        HarmonyCommand::KitHostContracts => kit::host(&root, tools.as_deref()),
        HarmonyCommand::KitVerify(options) => kit::verify(&root, &options),
        HarmonyCommand::KitConsumer(options) => kit::consumer(&root, tools.as_deref(), &options),
        HarmonyCommand::EmulatorGate(options) => kit::emulator(&root, tools.as_deref(), &options),
    }
}

fn emit(report: &Value) -> CliResult<()> {
    let mut output = std::io::stdout().lock();
    serde_json::to_writer_pretty(&mut output, report)
        .map_err(|error| CliError::worker(format!("cannot serialize Harmony report: {error}")))?;
    writeln!(output)
        .map_err(|error| CliError::worker(format!("cannot write Harmony report: {error}")))
}

fn capture(command: &mut Command, limit: usize, timeout: Duration) -> Result<String, String> {
    command.env("RUSTUP_AUTO_INSTALL", "0");
    let result = run_interruptible_capture_with_timeout(
        command,
        "Harmony host check",
        limit,
        limit,
        timeout,
    )
    .map_err(|error| error.to_string())?;
    if !result.status.success() {
        return Err(format!(
            "{}:\n{}\n{}",
            result.status,
            String::from_utf8_lossy(&result.stdout).trim(),
            String::from_utf8_lossy(&result.stderr).trim()
        ));
    }
    String::from_utf8(result.stdout).map_err(|error| format!("tool stdout is not UTF-8: {error}"))
}

fn record(checks: &mut Vec<Value>, id: &str, result: Result<String, String>) {
    let (status, detail) = match result {
        Ok(detail) => ("passed", detail),
        Err(detail) => ("blocked", detail),
    };
    checks.push(json!({ "id": id, "status": status, "detail": detail.trim() }));
}

fn doctor(root: &Path, tools: Option<&Path>) -> CliResult<()> {
    let mut checks = Vec::new();
    // Hvigor creates project-local state even for --version. Probe bundled tools
    // in a disposable directory so a read-only check leaves the checkout intact.
    let tool_directory = tempfile::tempdir().map_err(|error| {
        CliError::worker(format!("cannot create tool probe directory: {error}"))
    })?;
    let probe = |command: &mut Command| capture(command, MAX_TOOL_BYTES, Duration::from_secs(20));
    record(
        &mut checks,
        "rustc",
        probe(Command::new("rustc").arg("--version").current_dir(root)),
    );
    let cfg = probe(
        Command::new("rustc")
            .args(["--print", "cfg", "--target", TARGET])
            .current_dir(root),
    )
    .and_then(|cfg| {
        if [
            "target_arch=\"aarch64\"",
            "target_os=\"linux\"",
            "target_env=\"ohos\"",
        ]
        .iter()
        .all(|required| cfg.lines().any(|line| line == *required))
        {
            Ok(cfg)
        } else {
            Err(format!("unexpected OHOS target configuration: {cfg}"))
        }
    });
    record(&mut checks, "rust_target_cfg", cfg);
    let std = probe(
        Command::new("rustc")
            .args(["--print", "target-libdir", "--target", TARGET])
            .current_dir(root),
    )
    .and_then(|directory| {
        let path = Path::new(directory.trim());
        // Bound filesystem work even if a misconfigured compiler reports another directory.
        let installed = path.read_dir().ok().is_some_and(|entries| {
            entries.take(1024).filter_map(Result::ok).any(|entry| {
                let name = entry.file_name();
                let name = name.to_string_lossy();
                name.starts_with("libstd-") && name.ends_with(".rlib") && entry.path().is_file()
            })
        });
        if installed {
            Ok(directory)
        } else {
            Err(format!(
                "OHOS std is missing; run rustup target add {TARGET}"
            ))
        }
    });
    record(&mut checks, "rust_target_std", std);

    if let Some(tools) = tools {
        let tools = if tools.is_absolute() {
            tools.to_path_buf()
        } else {
            root.join(tools)
        };
        let native = tools.join("sdk/default/openharmony/native");
        let sdk = read_sdk_identity(&native);
        record(&mut checks, "sdk_release", sdk);
        record(
            &mut checks,
            "sdk_sysroot",
            if native
                .join("sysroot/usr/include/napi/native_api.h")
                .is_file()
                && native.join("sysroot/usr/lib/aarch64-linux-ohos").is_dir()
            {
                Ok(native.join("sysroot").display().to_string())
            } else {
                Err(format!(
                    "missing arm64 sysroot or Node-API headers under {}",
                    native.display()
                ))
            },
        );
        for (id, relative, args) in [
            (
                "clang",
                "sdk/default/openharmony/native/llvm/bin/clang",
                &["--version"][..],
            ),
            (
                "clang_cpp",
                "sdk/default/openharmony/native/llvm/bin/clang++",
                &["--version"][..],
            ),
            (
                "llvm_readelf",
                "sdk/default/openharmony/native/llvm/bin/llvm-readelf",
                &["--version"][..],
            ),
            (
                "llvm_nm",
                "sdk/default/openharmony/native/llvm/bin/llvm-nm",
                &["--version"][..],
            ),
            ("node", "tool/node/bin/node", &["--version"][..]),
            ("ohpm", "bin/ohpm", &["--version"][..]),
            ("hvigor", "bin/hvigorw", &["--version"][..]),
            ("hdc", "sdk/default/openharmony/toolchains/hdc", &["-v"][..]),
        ] {
            let path = tools.join(relative);
            let result = probe(
                Command::new(&path)
                    .args(args)
                    .current_dir(tool_directory.path())
                    .env("NODE_HOME", tools.join("tool/node"))
                    .env("DEVECO_NODE_HOME", tools.join("tool/node"))
                    .env("DEVECO_SDK_HOME", tools.join("sdk")),
            )
            .and_then(|version| {
                if version.trim().is_empty() {
                    Err(format!("{} returned no version identity", path.display()))
                } else {
                    Ok(format!("{}\n{}", path.display(), version.trim()))
                }
            });
            record(&mut checks, id, result);
        }
    } else {
        record(&mut checks, "command_line_tools", Err(
            "pass --command-line-tools /path/to/command-line-tools or set VESPER_HARMONY_COMMAND_LINE_TOOLS".to_owned(),
        ));
    }
    let passed = checks.iter().all(|check| check["status"] == "passed");
    emit(&json!({
        "schemaVersion": 1,
        "scope": "harmony-host-prerequisites",
        "target": TARGET,
        "plannedMinimumRuntimeApi": MINIMUM_RUNTIME_API,
        "status": if passed { "passed" } else { "blocked" },
        "checks": checks,
        "unverified": UNVERIFIED,
    }))?;
    if passed {
        Ok(())
    } else {
        Err(CliError::compatibility(
            "Harmony host prerequisites are incomplete; see the JSON report",
        ))
    }
}

fn read_sdk_identity(native: &Path) -> Result<String, String> {
    let path = native.join("oh-uni-package.json");
    let mut content = String::new();
    File::open(&path)
        .and_then(|file| {
            file.take(MAX_TOOL_BYTES as u64 + 1)
                .read_to_string(&mut content)
        })
        .map_err(|error| format!("cannot read {}: {error}", path.display()))?;
    if content.len() > MAX_TOOL_BYTES {
        return Err(format!("SDK identity exceeds {MAX_TOOL_BYTES} bytes"));
    }
    let identity: Value =
        serde_json::from_str(&content).map_err(|error| format!("invalid SDK identity: {error}"))?;
    let expected_api = MINIMUM_RUNTIME_API.to_string();
    if identity["apiVersion"].as_str() != Some(expected_api.as_str())
        || identity["platformVersion"] != "26.0.0"
        || identity["releaseType"] != "Release"
        || identity["version"].as_str().is_none_or(str::is_empty)
    {
        return Err(format!(
            "expected HarmonyOS SDK 26.0.0 Release (API {MINIMUM_RUNTIME_API}): {identity}"
        ));
    }
    Ok(format!("{}\n{identity}", path.display()))
}

fn cargo_command(root: &Path, operation: &str, offline: bool) -> Command {
    let mut command = Command::new("cargo");
    command
        .current_dir(root)
        .args([operation, "--locked", "--target", TARGET]);
    if offline {
        command.arg("--offline");
    }
    for package in SHARED_PACKAGES {
        command.args(["-p", package]);
    }
    command
}

fn audit_dependencies(root: &Path, offline: bool) -> CliResult<()> {
    let mut command = cargo_command(root, "tree", offline);
    command.args([
        "--edges",
        "normal,build",
        "--prefix",
        "none",
        "--format",
        "{p}",
    ]);
    let tree = capture(&mut command, MAX_CARGO_BYTES, Duration::from_secs(120))
        .map_err(CliError::worker)?;
    let packages = tree_packages(&tree)?;
    let forbidden: Vec<_> = packages
        .iter()
        .filter(|package| forbidden_package(package))
        .collect();
    emit(&json!({
        "schemaVersion": 1,
        "scope": "harmony-shared-mobile-dependency-audit",
        "target": TARGET,
        "roots": SHARED_PACKAGES,
        "status": if forbidden.is_empty() { "passed" } else { "failed" },
        "packages": packages,
        "forbiddenPackages": forbidden,
        "cargoTree": tree,
        "unverified": UNVERIFIED,
    }))?;
    if forbidden.is_empty() {
        Ok(())
    } else {
        Err(CliError::conformance(
            "desktop or optional media execution dependencies entered the Harmony shared-mobile candidate",
        ))
    }
}

fn tree_packages(tree: &str) -> CliResult<BTreeSet<String>> {
    let mut packages = BTreeSet::new();
    for line in tree.lines().filter(|line| !line.trim().is_empty()) {
        let mut fields = line.split_whitespace();
        let name = fields
            .next()
            .ok_or_else(|| CliError::conformance("missing Cargo package name"))?;
        let version = fields
            .next()
            .and_then(|value| value.strip_prefix('v'))
            .ok_or_else(|| CliError::conformance(format!("invalid Cargo tree record: {line}")))?;
        semver::Version::parse(version).map_err(|error| {
            CliError::conformance(format!("invalid Cargo package version: {error}"))
        })?;
        packages.insert(format!("{name} v{version}"));
    }
    for root in SHARED_PACKAGES {
        if !packages
            .iter()
            .any(|package| package.split_whitespace().next() == Some(root))
        {
            return Err(CliError::conformance(format!(
                "Cargo tree did not include required root {root}"
            )));
        }
    }
    Ok(packages)
}

fn forbidden_package(package: &str) -> bool {
    let name = package.split_whitespace().next().unwrap_or_default();
    matches!(
        name,
        "player-ffi"
            | "player-platform-desktop"
            | "player-platform-linux"
            | "player-platform-windows"
            | "player-platform-macos"
            | "player-host-desktop"
            | "player-platform-native-frame"
            | "player-backend-ffmpeg"
            | "player-audio-cpal"
            | "player-render-wgpu"
            | "cpal"
            | "winit"
            | "wgpu"
    ) || name.starts_with("ffmpeg-")
        || name.starts_with("player-decoder-")
        || name.starts_with("player-remux-")
        || name.starts_with("wasmtime")
}

fn check_shared(root: &Path, offline: bool) -> CliResult<()> {
    let result = capture(
        &mut cargo_command(root, "check", offline),
        MAX_CARGO_BYTES,
        Duration::from_secs(300),
    );
    emit(&json!({
        "schemaVersion": 1,
        "scope": "harmony-shared-rust-check",
        "target": TARGET,
        "roots": SHARED_PACKAGES,
        "status": if result.is_ok() { "passed" } else { "failed" },
        "detail": result.as_ref().map(String::as_str).unwrap_or_else(|error| error.as_str()),
        "unverified": UNVERIFIED,
    }))?;
    result.map(|_| ()).map_err(CliError::worker)
}