|
| 1 | +use anyhow::Result; |
| 2 | +use clap::ValueEnum; |
| 3 | +use log::info; |
| 4 | +use tokio::fs; |
| 5 | + |
| 6 | +use crate::ctx::AppContext; |
| 7 | +use crate::run::qemu::QemuConfig; |
| 8 | +use crate::run::uboot::UbootConfig; |
| 9 | + |
| 10 | +#[derive(ValueEnum, Clone, Debug)] |
| 11 | +pub enum MenuConfigMode { |
| 12 | + Qemu, |
| 13 | + Uboot, |
| 14 | +} |
| 15 | + |
| 16 | +pub struct MenuConfigHandler; |
| 17 | + |
| 18 | +impl MenuConfigHandler { |
| 19 | + pub async fn handle_menuconfig( |
| 20 | + ctx: &mut AppContext, |
| 21 | + mode: Option<MenuConfigMode>, |
| 22 | + ) -> Result<()> { |
| 23 | + match mode { |
| 24 | + Some(MenuConfigMode::Qemu) => { |
| 25 | + Self::handle_qemu_config(ctx).await?; |
| 26 | + } |
| 27 | + Some(MenuConfigMode::Uboot) => { |
| 28 | + Self::handle_uboot_config(ctx).await?; |
| 29 | + } |
| 30 | + None => { |
| 31 | + // 默认模式:显示当前构建配置 |
| 32 | + Self::handle_default_config(ctx).await?; |
| 33 | + } |
| 34 | + } |
| 35 | + Ok(()) |
| 36 | + } |
| 37 | + |
| 38 | + async fn handle_default_config(ctx: &mut AppContext) -> Result<()> { |
| 39 | + ctx.perpare_build_config(None, true).await?; |
| 40 | + |
| 41 | + Ok(()) |
| 42 | + } |
| 43 | + |
| 44 | + async fn handle_qemu_config(ctx: &mut AppContext) -> Result<()> { |
| 45 | + info!("配置 QEMU 运行参数"); |
| 46 | + let config_path = ctx.workspace_folder.join(".qemu.toml"); |
| 47 | + if config_path.exists() { |
| 48 | + println!("\n当前 U-Boot 配置文件: {}", config_path.display()); |
| 49 | + // 这里可以读取并显示当前的 U-Boot 配置 |
| 50 | + } else { |
| 51 | + println!("\n未找到 U-Boot 配置文件,将使用默认配置"); |
| 52 | + } |
| 53 | + |
| 54 | + let config = jkconfig::run::<QemuConfig>(config_path, true, &[]).await?; |
| 55 | + |
| 56 | + if let Some(c) = config { |
| 57 | + fs::write( |
| 58 | + ctx.value_replace_with_var(ctx.workspace_folder.join(".qemu.toml")), |
| 59 | + toml::to_string_pretty(&c)?, |
| 60 | + ) |
| 61 | + .await?; |
| 62 | + println!("\nQEMU 配置已保存到 .qemu.toml"); |
| 63 | + } else { |
| 64 | + println!("\n未更改 QEMU 配置"); |
| 65 | + } |
| 66 | + |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | + |
| 70 | + async fn handle_uboot_config(ctx: &mut AppContext) -> Result<()> { |
| 71 | + info!("配置 U-Boot 运行参数"); |
| 72 | + |
| 73 | + println!("=== U-Boot 配置模式 ==="); |
| 74 | + |
| 75 | + // 检查是否存在 U-Boot 配置文件 |
| 76 | + let uboot_config_path = ctx.workspace_folder.join(".uboot.toml"); |
| 77 | + if uboot_config_path.exists() { |
| 78 | + println!("\n当前 U-Boot 配置文件: {}", uboot_config_path.display()); |
| 79 | + // 这里可以读取并显示当前的 U-Boot 配置 |
| 80 | + } else { |
| 81 | + println!("\n未找到 U-Boot 配置文件,将使用默认配置"); |
| 82 | + } |
| 83 | + let config = jkconfig::run::<UbootConfig>(uboot_config_path, true, &[]).await?; |
| 84 | + if let Some(c) = config { |
| 85 | + fs::write( |
| 86 | + ctx.value_replace_with_var(ctx.workspace_folder.join(".uboot.toml")), |
| 87 | + toml::to_string_pretty(&c)?, |
| 88 | + ) |
| 89 | + .await?; |
| 90 | + println!("\nU-Boot 配置已保存到 .uboot.toml"); |
| 91 | + } else { |
| 92 | + println!("\n未更改 U-Boot 配置"); |
| 93 | + } |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
0 commit comments