updated CLI API for convenience
This commit is contained in:
parent
86f2103ac0
commit
90805b34e0
4 changed files with 73 additions and 18 deletions
|
@ -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<PathBuf> {
|
||||
let path = env::current_dir().unwrap();
|
||||
Self::get_path(&path)
|
||||
}
|
||||
|
||||
pub fn get(path: &Path) -> Option<Self> {
|
||||
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<PathBuf> {
|
||||
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<PathBuf> {
|
||||
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::<Self>(&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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
void test_it_works() {
|
||||
assert( (2 + 2) == (4) );
|
||||
}
|
||||
"#;
|
||||
path += "/test.ctest";
|
||||
fs::write(path, DEFAULT_CONTENT).unwrap();
|
||||
}
|
||||
|
|
37
src/main.rs
37
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<String>,
|
||||
|
||||
/// Specific tests to run.
|
||||
#[clap(short, long)]
|
||||
tests: Vec<String>,
|
||||
// /// Specific tests to run.
|
||||
// #[clap(short, long)]
|
||||
// tests: Vec<String>,
|
||||
},
|
||||
|
||||
/// 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<String>,
|
||||
/// 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 } => {
|
||||
|
|
17
src/push.rs
17
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<String>) {
|
||||
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 <path> <identifier>'",
|
||||
);
|
||||
log_error("no config file found.\nPlease initialize with 'pi init <tag-prefix>'");
|
||||
exit(1)
|
||||
})
|
||||
.identifier()
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
utils::{log_failure, log_process, log_success},
|
||||
};
|
||||
|
||||
pub fn main(_capture: bool, files: Vec<String>, args: Vec<String>, _test: Option<Vec<String>>) {
|
||||
pub fn main(_capture: bool, files: Vec<String>, args: Vec<String>) {
|
||||
log_process("testing");
|
||||
for path in files {
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue