|
| 1 | +use std::{env, process}; |
| 2 | + |
| 3 | +use camino::Utf8PathBuf; |
| 4 | + |
| 5 | +fn main() { |
| 6 | + let args: Vec<String> = env::args().collect(); |
| 7 | + |
| 8 | + // Show help if requested |
| 9 | + if args.len() > 1 && (args[1] == "--help" || args[1] == "-h") { |
| 10 | + eprintln!("UniFFI React Native Binding Generator"); |
| 11 | + eprintln!(); |
| 12 | + eprintln!("Usage: {} [library_path] [output_dir] [OPTIONS]", args[0]); |
| 13 | + eprintln!(); |
| 14 | + eprintln!("Arguments:"); |
| 15 | + eprintln!( |
| 16 | + " library_path Path to the compiled library (default: \ |
| 17 | + target/release/libdojo_uniffi.dylib)" |
| 18 | + ); |
| 19 | + eprintln!( |
| 20 | + " output_dir Output directory for bindings (default: bindings/react-native)" |
| 21 | + ); |
| 22 | + eprintln!(); |
| 23 | + eprintln!("Requirements:"); |
| 24 | + eprintln!(" This tool requires uniffi-bindgen-react-native to be installed:"); |
| 25 | + eprintln!(" cargo install --git https://github.com/jhugman/uniffi-bindgen-react-native --branch update-uniffi-0.30 uniffi-bindgen-react-native"); |
| 26 | + eprintln!(); |
| 27 | + eprintln!("Examples:"); |
| 28 | + eprintln!(" {} # Use defaults", args[0]); |
| 29 | + eprintln!(" {} target/release/libdojo_uniffi.dylib bindings/react-native", args[0]); |
| 30 | + eprintln!(); |
| 31 | + process::exit(0); |
| 32 | + } |
| 33 | + |
| 34 | + // Check if uniffi-bindgen-react-native is installed |
| 35 | + if which::which("uniffi-bindgen-react-native").is_err() { |
| 36 | + eprintln!("Error: uniffi-bindgen-react-native is not installed or not in PATH"); |
| 37 | + eprintln!(); |
| 38 | + eprintln!("Please install it with:"); |
| 39 | + eprintln!(" cargo install --git https://github.com/jhugman/uniffi-bindgen-react-native --branch update-uniffi-0.30 uniffi-bindgen-react-native"); |
| 40 | + eprintln!(); |
| 41 | + eprintln!("Or add it to your PATH if already installed"); |
| 42 | + process::exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + // Determine library extension based on platform |
| 46 | + let lib_ext = if cfg!(target_os = "macos") { |
| 47 | + "dylib" |
| 48 | + } else if cfg!(target_os = "windows") { |
| 49 | + "dll" |
| 50 | + } else { |
| 51 | + "so" |
| 52 | + }; |
| 53 | + |
| 54 | + // Default paths |
| 55 | + let default_lib = format!("target/release/libdojo_uniffi.{}", lib_ext); |
| 56 | + let default_out = "bindings/react-native"; |
| 57 | + |
| 58 | + // Parse arguments |
| 59 | + let positional_args: Vec<&String> = |
| 60 | + args.iter().skip(1).filter(|arg| !arg.starts_with("--")).collect(); |
| 61 | + |
| 62 | + let library_path = |
| 63 | + Utf8PathBuf::from(positional_args.first().map(|s| s.as_str()).unwrap_or(&default_lib)); |
| 64 | + let out_dir = |
| 65 | + Utf8PathBuf::from(positional_args.get(1).map(|s| s.as_str()).unwrap_or(default_out)); |
| 66 | + |
| 67 | + if !library_path.exists() { |
| 68 | + eprintln!("Error: Library file not found: {}", library_path); |
| 69 | + eprintln!("Build the library first with: cargo build --release -p dojo-uniffi"); |
| 70 | + eprintln!(); |
| 71 | + eprintln!("Hint: Run with --help to see usage information"); |
| 72 | + process::exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + // Create output directory if it doesn't exist |
| 76 | + if let Err(e) = std::fs::create_dir_all(&out_dir) { |
| 77 | + eprintln!("Error: Failed to create output directory {}: {}", out_dir, e); |
| 78 | + process::exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + println!("Generating React Native bindings..."); |
| 82 | + println!("Library: {}", library_path); |
| 83 | + println!("Output: {}", out_dir); |
| 84 | + |
| 85 | + // Build command for uniffi-bindgen-react-native |
| 86 | + let mut cmd = process::Command::new("uniffi-bindgen-react-native"); |
| 87 | + cmd.arg("--library").arg(&library_path); |
| 88 | + cmd.arg("--out-dir").arg(&out_dir); |
| 89 | + |
| 90 | + // Execute the command |
| 91 | + match cmd.output() { |
| 92 | + Ok(output) => { |
| 93 | + if output.status.success() { |
| 94 | + println!("✓ React Native bindings generated successfully!"); |
| 95 | + |
| 96 | + if !output.stdout.is_empty() { |
| 97 | + println!("{}", String::from_utf8_lossy(&output.stdout)); |
| 98 | + } |
| 99 | + } else { |
| 100 | + eprintln!("Error generating bindings:"); |
| 101 | + if !output.stderr.is_empty() { |
| 102 | + eprintln!("{}", String::from_utf8_lossy(&output.stderr)); |
| 103 | + } |
| 104 | + if !output.stdout.is_empty() { |
| 105 | + eprintln!("{}", String::from_utf8_lossy(&output.stdout)); |
| 106 | + } |
| 107 | + process::exit(1); |
| 108 | + } |
| 109 | + } |
| 110 | + Err(e) => { |
| 111 | + eprintln!("Error executing uniffi-bindgen-react-native: {}", e); |
| 112 | + process::exit(1); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments