From 7c6b9c3b2a4aa4782f6eb8e7285fbd70a5603d49 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Tue, 8 Nov 2022 00:37:27 +0100 Subject: [PATCH] shameful pile of providencial junk --- README.md | 3 ++- src/check/formatting.rs | 2 +- src/check/testables.rs | 2 +- src/config.rs | 25 +++++++++++++++++++------ src/main.rs | 35 +++++++++++++++++++++++------------ src/run.rs | 3 ++- src/tasks.rs | 21 +++++++++++++++++---- src/test.rs | 12 ++++++++---- src/utils.rs | 14 +++++++++++++- 9 files changed, 86 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7e8eaa4..51bda8d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ SUBCOMMANDS: ## TODO - [ ] add support and switch on run/test for strict mode. -- [ ] add `--` syntax to run subcommand for parameter piping. +- [x] add `-p` flag to run subcommand with parameter piping. - [ ] flag on push to add automatically - [ ] prevent double includes. - [ ] flag on init to copy personnal environment +- [ ] gc subcommand to free cache diff --git a/src/check/formatting.rs b/src/check/formatting.rs index 01aacda..e47db26 100644 --- a/src/check/formatting.rs +++ b/src/check/formatting.rs @@ -9,7 +9,7 @@ pub const FORMAT_CONFIG: &StaticConf = &[ pub fn formatted_config() -> String { let middle = FORMAT_CONFIG - .into_iter() + .iter() .map(|(key, value)| format!("{key}: {value}")) .collect::>() .join(", "); diff --git a/src/check/testables.rs b/src/check/testables.rs index 9226aab..78c8b21 100644 --- a/src/check/testables.rs +++ b/src/check/testables.rs @@ -31,7 +31,7 @@ fn ends_with_newline(source: String) -> RuleResult { fn function_under_50l(source: String) -> RuleResult { for character in source.chars() { - let c: char = character; + let _c: char = character; } Ok(()) diff --git a/src/config.rs b/src/config.rs index aa65dad..0db4e9e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -63,6 +63,7 @@ impl Config { Self::try_get(&path).or_else(|| path.parent().and_then(Self::get)) } + // get path of the current config file pub fn get_path(path: &Path) -> Option { let path = path.to_path_buf().canonicalize().unwrap(); Self::try_get_path(&path).or_else(|| path.parent().and_then(Self::get_path)) @@ -72,16 +73,28 @@ impl Config { &self.identifier } - pub fn main_file(&self) -> &str { - &self.main_file + pub fn main_file(&self) -> String { + Self::try_absolute(self.main_file.clone()) } - pub fn test_file(&self) -> &str { - &self.test_file + pub fn test_file(&self) -> String { + Self::try_absolute(self.test_file.clone()) } - pub fn includes(&self) -> &Vec { - &self.includes + fn try_absolute(path: String) -> String { + if let Some(conf_path) = Self::get_local_path() { + let dir_path = conf_path.parent().unwrap(); + dir_path.join(path).to_str().unwrap().to_string() + } else { + path + } + } + + pub fn includes(&self) -> Vec { + self.includes + .iter() + .map(|p| Self::try_absolute(p.clone())) + .collect() } pub fn strict_mode(&self) -> bool { diff --git a/src/main.rs b/src/main.rs index 32db48a..426d6a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,8 +35,13 @@ pub enum Commands { /// Runs a set of files or the default target. run { + // disables fsanitize + #[clap(short, long)] + disable_sanitize: bool, /// Files to run. files: Vec, + #[clap(short, long)] + pass: Vec, }, /// Runs tests contained within a particular test file or the default test file. @@ -84,16 +89,17 @@ fn append_includes(list: &mut Vec) { ); } -fn compilation_args() -> Vec { +fn compilation_args(sanitize: bool) -> Vec { let mut args = vec![ "-Wall".to_string(), - "-fsanitize=address".to_string(), "-Wextra".to_string(), "-std=c99".to_string(), "-g".to_string(), - "-fsanitize=address".to_string(), - // "-pedantic".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()); } @@ -108,13 +114,17 @@ fn main() { Commands::format { files } => check::format(files), - Commands::run { mut files } => { + Commands::run { + disable_sanitize, + mut files, + pass, + } => { if files.is_empty() { - files.push(Config::get_local_or_default().main_file().to_string()); + files.push(Config::get_local_or_default().main_file()); } append_includes(&mut files); - let args = compilation_args(); - run::main(files, args); + let args = compilation_args(!disable_sanitize); + run::main(files, args, pass); } Commands::test { @@ -123,11 +133,12 @@ fn main() { // tests, } => { if files.is_empty() { - files.push(Config::get_local_or_default().test_file().to_string()); + files.push(Config::get_local_or_default().test_file()); } - append_includes(&mut files); - let args = compilation_args(); - test::main(capture, files, args) + 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(); diff --git a/src/run.rs b/src/run.rs index f6dc3c8..7c3572f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -3,7 +3,7 @@ use crate::{ utils::{log_failure, log_process}, }; -pub fn main(files: Vec, flags: Vec) -> Option<()> { +pub fn main(files: Vec, flags: Vec, passed: Vec) -> Option<()> { let source_file = files.into_iter().map(|f| f.into()).collect(); log_process("compiling"); let mut task = CompileTask::new(source_file); @@ -19,6 +19,7 @@ pub fn main(files: Vec, flags: Vec) -> Option<()> { log_process("running"); RunTask::new(compiled) + .with_args(passed) .run() .map(Option::from) .unwrap_or_else(|_| { diff --git a/src/tasks.rs b/src/tasks.rs index ff2eaaf..759875a 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -5,7 +5,8 @@ use std::{ }; use crate::utils::{ - log_command_run, log_failure, log_separator_bottom, log_separator_top, tmp_file_path, Apply, + log_command_run, log_failure, log_separator_bottom, log_separator_top, remove_dupes, + tmp_file_path, Apply, }; pub struct CompileTask { @@ -55,13 +56,18 @@ impl CompileTask { } pub fn compile(&self, sources: Vec) -> Result { + let mut sources = sources + .iter() + .map(|s| s.to_str().unwrap().to_string()) + .collect(); + remove_dupes(&mut sources); let output_path = tmp_file_path().apply(|o| o.set_extension("b")); let output_path_ref = output_path.to_str().unwrap(); let mut command = Command::new("gcc"); command .args(["-o", output_path_ref]) .args(self.flags.clone()) - .args(sources.iter().map(|s| s.to_str().unwrap())); + .args(sources); if self.verbose { log_command_run(&command); log_separator_top(); @@ -77,6 +83,7 @@ impl CompileTask { pub struct RunTask { file: PathBuf, verbose: bool, + args: Vec, } impl RunTask { @@ -84,17 +91,23 @@ impl RunTask { Self { file, verbose: false, + args: vec![], } } + pub fn with_args(mut self, mut args: Vec) -> Self { + self.args.append(&mut args); + self + } + pub fn with_verbose(mut self) -> Self { self.verbose = true; self } pub fn run(self) -> Result<(), ExitStatus> { - let mut command = Command::new("sh"); - command.args(["-c", self.file.to_str().unwrap()]); + let mut command = Command::new(self.file.to_str().unwrap()); + command.args(self.args); if self.verbose { log_command_run(&command); log_separator_top(); diff --git a/src/test.rs b/src/test.rs index 23576d1..5264cd8 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,13 +1,14 @@ -use std::{fs, thread, time::Duration}; +use std::{fs, path::PathBuf, thread, time::Duration}; use crate::{ tasks::{CompileTask, GenTask, RunTask}, utils::{log_failure, log_process, log_success}, }; -pub fn main(_capture: bool, files: Vec, args: Vec) { +pub fn main(_capture: bool, test_files: Vec, includes: Vec, args: Vec) { log_process("testing"); - for path in files { + let includes: Vec<_> = includes.into_iter().map(PathBuf::from).collect(); + for path in test_files { let content = fs::read_to_string(&path).unwrap(); let tests = find_tests(content); for test in tests { @@ -18,7 +19,10 @@ pub fn main(_capture: bool, files: Vec, args: Vec) { thread::sleep(Duration::from_millis(100)); // compile with all files - let mut task = CompileTask::new(vec![generated_code]); + let mut files = vec![generated_code]; + let mut local_includes = includes.clone(); + files.append(&mut local_includes); + let mut task = CompileTask::new(files); for flag in args.clone() { task = task.with_flag(flag); } diff --git a/src/utils.rs b/src/utils.rs index c09bbe3..c68da8d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use std::{fs, path::PathBuf, process::Command}; +use std::{collections::HashSet, fs, mem::swap, path::PathBuf, process::Command}; use chrono::Utc; use termion::color; @@ -97,3 +97,15 @@ pub fn log_error(input: impl AsRef) { log_pi_prefix(); println!("error: {input}"); } + +pub fn remove_dupes(input: &mut Vec) { + let mut tmp = vec![]; + swap(input, &mut tmp); + let mut set = HashSet::new(); + for str in tmp { + set.insert(str); + } + for str in set { + input.push(str); + } +}