waterui-cli 0.1.4

Cross-platform tooling for WaterUI applications
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! `water package` command implementation.

use std::path::PathBuf;

use clap::{Args as ClapArgs, ValueEnum};
use color_eyre::eyre::{Result, bail};

use crate::shell::Shell;
use crate::toolchain_checks;
use crate::{header, success};
use waterui_cli::{
    android::platform::{AndroidAbi, AndroidPlatform},
    apple::platform::{build_rust_lib, package_apple},
    apple::toolchain::AppleSdk,
    backend::reinit_backend,
    build::BuildOptions,
    device::Artifact,
    gtk4::{
        backend::Gtk4Backend,
        platform::{build_gtk4, package_gtk4},
    },
    hydrolysis::{
        backend::HydrolysisBackend,
        platform::{build_hydrolysis, package_hydrolysis},
    },
    platform::{PackageOptions, TargetPlatform as LibTargetPlatform},
    project::Project,
};

/// Target platform for packaging.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum TargetPlatform {
    /// iOS (physical device).
    Ios,
    /// iOS Simulator.
    IosSimulator,
    /// Android.
    Android,
    /// macOS.
    Macos,
    /// Linux.
    Linux,
    /// Windows.
    Windows,
    /// Web.
    Web,
}

/// Target backend for packaging.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum TargetBackend {
    /// Apple backend (UIKit/AppKit).
    Apple,
    /// Android backend.
    Android,
    /// GTK4 backend.
    Gtk4,
    /// Hydrolysis backend.
    Hydrolysis,
}

/// Target architecture for Android builds.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum AndroidArch {
    /// ARM64 (arm64-v8a) - modern Android devices
    Arm64,
    /// `x86_64` - emulators on Intel/AMD
    X86_64,
    /// `ARMv7` (armeabi-v7a) - older 32-bit devices
    Armv7,
    /// x86 - older 32-bit emulators
    X86,
}

impl AndroidArch {
    /// Convert to Android ABI string.
    const fn to_abi(self) -> AndroidAbi {
        match self {
            Self::Arm64 => AndroidAbi::Arm64V8a,
            Self::X86_64 => AndroidAbi::X86_64,
            Self::Armv7 => AndroidAbi::ArmeabiV7a,
            Self::X86 => AndroidAbi::X86,
        }
    }
}

/// Arguments for the package command.
#[derive(ClapArgs, Debug)]
pub struct Args {
    /// Target platform to package for.
    #[arg(short, long, value_enum)]
    platform: TargetPlatform,

    /// Backend to use (overrides default for platform).
    /// Required: `water package` always needs an explicit backend.
    #[arg(short, long, value_enum)]
    backend: TargetBackend,

    /// Build in release mode (optimized).
    #[arg(long)]
    release: bool,

    /// Package for store distribution (App Store, Play Store).
    #[arg(long)]
    distribution: bool,

    /// Project directory path (defaults to current directory).
    #[arg(long, default_value = ".")]
    path: PathBuf,

    /// Target architectures for Android (comma-separated).
    /// Examples: --arch arm64, --arch `arm64,x86-64`
    /// Required when packaging Android backend.
    #[arg(long, value_enum, value_delimiter = ',')]
    arch: Vec<AndroidArch>,
}

struct PackagingContext {
    project: Project,
    backend: TargetBackend,
    build_options: BuildOptions,
}

/// Run the package command.
pub async fn run(shell: &Shell, args: Args) -> Result<()> {
    let context = prepare_packaging_context(shell, &args).await?;
    print_packaging_header(
        shell,
        &context.project,
        args.platform,
        context.backend,
        args.release,
        args.distribution,
    );
    check_packaging_toolchain(shell, args.platform, context.backend, &args.arch).await?;
    build_packaging_artifacts(shell, &args, &context).await?;
    package_artifact(shell, &args, &context).await
}

async fn prepare_packaging_context(shell: &Shell, args: &Args) -> Result<PackagingContext> {
    let project_path = crate::project_path::canonicalize(&args.path)?;
    let project = Project::open(&project_path).await?;
    let backend = resolve_backend(args.platform, args.backend)?;

    validate_arch_args(backend, &args.arch)?;
    validate_desktop_backend_platform_on_host(args.platform, backend)?;
    ensure_packaging_backend_ready(&project, backend)?;
    let project =
        ensure_packaging_backend_generated(shell, &project_path, project, backend).await?;

    let mut build_options = BuildOptions::packaging(args.release);
    if let Some(sccache_path) = super::detect_sccache_path(shell).await {
        build_options = build_options.with_sccache(sccache_path);
    }

    Ok(PackagingContext {
        project,
        backend,
        build_options,
    })
}

fn ensure_packaging_backend_ready(project: &Project, backend: TargetBackend) -> Result<()> {
    if project.is_playground() {
        return Ok(());
    }

    match backend {
        TargetBackend::Apple if project.apple_backend().is_none() => {
            bail!("Apple backend is not configured. Run `water backend add apple`.");
        }
        TargetBackend::Android if project.android_backend().is_none() => {
            bail!("Android backend is not configured. Run `water backend add android`.");
        }
        TargetBackend::Gtk4 if project.gtk4_backend().is_none() => {
            bail!("GTK4 backend is not configured. Run `water backend add gtk4`.");
        }
        TargetBackend::Hydrolysis if project.hydrolysis_backend().is_none() => {
            bail!("Hydrolysis backend is not configured. Run `water backend add hydrolysis`.");
        }
        _ => Ok(()),
    }
}

async fn ensure_packaging_backend_generated(
    shell: &Shell,
    project_path: &PathBuf,
    project: Project,
    backend: TargetBackend,
) -> Result<Project> {
    match backend {
        TargetBackend::Gtk4 if project.is_playground() => {
            let needs_reinit = Gtk4Backend::requires_regeneration(&project).await?;
            ensure_packaging_generated_backend::<Gtk4Backend>(
                shell,
                project_path,
                project,
                needs_reinit,
                "Initializing GTK4 backend...",
                "GTK4 backend initialized",
            )
            .await
        }
        TargetBackend::Hydrolysis if project.is_playground() => {
            let needs_reinit = HydrolysisBackend::requires_regeneration(&project).await?;
            ensure_packaging_generated_backend::<HydrolysisBackend>(
                shell,
                project_path,
                project,
                needs_reinit,
                "Initializing hydrolysis backend...",
                "Hydrolysis backend initialized",
            )
            .await
        }
        _ => Ok(project),
    }
}

async fn ensure_packaging_generated_backend<T>(
    shell: &Shell,
    project_path: &PathBuf,
    project: Project,
    needs_reinit: bool,
    spinner_message: &str,
    success_message: &str,
) -> Result<Project>
where
    T: waterui_cli::backend::Backend,
{
    if !needs_reinit {
        return Ok(project);
    }

    let spinner = shell.spinner(spinner_message);
    reinit_backend::<T>(&project).await?;
    let project = Project::open(project_path).await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "{success_message}");
    Ok(project)
}

fn print_packaging_header(
    shell: &Shell,
    project: &Project,
    platform: TargetPlatform,
    backend: TargetBackend,
    release: bool,
    distribution: bool,
) {
    let mode = if release { "release" } else { "debug" };
    let dist = if distribution { " (distribution)" } else { "" };
    header!(
        shell,
        "Packaging {} for {} via {} ({}){}",
        project.crate_name(),
        platform_name(platform),
        backend_name(backend),
        mode,
        dist
    );
}

async fn check_packaging_toolchain(
    shell: &Shell,
    platform: TargetPlatform,
    backend: TargetBackend,
    arch: &[AndroidArch],
) -> Result<()> {
    let spinner = shell.spinner("Checking toolchain...");
    check_toolchain_for_backend(platform, backend, arch).await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Toolchain ready");
    Ok(())
}

async fn build_packaging_artifacts(
    shell: &Shell,
    args: &Args,
    context: &PackagingContext,
) -> Result<()> {
    match context.backend {
        TargetBackend::Android => {
            build_android_packaging_artifacts(
                shell,
                &context.project,
                &args.arch,
                context.build_options.clone(),
            )
            .await
        }
        TargetBackend::Apple => {
            build_apple_packaging_artifacts(
                shell,
                &context.project,
                args.platform,
                context.build_options.clone(),
            )
            .await
        }
        TargetBackend::Gtk4 => {
            build_gtk4_packaging_artifacts(shell, &context.project, context.build_options.clone())
                .await
        }
        TargetBackend::Hydrolysis => {
            build_hydrolysis_packaging_artifacts(
                shell,
                &context.project,
                args.platform,
                context.build_options.clone(),
            )
            .await
        }
    }
}

async fn build_android_packaging_artifacts(
    shell: &Shell,
    project: &Project,
    arch: &[AndroidArch],
    build_options: BuildOptions,
) -> Result<()> {
    AndroidPlatform::clean_jni_libs(project).await?;
    for arch in arch {
        let abi = arch.to_abi();
        let spinner = shell.spinner(format!("Building Rust library ({})...", abi.as_str()));
        shell
            .display_output(AndroidPlatform::new(abi).build(project, build_options.clone()))
            .await?;
        if let Some(pb) = spinner {
            pb.finish_and_clear();
        }
        success!(shell, "Built for {}", abi.as_str());
    }
    Ok(())
}

async fn build_apple_packaging_artifacts(
    shell: &Shell,
    project: &Project,
    platform: TargetPlatform,
    build_options: BuildOptions,
) -> Result<()> {
    let spinner = shell.spinner("Building Rust library...");
    shell
        .display_output(build_rust_lib(
            project,
            lib_platform(platform),
            build_options,
        ))
        .await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Built Rust library");
    Ok(())
}

async fn build_gtk4_packaging_artifacts(
    shell: &Shell,
    project: &Project,
    build_options: BuildOptions,
) -> Result<()> {
    let spinner = shell.spinner("Building GTK4 app...");
    shell
        .display_output(build_gtk4(project, build_options))
        .await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Built GTK4 app");
    Ok(())
}

async fn build_hydrolysis_packaging_artifacts(
    shell: &Shell,
    project: &Project,
    platform: TargetPlatform,
    build_options: BuildOptions,
) -> Result<()> {
    if platform == TargetPlatform::Web {
        return Ok(());
    }

    let spinner = shell.spinner("Building hydrolysis app...");
    shell
        .display_output(build_hydrolysis(
            project,
            hydrolysis_platform(platform),
            build_options,
        ))
        .await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Built hydrolysis app");
    Ok(())
}

async fn package_artifact(shell: &Shell, args: &Args, context: &PackagingContext) -> Result<()> {
    let spinner = shell.spinner("Packaging application...");
    let artifact = shell
        .display_output(package_artifact_inner(args, context))
        .await?;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    success!(shell, "Packaged at {}", artifact.path().display());
    Ok(())
}

async fn package_artifact_inner(args: &Args, context: &PackagingContext) -> Result<Artifact> {
    let package_options = PackageOptions::packaging(args.distribution, !args.release);
    match context.backend {
        TargetBackend::Android => {
            let abis: Vec<AndroidAbi> = args.arch.iter().map(|arch| arch.to_abi()).collect();
            AndroidPlatform::package_with_abis(&context.project, package_options, &abis).await
        }
        TargetBackend::Apple => {
            package_apple(
                &context.project,
                lib_platform(args.platform),
                package_options,
            )
            .await
        }
        TargetBackend::Gtk4 => package_gtk4(&context.project, package_options).await,
        TargetBackend::Hydrolysis => {
            package_hydrolysis(
                &context.project,
                hydrolysis_platform(args.platform),
                package_options,
            )
            .await
        }
    }
}

fn resolve_backend(platform: TargetPlatform, backend: TargetBackend) -> Result<TargetBackend> {
    let supported = matches!(
        (platform, backend),
        (
            TargetPlatform::Ios | TargetPlatform::IosSimulator,
            TargetBackend::Apple
        ) | (
            TargetPlatform::Macos,
            TargetBackend::Apple | TargetBackend::Hydrolysis
        ) | (TargetPlatform::Android, TargetBackend::Android)
            | (
                TargetPlatform::Linux,
                TargetBackend::Gtk4 | TargetBackend::Hydrolysis
            )
            | (
                TargetPlatform::Windows | TargetPlatform::Web,
                TargetBackend::Hydrolysis
            )
    );

    if !supported {
        bail!(
            "Backend {:?} does not support platform {:?}.\n\
             Valid combinations:\n  \
             - iOS/iOS Simulator: apple\n  \
             - Android: android\n  \
             - macOS: apple, hydrolysis\n  \
             - Linux: gtk4, hydrolysis\n  \
             - Windows: hydrolysis\n  \
             - Web: hydrolysis",
            backend,
            platform
        );
    }

    Ok(backend)
}

fn validate_arch_args(backend: TargetBackend, arch: &[AndroidArch]) -> Result<()> {
    if backend == TargetBackend::Android && arch.is_empty() {
        bail!(
            "Android backend requires --arch.\n\
             Examples:\n  \
             water package --platform android --backend android --arch arm64\n  \
             water package --platform android --backend android --arch arm64,x86-64"
        );
    }

    if backend != TargetBackend::Android && !arch.is_empty() {
        bail!("--arch is only valid when packaging Android backend");
    }

    Ok(())
}

async fn check_toolchain_for_backend(
    platform: TargetPlatform,
    backend: TargetBackend,
    arch: &[AndroidArch],
) -> Result<()> {
    match backend {
        TargetBackend::Apple => {
            let sdk = match platform {
                TargetPlatform::Ios => AppleSdk::Ios,
                TargetPlatform::IosSimulator => AppleSdk::IosSimulator,
                TargetPlatform::Macos => AppleSdk::Macos,
                TargetPlatform::Android
                | TargetPlatform::Linux
                | TargetPlatform::Windows
                | TargetPlatform::Web => {
                    bail!("Internal error: Apple backend is not supported on {platform:?}");
                }
            };
            toolchain_checks::check_apple(sdk).await?;
        }
        TargetBackend::Android => {
            if platform != TargetPlatform::Android {
                bail!("Internal error: Android backend is not supported on {platform:?}");
            }
            let required_abis = arch.iter().map(|arch| arch.to_abi()).collect::<Vec<_>>();
            toolchain_checks::check_android_build_or_package_for_abis(&required_abis).await?;
        }
        TargetBackend::Gtk4 => {
            if platform != TargetPlatform::Linux {
                bail!("Internal error: GTK4 backend is not supported on {platform:?}");
            }
            toolchain_checks::check_gtk4().await?;
        }
        TargetBackend::Hydrolysis => {
            if platform != TargetPlatform::Macos
                && platform != TargetPlatform::Linux
                && platform != TargetPlatform::Windows
                && platform != TargetPlatform::Web
            {
                bail!("Internal error: hydrolysis backend is not supported on {platform:?}");
            }
            if platform == TargetPlatform::Web {
                toolchain_checks::check_web().await?;
            } else {
                toolchain_checks::check_hydrolysis().await?;
            }
        }
    }
    Ok(())
}

fn validate_desktop_backend_platform_on_host(
    platform: TargetPlatform,
    backend: TargetBackend,
) -> Result<()> {
    if platform == TargetPlatform::Web {
        return Ok(());
    }

    match backend {
        TargetBackend::Gtk4 => {
            #[cfg(target_os = "linux")]
            {
                if platform != TargetPlatform::Linux {
                    bail!("GTK4 backend on Linux host requires --platform linux");
                }
            }

            #[cfg(not(target_os = "linux"))]
            {
                bail!("GTK4 backend is only supported on Linux hosts");
            }
        }
        TargetBackend::Hydrolysis => {
            #[cfg(target_os = "macos")]
            if platform != TargetPlatform::Macos {
                bail!("Hydrolysis backend on macOS host requires --platform macos");
            }

            #[cfg(target_os = "linux")]
            if platform != TargetPlatform::Linux {
                bail!("Hydrolysis backend on Linux host requires --platform linux");
            }

            #[cfg(target_os = "windows")]
            if platform != TargetPlatform::Windows {
                bail!("Hydrolysis backend on Windows host requires --platform windows");
            }

            #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
            bail!("Hydrolysis backend is only supported on macOS, Linux, or Windows hosts");
        }
        TargetBackend::Apple => {
            #[cfg(not(target_os = "macos"))]
            bail!("Apple backend requires a macOS host");
        }
        TargetBackend::Android => {}
    }

    Ok(())
}

const fn lib_platform(platform: TargetPlatform) -> LibTargetPlatform {
    match platform {
        TargetPlatform::Ios => LibTargetPlatform::IOS,
        TargetPlatform::IosSimulator => LibTargetPlatform::IOSSimulator,
        TargetPlatform::Android => LibTargetPlatform::Android,
        TargetPlatform::Macos => LibTargetPlatform::MacOS,
        TargetPlatform::Linux => LibTargetPlatform::Linux,
        TargetPlatform::Windows => LibTargetPlatform::Windows,
        TargetPlatform::Web => panic!("web is not a native lib target"),
    }
}

const fn hydrolysis_platform(platform: TargetPlatform) -> LibTargetPlatform {
    match platform {
        TargetPlatform::Macos => LibTargetPlatform::MacOS,
        TargetPlatform::Linux => LibTargetPlatform::Linux,
        TargetPlatform::Windows => LibTargetPlatform::Windows,
        TargetPlatform::Web => LibTargetPlatform::Web,
        TargetPlatform::Ios | TargetPlatform::IosSimulator | TargetPlatform::Android => {
            panic!("unsupported hydrolysis platform")
        }
    }
}

const fn platform_name(platform: TargetPlatform) -> &'static str {
    match platform {
        TargetPlatform::Ios => "iOS",
        TargetPlatform::IosSimulator => "iOS Simulator",
        TargetPlatform::Android => "Android",
        TargetPlatform::Macos => "macOS",
        TargetPlatform::Linux => "Linux",
        TargetPlatform::Windows => "Windows",
        TargetPlatform::Web => "Web",
    }
}

const fn backend_name(backend: TargetBackend) -> &'static str {
    match backend {
        TargetBackend::Apple => "Apple",
        TargetBackend::Android => "Android",
        TargetBackend::Gtk4 => "GTK4",
        TargetBackend::Hydrolysis => "Hydrolysis",
    }
}

#[cfg(test)]
mod tests {
    use super::{AndroidArch, TargetBackend, TargetPlatform, resolve_backend, validate_arch_args};

    #[test]
    fn rejects_empty_arch_for_android_backend() {
        assert!(validate_arch_args(TargetBackend::Android, &[]).is_err());
    }

    #[test]
    fn rejects_arch_for_non_android_backend() {
        let err = validate_arch_args(TargetBackend::Apple, &[AndroidArch::Arm64])
            .expect_err("non-android --arch should fail");
        assert!(err.to_string().contains("--arch is only valid"));
    }

    #[test]
    fn accepts_android_arch_values() {
        assert!(validate_arch_args(TargetBackend::Android, &[AndroidArch::Arm64]).is_ok());
    }

    #[test]
    fn resolve_backend_validates_explicit_backend() {
        assert_eq!(
            resolve_backend(TargetPlatform::Android, TargetBackend::Android)
                .expect("android backend"),
            TargetBackend::Android
        );
        assert_eq!(
            resolve_backend(TargetPlatform::Windows, TargetBackend::Hydrolysis)
                .expect("windows backend"),
            TargetBackend::Hydrolysis
        );
        assert!(resolve_backend(TargetPlatform::Windows, TargetBackend::Gtk4).is_err());
        assert_eq!(
            resolve_backend(TargetPlatform::Web, TargetBackend::Hydrolysis).expect("web backend"),
            TargetBackend::Hydrolysis
        );
    }
}