mirror of
https://github.com/dexoron/dcr.git
synced 2026-10-21 13:38:30 +00:00
feat: add fmt command
This commit is contained in:
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[toolchain]
|
||||
channel = "nightly"
|
||||
56
src/cli/fmt.rs
Normal file
56
src/cli/fmt.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use crate::utils::log::error;
|
||||
use crate::utils::text::{colored, BOLD_GREEN};
|
||||
use glob::glob;
|
||||
use std::process::Command;
|
||||
|
||||
pub fn fmt(_args: &[String]) -> i32 {
|
||||
let patterns = [
|
||||
"src/**/*.c",
|
||||
"src/**/*.cpp",
|
||||
"src/**/*.h",
|
||||
"src/**/*.hpp",
|
||||
"tests/**/*.c",
|
||||
"tests/**/*.cpp",
|
||||
"tests/**/*.h",
|
||||
"tests/**/*.hpp",
|
||||
];
|
||||
|
||||
let mut files = Vec::new();
|
||||
for pattern in patterns {
|
||||
if let Ok(paths) = glob(pattern) {
|
||||
for path in paths.flatten() {
|
||||
if let Some(s) = path.to_str() {
|
||||
files.push(s.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if files.is_empty() {
|
||||
println!(" {} No files to format", colored("Format", BOLD_GREEN));
|
||||
return 0;
|
||||
}
|
||||
|
||||
println!(
|
||||
" {} {} files",
|
||||
colored("Formatting", BOLD_GREEN),
|
||||
files.len()
|
||||
);
|
||||
|
||||
let status = Command::new("clang-format").arg("-i").args(&files).status();
|
||||
|
||||
match status {
|
||||
Ok(s) if s.success() => {
|
||||
println!(" {} successful", colored("Format", BOLD_GREEN));
|
||||
0
|
||||
}
|
||||
Ok(s) => {
|
||||
error(&format!("clang-format failed with status: {}", s));
|
||||
1
|
||||
}
|
||||
Err(e) => {
|
||||
error(&format!("Failed to execute clang-format: {}", e));
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ pub fn help() -> i32 {
|
||||
println!(" run [--profile] Build and run the project (default: --debug)");
|
||||
println!(" tree Display the dependency tree");
|
||||
println!(" test Run project tests (alias: tests)");
|
||||
println!(" fmt Format all C/C++ source files using clang-format");
|
||||
println!(" clean Remove the target directory");
|
||||
println!(" gen <subcommand> Generate IDE integration files");
|
||||
printc("FLAGS:", BOLD_GREEN);
|
||||
|
||||
@@ -20,6 +20,7 @@ pub mod build;
|
||||
pub mod clean;
|
||||
pub mod flag_update;
|
||||
pub mod flags;
|
||||
pub mod fmt;
|
||||
pub mod r#gen;
|
||||
pub mod help;
|
||||
pub mod init;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::utils::log::error;
|
||||
mod cli;
|
||||
mod config;
|
||||
@@ -41,6 +40,7 @@ fn main() {
|
||||
"run" => cli::run::run(rest),
|
||||
"tree" => cli::tree::tree(rest),
|
||||
"test" | "tests" => cli::test::test(rest),
|
||||
"fmt" => cli::fmt::fmt(rest),
|
||||
"clean" => cli::clean::clean(rest),
|
||||
"gen" => cli::r#gen::r#gen(rest),
|
||||
"--version" => {
|
||||
|
||||
Reference in New Issue
Block a user