Skip to content

Commit 18584ee

Browse files
committed
feat: 添加菜单配置处理逻辑,支持 QEMU 和 U-Boot 模式
1 parent d04bf9b commit 18584ee

File tree

5 files changed

+122
-47
lines changed

5 files changed

+122
-47
lines changed

ostool/src/ctx.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
use std::{
2-
collections::HashMap,
3-
path::{Path, PathBuf},
4-
sync::Arc,
5-
};
1+
use std::{path::PathBuf, sync::Arc};
62

73
use anyhow::anyhow;
84
use cargo_metadata::Metadata;
95
use colored::Colorize;
106
use cursive::Cursive;
117
use jkconfig::{
128
ElemHock,
13-
data::{
14-
app_data::{AppData, default_schema_by_init},
15-
item::ItemType,
16-
types::ElementType,
17-
},
9+
data::{app_data::AppData, item::ItemType, types::ElementType},
1810
ui::components::editors::{show_feature_select, show_list_select},
1911
};
2012

@@ -50,39 +42,6 @@ impl AppContext {
5042
Ok(())
5143
}
5244

53-
// axvisor
54-
pub async fn launch_menuconfig_ui(&mut self) -> anyhow::Result<bool> {
55-
let config_path = match &self.build_config_path {
56-
Some(path) => path.clone(),
57-
None => self.workspace_folder.join(".build.toml"),
58-
};
59-
60-
// Ensure the configuration file exists
61-
if !config_path.exists() {
62-
println!(
63-
"Configuration file does not exist, creating new file: {}",
64-
config_path.display()
65-
);
66-
tokio::fs::write(&config_path, "").await?;
67-
}
68-
69-
// Generate schema path
70-
let schema_path = default_schema_by_init(&config_path);
71-
72-
// Create empty config file if it doesn't exist
73-
if !config_path.exists() {
74-
println!(
75-
"Creating empty configuration file: {}",
76-
config_path.display()
77-
);
78-
std::fs::write(&config_path, "")?;
79-
}
80-
81-
let mut app_data = AppData::new(Some(config_path), Some(schema_path))?;
82-
83-
Ok(true)
84-
}
85-
8645
pub fn command(&self, program: &str) -> crate::utils::Command {
8746
let this = self.clone();
8847
crate::utils::Command::new(program, &self.manifest_dir, move |s| {
@@ -275,6 +234,11 @@ impl AppContext {
275234
format!("{}", self.workspace_folder.display()).as_ref(),
276235
)
277236
}
237+
238+
pub fn ui_hocks(&self) -> Vec<ElemHock> {
239+
vec![self.ui_hock_feature_select(), self.ui_hock_pacage_select()]
240+
}
241+
278242
fn ui_hock_feature_select(&self) -> ElemHock {
279243
let path = "system.features";
280244
let cargo_toml = self.workspace_folder.join("Cargo.toml");

ostool/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
pub mod build;
44
pub mod ctx;
5+
pub mod menuconfig;
56
pub mod run;
67
pub mod sterm;
78
pub mod utils;

ostool/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use log::info;
77
use ostool::{
88
build,
99
ctx::AppContext,
10+
menuconfig::{MenuConfigHandler, MenuConfigMode},
1011
run::{cargo::CargoRunnerKind, qemu::RunQemuArgs, uboot::RunUbootArgs},
1112
};
1213

@@ -27,7 +28,11 @@ enum SubCommands {
2728
config: Option<PathBuf>,
2829
},
2930
Run(RunArgs),
30-
Menuconfig,
31+
Menuconfig {
32+
/// Menu configuration mode (qemu or uboot)
33+
#[arg(value_enum)]
34+
mode: Option<MenuConfigMode>,
35+
},
3136
}
3237

3338
#[derive(Args, Debug)]
@@ -148,9 +153,8 @@ async fn main() -> Result<()> {
148153
}
149154
}
150155
}
151-
SubCommands::Menuconfig => {
152-
let build_config = ctx.perpare_build_config(None, true).await?;
153-
println!("Build configuration: {:?}", build_config);
156+
SubCommands::Menuconfig { mode } => {
157+
MenuConfigHandler::handle_menuconfig(&mut ctx, mode).await?;
154158
}
155159
}
156160

ostool/src/menuconfig.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

test-build.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[system]
2+
# 使用 Cargo 系统
3+
4+
[system.cargo]
5+
target = "x86_64-unknown-none"
6+
package = "test-kernel"
7+
features = []
8+
log = "Info"
9+
to_bin = true

0 commit comments

Comments
 (0)