use super::*;
#[must_use]
pub fn collect_full_doctor_report() -> FullDoctorReport {
let probe: HostCapabilityProbe = HostCapabilityProbe::detect();
collect_full_doctor_report_from_probe(&probe)
}
#[must_use]
pub fn collect_full_doctor_report_from_probe(probe: &HostCapabilityProbe) -> FullDoctorReport {
let tool_specs: [(&str, &str, bool, &str, &str); 25] = [
(
BIN_RUSTC,
TOOL_CAT_RUST,
false,
REQ_ALL_TARGETS,
HINT_RUSTUP_INSTALL,
),
(
BIN_CARGO,
TOOL_CAT_RUST,
false,
REQ_ALL_TARGETS,
HINT_RUSTUP_INSTALL,
),
(
BIN_RUSTUP,
TOOL_CAT_RUST,
false,
REQ_ALL_TARGETS,
HINT_RUSTUP_INSTALL,
),
(
BIN_WASM_BINDGEN,
TOOL_CAT_WEB,
false,
REQ_WEB_MINIAPP,
HINT_INSTALL_WASM_BINDGEN,
),
(
BIN_WASM_OPT,
TOOL_CAT_WEB,
false,
REQ_WEB_SIZE_OPT,
HINT_WASM_OPT,
),
(
BIN_WASM_PACK,
TOOL_CAT_WEB,
false,
REQ_WEB_LEGACY,
HINT_WASM_PACK,
),
(
BIN_CARGO_NDK,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_CARGO_NDK,
),
(
ENV_ANDROID_HOME,
TOOL_CAT_ANDROID,
true,
REQ_ANDROID_WEAROS,
HINT_ANDROID_HOME,
),
(
ENV_ANDROID_NDK,
TOOL_CAT_ANDROID,
true,
REQ_ANDROID_WEAROS,
HINT_INSTALL_ANDROID_NDK,
),
(
BIN_ADB,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_INSTALL,
HINT_ADB,
),
(
BIN_JAVA,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_JDK,
),
(
BIN_JAVAC,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_JDK,
),
(
BIN_AAPT2,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_INSTALL_ANDROID_BUILD_TOOLS,
),
(
BIN_D8,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_INSTALL_ANDROID_BUILD_TOOLS,
),
(
BIN_ZIPALIGN,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_INSTALL_ANDROID_BUILD_TOOLS,
),
(
BIN_APKSIGNER,
TOOL_CAT_ANDROID,
false,
REQ_ANDROID_WEAROS,
HINT_INSTALL_ANDROID_BUILD_TOOLS,
),
(
BIN_XCRUN,
TOOL_CAT_APPLE,
false,
REQ_APPLE_TARGETS,
HINT_INSTALL_XCODE,
),
(
BIN_XCODEBUILD,
TOOL_CAT_APPLE,
false,
REQ_APPLE_TARGETS,
HINT_XCODE_FULL,
),
(
TOOL_KEY_SIMCTL,
TOOL_CAT_APPLE,
false,
REQ_APPLE_SIM,
HINT_XCODE_FULL,
),
(
BIN_CODESIGN,
TOOL_CAT_APPLE,
false,
REQ_APPLE_TARGETS,
HINT_INSTALL_XCODE,
),
(
ENV_OHOS_SDK,
TOOL_CAT_HARMONY,
true,
REQ_HARMONY_TARGETS,
HINT_INSTALL_OHOS_SDK,
),
(
BIN_HVIGORW,
TOOL_CAT_HARMONY,
false,
REQ_HARMONY_TARGETS,
HINT_HARMONY_TOOLS,
),
(
BIN_HDC,
TOOL_CAT_HARMONY,
false,
REQ_HARMONY_TARGETS,
HINT_HARMONY_TOOLS,
),
(
BIN_CRATE_CLI,
TOOL_CAT_DX,
false,
REQ_FMT_STANDARDS,
HINT_CRATE_CLI,
),
(BIN_NODE, TOOL_CAT_DX, false, REQ_ECOSYSTEM_TOOLS, HINT_NODE),
];
let mut tools: Vec<ToolProbeEntry> = Vec::with_capacity(tool_specs.len());
for (name, category, is_sdk, required_for, install_hint) in tool_specs.iter() {
let found: Option<&String> = if *is_sdk {
probe.get_env_sdks().get(*name)
} else {
probe.get_available_tools().get(*name)
};
let (available, detail): (bool, String) = match found {
Some(val) => (true, val.clone()),
None => (false, STATUS_MISSING.to_string()),
};
tools.push(ToolProbeEntry::new(
(*name).to_string(),
(*category).to_string(),
available,
detail,
(*required_for).to_string(),
(*install_hint).to_string(),
));
}
let targets: Vec<TargetCapability> = probe.probe_all();
let ready_target_count: usize = targets
.iter()
.filter(|cap: &&TargetCapability| cap.get_ready())
.count();
let total_target_count: usize = targets.len();
FullDoctorReport::new(
probe.get_host_os().clone(),
probe.get_host_arch().clone(),
tools,
targets,
ready_target_count,
total_target_count,
)
}
pub fn run_doctor_fix(probe: &HostCapabilityProbe) -> Vec<DoctorFixResult> {
let missing_triples: Vec<String> = probe.missing_installable_triples();
let mut results: Vec<DoctorFixResult> = Vec::with_capacity(missing_triples.len());
for triple in missing_triples.into_iter() {
let cmd_args: Vec<String> = vec![
ARG_TARGET_SUB.to_string(),
ARG_ADD.to_string(),
triple.clone(),
];
let cmd_str: String = format!("{BIN_RUSTUP} {} {} {triple}", ARG_TARGET_SUB, ARG_ADD);
match Command::new(BIN_RUSTUP).args(&cmd_args).output() {
Ok(out) if out.status.success() => {
results.push(DoctorFixResult::new(
triple,
cmd_str,
true,
STATUS_OK.to_string(),
));
}
Ok(out) => {
let stderr_msg: String = String::from_utf8_lossy(&out.stderr).trim().to_string();
results.push(DoctorFixResult::new(triple, cmd_str, false, stderr_msg));
}
Err(err) => {
results.push(DoctorFixResult::new(
triple,
cmd_str,
false,
err.to_string(),
));
}
}
}
results
}
#[must_use]
pub fn format_full_doctor_report(report: &FullDoctorReport, targets_only: bool) -> String {
let mut out: String = format!(
"euv doctor — host: {}-{} ({}/{} targets ready)\n\n",
report.get_host_os(),
report.get_host_arch(),
report.get_ready_target_count(),
report.get_total_target_count()
);
if !targets_only {
let mut tool_table: TerminalTable = TerminalTable::with_headers(vec![
HEADER_STATUS.to_string(),
DOCTOR_HDR_TOOL.to_string(),
DOCTOR_HDR_CATEGORY.to_string(),
DOCTOR_HDR_DETAIL.to_string(),
]);
for entry in report.get_tools().iter() {
let badge: &str = if entry.get_available() {
StatusBadge::Pass.as_str()
} else {
StatusBadge::Skip.as_str()
};
let detail: String = if entry.get_available() {
entry.get_detail().clone()
} else {
format!("{} ({})", STATUS_MISSING, entry.get_install_hint())
};
tool_table.push_row(vec![
badge.to_string(),
entry.get_name().clone(),
entry.get_category().clone(),
detail,
]);
}
out.push_str(&tool_table.render());
out.push('\n');
}
out.push_str(&format_targets_table(report.get_targets(), false));
if !report.get_fix_actions().is_empty() {
out.push_str(DOCTOR_FIX_SECTION_HDR);
for action in report.get_fix_actions().iter() {
let badge: &str = if action.get_succeeded() {
StatusBadge::Pass.as_str()
} else {
StatusBadge::Fail.as_str()
};
out.push_str(&format!(
" [{}] {} — {}\n",
badge,
action.get_command(),
action.get_message()
));
}
}
out
}
#[must_use]
pub fn format_targets_table(capabilities: &[TargetCapability], ready_only: bool) -> String {
let mut table: TerminalTable = TerminalTable::with_headers(vec![
HEADER_STATUS.to_string(),
DOCTOR_HDR_TARGET.to_string(),
DOCTOR_HDR_CATEGORY.to_string(),
DOCTOR_HDR_TRIPLE.to_string(),
DOCTOR_HDR_MISSING.to_string(),
]);
let mut ready_count: usize = 0;
let mut shown_count: usize = 0;
for cap in capabilities.iter() {
if cap.get_ready() {
ready_count += 1;
}
if ready_only && !cap.get_ready() {
continue;
}
shown_count += 1;
let status_label: &str = if cap.get_ready() {
StatusBadge::Pass.as_str()
} else if !cap.get_host_supported() {
DOCTOR_STATUS_UNSUPPORTED
} else {
StatusBadge::Skip.as_str()
};
let note: String = if cap.get_ready() {
DOCTOR_STATUS_READY.to_string()
} else if let Some(first_hint) = cap.get_install_hints().first() {
format!("{} | {first_hint}", cap.get_missing_tools().join(", "))
} else {
cap.get_missing_tools().join(", ")
};
table.push_row(vec![
status_label.to_string(),
cap.get_target().clone(),
cap.get_category().clone(),
cap.get_triple().clone(),
note,
]);
}
let mut out: String = table.render();
out.push_str(&format!(
"Targets: {ready_count}/{} ready (showing {shown_count})\n",
capabilities.len()
));
out
}