Add format and linting
This commit is contained in:
2
PKGBUILD
2
PKGBUILD
@@ -6,7 +6,7 @@ pkgdesc="A simple C project manager inspired by Cargo"
|
||||
arch=('x86_64')
|
||||
url=""
|
||||
license=('MIT')
|
||||
depends=('gcc' 'pkgconf')
|
||||
depends=('gcc' 'pkgconf' 'clang')
|
||||
makedepends=('rust' 'cargo')
|
||||
source=()
|
||||
|
||||
|
||||
88
src/app.rs
88
src/app.rs
@@ -10,7 +10,9 @@ use colored::Colorize;
|
||||
use glob::glob;
|
||||
|
||||
const MAIN_C: &str = include_str!("templates/main.c");
|
||||
const GITIGNORE: &str = "target/\ncompile_commands.json\n";
|
||||
const GITIGNORE: &str = include_str!("templates/.gitignore");
|
||||
const CLANG_FORMAT: &str = include_str!("templates/.clang-format");
|
||||
const CLANG_TIDY: &str = include_str!("templates/.clang-tidy");
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
#[clap(version, about)]
|
||||
@@ -74,6 +76,8 @@ enum Subcommand {
|
||||
/// The package to remove
|
||||
package: String,
|
||||
},
|
||||
Fmt,
|
||||
Lint,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand)]
|
||||
@@ -212,6 +216,18 @@ impl App {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Subcommand::Fmt => {
|
||||
if let Err(e) = fmt() {
|
||||
eprintln!("Error formatting project: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Subcommand::Lint => {
|
||||
if let Err(e) = lint() {
|
||||
eprintln!("Error linting project: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -542,6 +558,8 @@ fn create_project<P: AsRef<Path>>(directory: P) -> std::io::Result<()> {
|
||||
|
||||
std::fs::write("src/main.c", MAIN_C)?;
|
||||
std::fs::write(".gitignore", GITIGNORE)?;
|
||||
std::fs::write(".clang-tidy", CLANG_TIDY)?;
|
||||
std::fs::write(".clang-format", CLANG_FORMAT)?;
|
||||
|
||||
let lossy = pathdir.to_string_lossy();
|
||||
|
||||
@@ -792,3 +810,71 @@ fn clean() -> std::io::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt() -> std::io::Result<()> {
|
||||
let source_files: Vec<PathBuf> = glob("src/*.c")
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, format!("{e}")))?
|
||||
.chain(
|
||||
glob("src/*.h")
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, format!("{e}")))?,
|
||||
)
|
||||
.filter_map(|e| e.ok())
|
||||
.collect();
|
||||
|
||||
let status = Command::new("clang-format")
|
||||
.arg("-i")
|
||||
.args(&source_files)
|
||||
.status()
|
||||
.map_err(|_| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"clang-format not found — try installing it (e.g. sudo pacman -S clang)",
|
||||
)
|
||||
})?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(std::io::Error::other("clang-format failed"));
|
||||
}
|
||||
|
||||
println!(" {} all source files", "Formatted".green().bold());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn lint() -> std::io::Result<()> {
|
||||
gen_compile_commands(&None)?;
|
||||
|
||||
let source_files: Vec<PathBuf> = glob("src/*.c")
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, format!("{e}")))?
|
||||
.filter_map(|e| e.ok())
|
||||
.collect();
|
||||
|
||||
let mut any_warnings = false;
|
||||
|
||||
for src in &source_files {
|
||||
println!(" {} {}", "Linting".green().bold(), src.display());
|
||||
|
||||
let status = Command::new("clang-tidy")
|
||||
.arg(src)
|
||||
.arg("--use-color")
|
||||
.status()
|
||||
.map_err(|_| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"clang-tidy not found — try installing it (e.g. sudo pacman -S clang)",
|
||||
)
|
||||
})?;
|
||||
|
||||
if !status.success() {
|
||||
any_warnings = true;
|
||||
}
|
||||
}
|
||||
|
||||
if any_warnings {
|
||||
println!("\n {} lint warnings found", "Warning".yellow().bold());
|
||||
} else {
|
||||
println!("\n {} no issues found", "Finished".green().bold());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
23
src/templates/.clang-format
Normal file
23
src/templates/.clang-format
Normal file
@@ -0,0 +1,23 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ColumnLimit: 100
|
||||
|
||||
BreakBeforeBraces: Attach
|
||||
BraceWrapping:
|
||||
AfterFunction: false
|
||||
AfterControlStatement: false
|
||||
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
|
||||
PointerAlignment: Right
|
||||
|
||||
SortIncludes: CaseSensitive
|
||||
IncludeBlocks: Regroup
|
||||
2
src/templates/.clang-tidy
Normal file
2
src/templates/.clang-tidy
Normal file
@@ -0,0 +1,2 @@
|
||||
Checks: "clang-diagnostic-*,clang-analyzer-*"
|
||||
WarningsAsErrors: ""
|
||||
2
src/templates/.gitignore
vendored
Normal file
2
src/templates/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
compile_commands.json
|
||||
Reference in New Issue
Block a user