|
| 1 | +use template::Template; |
| 2 | + |
| 3 | +use crate::colors::*; |
| 4 | +use crate::internal::template; |
| 5 | +use crate::package_manager::PackageManager; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +fn is_rustc_installed() -> bool { |
| 9 | + Command::new("rustc").arg("-V").output().is_ok() |
| 10 | +} |
| 11 | +fn is_cargo_installed() -> bool { |
| 12 | + Command::new("cargo").arg("-V").output().is_ok() |
| 13 | +} |
| 14 | +fn is_node_installed() -> bool { |
| 15 | + Command::new("node").arg("-v").output().is_ok() |
| 16 | +} |
| 17 | + |
| 18 | +fn is_trunk_installed() -> bool { |
| 19 | + Command::new("trunk").arg("-V").output().is_ok() |
| 20 | +} |
| 21 | +fn is_tauri_cli_installed() -> bool { |
| 22 | + Command::new("cargo") |
| 23 | + .arg("tauri") |
| 24 | + .arg("-V") |
| 25 | + .output() |
| 26 | + .map(|o| { |
| 27 | + let s = String::from_utf8_lossy(&o.stderr); |
| 28 | + !s.starts_with("error:") |
| 29 | + }) |
| 30 | + .unwrap_or(false) |
| 31 | +} |
| 32 | +fn is_wasm32_installed() -> bool { |
| 33 | + Command::new("rustup") |
| 34 | + .args(["target", "list", "--installed"]) |
| 35 | + .output() |
| 36 | + .map(|o| { |
| 37 | + let s = String::from_utf8_lossy(&o.stdout); |
| 38 | + s.contains("wasm32-unknown-unknown") |
| 39 | + }) |
| 40 | + .unwrap_or(false) |
| 41 | +} |
| 42 | + |
| 43 | +pub fn print_missing_deps(pkg_manager: PackageManager, template: Template, alpha: bool) { |
| 44 | + let rustc_installed = is_rustc_installed(); |
| 45 | + let cargo_installed = is_cargo_installed(); |
| 46 | + let deps: &[(&str, String, &dyn Fn() -> bool, bool)] = &[ |
| 47 | + ( |
| 48 | + "Rust", |
| 49 | + format!("Visit {BLUE}{BOLD}https://www.rust-lang.org/learn/get-started#installing-rust{RESET}"), |
| 50 | + &|| rustc_installed && cargo_installed, |
| 51 | + rustc_installed || cargo_installed, |
| 52 | + ), |
| 53 | + ( |
| 54 | + "rustc", |
| 55 | + format!("Visit {BLUE}{BOLD}https://www.rust-lang.org/learn/get-started#installing-rust{RESET} to install Rust"), |
| 56 | + &|| rustc_installed, |
| 57 | + !rustc_installed && !cargo_installed, |
| 58 | + ), |
| 59 | + ( |
| 60 | + "Cargo", |
| 61 | + format!("Visit {BLUE}{BOLD}https://www.rust-lang.org/learn/get-started#installing-rust{RESET} to install Rust"), |
| 62 | + &|| cargo_installed, |
| 63 | + !rustc_installed && !cargo_installed, |
| 64 | + ), |
| 65 | + ( |
| 66 | + "Tauri CLI", |
| 67 | + if alpha { |
| 68 | + format!("Run `{BLUE}{BOLD}cargo install tauri-cli --version 2.0.0-alpha.2{RESET}`") |
| 69 | + } else { |
| 70 | + format!("Run `{BLUE}{BOLD}cargo install tauri-cli{RESET}`") |
| 71 | + }, |
| 72 | + &is_tauri_cli_installed, |
| 73 | + pkg_manager.is_node() || !template.needs_tauri_cli(), |
| 74 | + ), |
| 75 | + ( |
| 76 | + "Trunk", |
| 77 | + if alpha { |
| 78 | + format!("Run `{BLUE}{BOLD}cargo install trunk --git https://github.com/amrbashir/trunk{RESET}`") |
| 79 | + } else { |
| 80 | + format!("Visit {BLUE}{BOLD}https://trunkrs.dev/#install{RESET}") |
| 81 | + }, |
| 82 | + &is_trunk_installed, |
| 83 | + pkg_manager.is_node() || !template.needs_trunk(), |
| 84 | + ), |
| 85 | + ( |
| 86 | + "wasm32 target", |
| 87 | + format!("Run `{BLUE}{BOLD}rustup target add wasm32-unknown-unknown{RESET}`"), |
| 88 | + &is_wasm32_installed, |
| 89 | + pkg_manager.is_node() || !template.needs_wasm32_target(), |
| 90 | + ), |
| 91 | + ( |
| 92 | + "Node.js", |
| 93 | + format!("Visit {BLUE}{BOLD}https://nodejs.org/en/{RESET}"), |
| 94 | + &is_node_installed, |
| 95 | + !pkg_manager.is_node(), |
| 96 | + ), |
| 97 | + ]; |
| 98 | + |
| 99 | + let missing_deps: Vec<(String, String)> = deps |
| 100 | + .iter() |
| 101 | + .filter(|(_, _, exists, skip)| !skip && !exists()) |
| 102 | + .map(|(s, d, _, _)| (s.to_string(), d.clone())) |
| 103 | + .collect(); |
| 104 | + |
| 105 | + let (largest_first_cell, largest_second_cell) = |
| 106 | + missing_deps |
| 107 | + .iter() |
| 108 | + .fold((0, 0), |(mut prev_f, mut prev_s), (f, s)| { |
| 109 | + let f_len = f.len(); |
| 110 | + if f_len > prev_f { |
| 111 | + prev_f = f_len; |
| 112 | + } |
| 113 | + |
| 114 | + let s_len = remove_colors(s).len(); |
| 115 | + if s_len > prev_s { |
| 116 | + prev_s = s_len; |
| 117 | + } |
| 118 | + |
| 119 | + (prev_f, prev_s) |
| 120 | + }); |
| 121 | + |
| 122 | + if !missing_deps.is_empty() { |
| 123 | + println!("\n\nYour system is {YELLOW}missing dependencies{RESET} (or they do not exist in {YELLOW}$PATH{RESET}):"); |
| 124 | + for (index, (name, instruction)) in missing_deps.iter().enumerate() { |
| 125 | + if index == 0 { |
| 126 | + println!( |
| 127 | + "╭{}┬{}╮", |
| 128 | + "─".repeat(largest_first_cell + 2), |
| 129 | + "─".repeat(largest_second_cell + 2) |
| 130 | + ); |
| 131 | + } else { |
| 132 | + println!( |
| 133 | + "├{}┼{}┤", |
| 134 | + "─".repeat(largest_first_cell + 2), |
| 135 | + "─".repeat(largest_second_cell + 2) |
| 136 | + ); |
| 137 | + } |
| 138 | + println!( |
| 139 | + "│ {YELLOW}{name}{RESET}{} │ {instruction}{} │", |
| 140 | + " ".repeat(largest_first_cell - name.len()), |
| 141 | + " ".repeat(largest_second_cell - remove_colors(instruction).len()), |
| 142 | + ); |
| 143 | + } |
| 144 | + println!( |
| 145 | + "╰{}┴{}╯", |
| 146 | + "─".repeat(largest_first_cell + 2), |
| 147 | + "─".repeat(largest_second_cell + 2), |
| 148 | + ); |
| 149 | + println!(); |
| 150 | + println!("Make sure you have installed the prerequisites for your OS: {BLUE}{BOLD}https://tauri.app/v1/guides/getting-started/prerequisites{RESET}, then run:"); |
| 151 | + } else { |
| 152 | + println!(" To get started run:") |
| 153 | + } |
| 154 | +} |
0 commit comments