完善了new指令

This commit is contained in:
Cloyir 2023-05-03 19:38:18 +08:00
parent 2bee2f325f
commit cf186539d9
6 changed files with 62 additions and 19 deletions

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.2.2", features = ["derive"] } clap = { version = "4.2.2", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -1,5 +1,7 @@
use clap::Parser; use clap::Parser;
use crate::{const_value::console_log, rtools};
mod text; mod text;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -13,38 +15,33 @@ pub struct CommandsNew {
quiet: bool, quiet: bool,
/// 从自定义模板构建 /// 从自定义模板构建
#[clap(short, long, name = "module_name", default_value = "defult")] #[clap(short, long, name = "template_name", default_value = "default")]
module: String, template: String,
} }
/// 创建新项目 /// 创建新项目
pub fn run(config: CommandsNew) -> Result<(), std::io::Error> { pub fn run(config: CommandsNew) -> Result<(), std::io::Error> {
let target_path = std::env::current_dir().unwrap().join(&config.project_name); let target_path = std::env::current_dir()?.join(&config.project_name);
let mould_path = rtools::get_home_path().join("mould").join(&config.template);
if !config.quiet {
println!("新建项目: {}", config.project_name);
println!("目标路径: {:?}", target_path);
}
if let Ok(_) = target_path.read_dir() { if let Ok(_) = target_path.read_dir() {
if !config.quiet { if !config.quiet {
println!("创建项目失败, 已存在名为{}的文件夹", config.project_name); console_log(&format!(
"创建项目失败, 已存在名为{}的文件夹",
config.project_name
));
} }
return Ok(()); return Ok(());
} }
let src_path = target_path.join("src");
std::fs::create_dir_all(&src_path).unwrap();
// 新建README.md文件
std::fs::write(target_path.join("README.md"), text::get_readme_text())?;
// 新建main.cpp文件
std::fs::write(src_path.join("main.cpp"), text::get_maincpp_text())?;
if !config.quiet { if !config.quiet {
println!("创建成功"); console_log(&format!("新建项目: {}", config.project_name));
println!("请进入{}文件夹再使用build指令构建项目", config.project_name); console_log(&format!("目标路径: {:?}", target_path));
console_log(&format!("模板名称: {:?}", config.template));
} }
// 从mould文件夹下获取模板并复制到目标路径
rtools::copy_dir(&mould_path, &target_path)?;
Ok(()) Ok(())
} }

View File

@ -3,6 +3,11 @@
/// 本项目的名称 /// 本项目的名称
pub const THIS_PROJECT_NAME: &str = "rust-cmaker"; pub const THIS_PROJECT_NAME: &str = "rust-cmaker";
/// 以本项目名称发言: rust-cmaker: xxxx
pub fn console_log(msg: &str) {
println!("{}: {}", THIS_PROJECT_NAME, msg);
}
/// 作者信息 /// 作者信息
pub const AUTHOR: &str = "Cloyir mine123456@foxmail.com"; pub const AUTHOR: &str = "Cloyir mine123456@foxmail.com";

View File

@ -1,5 +1,7 @@
use clap::Parser; use clap::Parser;
use crate::rtools;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct CommandsTest { pub struct CommandsTest {
/// 每完成一个测试暂停一次 /// 每完成一个测试暂停一次

View File

@ -5,6 +5,7 @@ mod cbuild;
mod cnew; mod cnew;
mod cstore; mod cstore;
mod ctest; mod ctest;
mod rtools;
use clap::{command, Parser}; use clap::{command, Parser};
use const_value::{ABOUT, AUTHOR, LONG_ABOUT, VERSION}; use const_value::{ABOUT, AUTHOR, LONG_ABOUT, VERSION};

36
src/rtools/mod.rs Normal file
View File

@ -0,0 +1,36 @@
use std::path::{Path, PathBuf};
/// 获取rust-cmaker文件夹目录
pub fn get_home_path() -> PathBuf {
let path = std::env::current_exe().unwrap();
let path = path.parent().unwrap().to_path_buf();
path.parent().unwrap().to_path_buf()
}
/// 复制文件夹到指定路径
pub fn copy_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
if src.is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"源路径必须是一个文件夹",
));
}
if !dst.exists() {
std::fs::create_dir_all(dst)?;
}
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let path = entry.path();
let dst_path = dst.join(path.file_name().unwrap());
if entry.file_type()?.is_dir() {
copy_dir(&path, &dst_path)?;
} else {
std::fs::copy(&path, &&dst_path)?;
}
}
Ok(())
}