Skip to content

Commit

Permalink
refactor: ♻️ 从 BabelNAR.rs 迁入模块:cli_supportcrate::support
Browse files Browse the repository at this point in the history
导入旧模块代码,并随之更新`use`代码和模块结构
  • Loading branch information
ARCJ137442 committed Sep 12, 2024
1 parent c884f34 commit 319d4cd
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 166 deletions.
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ version = "0.17" # 现已发布于`crates.io`
features = [] # ! 【2024-03-21 09:24:51】暂时没有特性

[dependencies.babel_nar]
version = "0.25"
features = [ # * 🚩【2024-09-12 18:04:39】目前锁定以下两个特性
"cin_implements", # 各大CIN的NAVM实现
"test_tools", # 测试工具集
]
path = "../BabelNAR.rs" # TODO: 待BabelNAR.rs发布后恢复
features = ["bundled"]
# version = "0.25"
# features = [ # * 🚩【2024-09-12 18:04:39】目前锁定以下两个特性
# "cin_implements", # 各大CIN的NAVM实现
# "test_tools", # 测试工具集
# ]

## 依赖特性的可选依赖 ##

Expand Down
8 changes: 5 additions & 3 deletions src/cli/arg_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//! * ⚠️【2024-04-01 14:31:09】特定于二进制crate,目前不要并入[`babel_nar`]
//! * 🚩【2024-04-04 03:03:58】现在移出所有与「启动配置」相关的逻辑到[`super::vm_config`]
use crate::cli::{load_config_extern, read_config_extern, LaunchConfig};
use babel_nar::println_cli;
use crate::{
cli::{load_config_extern, read_config_extern, LaunchConfig},
println_cli,
};
use clap::Parser;
use std::{
env::{current_dir, current_exe},
Expand Down Expand Up @@ -113,7 +115,7 @@ pub fn load_config(args: &CliArgs) -> LaunchConfig {
#[cfg(test)]
mod tests {
use super::*;
use babel_nar::tests::*;
use crate::tests::*;
use nar_dev_utils::fail_tests;

/// 测试/参数解析
Expand Down
12 changes: 7 additions & 5 deletions src/cli/config_launcher.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
//! 用于从「启动参数」启动NAVM运行时
use crate::cli::{
read_config_extern, search_configs, LaunchConfig, LaunchConfigCommand, LaunchConfigTranslators,
RuntimeConfig, SUPPORTED_CONFIG_EXTENSIONS,
use crate::{
cli::{
read_config_extern, search_configs, LaunchConfig, LaunchConfigCommand,
LaunchConfigTranslators, RuntimeConfig, SUPPORTED_CONFIG_EXTENSIONS,
},
eprintln_cli, println_cli,
support::{cin_search::name_match::name_match, io::readline_iter::ReadlineIter},
};
use anyhow::{anyhow, Result};
use babel_nar::{
cin_implements::{
common::generate_command, cxin_js, nars_python, native, ona, openjunars, opennars, pynars,
},
cli_support::{cin_search::name_match::name_match, io::readline_iter::ReadlineIter},
eprintln_cli, println_cli,
runtimes::{
api::{InputTranslator, IoTranslators},
CommandVm, OutputTranslator,
Expand Down
8 changes: 3 additions & 5 deletions src/cli/config_search.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! CIN自动搜索
use crate::cli::{read_config_extern, LaunchConfig};
use crate::println_cli;
use crate::support::cin_search::{name_match::is_name_match, path_walker::PathWalkerV1};
use anyhow::Result;
use babel_nar::{
cli_support::cin_search::{name_match::is_name_match, path_walker::PathWalkerV1},
println_cli,
};
use nar_dev_utils::ToDebug;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -81,7 +79,7 @@ pub fn search_configs<S: AsRef<str>>(
#[cfg(test)]
mod tests {
use super::*;
use babel_nar::tests::config_paths::ARG_PARSE_TEST;
use crate::tests::config_paths::ARG_PARSE_TEST;
// use std::env::current_dir;

#[test]
Expand Down
100 changes: 88 additions & 12 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,93 @@
//! 原BabelNAR.rs `src/bin/babelnar_cli/*.rs`
//! * 🚩【2024-09-12 17:41:35】现在统一放置在`src/cli`下
nar_dev_utils::mods! {
// 启动参数
pub use vm_config;
// 命令行解析
pub use arg_parse;
// 配置(自动)搜索
pub use config_search;
// 从配置启动
pub use config_launcher;
use crate::{eprintln_cli, println_cli};
use anyhow::Result;
use clap::Parser;
use std::{io::Result as IoResult, path::PathBuf, thread::sleep, time::Duration};

// 启动参数
mod vm_config;
pub use vm_config::*;

// 命令行解析
mod arg_parse;
pub use arg_parse::*;

// 配置(自动)搜索
mod config_search;
pub use config_search::*;

// 从配置启动
mod config_launcher;
pub use config_launcher::*;

// 运行时交互、管理
mod runtime_manage;
pub use runtime_manage::*;

// Websocket服务端
mod websocket_server;
pub use websocket_server::*;

/// 以特定参数开始命令行主程序
/// * 🚩此处只应该有自[`env`]传入的参数
/// * 🚩【2024-04-01 14:25:38】暂时用不到「当前工作路径」
pub fn main_args(cwd: IoResult<PathBuf>, args: impl Iterator<Item = String>) -> Result<()> {
// 解包当前工作目录
let cwd = cwd
.inspect_err(|e| println_cli!([Warn] "无法获取当前工作目录:{e}"))
.ok();

// (Windows下)启用终端颜色
let _ = colored::control::set_virtual_terminal(true)
.inspect_err(|_| eprintln_cli!([Error] "无法启动终端彩色显示。。"));

// 解析命令行参数
let args = CliArgs::parse_from(args);

// 读取配置 | with 默认配置文件
let mut config = load_config(&args);

// 是否向用户展示「详细信息」 | 用于等待、提示等
let user_verbose = config.user_input.is_none() || config.user_input.unwrap();

// 用户填充配置项 | 需要用户输入、工作路径(🎯自动搜索)
polyfill_config_from_user(&mut config, cwd);

// 清屏,预备启动
if user_verbose {
println_cli!([Info] "配置加载完毕!程序将在1s后启动。。。");
sleep(Duration::from_secs(1));
}
let _ = clearscreen::clear().inspect_err(|e| eprintln_cli!([Warn] "清屏失败:{e}"));

// 从配置项启动 | 复制一个新配置,不会附带任何非基础类型开销
let (runtime, config) = match launch_by_config(config.clone()) {
// 启动成功⇒返回
Ok((r, c)) => (r, c),
// 启动失败⇒打印错误信息,等待并退出
Err(e) => {
println_cli!([Error] "NARS运行时启动错误:{e}");
// 空配置/启用用户输入⇒延时提示
if user_verbose {
println_cli!([Info] "程序将在 3 秒后自动退出。。。");
sleep(Duration::from_secs(3));
}
return Err(e);
}
};

// 运行时交互、管理
pub use runtime_manage;
// Websocket服务端
pub use websocket_server;
let manager = RuntimeManager::new(runtime, config.clone());
let result = loop_manage(manager, &config);

// 启用用户输入时延时提示
if config.user_input {
println_cli!([Info] "程序将在 5 秒后自动退出。。。");
sleep(Duration::from_secs(3));
}

// 返回结果
result
}
12 changes: 7 additions & 5 deletions src/cli/runtime_manage.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
//! 启动后运行时的(交互与)管理
use super::websocket_server::*;
use crate::cli::{launch_by_runtime_config, InputMode, LaunchConfigPreludeNAL, RuntimeConfig};
use anyhow::{anyhow, Result};
use babel_nar::{
cli_support::{
use crate::{
cli::{launch_by_runtime_config, InputMode, LaunchConfigPreludeNAL, RuntimeConfig},
eprintln_cli, if_let_err_eprintln_cli, println_cli,
support::{
error_handling_boost::error_anyhow,
io::{
navm_output_cache::{ArcMutex, OutputCache},
readline_iter::ReadlineIter,
},
},
eprintln_cli, if_let_err_eprintln_cli, println_cli,
};
use anyhow::{anyhow, Result};
use babel_nar::{
runtimes::TranslateError,
test_tools::{nal_format::parse, put_nal, VmOutputCache},
};
Expand Down
4 changes: 2 additions & 2 deletions src/cli/vm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
//! }
//! ```
use crate::println_cli;
use anyhow::{anyhow, Result};
use babel_nar::println_cli;
use nar_dev_utils::{if_return, pipe, OptionBoost, ResultBoost};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -742,8 +742,8 @@ pub fn try_complete_path(path: &Path) -> PathBuf {
#[cfg(test)]
pub mod tests {
use super::*;
use crate::tests::*;
use anyhow::Result;
use babel_nar::tests::*;
use nar_dev_utils::{asserts, macro_once};

/// 测试/解析
Expand Down
4 changes: 2 additions & 2 deletions src/cli/websocket_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::cli::{LaunchConfigWebsocket, RuntimeConfig, RuntimeManager};
use anyhow::Result;
use babel_nar::{
cli_support::{
use crate::{
support::{
error_handling_boost::error_anyhow,
io::{
navm_output_cache::{ArcMutex, OutputCache},
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ pub mod support;

// CLI主程序功能
pub mod cli;

// 单元测试
#[cfg(test)]
mod tests;
70 changes: 1 addition & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,11 @@
//! ```
use anyhow::Result;
use babel_nar::{eprintln_cli, println_cli};
use babel_nar_cli::cli::*;
use clap::Parser;
use std::{env, io::Result as IoResult, path::PathBuf, thread::sleep, time::Duration};
use std::env;

/// 主入口
pub fn main() -> Result<()> {
// 以默认参数启动
main_args(env::current_dir(), env::args())
}

/// 以特定参数开始命令行主程序
/// * 🚩此处只应该有自[`env`]传入的参数
/// * 🚩【2024-04-01 14:25:38】暂时用不到「当前工作路径」
pub fn main_args(cwd: IoResult<PathBuf>, args: impl Iterator<Item = String>) -> Result<()> {
// 解包当前工作目录
let cwd = cwd
.inspect_err(|e| println_cli!([Warn] "无法获取当前工作目录:{e}"))
.ok();

// (Windows下)启用终端颜色
let _ = colored::control::set_virtual_terminal(true)
.inspect_err(|_| eprintln_cli!([Error] "无法启动终端彩色显示。。"));

// 解析命令行参数
let args = CliArgs::parse_from(args);

// 读取配置 | with 默认配置文件
let mut config = load_config(&args);

// 是否向用户展示「详细信息」 | 用于等待、提示等
let user_verbose = config.user_input.is_none() || config.user_input.unwrap();

// 用户填充配置项 | 需要用户输入、工作路径(🎯自动搜索)
polyfill_config_from_user(&mut config, cwd);

// 清屏,预备启动
if user_verbose {
println_cli!([Info] "配置加载完毕!程序将在1s后启动。。。");
sleep(Duration::from_secs(1));
}
let _ = clearscreen::clear().inspect_err(|e| eprintln_cli!([Warn] "清屏失败:{e}"));

// 从配置项启动 | 复制一个新配置,不会附带任何非基础类型开销
let (runtime, config) = match launch_by_config(config.clone()) {
// 启动成功⇒返回
Ok((r, c)) => (r, c),
// 启动失败⇒打印错误信息,等待并退出
Err(e) => {
println_cli!([Error] "NARS运行时启动错误:{e}");
// 空配置/启用用户输入⇒延时提示
if user_verbose {
println_cli!([Info] "程序将在 3 秒后自动退出。。。");
sleep(Duration::from_secs(3));
}
return Err(e);
}
};

// 运行时交互、管理
let manager = RuntimeManager::new(runtime, config.clone());
let result = loop_manage(manager, &config);

// 启用用户输入时延时提示
if config.user_input {
println_cli!([Info] "程序将在 5 秒后自动退出。。。");
sleep(Duration::from_secs(3));
}

// 返回结果
result
}

// 单元测试
#[cfg(test)]
mod tests;
18 changes: 8 additions & 10 deletions src/support/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
//! * ✨终端美化相关
//!
nar_dev_utils::mods! {
// 输出打印
pub output_print;
// 输出打印
pub mod output_print;

// 读取行迭代器
pub readline_iter;
// 读取行迭代器
pub mod readline_iter;

// NAVM输出缓存
pub navm_output_cache;
// NAVM输出缓存
pub mod navm_output_cache;

// Websocket支持
pub websocket;
}
// Websocket支持
pub mod websocket;
Loading

0 comments on commit 319d4cd

Please sign in to comment.