更新项目结构,添加新依赖,优化交互模式和命令行参数处理
All checks were successful
Rust Cross-Compile and Release / build_and_release (push) Successful in 8m40s
All checks were successful
Rust Cross-Compile and Release / build_and_release (push) Successful in 8m40s
This commit is contained in:
356
src/main.rs
356
src/main.rs
@@ -1,135 +1,325 @@
|
||||
use inquire::{Select, Text};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::{Parser, ValueEnum};
|
||||
use inquire::{Confirm, Select, Text};
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Debug, Clone, ValueEnum)]
|
||||
enum Lang {
|
||||
C,
|
||||
Cpp,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, ValueEnum)]
|
||||
enum ProjectType {
|
||||
Exe,
|
||||
StaticLib,
|
||||
SharedLib,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, ValueEnum)]
|
||||
enum CxxStandard {
|
||||
#[value(name = "11")]
|
||||
Cxx11,
|
||||
#[value(name = "14")]
|
||||
Cxx14,
|
||||
#[value(name = "17")]
|
||||
Cxx17,
|
||||
#[value(name = "20")]
|
||||
Cxx20,
|
||||
#[value(name = "23")]
|
||||
Cxx23,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "ccmk")]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// Project name
|
||||
#[arg(short, long)]
|
||||
name: Option<String>,
|
||||
|
||||
/// Language: C or C++
|
||||
#[arg(short, long, value_enum)]
|
||||
lang: Option<Lang>,
|
||||
|
||||
/// Project type: exe, static-lib, shared-lib
|
||||
#[arg(short, long, value_enum)]
|
||||
project_type: Option<ProjectType>,
|
||||
|
||||
/// C++ standard (for C++ projects)
|
||||
#[arg(short, long, value_enum)]
|
||||
cxx_standard: Option<CxxStandard>,
|
||||
|
||||
/// Output directory
|
||||
#[arg(short, long)]
|
||||
output: Option<String>,
|
||||
|
||||
/// Force overwrite existing directory
|
||||
#[arg(long)]
|
||||
force: bool,
|
||||
}
|
||||
|
||||
fn get_lang_ext(lang: &Lang) -> &'static str {
|
||||
match lang {
|
||||
Lang::C => "c",
|
||||
Lang::Cpp => "cpp",
|
||||
}
|
||||
}
|
||||
|
||||
fn get_lang_cmake(lang: &Lang) -> &'static str {
|
||||
match lang {
|
||||
Lang::C => "C",
|
||||
Lang::Cpp => "CXX",
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_cmakelists(
|
||||
project_name: &str,
|
||||
lang: &str,
|
||||
project_type: &str,
|
||||
cxx_standard: &str,
|
||||
lang: &Lang,
|
||||
project_type: &ProjectType,
|
||||
cxx_standard: &Option<CxxStandard>,
|
||||
) -> String {
|
||||
let mut lines = vec![
|
||||
"cmake_minimum_required(VERSION 3.20)".to_string(),
|
||||
format!("project({} LANGUAGES {})", project_name, lang.to_uppercase()),
|
||||
format!("project({} LANGUAGES {})", project_name, get_lang_cmake(lang)),
|
||||
"".to_string(),
|
||||
];
|
||||
|
||||
// 添加C++标准设置
|
||||
if lang == "cxx" {
|
||||
lines.push(format!("set(CMAKE_CXX_STANDARD {})", cxx_standard));
|
||||
if let Lang::Cpp = lang {
|
||||
let standard = match cxx_standard {
|
||||
Some(CxxStandard::Cxx11) => "11",
|
||||
Some(CxxStandard::Cxx14) => "14",
|
||||
Some(CxxStandard::Cxx17) => "17",
|
||||
Some(CxxStandard::Cxx20) => "20",
|
||||
Some(CxxStandard::Cxx23) => "23",
|
||||
None => "17",
|
||||
};
|
||||
lines.push(format!("set(CMAKE_CXX_STANDARD {})", standard));
|
||||
lines.push("set(CMAKE_CXX_STANDARD_REQUIRED ON)".to_string());
|
||||
lines.push("set(CMAKE_CXX_EXTENSIONS OFF)".to_string());
|
||||
lines.push("".to_string());
|
||||
}
|
||||
|
||||
// 处理不同项目类型
|
||||
let ext = get_lang_ext(lang);
|
||||
match project_type {
|
||||
"exe" => {
|
||||
ProjectType::Exe => {
|
||||
lines.push(format!(
|
||||
"add_executable({} src/main.{})",
|
||||
project_name,
|
||||
if lang == "c" { "c" } else { "cpp" }, // 使用.cpp扩展名但CMake语言为CXX
|
||||
project_name, ext,
|
||||
));
|
||||
}
|
||||
"static_lib" => {
|
||||
ProjectType::StaticLib => {
|
||||
lines.push(format!(
|
||||
"add_library({} STATIC src/main.{})",
|
||||
project_name,
|
||||
if lang == "c" { "c" } else { "cpp" }, // 使用.cpp扩展名但CMake语言为CXX
|
||||
project_name, ext,
|
||||
));
|
||||
lines.push(format!("target_include_directories({} PRIVATE include)", project_name));
|
||||
lines.push(format!("target_include_directories({} PUBLIC include)", project_name));
|
||||
}
|
||||
ProjectType::SharedLib => {
|
||||
lines.push(format!(
|
||||
"target_compile_definitions({} PUBLIC {}_EXPORTS)",
|
||||
"add_library({} SHARED src/main.{})",
|
||||
project_name, ext,
|
||||
));
|
||||
lines.push(format!("target_include_directories({} PUBLIC include)", project_name));
|
||||
lines.push(format!(
|
||||
"target_compile_definitions({} PUBLIC {}_EXPORTS)",
|
||||
project_name,
|
||||
project_name.to_uppercase()
|
||||
project_name.to_uppercase().replace('-', "_")
|
||||
));
|
||||
}
|
||||
_ => panic!("Unsupported project type"),
|
||||
}
|
||||
|
||||
// 添加导出设置(仅库类型)
|
||||
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
|
||||
lines.push("".to_string());
|
||||
lines.push("# Export targets".to_string());
|
||||
lines.push("include(GNUInstallDirs)".to_string());
|
||||
lines.push(format!(
|
||||
"install(TARGETS {} DESTINATION lib)",
|
||||
project_name
|
||||
));
|
||||
lines.push(
|
||||
"install(DIRECTORY include/ DESTINATION include)".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
fn generate_source_file(lang: &str) -> String {
|
||||
if lang == "c" {
|
||||
"#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}".to_string()
|
||||
} else {
|
||||
"#include <iostream>\n\nint main() {\n std::cout << \"Hello, World!\" << std::endl;\n return 0;\n}".to_string()
|
||||
fn generate_source_file(lang: &Lang) -> String {
|
||||
match lang {
|
||||
Lang::C => {
|
||||
"#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}"
|
||||
.to_string()
|
||||
}
|
||||
Lang::Cpp => {
|
||||
"#include <iostream>\n\nint main() {\n std::cout << \"Hello, World!\" << std::endl;\n return 0;\n}"
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 清理项目名,移除不适合作为文件夹名的字符
|
||||
fn sanitize_folder_name(name: &str) -> String {
|
||||
// 使用正则表达式替换所有非字母数字、下划线和连字符的字符
|
||||
let re = Regex::new(r"[^a-zA-Z0-9_-]").unwrap();
|
||||
let sanitized = re.replace_all(name, "").to_string();
|
||||
|
||||
// 确保不为空,如果为空则使用默认名称
|
||||
|
||||
if sanitized.is_empty() {
|
||||
return "cmake_project".to_string();
|
||||
}
|
||||
|
||||
|
||||
sanitized
|
||||
}
|
||||
|
||||
fn main() -> Result<(), inquire::InquireError> {
|
||||
// 1. 输入项目名称
|
||||
let project_name = Text::new("请输入项目名称:").prompt()?;
|
||||
|
||||
// 2. 选择语言类型
|
||||
let lang_choices = vec!["C", "C++"];
|
||||
let lang = Select::new("请选择编程语言:", lang_choices).prompt()?;
|
||||
|
||||
// 3. 选择项目类型
|
||||
let type_choices = vec!["Executable", "Static Library"];
|
||||
let project_type = Select::new("请选择项目类型:", type_choices).prompt()?;
|
||||
|
||||
// 4. 选择C++标准版本(仅当选择C++时)
|
||||
let cxx_standard = if lang == "C++" {
|
||||
let standard_choices = vec!["11", "14", "17", "20"];
|
||||
Select::new("请选择C++标准版本:", standard_choices).prompt()?
|
||||
} else {
|
||||
&"00".to_string()
|
||||
};
|
||||
|
||||
// 清理项目名,创建有效的文件夹名
|
||||
let folder_name = sanitize_folder_name(&project_name);
|
||||
|
||||
// 创建项目文件夹和目录结构
|
||||
let output_path = Path::new(".").join(&folder_name);
|
||||
fs::create_dir_all(&output_path)?;
|
||||
fs::create_dir_all(output_path.join("src"))?;
|
||||
fs::create_dir_all(output_path.join("include"))?;
|
||||
|
||||
// 写入CMakeLists.txt
|
||||
fs::write(
|
||||
output_path.join("CMakeLists.txt"),
|
||||
generate_cmakelists(
|
||||
&project_name,
|
||||
if lang == "C" { "c" } else { "cxx" },
|
||||
match project_type {
|
||||
"Executable" => "exe",
|
||||
"Static Library" => "static_lib",
|
||||
_ => "exe",
|
||||
},
|
||||
&cxx_standard,
|
||||
),
|
||||
)?;
|
||||
|
||||
// 写入示例源文件
|
||||
let source_ext = if lang == "C" { "c" } else { "cpp" }; // 源文件扩展名保持.cpp,CMake语言使用CXX
|
||||
fs::write(
|
||||
output_path.join("src").join(format!("main.{}", source_ext)),
|
||||
generate_source_file(if lang == "C" { "c" } else { "cpp" }),
|
||||
)?;
|
||||
|
||||
println!("✅ 项目已成功创建!");
|
||||
fn print_project_structure(folder_name: &str, ext: &str, project_type: &ProjectType) {
|
||||
println!("\n项目已成功创建!");
|
||||
println!("📁 项目结构:");
|
||||
println!(" - {}/", folder_name);
|
||||
println!(" ├── CMakeLists.txt");
|
||||
println!(" ├── include/");
|
||||
println!(" └── src/");
|
||||
println!(" └── main.{}", source_ext);
|
||||
|
||||
|
||||
match project_type {
|
||||
ProjectType::Exe => {
|
||||
println!(" └── src/");
|
||||
println!(" └── main.{}", ext);
|
||||
}
|
||||
ProjectType::StaticLib | ProjectType::SharedLib => {
|
||||
println!(" ├── src/");
|
||||
println!(" │ └── main.{}", ext);
|
||||
println!(" └── README.md");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn interactive_mode() -> Result<(String, Lang, ProjectType, Option<CxxStandard>)> {
|
||||
let project_name = Text::new("请输入项目名称:").prompt()?;
|
||||
|
||||
let lang_choices = vec!["C", "C++"];
|
||||
let lang_raw = Select::new("请选择编程语言:", lang_choices).prompt()?;
|
||||
let lang = if lang_raw == "C" { Lang::C } else { Lang::Cpp };
|
||||
|
||||
let type_choices = vec!["Executable", "Static Library", "Shared Library"];
|
||||
let type_raw = Select::new("请选择项目类型:", type_choices).prompt()?;
|
||||
let project_type = match type_raw {
|
||||
"Executable" => ProjectType::Exe,
|
||||
"Static Library" => ProjectType::StaticLib,
|
||||
"Shared Library" => ProjectType::SharedLib,
|
||||
_ => ProjectType::Exe,
|
||||
};
|
||||
|
||||
let cxx_standard = if matches!(lang, Lang::Cpp) {
|
||||
let standard_choices = vec!["11", "14", "17", "20", "23"];
|
||||
let standard_raw = Select::new("请选择C++标准版本:", standard_choices).prompt()?;
|
||||
Some(match standard_raw {
|
||||
"11" => CxxStandard::Cxx11,
|
||||
"14" => CxxStandard::Cxx14,
|
||||
"17" => CxxStandard::Cxx17,
|
||||
"20" => CxxStandard::Cxx20,
|
||||
_ => CxxStandard::Cxx23,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok((project_name, lang, project_type, cxx_standard))
|
||||
}
|
||||
|
||||
fn create_project(
|
||||
project_name: &str,
|
||||
lang: &Lang,
|
||||
project_type: &ProjectType,
|
||||
cxx_standard: &Option<CxxStandard>,
|
||||
output_dir: &Path,
|
||||
force: bool,
|
||||
) -> Result<()> {
|
||||
// 检查目录是否存在
|
||||
if output_dir.exists() {
|
||||
if force {
|
||||
println!("⚠️ 目录已存在,将被覆盖");
|
||||
} else {
|
||||
let overwrite = Confirm::new(&format!(
|
||||
"目录 '{}' 已存在,是否覆盖?",
|
||||
output_dir.display()
|
||||
))
|
||||
.prompt()?;
|
||||
if !overwrite {
|
||||
return Err(anyhow!("已取消"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建目录结构
|
||||
fs::create_dir_all(output_dir.join("src"))?;
|
||||
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
|
||||
fs::create_dir_all(output_dir.join("include"))?;
|
||||
}
|
||||
|
||||
// 写入 CMakeLists.txt
|
||||
fs::write(
|
||||
output_dir.join("CMakeLists.txt"),
|
||||
generate_cmakelists(project_name, lang, project_type, cxx_standard),
|
||||
)
|
||||
.with_context(|| "写入 CMakeLists.txt 失败")?;
|
||||
|
||||
// 写入源文件
|
||||
let ext = get_lang_ext(lang);
|
||||
fs::write(
|
||||
output_dir.join("src").join(format!("main.{}", ext)),
|
||||
generate_source_file(lang),
|
||||
)
|
||||
.with_context(|| "写入源文件失败")?;
|
||||
|
||||
// 为库类型创建 README.md
|
||||
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
|
||||
let readme_content = format!(
|
||||
"# {}\n\nA CMake library project.\n\n## Build\n\n```bash\nmkdir build && cd build\ncmake ..\nmake\n```\n\n## Install\n\n```bash\ncmake --install .\n```\n",
|
||||
project_name
|
||||
);
|
||||
fs::write(output_dir.join("README.md"), readme_content)?;
|
||||
}
|
||||
|
||||
print_project_structure(
|
||||
&output_dir.file_name().unwrap().to_string_lossy(),
|
||||
ext,
|
||||
project_type,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
// 如果没有提供任何参数,使用交互模式
|
||||
if args.name.is_none()
|
||||
&& args.lang.is_none()
|
||||
&& args.project_type.is_none()
|
||||
&& args.cxx_standard.is_none()
|
||||
{
|
||||
let (name, lang, project_type, cxx_standard) = interactive_mode()?;
|
||||
let folder_name = sanitize_folder_name(&name);
|
||||
let output_path = Path::new(".").join(&folder_name);
|
||||
create_project(&name, &lang, &project_type, &cxx_standard, &output_path, args.force)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 命令行模式
|
||||
let name = args.name.ok_or_else(|| anyhow!("项目名称为必填项"))?;
|
||||
let lang = args.lang.unwrap_or(Lang::Cpp);
|
||||
let project_type = args.project_type.unwrap_or(ProjectType::Exe);
|
||||
let cxx_standard = args.cxx_standard.or(Some(CxxStandard::Cxx17));
|
||||
|
||||
let folder_name = sanitize_folder_name(&name);
|
||||
let output_path = if let Some(output) = args.output {
|
||||
Path::new(&output).to_path_buf()
|
||||
} else {
|
||||
Path::new(".").join(&folder_name)
|
||||
};
|
||||
|
||||
create_project(&name, &lang, &project_type, &cxx_standard, &output_path, args.force)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user