shameful pile of providencial junk

This commit is contained in:
JOLIMAITRE Matthieu 2022-11-08 00:37:27 +01:00
parent e188609c18
commit 7c6b9c3b2a
9 changed files with 86 additions and 31 deletions

View file

@ -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

View file

@ -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::<Vec<_>>()
.join(", ");

View file

@ -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(())

View file

@ -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<PathBuf> {
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<String> {
&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<String> {
self.includes
.iter()
.map(|p| Self::try_absolute(p.clone()))
.collect()
}
pub fn strict_mode(&self) -> bool {

View file

@ -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<String>,
#[clap(short, long)]
pass: Vec<String>,
},
/// Runs tests contained within a particular test file or the default test file.
@ -84,16 +89,17 @@ fn append_includes(list: &mut Vec<String>) {
);
}
fn compilation_args() -> Vec<String> {
fn compilation_args(sanitize: bool) -> Vec<String> {
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();

View file

@ -3,7 +3,7 @@ use crate::{
utils::{log_failure, log_process},
};
pub fn main(files: Vec<String>, flags: Vec<String>) -> Option<()> {
pub fn main(files: Vec<String>, flags: Vec<String>, passed: Vec<String>) -> 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<String>, flags: Vec<String>) -> Option<()> {
log_process("running");
RunTask::new(compiled)
.with_args(passed)
.run()
.map(Option::from)
.unwrap_or_else(|_| {

View file

@ -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<PathBuf>) -> Result<PathBuf, ExitStatus> {
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<String>,
}
impl RunTask {
@ -84,17 +91,23 @@ impl RunTask {
Self {
file,
verbose: false,
args: vec![],
}
}
pub fn with_args(mut self, mut args: Vec<String>) -> 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();

View file

@ -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<String>, args: Vec<String>) {
pub fn main(_capture: bool, test_files: Vec<String>, includes: Vec<String>, args: Vec<String>) {
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<String>, args: Vec<String>) {
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);
}

View file

@ -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<str>) {
log_pi_prefix();
println!("error: {input}");
}
pub fn remove_dupes(input: &mut Vec<String>) {
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);
}
}