Skip to content

Commit 2358e62

Browse files
committed
feat: 添加日志功能支持,更新相关结构体和回调逻辑
1 parent fc95ca7 commit 2358e62

File tree

10 files changed

+163
-92
lines changed

10 files changed

+163
-92
lines changed

jkconfig/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ tower-http = {version = "0.6", features = ["fs", "cors"], optional = true}
4242
[features]
4343
default = ["web"]
4444
web = ["axum", "tokio", "tower", "tower-http", "chrono"]
45+
logging = []
4546

4647
[dev-dependencies]
4748
env_logger = "0.11"

jkconfig/src/data/app_data.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ use std::{
77
};
88

99
use anyhow::bail;
10+
use cursive::Cursive;
1011

1112
use crate::data::{menu::MenuRoot, types::ElementType};
1213

14+
pub type FeaturesCallback = Arc<dyn Fn() -> Vec<String> + Send + Sync>;
15+
16+
#[derive(Clone)]
17+
pub struct ElemHock {
18+
pub path: String,
19+
pub callback: Arc<dyn Fn(&mut Cursive, &str) + Send + Sync>,
20+
}
21+
1322
#[derive(Clone)]
1423
pub struct AppData {
1524
pub root: MenuRoot,
1625
pub current_key: Vec<String>,
1726
pub needs_save: bool,
1827
pub config: PathBuf,
1928
pub temp_data: Option<(String, serde_json::Value)>,
29+
pub elem_hocks: Vec<ElemHock>,
2030
pub features_callback: Option<Arc<dyn Fn() -> Vec<String> + Send + Sync>>,
2131
pub depend_features_callback:
2232
Option<Arc<dyn Fn() -> HashMap<String, Vec<String>> + Send + Sync>>,
@@ -45,10 +55,7 @@ impl AppData {
4555
config: Option<impl AsRef<Path>>,
4656
schema: Option<impl AsRef<Path>>,
4757
) -> anyhow::Result<Self> {
48-
let mut init_value_path = PathBuf::from(DEFAULT_CONFIG_PATH);
49-
if let Some(cfg) = config {
50-
init_value_path = cfg.as_ref().to_path_buf();
51-
}
58+
let init_value_path = Self::init_value_path(config);
5259

5360
let schema_path = if let Some(sch) = schema {
5461
sch.as_ref().to_path_buf()
@@ -62,8 +69,24 @@ impl AppData {
6269

6370
let schema_content = fs::read_to_string(&schema_path)?;
6471
let schema_json: serde_json::Value = serde_json::from_str(&schema_content)?;
72+
Self::new_with_schema(Some(init_value_path), &schema_json)
73+
}
74+
75+
fn init_value_path(config: Option<impl AsRef<Path>>) -> PathBuf {
76+
let mut init_value_path = PathBuf::from(DEFAULT_CONFIG_PATH);
77+
if let Some(cfg) = config {
78+
init_value_path = cfg.as_ref().to_path_buf();
79+
}
80+
init_value_path
81+
}
82+
83+
pub fn new_with_schema(
84+
config: Option<impl AsRef<Path>>,
85+
schema: &serde_json::Value,
86+
) -> anyhow::Result<Self> {
87+
let init_value_path = Self::init_value_path(config);
6588

66-
let mut root = MenuRoot::try_from(&schema_json)?;
89+
let mut root = MenuRoot::try_from(schema)?;
6790

6891
if init_value_path.exists() {
6992
let init_content = fs::read_to_string(&init_value_path)?;
@@ -94,6 +117,7 @@ impl AppData {
94117
temp_data: None,
95118
features_callback: None,
96119
depend_features_callback: None,
120+
elem_hocks: Vec::new(),
97121
})
98122
}
99123

jkconfig/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// #[macro_use]
2+
// extern crate log;
3+
14
#[macro_use]
2-
extern crate log;
5+
mod log;
36

47
pub mod data;
58
// UI模块暂时注释掉,使用主程序中的 MenuView

jkconfig/src/log.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// #[cfg(feature = "logging")]
2+
// macro_rules! debug {
3+
// ($($arg:tt)*) => {
4+
// log::debug!($($arg)*);
5+
// };
6+
// }
7+
8+
// #[cfg(not(feature = "logging"))]
9+
// macro_rules! debug {
10+
// ($($arg:tt)*) => {};
11+
// }
12+
13+
#[cfg(feature = "logging")]
14+
macro_rules! info {
15+
($($arg:tt)*) => {
16+
log::info!($($arg)*);
17+
};
18+
}
19+
20+
#[cfg(not(feature = "logging"))]
21+
macro_rules! info {
22+
($($arg:tt)*) => {};
23+
}
24+
25+
#[cfg(feature = "logging")]
26+
macro_rules! warn {
27+
($($arg:tt)*) => {
28+
log::warn!($($arg)*);
29+
};
30+
}
31+
32+
#[cfg(not(feature = "logging"))]
33+
macro_rules! warn {
34+
($($arg:tt)*) => {};
35+
}

jkconfig/src/ui/components/editors/multi_select_editor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use cursive::{
44
view::{Nameable, Resizable},
55
views::{Dialog, DummyView, LinearLayout, OnEventView, ScrollView, SelectView, TextView},
66
};
7-
use log::info;
87

98
use crate::{
109
data::{app_data::AppData, item::ItemType, types::ElementType},

jkconfig/src/ui/components/menu.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use cursive::{
1616
view::{IntoBoxedView, Nameable, Resizable, Scrollable},
1717
views::{Dialog, DummyView, LinearLayout, OnEventView, Panel, SelectView, TextView},
1818
};
19-
use log::info;
20-
// 移除对ostool的依赖导入
2119

2220
use super::editors::*;
2321
use crate::ui::components::editors::depend_features_editor::show_depend_features_editor;
@@ -632,6 +630,21 @@ fn enter_elem(s: &mut Cursive, elem: &ElementType) {
632630
path = app.key_string();
633631
}
634632

633+
let mut hocked = false;
634+
if let Some(app_data) = s.user_data::<AppData>() {
635+
for hook in app_data.elem_hocks.iter().cloned() {
636+
if hook.path == path {
637+
info!("Found hock for path: {}, type: {}", path, elem.struct_name);
638+
(hook.callback)(s, &path);
639+
hocked = true;
640+
break;
641+
}
642+
}
643+
}
644+
if hocked {
645+
return;
646+
}
647+
635648
match elem {
636649
ElementType::Menu(menu) => {
637650
info!("Handling Menu: {}", menu.title);
@@ -724,32 +737,34 @@ fn enter_elem(s: &mut Cursive, elem: &ElementType) {
724737
create_multi_select_from_array_item(array_item, &enum_item.variants);
725738

726739
show_multi_select(s, &item.base.title, &multi_select_item);
727-
} else if path == "features.depend_features"
728-
|| path == "system.features.depend_features"
729-
{
730-
let app_data = s.user_data::<AppData>().unwrap();
731-
let mut depend_map = std::collections::HashMap::new();
732-
733-
if let Some(callback) = &app_data.depend_features_callback {
734-
let get_depend_features = || callback();
735-
if let Ok(features_map) = std::panic::catch_unwind(
736-
std::panic::AssertUnwindSafe(get_depend_features),
737-
) {
738-
info!("depend_features_callback returned: {:?}", features_map);
739-
depend_map = features_map;
740-
}
741-
} else {
742-
info!("depend_features_callback is not set");
743-
}
744-
745-
let depend_names: Vec<String> = depend_map.keys().cloned().collect();
746-
show_depend_features_editor(
747-
s,
748-
&item.base.title,
749-
&depend_names,
750-
&depend_map,
751-
);
752-
} else {
740+
}
741+
// else if path == "features.depend_features"
742+
// || path == "system.features.depend_features"
743+
// {
744+
// let app_data = s.user_data::<AppData>().unwrap();
745+
// let mut depend_map = std::collections::HashMap::new();
746+
747+
// if let Some(callback) = &app_data.depend_features_callback {
748+
// let get_depend_features = || callback();
749+
// if let Ok(features_map) = std::panic::catch_unwind(
750+
// std::panic::AssertUnwindSafe(get_depend_features),
751+
// ) {
752+
// info!("depend_features_callback returned: {:?}", features_map);
753+
// depend_map = features_map;
754+
// }
755+
// } else {
756+
// info!("depend_features_callback is not set");
757+
// }
758+
759+
// let depend_names: Vec<String> = depend_map.keys().cloned().collect();
760+
// show_depend_features_editor(
761+
// s,
762+
// &item.base.title,
763+
// &depend_names,
764+
// &depend_map,
765+
// );
766+
// }
767+
else {
753768
show_array_edit(s, &item.base.key(), &item.base.title, &array_item.values);
754769
}
755770
}

ostool/src/build/config.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct Cargo {
3333
/// package name
3434
pub package: String,
3535
/// features to enable
36-
pub features: Features,
36+
pub features: Vec<String>,
3737
/// log level feature
3838
pub log: Option<LogLevel>,
3939
/// extra cargo .config.toml file
@@ -50,12 +50,6 @@ pub struct Cargo {
5050
pub to_bin: bool,
5151
}
5252

53-
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
54-
pub struct Features {
55-
pub self_features: Vec<String>,
56-
pub depend_features: Vec<Depend>,
57-
}
58-
5953
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
6054
pub struct Depend {
6155
pub name: String,

ostool/src/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl AppContext {
3636
self.shell_run_cmd(cmd)?;
3737
}
3838

39-
let mut features = config.features.self_features.clone();
39+
let mut features = config.features.clone();
4040
if let Some(log_level) = &self.log_level_feature(config) {
4141
features.push(log_level.to_string());
4242
}

ostool/src/ctx.rs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -80,54 +80,54 @@ impl AppContext {
8080
pub fn launch_jkconfig_ui(config_path: &Path, schema_path: &Path) -> anyhow::Result<bool> {
8181
let mut app_data = AppData::new(Some(config_path), Some(schema_path))?;
8282

83-
app_data.features_callback = Some(std::sync::Arc::new(|| {
84-
let mut features = Vec::new();
85-
86-
if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
87-
let workspace_root = metadata.workspace_root.clone();
88-
if let Some(current_package) = metadata
89-
.packages
90-
.iter()
91-
.find(|p| p.manifest_path.starts_with(&workspace_root))
92-
{
93-
for (feature_name, _) in &current_package.features {
94-
features.push(feature_name.clone());
95-
}
96-
}
97-
}
98-
features
99-
}));
100-
101-
// 设置depend_features_callback以获取依赖项及其features
102-
app_data.depend_features_callback = Some(std::sync::Arc::new(|| {
103-
let mut depend_features = HashMap::new();
104-
105-
if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
106-
let workspace_root = metadata.workspace_root.clone();
107-
if let Some(current_package) = metadata
108-
.packages
109-
.iter()
110-
.find(|p| p.manifest_path.starts_with(&workspace_root))
111-
{
112-
// 遍历所有依赖项
113-
for dependency in &current_package.dependencies {
114-
let dep_name = dependency.name.clone();
115-
let mut dep_features = Vec::new();
116-
117-
if let Some(dep_package) =
118-
metadata.packages.iter().find(|p| p.name == dep_name)
119-
{
120-
for (feature_name, _) in &dep_package.features {
121-
dep_features.push(feature_name.clone());
122-
}
123-
}
124-
125-
depend_features.insert(dep_name, dep_features);
126-
}
127-
}
128-
}
129-
depend_features
130-
}));
83+
// app_data.features_callback = Some(std::sync::Arc::new(|| {
84+
// let mut features = Vec::new();
85+
86+
// if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
87+
// let workspace_root = metadata.workspace_root.clone();
88+
// if let Some(current_package) = metadata
89+
// .packages
90+
// .iter()
91+
// .find(|p| p.manifest_path.starts_with(&workspace_root))
92+
// {
93+
// for (feature_name, _) in &current_package.features {
94+
// features.push(feature_name.clone());
95+
// }
96+
// }
97+
// }
98+
// features
99+
// }));
100+
101+
// // 设置depend_features_callback以获取依赖项及其features
102+
// app_data.depend_features_callback = Some(std::sync::Arc::new(|| {
103+
// let mut depend_features = HashMap::new();
104+
105+
// if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
106+
// let workspace_root = metadata.workspace_root.clone();
107+
// if let Some(current_package) = metadata
108+
// .packages
109+
// .iter()
110+
// .find(|p| p.manifest_path.starts_with(&workspace_root))
111+
// {
112+
// // 遍历所有依赖项
113+
// for dependency in &current_package.dependencies {
114+
// let dep_name = dependency.name.clone();
115+
// let mut dep_features = Vec::new();
116+
117+
// if let Some(dep_package) =
118+
// metadata.packages.iter().find(|p| p.name == dep_name)
119+
// {
120+
// for (feature_name, _) in &dep_package.features {
121+
// dep_features.push(feature_name.clone());
122+
// }
123+
// }
124+
125+
// depend_features.insert(dep_name, dep_features);
126+
// }
127+
// }
128+
// }
129+
// depend_features
130+
// }));
131131

132132
run_tui(app_data)?;
133133

ostool/src/run/cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl CargoRunner {
101101
ctx.shell_run_cmd(cmd)?;
102102
}
103103

104-
let mut features = config.features.self_features.clone();
104+
let mut features = config.features.clone();
105105
if let Some(log_level) = &self.log_level_feature(ctx, &config) {
106106
features.push(log_level.to_string());
107107
}

0 commit comments

Comments
 (0)