use clap::{Parser, Subcommand};
use config::Config;
pub mod check;
pub mod config;
pub mod push;
pub mod run;
pub mod tasks;
pub mod test;
pub mod utils;
pub mod watch;
#[derive(Parser)]
pub struct Arguments {
#[clap(subcommand)]
command: Commands,
}
#[allow(non_camel_case_types)]
#[derive(Subcommand)]
pub enum Commands {
check {
files: Vec<String>,
},
format {
files: Vec<String>,
},
run {
#[clap(short, long)]
disable_sanitize: bool,
files: Vec<String>,
#[clap(short, long)]
pass: Vec<String>,
},
test {
#[clap(short, long)]
capture: bool,
files: Vec<String>,
},
watch {
#[clap(short)]
files: Option<Vec<String>>,
command: String,
},
init {
path: String,
prefix: Option<String>,
#[clap(short, long)]
tests: bool,
},
push { message: Option<String> },
}
fn append_includes(list: &mut Vec<String>) {
list.extend(
Config::get_local_or_default()
.includes()
.iter()
.map(|f| f.to_string()),
);
}
fn compilation_args(sanitize: bool) -> Vec<String> {
let mut args = vec![
"-Wall".to_string(),
"-Wextra".to_string(),
"-std=c99".to_string(),
"-g".to_string(),
"-pedantic".to_string(),
];
if sanitize {
args.push("-fsanitize=address".to_string())
}
if Config::get_local_or_default().strict_mode() {
args.push("-Werror".to_string());
}
args
}
fn main() {
let args: Arguments = Parser::parse();
match args.command {
Commands::check { files } => check::main(files),
Commands::format { files } => check::format(files),
Commands::run {
disable_sanitize,
mut files,
pass,
} => {
if files.is_empty() {
files.push(Config::get_local_or_default().main_file());
}
append_includes(&mut files);
let args = compilation_args(!disable_sanitize);
run::main(files, args, pass);
}
Commands::test {
capture,
mut files,
} => {
if files.is_empty() {
files.push(Config::get_local_or_default().test_file());
}
let mut includes = vec![];
append_includes(&mut includes);
let args = compilation_args(true);
test::main(capture, files, includes, args)
}
Commands::watch { command, files } => {
let mut files = files.unwrap_or_default();
append_includes(&mut files);
watch::main(files, command);
}
Commands::init {
path,
prefix,
tests,
} => {
let prefix = prefix.unwrap_or_else(|| ".".to_string());
let prefix = prefix.trim().trim_end_matches('*');
config::create(path.clone(), prefix.to_string());
if tests {
config::create_test(path);
}
}
Commands::push { message } => {
push::main(message);
}
}
}