diff --git a/src/config.rs b/src/config.rs index 9d2da36..4822548 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,7 +23,7 @@ impl Config { Self { identifier, main_file: "main.c".into(), - test_file: "test.c".into(), + test_file: "test.ctest".into(), includes: vec![], strict_mode: false, } @@ -47,11 +47,21 @@ impl Config { .unwrap_or_else(|| Self::new(path.file_name().unwrap().to_str().unwrap().to_string())) } + pub fn get_local_path() -> Option { + let path = env::current_dir().unwrap(); + Self::get_path(&path) + } + pub fn get(path: &Path) -> Option { let path = path.to_path_buf().canonicalize().unwrap(); Self::try_get(&path).or_else(|| path.parent().and_then(Self::get)) } + 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)) + } + pub fn identifier(&self) -> &str { &self.identifier } @@ -78,6 +88,15 @@ impl Config { .ok() .and_then(|content| ron::from_str(&content).ok()) } + + fn try_get_path(path: &Path) -> Option { + let path = path.to_path_buf().apply(|p| p.push(Self::CONFIG_FILE_NAME)); + fs::read_to_string(&path) + .ok() + .and_then(|content| ron::from_str::(&content).ok()) + .is_some() + .then_some(path) + } } pub fn create(path: String, identifier: String) { @@ -94,3 +113,17 @@ pub fn create(path: String, identifier: String) { .to_string(); log_success(&format!("created '{path}'")); } + +pub fn create_test(mut path: String) { + const DEFAULT_CONTENT: &str = r#" +#include +#include +#include + +void test_it_works() { + assert( (2 + 2) == (4) ); +} +"#; + path += "/test.ctest"; + fs::write(path, DEFAULT_CONTENT).unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 138d37e..d79e861 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::env; + use clap::{Parser, Subcommand}; use config::Config; @@ -45,10 +47,9 @@ pub enum Commands { /// Files to run tests from. files: Vec, - - /// Specific tests to run. - #[clap(short, long)] - tests: Vec, + // /// Specific tests to run. + // #[clap(short, long)] + // tests: Vec, }, /// Watches changes to the project included files and runs a command on changes. @@ -61,11 +62,13 @@ pub enum Commands { /// Initializes a project directory configuration, useful for custom flags, includes and custop push messages. init { - /// Path to the folder containing the project. - path: String, - /// Identifier for the automated tests. - identifier: String, + prefix: String, + /// Path to the folder containing the project. + path: Option, + /// e + #[clap(short, long)] + tests: bool, }, /// Pushes changes to the git server with a custom tag. @@ -113,15 +116,14 @@ fn main() { Commands::test { capture, mut files, - tests, + // tests, } => { if files.is_empty() { files.push(Config::get_local_or_default().test_file().to_string()); } append_includes(&mut files); let args = compilation_args(); - let tests = (!tests.is_empty()).then_some(tests); - test::main(capture, files, args, tests) + test::main(capture, files, args) } Commands::watch { command, files } => { let mut files = files.unwrap_or_default(); @@ -129,8 +131,17 @@ fn main() { watch::main(files, command); } - Commands::init { path, identifier } => { - config::create(path, identifier); + Commands::init { + path, + prefix, + tests, + } => { + let path = + path.unwrap_or_else(|| env::current_dir().unwrap().to_str().unwrap().to_string()); + config::create(path.clone(), prefix); + if tests { + config::create_test(path); + } } Commands::push { message } => { diff --git a/src/push.rs b/src/push.rs index a1a2a79..bb413c2 100644 --- a/src/push.rs +++ b/src/push.rs @@ -7,15 +7,26 @@ use crate::{ utils::{log_error, log_process, log_success}, }; +pub fn add() { + let path = Config::get_local_path().unwrap(); + let path = path.parent().unwrap(); + let path = path.to_str().unwrap(); + Command::new("git") + .args(["add", path]) + .status() + .unwrap() + .success() + .then_some(()) + .unwrap_or_else(|| exit(1)); +} + pub fn main(message: Option) { let message = message.unwrap_or_else(|| Utc::now().format("pi - %d/%m/%Y %H:%M").to_string()); let timestamp = Utc::now().timestamp(); let suffix = format!("pi-{timestamp}"); let tag = Config::get_local() .unwrap_or_else(|| { - log_error( - "no config file found.\nPlease initialize with 'pi init '", - ); + log_error("no config file found.\nPlease initialize with 'pi init '"); exit(1) }) .identifier() diff --git a/src/test.rs b/src/test.rs index 7b0e0ea..23576d1 100644 --- a/src/test.rs +++ b/src/test.rs @@ -5,7 +5,7 @@ use crate::{ utils::{log_failure, log_process, log_success}, }; -pub fn main(_capture: bool, files: Vec, args: Vec, _test: Option>) { +pub fn main(_capture: bool, files: Vec, args: Vec) { log_process("testing"); for path in files { let content = fs::read_to_string(&path).unwrap();