added push subcommand

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-30 00:39:05 +02:00
parent 5ac086b67b
commit 9d7fa48390
4 changed files with 49 additions and 10 deletions

View file

@ -23,8 +23,6 @@ notify-debouncer-mini = "0.2"
ron = "0.8"
serde = { version = "1.0", features = ["derive"] }
termion = "1.5"
# tree-sitter = "0.20"
# tree-sitter-c = "0.20"
[profile.release]
lto = true

View file

@ -36,7 +36,12 @@ impl Config {
fs::write(path, content).unwrap();
}
pub fn get_current() -> Self {
pub fn get_local() -> Option<Self> {
let path = env::current_dir().unwrap();
Self::get(&path)
}
pub fn get_local_or_default() -> Self {
let path = env::current_dir().unwrap();
Self::get(&path)
.unwrap_or_else(|| Self::new(path.file_name().unwrap().to_str().unwrap().to_string()))

View file

@ -3,6 +3,7 @@ use config::Config;
pub mod check;
pub mod config;
pub mod push;
pub mod run;
pub mod tasks;
pub mod test;
@ -68,12 +69,12 @@ pub enum Commands {
},
/// Pushes changes to the git server with a custom tag.
push,
push { message: Option<String> },
}
fn append_includes(list: &mut Vec<String>) {
list.extend(
Config::get_current()
Config::get_local_or_default()
.includes()
.iter()
.map(|f| f.to_string()),
@ -86,7 +87,7 @@ fn compilation_args() -> Vec<String> {
"-Wextra".to_string(),
"-std=c99".to_string(),
];
if Config::get_current().strict_mode() {
if Config::get_local_or_default().strict_mode() {
args.push("-Werror".to_string());
}
args
@ -102,7 +103,7 @@ fn main() {
Commands::run { mut files } => {
if files.is_empty() {
files.push(Config::get_current().main_file().to_string());
files.push(Config::get_local_or_default().main_file().to_string());
}
append_includes(&mut files);
let args = compilation_args();
@ -115,7 +116,7 @@ fn main() {
tests,
} => {
if files.is_empty() {
files.push(Config::get_current().test_file().to_string());
files.push(Config::get_local_or_default().test_file().to_string());
}
append_includes(&mut files);
let args = compilation_args();
@ -132,8 +133,8 @@ fn main() {
config::create(path, identifier);
}
Commands::push => {
todo!();
Commands::push { message } => {
push::main(message);
}
}
}

35
src/push.rs Normal file
View file

@ -0,0 +1,35 @@
use std::process::Command;
use chrono::Utc;
use crate::{config::Config, utils::log_success};
pub fn main(message: Option<String>) {
let message = message.unwrap_or_else(|| Utc::now().format("pi - %d/%m/%Y %H:%M").to_string());
// commit
Command::new("git")
.args(["commit", "-m", &message])
.status()
.unwrap();
// push
Command::new("git").arg("push").status().unwrap();
// tag
let timestamp = Utc::now().timestamp();
let suffix = format!("pi-{timestamp}");
let tag = Config::get_local().unwrap().identifier().to_string() + &suffix;
Command::new("git")
.args(["tag", "-a", &tag, "-m", ""])
.status()
.unwrap();
// push tag
Command::new("git")
.args(["push", "--follow-tags"])
.status()
.unwrap();
log_success(&format!("pushed with tag '{tag}'"));
}