- Reworked `dcr.toml` handling: added read, validation, and edit through `core::config`
- `new` and `init` use the new config creation logic
- `build` and `run` now require `dcr.toml` and read `compiler` and `name` from it
This commit is contained in:
2026-02-20 17:54:01 +08:00
parent 06e7af6ab6
commit 5a2025fb80
10 changed files with 312 additions and 50 deletions

View File

@@ -1,4 +1,5 @@
use crate::config::{PROFILE, PROJECT_COMPILER, flags};
use crate::config::{PROFILE, flags};
use crate::core::config::Config;
use crate::utils::fs::check_dir;
use crate::utils::log::{error, warn};
use crate::utils::text::{BOLD_GREEN, colored};
@@ -8,10 +9,6 @@ use std::time::Instant;
pub fn build(args: &[String]) -> i32 {
let mut active_profile = PROFILE.to_string();
let project_name = std::env::current_dir()
.ok()
.and_then(|p| p.file_name().map(|v| v.to_string_lossy().to_string()))
.unwrap_or_else(|| "project".to_string());
let items = check_dir(None).unwrap_or_default();
if !items.contains(&"dcr.toml".to_string()) {
@@ -19,6 +16,22 @@ pub fn build(args: &[String]) -> i32 {
return 1;
}
let config = match Config::open("./dcr.toml") {
Ok(cfg) => cfg,
Err(_) => {
error("dcr.toml file not found");
return 1;
}
};
let project_compiler: &str = config
.get("package.compiler")
.and_then(|v| v.as_str())
.unwrap_or("");
let project_name: &str = config
.get("package.name")
.and_then(|v| v.as_str())
.unwrap_or("");
if let Some(first_arg) = args.first() {
if first_arg.starts_with("--") {
let candidate = first_arg.trim_start_matches("--");
@@ -36,9 +49,9 @@ pub fn build(args: &[String]) -> i32 {
println!(
" Building project `{}`\n Profile: {}\n Compiler: {}\n",
colored(&project_name, BOLD_GREEN),
colored(project_name, BOLD_GREEN),
colored(&active_profile, BOLD_GREEN),
colored(PROJECT_COMPILER, BOLD_GREEN)
colored(project_compiler, BOLD_GREEN)
);
if !items.contains(&"target".to_string()) {
@@ -53,12 +66,13 @@ pub fn build(args: &[String]) -> i32 {
if src_items.contains(&"main.c".to_string()) {
let compile_flags = flags(&active_profile).unwrap_or(&[]);
let start_time = Instant::now();
let mut cmd = Command::new(PROJECT_COMPILER);
let mut cmd = Command::new(project_compiler);
cmd.arg("./src/main.c");
for flag in compile_flags {
cmd.arg(flag);
}
cmd.arg("-o").arg(format!("./target/{active_profile}/main"));
cmd.arg("-o")
.arg(format!("./target/{active_profile}/{project_name}"));
match cmd.status() {
Ok(status) if status.success() => {

View File

@@ -1,9 +1,11 @@
use crate::config::{FILE_MAIN_C, file_dcr_toml};
use crate::config::FILE_MAIN_C;
use crate::core::config::Config;
use crate::utils::fs::check_dir;
use crate::utils::log::{error, warn};
use crate::utils::text::{BOLD_CYAN, BOLD_GREEN, colored, printc};
use std::fs;
use std::io::Write;
use toml::Value;
pub fn init(args: &[String]) -> i32 {
if !args.is_empty() {
@@ -27,15 +29,15 @@ pub fn init(args: &[String]) -> i32 {
.unwrap_or_else(|_| ".".to_string());
println!("Initializing the project in {cwd}");
let mut dcr_toml = match fs::File::create("./dcr.toml") {
Ok(file) => file,
let mut config = match Config::new("./dcr.toml") {
Ok(cfg) => cfg,
Err(_) => {
error("Failed to create dcr.toml");
return 1;
}
};
if dcr_toml
.write_all(file_dcr_toml(&project_name).as_bytes())
if config
.edit("package.name", Value::String(project_name.clone()))
.is_err()
{
error("Failed to write dcr.toml");
@@ -43,7 +45,7 @@ pub fn init(args: &[String]) -> i32 {
}
println!(
" {} Created file {}",
colored("✔", BOLD_CYAN),
colored("✔", BOLD_GREEN),
colored("dcr.toml", BOLD_CYAN)
);
@@ -73,6 +75,6 @@ pub fn init(args: &[String]) -> i32 {
colored(project_name.as_str(), BOLD_GREEN)
);
printc("Next step:", BOLD_GREEN);
printc(" cd {}\n dcr run", project_name.as_str());
printc(" dcr run", BOLD_CYAN);
0
}

View File

@@ -1,9 +1,11 @@
use crate::config::{FILE_MAIN_C, file_dcr_toml};
use crate::config::FILE_MAIN_C;
use crate::core::config::Config;
use crate::utils::fs::check_dir;
use crate::utils::log::{error, warn};
use crate::utils::text::{BOLD_CYAN, BOLD_GREEN, colored, printc};
use std::fs;
use std::io::Write;
use toml::Value;
pub fn new(args: &[String]) -> i32 {
let items = check_dir(None).unwrap_or_default();
@@ -42,20 +44,20 @@ pub fn new(args: &[String]) -> i32 {
}
println!(
" {} Directory created {}",
colored("✔", BOLD_CYAN),
colored("✔", BOLD_GREEN),
project_name
);
let toml_path = format!("./{project_name}/dcr.toml");
let mut dcr_toml = match fs::File::create(&toml_path) {
Ok(file) => file,
let mut config = match Config::new(&toml_path) {
Ok(cfg) => cfg,
Err(_) => {
error("Failed to create dcr.toml");
return 1;
}
};
if dcr_toml
.write_all(file_dcr_toml(project_name).as_bytes())
if config
.edit("package.name", Value::String(project_name.to_string()))
.is_err()
{
error("Failed to write dcr.toml");
@@ -63,7 +65,7 @@ pub fn new(args: &[String]) -> i32 {
}
println!(
" {} Created file {}",
colored("✔", BOLD_CYAN),
colored("✔", BOLD_GREEN),
colored("dcr.toml", BOLD_CYAN)
);
@@ -94,6 +96,6 @@ pub fn new(args: &[String]) -> i32 {
colored(project_name, BOLD_GREEN)
);
printc("Next step:", BOLD_GREEN);
printc(" cd {}\n dcr run", project_name);
printc(&format!(" cd {}\n dcr run", project_name), BOLD_CYAN);
0
}

View File

@@ -1,5 +1,6 @@
use crate::cli::build::build;
use crate::config::{PROFILE, flags};
use crate::core::config::Config;
use crate::utils::fs::check_dir;
use crate::utils::log::{error, warn};
use crate::utils::text::{BOLD_GREEN, colored};
@@ -14,6 +15,18 @@ pub fn run(args: &[String]) -> i32 {
return 1;
}
let config = match Config::open("./dcr.toml") {
Ok(cfg) => cfg,
Err(_) => {
error("dcr.toml file not found");
return 1;
}
};
let project_name: &str = config
.get("package.name")
.and_then(|v| v.as_str())
.unwrap_or("");
if let Some(first_arg) = args.first() {
if first_arg.starts_with("--") {
let candidate = first_arg.trim_start_matches("--");
@@ -32,11 +45,11 @@ pub fn run(args: &[String]) -> i32 {
let build_status = build(args);
if build_status == 0 {
println!(
"\n {} target/{active_profile}/main",
"\n {} target/{active_profile}/{project_name}",
colored("Running", BOLD_GREEN)
);
println!("--------------------------------");
return Command::new(format!("./target/{active_profile}/main"))
return Command::new(format!("./target/{active_profile}/{project_name}"))
.status()
.map(|status| status.code().unwrap_or(1))
.unwrap_or(1);
@@ -47,7 +60,7 @@ pub fn run(args: &[String]) -> i32 {
.contains(&active_profile)
{
warn("Launch of the latest release");
return Command::new(format!("./target/{active_profile}/main"))
return Command::new(format!("./target/{active_profile}/{project_name}"))
.status()
.map(|status| status.code().unwrap_or(1))
.unwrap_or(1);