From cf186539d9268a16aa23ec559d41b1f54dcb3293 Mon Sep 17 00:00:00 2001 From: Cloyir Date: Wed, 3 May 2023 19:38:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86new=E6=8C=87?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 ++ src/cnew/mod.rs | 35 ++++++++++++++++------------------- src/const_value.rs | 5 +++++ src/ctest/mod.rs | 2 ++ src/main.rs | 1 + src/rtools/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/rtools/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 1c63f12..32153d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] clap = { version = "4.2.2", features = ["derive"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/cnew/mod.rs b/src/cnew/mod.rs index 2adf771..b313010 100644 --- a/src/cnew/mod.rs +++ b/src/cnew/mod.rs @@ -1,5 +1,7 @@ use clap::Parser; +use crate::{const_value::console_log, rtools}; + mod text; #[derive(Parser, Debug)] @@ -13,38 +15,33 @@ pub struct CommandsNew { quiet: bool, /// 从自定义模板构建 - #[clap(short, long, name = "module_name", default_value = "defult")] - module: String, + #[clap(short, long, name = "template_name", default_value = "default")] + template: String, } /// 创建新项目 pub fn run(config: CommandsNew) -> Result<(), std::io::Error> { - let target_path = std::env::current_dir().unwrap().join(&config.project_name); - - if !config.quiet { - println!("新建项目: {}", config.project_name); - println!("目标路径: {:?}", target_path); - } + let target_path = std::env::current_dir()?.join(&config.project_name); + let mould_path = rtools::get_home_path().join("mould").join(&config.template); if let Ok(_) = target_path.read_dir() { if !config.quiet { - println!("创建项目失败, 已存在名为{}的文件夹", config.project_name); + console_log(&format!( + "创建项目失败, 已存在名为{}的文件夹", + config.project_name + )); } 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 { - println!("创建成功"); - println!("请进入{}文件夹再使用build指令构建项目", config.project_name); + console_log(&format!("新建项目: {}", config.project_name)); + console_log(&format!("目标路径: {:?}", target_path)); + console_log(&format!("模板名称: {:?}", config.template)); } + // 从mould文件夹下获取模板并复制到目标路径 + rtools::copy_dir(&mould_path, &target_path)?; + Ok(()) } diff --git a/src/const_value.rs b/src/const_value.rs index 678e2dd..f533112 100644 --- a/src/const_value.rs +++ b/src/const_value.rs @@ -3,6 +3,11 @@ /// 本项目的名称 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"; diff --git a/src/ctest/mod.rs b/src/ctest/mod.rs index 5ecd984..12e408e 100644 --- a/src/ctest/mod.rs +++ b/src/ctest/mod.rs @@ -1,5 +1,7 @@ use clap::Parser; +use crate::rtools; + #[derive(Parser, Debug)] pub struct CommandsTest { /// 每完成一个测试暂停一次 diff --git a/src/main.rs b/src/main.rs index 83de179..9bc8061 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod cbuild; mod cnew; mod cstore; mod ctest; +mod rtools; use clap::{command, Parser}; use const_value::{ABOUT, AUTHOR, LONG_ABOUT, VERSION}; diff --git a/src/rtools/mod.rs b/src/rtools/mod.rs new file mode 100644 index 0000000..b1557e0 --- /dev/null +++ b/src/rtools/mod.rs @@ -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(()) +}