Add Pallet Lint and Pallet Fmt #28

Merged
sfrembling merged 4 commits from working/lint-and-format into main 2026-03-23 16:34:31 -06:00
5 changed files with 115 additions and 2 deletions
Showing only changes of commit 9b60e6092a - Show all commits

View File

@@ -6,7 +6,7 @@ pkgdesc="A simple C project manager inspired by Cargo"
arch=('x86_64') arch=('x86_64')
url="" url=""
license=('MIT') license=('MIT')
depends=('gcc' 'pkgconf') depends=('gcc' 'pkgconf' 'clang')
makedepends=('rust' 'cargo') makedepends=('rust' 'cargo')
source=() source=()

View File

@@ -10,7 +10,9 @@ use colored::Colorize;
use glob::glob; use glob::glob;
const MAIN_C: &str = include_str!("templates/main.c"); 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)] #[derive(clap::Parser)]
#[clap(version, about)] #[clap(version, about)]
@@ -74,6 +76,8 @@ enum Subcommand {
/// The package to remove /// The package to remove
package: String, package: String,
}, },
Fmt,
Lint,
} }
#[derive(clap::Subcommand)] #[derive(clap::Subcommand)]
@@ -212,6 +216,18 @@ impl App {
std::process::exit(1); 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("src/main.c", MAIN_C)?;
std::fs::write(".gitignore", GITIGNORE)?; 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(); let lossy = pathdir.to_string_lossy();
@@ -792,3 +810,71 @@ fn clean() -> std::io::Result<()> {
Ok(()) 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(())
}

View 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

View File

@@ -0,0 +1,2 @@
Checks: "clang-diagnostic-*,clang-analyzer-*"
WarningsAsErrors: ""

2
src/templates/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target/
compile_commands.json