diff --git a/Cargo.lock b/Cargo.lock index 231bdb2..3defe50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,7 +138,7 @@ checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" [[package]] name = "epitls-pi" -version = "1.2.1" +version = "1.3.0" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 66f44c4..0f404f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "epitls-pi" -version = "1.2.1" +version = "1.3.1" edition = "2021" license = "GPL-3.0+" description = "A little helper tool meant to ease the developpment of the C piscine at EPITA/Toulouse." diff --git a/src/config.rs b/src/config.rs index 9d2da36..aa65dad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,13 @@ use std::{ env, fs, + io::stdin, path::{Path, PathBuf}, }; use ron::ser::PrettyConfig; use serde::{Deserialize, Serialize}; -use crate::utils::{log_success, Apply}; +use crate::utils::{log_process, log_success, Apply}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { @@ -23,7 +24,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, } @@ -33,6 +34,11 @@ impl Config { path.extend([Self::CONFIG_FILE_NAME]); let content = ron::ser::to_string_pretty(self, PrettyConfig::default().struct_names(true)).unwrap(); + if path.exists() { + log_process("config already exists, overwrite it?"); + let mut buff = String::new(); + stdin().read_line(&mut buff).unwrap(); + } fs::write(path, content).unwrap(); } @@ -47,11 +53,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 +94,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 +119,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 0b44c4b..7f45af9 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. @@ -87,6 +90,7 @@ fn compilation_args() -> Vec { "-fsanitize=address".to_string(), "-Wextra".to_string(), "-std=c99".to_string(), + // "-pedantic".to_string(), ]; if Config::get_local_or_default().strict_mode() { args.push("-Werror".to_string()); @@ -114,15 +118,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(); @@ -130,8 +133,18 @@ 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()); + let prefix = prefix.trim().trim_end_matches('*'); + config::create(path.clone(), prefix.to_string()); + if tests { + config::create_test(path); + } } Commands::push { message } => { diff --git a/src/push.rs b/src/push.rs index c750f2f..198a545 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();