jffi 0.1.1

Cross-platform framework for building native apps with Rust business logic and platform-native UIs
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use anyhow::{Context, Result};
use colored::*;
use std::process::Command;
use std::path::Path;

fn validate_project_structure() -> Result<()> {
    if !Path::new("jffi.toml").exists() {
        anyhow::bail!(
            "{}\n\n{}",
            "Error: Not in a JFFI project directory.".red().bold(),
            format!(
                "This command must be run from a project created with:\n  {} {}\n\nOr navigate to an existing JFFI project directory.",
                "jffi new".bright_cyan(),
                "<project-name>".bright_yellow()
            )
        );
    }
    
    if !Path::new("ffi").exists() {
        anyhow::bail!(
            "{}\n\n{}",
            "Error: Missing 'ffi' directory.".red().bold(),
            "Your project structure appears incomplete. Expected directories: core/, ffi/, platforms/"
        );
    }
    
    if !Path::new("core").exists() {
        anyhow::bail!(
            "{}\n\n{}",
            "Error: Missing 'core' directory.".red().bold(),
            "Your project structure appears incomplete. Expected directories: core/, ffi/, platforms/"
        );
    }
    
    Ok(())
}

pub fn build_project(platform: Option<String>, all: bool, release: bool, device: bool) -> Result<()> {
    validate_project_structure()?;
    
    if all {
        build_all_platforms(release)?;
    } else if let Some(platform) = platform {
        build_platform_with_options(&platform, release, device)?;
    } else {
        anyhow::bail!("Specify --platform <platform> or --all");
    }
    
    Ok(())
}

pub fn build_platform_with_options(platform: &str, release: bool, device: bool) -> Result<()> {
    validate_project_structure()?;
    
    println!("{}", format!("🔨 Building for {}...", platform).bright_green().bold());
    
    match platform {
        "ios" => {
            let target_type = if device { "device" } else { "simulator" };
            build_ios_target(release, target_type)
        },
        "android" => build_android(release),
        "macos" | "macos-arm64" => build_macos("aarch64", release),
        "macos-x64" => build_macos("x86_64", release),
        "windows" | "windows-x64" => build_windows("x86_64", release),
        "windows-x86" => build_windows("i686", release),
        "linux" => build_linux(release),
        "web" => build_web(release),
        _ => anyhow::bail!("Unknown platform: {}", platform),
    }
}

fn build_all_platforms(release: bool) -> Result<()> {
    println!("{}", "🔨 Building all platforms...".bright_green().bold());
    println!();
    
    // Read config to get enabled platforms
    let config = crate::platforms::config::load_config()?;
    
    for platform in &config.platforms.enabled {
        println!("{} Building {}...", "".bright_blue(), platform.bright_cyan());
        if let Err(e) = build_platform(platform, release) {
            println!("{} Failed to build {}: {}", "".red(), platform, e);
        } else {
            println!("{} {} built successfully", "".green(), platform);
        }
        println!();
    }
    
    Ok(())
}

pub fn build_platform(platform: &str, release: bool) -> Result<()> {
    validate_project_structure()?;
    
    println!("{}", format!("🔨 Building for {}...", platform).bright_green().bold());
    
    match platform {
        "ios" => build_ios(release),
        "android" => build_android(release),
        "macos" | "macos-arm64" => build_macos("aarch64", release),
        "macos-x64" => build_macos("x86_64", release),
        "windows" | "windows-x64" => build_windows("x86_64", release),
        "windows-x86" => build_windows("i686", release),
        "linux" => build_linux(release),
        "web" => build_web(release),
        _ => anyhow::bail!("Unknown platform: {}", platform),
    }
}

fn build_ios(release: bool) -> Result<()> {
    build_ios_target(release, "simulator")
}

fn build_ios_target(release: bool, target_type: &str) -> Result<()> {
    let profile = if release { "release" } else { "debug" };
    
    // Choose target based on device vs simulator
    let (target, target_name) = match target_type {
        "device" => ("aarch64-apple-ios", "iOS Device"),
        "simulator" | _ => ("aarch64-apple-ios-sim", "iOS Simulator"),
    };
    
    println!("  {} Building Rust library for {}...", "".bright_blue(), target_name);
    let mut args = vec!["build"];
    if release {
        args.push("--release");
    }
    args.extend(&["--manifest-path", "ffi/Cargo.toml", "--target", target]);
    
    let status = Command::new("cargo")
        .args(&args)
        .status()
        .context("Failed to build Rust library")?;
    
    if !status.success() {
        anyhow::bail!("Rust build failed");
    }
    
    // Ensure uniffi-bindgen-cli is installed
    ensure_uniffi_bindgen_cli()?;
    
    println!("  {} Generating Swift bindings...", "".bright_blue());
    
    // Find the actual library file (it will have underscores instead of hyphens)
    let lib_dir = format!("target/{}/{}", target, profile);
    let _lib_pattern = format!("{}/lib*ffi.dylib", lib_dir);
    
    let lib_path = std::fs::read_dir(&lib_dir)
        .context("Failed to read target directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.starts_with("lib") && name_str.ends_with("ffi.dylib")
        })
        .map(|e| e.path())
        .context("Could not find FFI library")?;
    
    let status = Command::new("uniffi-bindgen-cli")
        .args(&[
            "generate",
            "--library",
            lib_path.to_str().unwrap(),
            "--language",
            "swift",
            "--out-dir",
            "platforms/ios",
        ])
        .status()
        .context("Failed to generate Swift bindings")?;
    
    if !status.success() {
        anyhow::bail!("Binding generation failed");
    }
    
    println!("{}", "  ✅ iOS build complete".green());
    Ok(())
}

fn build_android(release: bool) -> Result<()> {
    let profile = if release { "release" } else { "debug" };
    
    println!("  {} Building Rust library for Android...", "".bright_blue());
    
    // Build for multiple Android architectures
    let targets = vec![
        "aarch64-linux-android",
        "armv7-linux-androideabi",
        "x86_64-linux-android",
    ];
    
    // Ensure Android targets are installed
    ensure_android_targets(&targets)?;
    
    // Ensure cargo-ndk is installed for proper linking
    ensure_cargo_ndk()?;
    
    for target in targets {
        println!("    Building for {}...", target);
        
        let mut args = vec!["ndk", "build"];
        if release {
            args.push("--release");
        }
        args.extend(&["--manifest-path", "ffi/Cargo.toml", "--target", target]);
        
        let status = Command::new("cargo")
            .args(&args)
            .status()
            .context(format!("Failed to build for {}", target))?;
        
        if !status.success() {
            anyhow::bail!("Rust build failed for {}", target);
        }
    }
    
    println!("  {} Generating Kotlin bindings...", "".bright_blue());
    
    // Find the actual library file (it will have underscores instead of hyphens)
    let lib_dir = format!("target/aarch64-linux-android/{}", profile);
    
    let lib_path = std::fs::read_dir(&lib_dir)
        .context("Failed to read target directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.starts_with("lib") && name_str.ends_with("ffi.so")
        })
        .map(|e| e.path())
        .context("Could not find FFI library")?;
    
    let status = Command::new("uniffi-bindgen-cli")
        .args(&[
            "generate",
            "--library",
            lib_path.to_str().unwrap(),
            "--language",
            "kotlin",
            "--out-dir",
            "platforms/android/app/src/main/java",
        ])
        .status()
        .context("Failed to generate Kotlin bindings")?;
    
    if !status.success() {
        anyhow::bail!("Binding generation failed");
    }
    
    println!("{}", "  ✅ Android build complete".green());
    Ok(())
}

fn build_macos(arch: &str, release: bool) -> Result<()> {
    // Ensure uniffi-bindgen-cli is installed
    ensure_uniffi_bindgen_cli()?;
    
    let profile = if release { "release" } else { "debug" };
    let target = format!("{}-apple-darwin", arch);
    
    println!("  {} Building Rust library for macOS ({})...", "".bright_blue(), arch);
    let mut args = vec!["build"];
    if release {
        args.push("--release");
    }
    args.extend(&["--manifest-path", "ffi/Cargo.toml", "--target", &target]);
    
    let status = Command::new("cargo")
        .args(&args)
        .status()
        .context("Failed to build Rust library")?;
    
    if !status.success() {
        anyhow::bail!("Rust build failed");
    }
    
    println!("  {} Generating Swift bindings...", "".bright_blue());
    
    // Find the actual library file (it will have underscores instead of hyphens)
    let lib_dir = format!("target/{}/{}", target, profile);
    
    let lib_path = std::fs::read_dir(&lib_dir)
        .context("Failed to read target directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.starts_with("lib") && name_str.ends_with("ffi.dylib")
        })
        .map(|e| e.path())
        .context("Could not find FFI library")?;
    
    let status = Command::new("uniffi-bindgen-cli")
        .args(&[
            "generate",
            "--library",
            lib_path.to_str().unwrap(),
            "--language",
            "swift",
            "--out-dir",
            "platforms/macos",
        ])
        .status()
        .context("Failed to generate Swift bindings")?;
    
    if !status.success() {
        anyhow::bail!("Binding generation failed");
    }
    
    println!("{}", "  ✅ macOS build complete".green());
    Ok(())
}

fn build_windows(arch: &str, release: bool) -> Result<()> {
    // Ensure uniffi-bindgen-cs is installed
    ensure_uniffi_bindgen_cs()?;
    
    let profile = if release { "release" } else { "debug" };
    let target = format!("{}-pc-windows-msvc", arch);
    
    println!("  {} Building Rust library for Windows ({})...", "".bright_blue(), arch);
    
    let mut args = vec!["build"];
    if release {
        args.push("--release");
    }
    args.extend(&["--target", &target, "--manifest-path", "ffi/Cargo.toml"]);
    
    let status = Command::new("cargo")
        .args(&args)
        .status()
        .context("Failed to build Rust library")?;
    
    if !status.success() {
        anyhow::bail!("Rust build failed");
    }
    
    println!("  {} Generating C# bindings with uniffi-bindgen-cs...", "".bright_blue());
    
    // Use library mode to generate bindings directly from the compiled DLL
    // On Windows, Rust builds cdylib as <name>.dll (not lib<name>.dll)
    let lib_name = std::env::current_dir()?
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("ffi")
        .replace("-", "_");
    let lib_path = format!("target/{}/{}/{}_ffi.dll", target, profile, lib_name);
    
    let status = Command::new("uniffi-bindgen-cs")
        .arg("--library")
        .arg(&lib_path)
        .arg("--out-dir")
        .arg("platforms/windows")
        .status()
        .context("Failed to run uniffi-bindgen-cs")?;
    
    if !status.success() {
        anyhow::bail!("C# bindings generation failed");
    }
    
    // Copy the .dll to platforms/windows for the build
    let lib_path = format!("target/{}/{}/{}_ffi.dll", target, profile, lib_name);
    let dll_name = format!("{}_ffi.dll", lib_name);
    if std::path::Path::new(&lib_path).exists() {
        std::fs::copy(&lib_path, format!("platforms/windows/{}", dll_name))
            .context("Failed to copy DLL to platforms/windows")?;
    }
    
    println!("  {} Building C# project with MSBuild...", "".bright_blue());
    
    // Find the .csproj file
    let csproj_file = std::fs::read_dir("platforms/windows")
        .context("Failed to read platforms/windows directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.ends_with(".csproj")
        })
        .map(|e| e.path())
        .context("Could not find .csproj file")?;
    
    // Build with MSBuild (or dotnet build)
    let build_cmd = if Command::new("dotnet").arg("--version").output().is_ok() {
        "dotnet"
    } else {
        "msbuild"
    };
    
    let mut build_args = vec!["build"];
    if build_cmd == "dotnet" {
        build_args.push(csproj_file.to_str().unwrap());
        // WinUI 3 requires a specific platform, not AnyCPU
        build_args.extend(&["-p:Platform=x64"]);
        // Make it self-contained to include WinUI 3 runtime
        build_args.extend(&["-p:WindowsAppSDKSelfContained=true"]);
        if release {
            build_args.extend(&["-c", "Release"]);
        }
    } else {
        build_args.push(csproj_file.to_str().unwrap());
        build_args.extend(&["/p:Platform=x64"]);
        build_args.extend(&["/p:WindowsAppSDKSelfContained=true"]);
        if release {
            build_args.extend(&["/p:Configuration=Release"]);
        }
    }
    
    let status = Command::new(build_cmd)
        .args(&build_args)
        .status()
        .context(format!("Failed to build with {}", build_cmd))?;
    
    if !status.success() {
        anyhow::bail!("C# build failed");
    }
    
    // Copy the FFI DLL to the output directory after build
    println!("  {} Copying FFI DLL to output directory...", "".bright_blue());
    let output_dir = "platforms/windows/bin/x64/Debug/net8.0-windows10.0.19041.0";
    if std::path::Path::new(output_dir).exists() {
        let dll_source = format!("platforms/windows/{}_ffi.dll", lib_name);
        let dll_dest = format!("{}/{}_ffi.dll", output_dir, lib_name);
        if std::path::Path::new(&dll_source).exists() {
            std::fs::copy(&dll_source, &dll_dest)
                .context("Failed to copy FFI DLL to output directory")?;
            println!("  {} Copied {} to output directory", "".green(), format!("{}_ffi.dll", lib_name));
        }
    }
    
    println!("{}", "  ✅ Windows build complete".green());
    Ok(())
}

fn ensure_uniffi_bindgen_cli() -> Result<()> {
    println!("  {} Checking uniffi-bindgen-cli...", "".bright_blue());
    
    let check = Command::new("uniffi-bindgen-cli")
        .arg("--version")
        .output();
    
    if check.is_err() || !check.unwrap().status.success() {
        println!("    Installing uniffi-bindgen-cli (this may take a few minutes)...");
        let status = Command::new("cargo")
            .args(&["install", "uniffi-bindgen-cli"])
            .status()
            .context("Failed to install uniffi-bindgen-cli")?;
        
        if !status.success() {
            anyhow::bail!("Failed to install uniffi-bindgen-cli");
        }
        println!("    {} uniffi-bindgen-cli installed successfully", "".green());
    } else {
        println!("    {} uniffi-bindgen-cli is already installed", "".green());
    }
    
    Ok(())
}

fn ensure_uniffi_bindgen_cs() -> Result<()> {
    println!("  {} Checking uniffi-bindgen-cs...", "".bright_blue());
    
    let check = Command::new("uniffi-bindgen-cs")
        .arg("--version")
        .output();
    
    if check.is_err() || !check.unwrap().status.success() {
        println!("    Installing uniffi-bindgen-cs from main branch (this may take a few minutes)...");
        println!("    {} Note: uniffi-bindgen-cs targets UniFFI v0.29.4, but JFFI uses v0.31.0", "".yellow());
        println!("    {} This may cause compatibility issues", "".yellow());
        let status = Command::new("cargo")
            .args(&["install", "uniffi-bindgen-cs", "--git", "https://github.com/microsoft/uniffi-bindgen-cs", "--branch", "main"])
            .status()
            .context("Failed to install uniffi-bindgen-cs")?;
        
        if !status.success() {
            anyhow::bail!("Failed to install uniffi-bindgen-cs");
        }
        println!("    {} uniffi-bindgen-cs installed successfully", "".green());
    } else {
        println!("    {} uniffi-bindgen-cs is already installed", "".green());
    }
    
    Ok(())
}

fn build_linux(release: bool) -> Result<()> {
    let profile = if release { "release" } else { "debug" };
    
    println!("  {} Building Rust library for Linux...", "".bright_blue());
    
    let mut args = vec!["build"];
    if release {
        args.push("--release");
    }
    args.extend(&["--manifest-path", "ffi/Cargo.toml"]);
    
    let status = Command::new("cargo")
        .args(&args)
        .status()
        .context("Failed to build Rust library")?;
    
    if !status.success() {
        anyhow::bail!("Rust build failed");
    }
    
    println!("  {} Generating Python bindings...", "".bright_blue());
    
    // Find the library file
    let lib_dir = format!("target/{}", profile);
    let lib_path = std::fs::read_dir(&lib_dir)
        .context("Failed to read target directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.starts_with("lib") && name_str.ends_with("ffi.so")
        })
        .map(|e| e.path())
        .context("Could not find FFI library")?;
    
    // Use cargo run with uniffi/cli feature from the ffi crate
    let status = Command::new("cargo")
        .args(&[
            "run",
            "--manifest-path",
            "ffi/Cargo.toml",
            "--features",
            "uniffi/cli",
            "--bin",
            "uniffi-bindgen",
            "--",
        ])
        .args(&[
            "generate",
            "--library",
            lib_path.to_str().unwrap(),
            "--language",
            "python",
            "--out-dir",
            "platforms/linux",
        ])
        .status()
        .context("Failed to generate Python bindings")?;
    
    if !status.success() {
        anyhow::bail!("Binding generation failed");
    }
    
    println!("{}", "  ✅ Linux build complete".green());
    Ok(())
}

fn build_web(release: bool) -> Result<()> {
    // Ensure wasm32-unknown-unknown target is installed
    ensure_wasm_target()?;
    
    // Ensure wasm-bindgen-cli is installed
    ensure_wasm_bindgen_cli()?;
    
    println!("  {} Building Rust library for Web (WASM)...", "".bright_blue());
    
    let profile = if release { "release" } else { "debug" };
    
    // Build the ffi-web crate for wasm32
    let mut args = vec!["build", "--target", "wasm32-unknown-unknown"];
    if release {
        args.push("--release");
    }
    args.extend(&["--manifest-path", "ffi-web/Cargo.toml"]);
    
    let status = Command::new("cargo")
        .args(&args)
        .status()
        .context("Failed to build Rust library for WASM")?;
    
    if !status.success() {
        anyhow::bail!("Rust WASM build failed");
    }
    
    println!("  {} Generating JavaScript bindings with wasm-bindgen...", "".bright_blue());
    
    // Find the .wasm file - need to get the actual project name
    let wasm_dir = format!("target/wasm32-unknown-unknown/{}", profile);
    let wasm_file = std::fs::read_dir(&wasm_dir)
        .context("Failed to read wasm target directory")?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name_str = name.to_string_lossy();
            name_str.ends_with("_ffi_web.wasm")
        })
        .map(|e| e.path())
        .context("Could not find WASM file")?;
    
    // Run wasm-bindgen to generate JS bindings
    let status = Command::new("wasm-bindgen")
        .arg(wasm_file.to_str().unwrap())
        .arg("--out-dir")
        .arg("platforms/web/pkg")
        .arg("--target")
        .arg("web")
        .arg("--out-name")
        .arg("wasm")
        .status()
        .context("Failed to run wasm-bindgen")?;
    
    if !status.success() {
        anyhow::bail!("wasm-bindgen failed");
    }
    
    println!("{}", "  ✅ Web build complete".green());
    Ok(())
}

fn ensure_wasm_target() -> Result<()> {
    println!("  {} Checking wasm32-unknown-unknown target...", "".bright_blue());
    
    let output = Command::new("rustup")
        .args(&["target", "list", "--installed"])
        .output()
        .context("Failed to check installed targets")?;
    
    let installed = String::from_utf8_lossy(&output.stdout);
    
    if !installed.contains("wasm32-unknown-unknown") {
        println!("    Installing wasm32-unknown-unknown...");
        let status = Command::new("rustup")
            .args(&["target", "add", "wasm32-unknown-unknown"])
            .status()
            .context("Failed to install wasm32-unknown-unknown target")?;
        
        if !status.success() {
            anyhow::bail!("Failed to install wasm32-unknown-unknown target");
        }
    }
    
    Ok(())
}

fn ensure_wasm_bindgen_cli() -> Result<()> {
    println!("  {} Checking wasm-bindgen-cli...", "".bright_blue());
    
    // Get the wasm-bindgen version from ffi-web/Cargo.toml
    let cargo_toml = std::fs::read_to_string("ffi-web/Cargo.toml")
        .context("Failed to read ffi-web/Cargo.toml")?;
    
    let required_version = cargo_toml
        .lines()
        .find(|line| line.contains("wasm-bindgen ="))
        .and_then(|line| {
            line.split('"').nth(1)
        })
        .unwrap_or("0.2");
    
    // Check installed version
    let check = Command::new("wasm-bindgen")
        .arg("--version")
        .output();
    
    let needs_install = if let Ok(output) = check {
        if output.status.success() {
            let installed_version = String::from_utf8_lossy(&output.stdout);
            let installed_version = installed_version.trim().split_whitespace().last().unwrap_or("");
            
            // Check if versions match
            !installed_version.starts_with(required_version)
        } else {
            true
        }
    } else {
        true
    };
    
    if needs_install {
        println!("    Installing wasm-bindgen-cli {} (this may take a few minutes)...", required_version);
        let status = Command::new("cargo")
            .args(&["install", "-f", "wasm-bindgen-cli", "--version", required_version])
            .status()
            .context("Failed to install wasm-bindgen-cli")?;
        
        if !status.success() {
            anyhow::bail!("Failed to install wasm-bindgen-cli");
        }
        println!("  {} wasm-bindgen-cli {} installed", "".green(), required_version);
    }
    
    Ok(())
}

fn ensure_android_targets(targets: &[&str]) -> Result<()> {
    println!("  {} Checking Android targets...", "".bright_blue());
    
    // Check which targets are installed
    let output = Command::new("rustup")
        .args(&["target", "list", "--installed"])
        .output()
        .context("Failed to check installed targets")?;
    
    let installed = String::from_utf8_lossy(&output.stdout);
    
    for target in targets {
        if !installed.contains(target) {
            println!("    Installing {}...", target.bright_yellow());
            let status = Command::new("rustup")
                .args(&["target", "add", target])
                .status()
                .context(format!("Failed to install target {}", target))?;
            
            if !status.success() {
                anyhow::bail!("Failed to install Android target: {}", target);
            }
        }
    }
    
    println!("  {} Android targets ready", "".green());
    Ok(())
}

fn ensure_cargo_ndk() -> Result<()> {
    println!("  {} Checking cargo-ndk...", "".bright_blue());
    
    // Check if cargo-ndk is installed
    let check = Command::new("cargo")
        .args(&["ndk", "--version"])
        .output();
    
    if check.is_err() || !check.unwrap().status.success() {
        println!("    Installing cargo-ndk...");
        let status = Command::new("cargo")
            .args(&["install", "cargo-ndk"])
            .status()
            .context("Failed to install cargo-ndk")?;
        
        if !status.success() {
            anyhow::bail!("Failed to install cargo-ndk");
        }
        println!("  {} cargo-ndk installed", "".green());
    }
    
    Ok(())
}